Open Lighting Architecture  0.9.5
 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 
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 
432 template<typename T1>
433 bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key) {
434  typename T1::iterator iter = container->find(key);
435  if (iter == container->end()) {
436  return false;
437  } else {
438  delete iter->second;
439  container->erase(iter);
440  return true;
441  }
442 }
443 
454 template<typename T1>
455 typename T1::mapped_type STLLookupAndRemovePtr(
456  T1 *container,
457  const typename T1::key_type &key) {
458  typename T1::iterator iter = container->find(key);
459  if (iter == container->end()) {
460  return NULL;
461  } else {
462  typename T1::mapped_type value = iter->second;
463  container->erase(iter);
464  return value;
465  }
466 }
467 
479 template<typename T1, typename T2>
480 void STLMapFromKeys(T1 *output, const T2 input,
481  typename T1::mapped_type value) {
482  typename T2::const_iterator iter = input.begin();
483  for (; iter != input.end(); ++iter) {
484  std::pair<typename T1::iterator, bool> p = output->insert(
485  typename T1::value_type(*iter, value));
486  if (!p.second) {
487  p.first->second = value;
488  }
489  }
490 }
491 } // namespace ola
492 #endif // INCLUDE_OLA_STL_STLUTILS_H_
493