Open Lighting Architecture  0.9.1
 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  delete *iter;
103  sequence->clear();
104 }
105 
130 template<typename T>
131 void STLDeleteValues(T *container) {
132  typename T::iterator iter = container->begin();
133  for (; iter != container->end(); ++iter)
134  delete iter->second;
135  container->clear();
136 }
137 
138 
146 template<typename T1, typename T2>
147 inline bool STLContains(const T1 &container, const T2 &value) {
148  return container.find(value) != container.end();
149 }
150 
157 template<typename T1>
158 void STLKeys(const T1 &container, std::vector<typename T1::key_type> *keys) {
159  keys->reserve(keys->size() + container.size());
160  typename T1::const_iterator iter = container.begin();
161  for (; iter != container.end(); ++iter)
162  keys->push_back(iter->first);
163 }
164 
172 template<typename T1, typename T2>
173 void STLValues(const T1 &container, std::vector<T2> *values) {
174  values->reserve(values->size() + container.size());
175  typename T1::const_iterator iter = container.begin();
176  for (; iter != container.end(); ++iter)
177  values->push_back(iter->second);
178 }
179 
187 template<typename T1>
188 typename T1::mapped_type* STLFind(T1 *container,
189  const typename T1::key_type &key) {
190  typename T1::iterator iter = container->find(key);
191  if (iter == container->end()) {
192  return NULL;
193  } else {
194  return &iter->second;
195  }
196 }
197 
208 template<typename T1>
209 typename T1::mapped_type STLFindOrNull(const T1 &container,
210  const typename T1::key_type &key) {
211  typename T1::const_iterator iter = container.find(key);
212  if (iter == container.end()) {
213  return NULL;
214  } else {
215  return iter->second;
216  }
217 }
218 
219 
235 template<typename T1>
236 bool STLReplace(T1 *container, const typename T1::key_type &key,
237  const typename T1::mapped_type &value) {
238  std::pair<typename T1::iterator, bool> p = container->insert(
239  typename T1::value_type(key, value));
240  if (!p.second) {
241  p.first->second = value;
242  return true;
243  }
244  return false;
245 }
246 
261 template<typename T1>
262 typename T1::mapped_type STLReplacePtr(T1 *container,
263  const typename T1::key_type &key,
264  const typename T1::mapped_type &value) {
265  std::pair<typename T1::iterator, bool> p = container->insert(
266  typename T1::value_type(key, value));
267  if (!p.second) {
268  typename T1::mapped_type old_value = p.first->second;
269  p.first->second = value;
270  return old_value;
271  }
272  return NULL;
273 }
274 
275 
285 template<typename T1>
286 bool STLReplaceAndDelete(T1 *container, 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  delete p.first->second;
292  p.first->second = value;
293  return true;
294  }
295  return false;
296 }
297 
298 
307 template<typename T1>
308 bool STLInsertIfNotPresent(T1 *container,
309  const typename T1::value_type &key_value) {
310  return container->insert(key_value).second;
311 }
312 
313 
324 template<typename T1>
325 bool STLInsertIfNotPresent(T1 *container, const typename T1::key_type &key,
326  const typename T1::mapped_type &value) {
327  return container->insert(typename T1::value_type(key, value)).second;
328 }
329 
330 
341 template<typename T1>
342 void STLInsertOrDie(T1 *container, const typename T1::key_type &key,
343  const typename T1::mapped_type &value) {
344  assert(container->insert(typename T1::value_type(key, value)).second);
345 }
346 
347 
355 template<typename T1>
356 bool STLRemove(T1 *container, const typename T1::key_type &key) {
357  return container->erase(key);
358 }
359 
372 template<typename T1>
373 bool STLLookupAndRemove(T1 *container,
374  const typename T1::key_type &key,
375  typename T1::mapped_type *value) {
376  typename T1::iterator iter = container->find(key);
377  if (iter == container->end()) {
378  return false;
379  } else {
380  *value = iter->second;
381  container->erase(iter);
382  return true;
383  }
384 }
385 
394 template<typename T1>
395 bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key) {
396  typename T1::iterator iter = container->find(key);
397  if (iter == container->end()) {
398  return false;
399  } else {
400  delete iter->second;
401  container->erase(iter);
402  return true;
403  }
404 }
405 
416 template<typename T1>
417 typename T1::mapped_type STLLookupAndRemovePtr(
418  T1 *container,
419  const typename T1::key_type &key) {
420  typename T1::iterator iter = container->find(key);
421  if (iter == container->end()) {
422  return NULL;
423  } else {
424  typename T1::mapped_type value = iter->second;
425  container->erase(iter);
426  return value;
427  }
428 }
429 } // namespace ola
430 #endif // INCLUDE_OLA_STL_STLUTILS_H_
431