Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
257 template<typename T1>
258 bool STLReplace(T1 *container, const typename T1::key_type &key,
259  const typename T1::mapped_type &value) {
260  std::pair<typename T1::iterator, bool> p = container->insert(
261  typename T1::value_type(key, value));
262  if (!p.second) {
263  p.first->second = value;
264  return true;
265  }
266  return false;
267 }
268 
282 template<typename T1>
283 typename T1::mapped_type STLReplacePtr(T1 *container,
284  const typename T1::key_type &key,
285  const typename T1::mapped_type &value) {
286  std::pair<typename T1::iterator, bool> p = container->insert(
287  typename T1::value_type(key, value));
288  if (!p.second) {
289  typename T1::mapped_type old_value = p.first->second;
290  p.first->second = value;
291  return old_value;
292  }
293  return NULL;
294 }
295 
296 
306 template<typename T1>
307 bool STLReplaceAndDelete(T1 *container, const typename T1::key_type &key,
308  const typename T1::mapped_type &value) {
309  std::pair<typename T1::iterator, bool> p = container->insert(
310  typename T1::value_type(key, value));
311  if (!p.second) {
312  delete p.first->second;
313  p.first->second = value;
314  return true;
315  }
316  return false;
317 }
318 
319 
328 template<typename T1>
329 bool STLInsertIfNotPresent(T1 *container,
330  const typename T1::value_type &key_value) {
331  return container->insert(key_value).second;
332 }
333 
334 
345 template<typename T1>
346 bool STLInsertIfNotPresent(T1 *container, const typename T1::key_type &key,
347  const typename T1::mapped_type &value) {
348  return container->insert(typename T1::value_type(key, value)).second;
349 }
350 
351 
361 template<typename T1>
362 void STLInsertOrDie(T1 *container, const typename T1::key_type &key,
363  const typename T1::mapped_type &value) {
364  assert(container->insert(typename T1::value_type(key, value)).second);
365 }
366 
367 
375 template<typename T1>
376 bool STLRemove(T1 *container, const typename T1::key_type &key) {
377  return container->erase(key);
378 }
379 
392 template<typename T1>
393 bool STLLookupAndRemove(T1 *container,
394  const typename T1::key_type &key,
395  typename T1::mapped_type *value) {
396  typename T1::iterator iter = container->find(key);
397  if (iter == container->end()) {
398  return false;
399  } else {
400  *value = iter->second;
401  container->erase(iter);
402  return true;
403  }
404 }
405 
416 template<typename T1>
417 typename T1::iterator STLLookupOrInsertNull(T1 *container,
418  const typename T1::key_type &key) {
419  std::pair<typename T1::iterator, bool> p = container->insert(
420  typename T1::value_type(key, NULL));
421  return p.first;
422 }
423 
424 template<typename T1>
425 void PairAssociativeAssignNew(T1 **location) {
426  *location = new T1();
427 }
428 
436 template<typename T1>
437 typename T1::iterator STLLookupOrInsertNew(T1 *container,
438  const typename T1::key_type &key) {
439  std::pair<typename T1::iterator, bool> p = container->insert(
440  typename T1::value_type(key, NULL));
441  if (p.second) {
442  PairAssociativeAssignNew(&p.first->second);
443  }
444  return p.first;
445 }
446 
455 template<typename T1>
456 bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key) {
457  typename T1::iterator iter = container->find(key);
458  if (iter == container->end()) {
459  return false;
460  } else {
461  delete iter->second;
462  container->erase(iter);
463  return true;
464  }
465 }
466 
477 template<typename T1>
478 typename T1::mapped_type STLLookupAndRemovePtr(
479  T1 *container,
480  const typename T1::key_type &key) {
481  typename T1::iterator iter = container->find(key);
482  if (iter == container->end()) {
483  return NULL;
484  } else {
485  typename T1::mapped_type value = iter->second;
486  container->erase(iter);
487  return value;
488  }
489 }
490 
502 template<typename T1, typename T2>
503 void STLMapFromKeys(T1 *output, const T2 input,
504  typename T1::mapped_type value) {
505  typename T2::const_iterator iter = input.begin();
506  for (; iter != input.end(); ++iter) {
507  std::pair<typename T1::iterator, bool> p = output->insert(
508  typename T1::value_type(*iter, value));
509  if (!p.second) {
510  p.first->second = value;
511  }
512  }
513 }
514 } // namespace ola
515 #endif // INCLUDE_OLA_STL_STLUTILS_H_
516 
T1::mapped_type STLFindOrNull(const T1 &container, const typename T1::key_type &key)
Lookup a value by key in a associative container.
Definition: STLUtils.h:232
void STLMapFromKeys(T1 *output, const T2 input, typename T1::mapped_type value)
Definition: STLUtils.h:503
bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key)
Remove a value from a pair associative container and delete it.
Definition: STLUtils.h:456
bool STLReplaceAndDelete(T1 *container, const typename T1::key_type &key, const typename T1::mapped_type &value)
Similar to STLReplace but this will delete the value if the replacement occurs.
Definition: STLUtils.h:307
void STLEmptyStack(T *stack)
Clear a stack.
Definition: STLUtils.h:56
T1::iterator STLLookupOrInsertNull(T1 *container, const typename T1::key_type &key)
Lookup or insert a NULL value into a pair associative container.
Definition: STLUtils.h:417
bool STLLookupAndRemove(T1 *container, const typename T1::key_type &key, typename T1::mapped_type *value)
Lookup and remove a key from a pair associative container.
Definition: STLUtils.h:393
void STLEmptyStackAndDelete(T *stack)
Clear a stack and delete all pointers..
Definition: STLUtils.h:68
bool STLRemove(T1 *container, const typename T1::key_type &key)
Remove a key / value from a container.
Definition: STLUtils.h:376
void STLDeleteElements(T *sequence)
Delete the elements of a Sequence.
Definition: STLUtils.h:99
void STLValues(const T1 &container, std::vector< T2 > *values)
Extract a vector of values from a pair associative container.
Definition: STLUtils.h:178
T1::iterator STLLookupOrInsertNew(T1 *container, const typename T1::key_type &key)
Lookup or insert a new object into a pair associative container.
Definition: STLUtils.h:437
bool STLContains(const T1 &container, const T2 &value)
Definition: STLUtils.h:152
void STLKeys(const T1 &container, std::vector< typename T1::key_type > *keys)
Definition: STLUtils.h:163
void STLInsertOrDie(T1 *container, const typename T1::key_type &key, const typename T1::mapped_type &value)
Insert an key : value into a pair associative container, or abort the program if the key already exis...
Definition: STLUtils.h:362
void STLDeleteValues(T *container)
Definition: STLUtils.h:134
bool STLReplace(T1 *container, const typename T1::key_type &key, const typename T1::mapped_type &value)
Replace a value in a pair associative container, inserting the key, value if it doesn't already exist...
Definition: STLUtils.h:258
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
T1::mapped_type * STLFind(T1 *container, const typename T1::key_type &key)
Lookup a value by key in a associative container.
Definition: STLUtils.h:193
T1::mapped_type STLLookupAndRemovePtr(T1 *container, const typename T1::key_type &key)
Remove a value from a pair associative container and return the value.
Definition: STLUtils.h:478
bool STLInsertIfNotPresent(T1 *container, const typename T1::value_type &key_value)
Insert a value into a container only if this value doesn't already exist.
Definition: STLUtils.h:329
T1::mapped_type STLReplacePtr(T1 *container, const typename T1::key_type &key, const typename T1::mapped_type &value)
Replace a value in a pair associative container. If the key existed, the old value is returned...
Definition: STLUtils.h:283