Open Lighting Architecture  0.9.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
STLUtils.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * STLUtils.h
17  * Helper functions for dealing with the STL.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
33 #ifndef INCLUDE_OLA_STL_STLUTILS_H_
34 #define INCLUDE_OLA_STL_STLUTILS_H_
35 
36 #include <assert.h>
37 #include <cstddef>
38 #include <map>
39 #include <set>
40 #include <vector>
41 #include <utility>
42 
43 namespace ola {
44 
55 template<typename T>
56 void STLEmptyStack(T *stack) {
57  while (!stack->empty()) {
58  stack->pop();
59  }
60 }
61 
67 template<typename T>
68 void STLEmptyStackAndDelete(T *stack) {
69  while (!stack->empty()) {
70  delete stack->top();
71  stack->pop();
72  }
73 }
74 
98 template<typename T>
99 void STLDeleteElements(T *sequence) {
100  typename T::iterator iter = sequence->begin();
101  for (; iter != sequence->end(); ++iter) {
102  if (*iter) {
103  delete *iter;
104  }
105  }
106  sequence->clear();
107 }
108 
133 template<typename T>
134 void STLDeleteValues(T *container) {
135  typename T::iterator iter = container->begin();
136  for (; iter != container->end(); ++iter) {
137  if (iter->second) {
138  delete iter->second;
139  }
140  }
141  container->clear();
142 }
143 
151 template<typename T1, typename T2>
152 inline bool STLContains(const T1 &container, const T2 &value) {
153  return container.find(value) != container.end();
154 }
155 
162 template<typename T1>
163 void STLKeys(const T1 &container, std::vector<typename T1::key_type> *keys) {
164  keys->reserve(keys->size() + container.size());
165  typename T1::const_iterator iter = container.begin();
166  for (; iter != container.end(); ++iter)
167  keys->push_back(iter->first);
168 }
169 
177 template<typename T1, typename T2>
178 void STLValues(const T1 &container, std::vector<T2> *values) {
179  values->reserve(values->size() + container.size());
180  typename T1::const_iterator iter = container.begin();
181  for (; iter != container.end(); ++iter)
182  values->push_back(iter->second);
183 }
184 
192 template<typename T1>
193 typename T1::mapped_type* STLFind(T1 *container,
194  const typename T1::key_type &key) {
195  typename T1::iterator iter = container->find(key);
196  if (iter == container->end()) {
197  return NULL;
198  } else {
199  return &iter->second;
200  }
201 }
202 
210 template<typename T1>
211 typename T1::mapped_type const* STLFind(const T1 *container,
212  const typename T1::key_type &key) {
213  typename T1::const_iterator iter = container->find(key);
214  if (iter == container->end()) {
215  return NULL;
216  } else {
217  return &iter->second;
218  }
219 }
220 
231 template<typename T1>
232 typename T1::mapped_type STLFindOrNull(const T1 &container,
233  const typename T1::key_type &key) {
234  typename T1::const_iterator iter = container.find(key);
235  if (iter == container.end()) {
236  return NULL;
237  } else {
238  return iter->second;
239  }
240 }
241 
242 
258 template<typename T1>
259 bool STLReplace(T1 *container, const typename T1::key_type &key,
260  const typename T1::mapped_type &value) {
261  std::pair<typename T1::iterator, bool> p = container->insert(
262  typename T1::value_type(key, value));
263  if (!p.second) {
264  p.first->second = value;
265  return true;
266  }
267  return false;
268 }
269 
284 template<typename T1>
285 typename T1::mapped_type STLReplacePtr(T1 *container,
286  const typename T1::key_type &key,
287  const typename T1::mapped_type &value) {
288  std::pair<typename T1::iterator, bool> p = container->insert(
289  typename T1::value_type(key, value));
290  if (!p.second) {
291  typename T1::mapped_type old_value = p.first->second;
292  p.first->second = value;
293  return old_value;
294  }
295  return NULL;
296 }
297 
298 
308 template<typename T1>
309 bool STLReplaceAndDelete(T1 *container, const typename T1::key_type &key,
310  const typename T1::mapped_type &value) {
311  std::pair<typename T1::iterator, bool> p = container->insert(
312  typename T1::value_type(key, value));
313  if (!p.second) {
314  delete p.first->second;
315  p.first->second = value;
316  return true;
317  }
318  return false;
319 }
320 
321 
330 template<typename T1>
331 bool STLInsertIfNotPresent(T1 *container,
332  const typename T1::value_type &key_value) {
333  return container->insert(key_value).second;
334 }
335 
336 
347 template<typename T1>
348 bool STLInsertIfNotPresent(T1 *container, const typename T1::key_type &key,
349  const typename T1::mapped_type &value) {
350  return container->insert(typename T1::value_type(key, value)).second;
351 }
352 
353 
364 template<typename T1>
365 void STLInsertOrDie(T1 *container, const typename T1::key_type &key,
366  const typename T1::mapped_type &value) {
367  assert(container->insert(typename T1::value_type(key, value)).second);
368 }
369 
370 
378 template<typename T1>
379 bool STLRemove(T1 *container, const typename T1::key_type &key) {
380  return container->erase(key);
381 }
382 
395 template<typename T1>
396 bool STLLookupAndRemove(T1 *container,
397  const typename T1::key_type &key,
398  typename T1::mapped_type *value) {
399  typename T1::iterator iter = container->find(key);
400  if (iter == container->end()) {
401  return false;
402  } else {
403  *value = iter->second;
404  container->erase(iter);
405  return true;
406  }
407 }
408 
419 template<typename T1>
420 typename T1::iterator STLLookupOrInsertNull(T1 *container,
421  const typename T1::key_type &key) {
422  std::pair<typename T1::iterator, bool> p = container->insert(
423  typename T1::value_type(key, NULL));
424  return p.first;
425 }
426 
435 template<typename T1>
436 bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key) {
437  typename T1::iterator iter = container->find(key);
438  if (iter == container->end()) {
439  return false;
440  } else {
441  delete iter->second;
442  container->erase(iter);
443  return true;
444  }
445 }
446 
457 template<typename T1>
458 typename T1::mapped_type STLLookupAndRemovePtr(
459  T1 *container,
460  const typename T1::key_type &key) {
461  typename T1::iterator iter = container->find(key);
462  if (iter == container->end()) {
463  return NULL;
464  } else {
465  typename T1::mapped_type value = iter->second;
466  container->erase(iter);
467  return value;
468  }
469 }
470 
482 template<typename T1, typename T2>
483 void STLMapFromKeys(T1 *output, const T2 input,
484  typename T1::mapped_type value) {
485  typename T2::const_iterator iter = input.begin();
486  for (; iter != input.end(); ++iter) {
487  std::pair<typename T1::iterator, bool> p = output->insert(
488  typename T1::value_type(*iter, value));
489  if (!p.second) {
490  p.first->second = value;
491  }
492  }
493 }
494 } // namespace ola
495 #endif // INCLUDE_OLA_STL_STLUTILS_H_
496