Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 
73 template<typename T>
74 void STLDeleteElements(T *sequence) {
75  typename T::iterator iter = sequence->begin();
76  for (; iter != sequence->end(); ++iter)
77  delete *iter;
78  sequence->clear();
79 }
80 
105 template<typename T>
106 void STLDeleteValues(T *container) {
107  typename T::iterator iter = container->begin();
108  for (; iter != container->end(); ++iter)
109  delete iter->second;
110  container->clear();
111 }
112 
113 
121 template<typename T1, typename T2>
122 inline bool STLContains(const T1 &container, const T2 &value) {
123  return container.find(value) != container.end();
124 }
125 
132 template<typename T1>
133 void STLKeys(const T1 &container, std::vector<typename T1::key_type> *keys) {
134  keys->reserve(keys->size() + container.size());
135  typename T1::const_iterator iter = container.begin();
136  for (; iter != container.end(); ++iter)
137  keys->push_back(iter->first);
138 }
139 
147 template<typename T1, typename T2>
148 void STLValues(const T1 &container, std::vector<T2> *values) {
149  values->reserve(values->size() + container.size());
150  typename T1::const_iterator iter = container.begin();
151  for (; iter != container.end(); ++iter)
152  values->push_back(iter->second);
153 }
154 
162 template<typename T1>
163 typename T1::mapped_type* STLFind(T1 *container,
164  const typename T1::key_type &key) {
165  typename T1::iterator iter = container->find(key);
166  if (iter == container->end()) {
167  return NULL;
168  } else {
169  return &iter->second;
170  }
171 }
172 
183 template<typename T1>
184 typename T1::mapped_type STLFindOrNull(const T1 &container,
185  const typename T1::key_type &key) {
186  typename T1::const_iterator iter = container.find(key);
187  if (iter == container.end()) {
188  return NULL;
189  } else {
190  return iter->second;
191  }
192 }
193 
194 
210 template<typename T1>
211 bool STLReplace(T1 *container, const typename T1::key_type &key,
212  const typename T1::mapped_type &value) {
213  std::pair<typename T1::iterator, bool> p = container->insert(
214  typename T1::value_type(key, value));
215  if (!p.second) {
216  p.first->second = value;
217  return true;
218  }
219  return false;
220 }
221 
236 template<typename T1>
237 typename T1::mapped_type STLReplacePtr(T1 *container,
238  const typename T1::key_type &key,
239  const typename T1::mapped_type &value) {
240  std::pair<typename T1::iterator, bool> p = container->insert(
241  typename T1::value_type(key, value));
242  if (!p.second) {
243  typename T1::mapped_type old_value = p.first->second;
244  p.first->second = value;
245  return old_value;
246  }
247  return NULL;
248 }
249 
250 
260 template<typename T1>
261 bool STLReplaceAndDelete(T1 *container, const typename T1::key_type &key,
262  const typename T1::mapped_type &value) {
263  std::pair<typename T1::iterator, bool> p = container->insert(
264  typename T1::value_type(key, value));
265  if (!p.second) {
266  delete p.first->second;
267  p.first->second = value;
268  return true;
269  }
270  return false;
271 }
272 
273 
282 template<typename T1>
283 bool STLInsertIfNotPresent(T1 *container,
284  const typename T1::value_type &key_value) {
285  return container->insert(key_value).second;
286 }
287 
288 
299 template<typename T1>
300 bool STLInsertIfNotPresent(T1 *container, const typename T1::key_type &key,
301  const typename T1::mapped_type &value) {
302  return container->insert(typename T1::value_type(key, value)).second;
303 }
304 
305 
316 template<typename T1>
317 void STLInsertOrDie(T1 *container, const typename T1::key_type &key,
318  const typename T1::mapped_type &value) {
319  assert(container->insert(typename T1::value_type(key, value)).second);
320 }
321 
322 
330 template<typename T1>
331 bool STLRemove(T1 *container, const typename T1::key_type &key) {
332  return container->erase(key);
333 }
334 
347 template<typename T1>
348 bool STLLookupAndRemove(T1 *container,
349  const typename T1::key_type &key,
350  typename T1::mapped_type *value) {
351  typename T1::iterator iter = container->find(key);
352  if (iter == container->end()) {
353  return false;
354  } else {
355  *value = iter->second;
356  container->erase(iter);
357  return true;
358  }
359 }
360 
369 template<typename T1>
370 bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key) {
371  typename T1::iterator iter = container->find(key);
372  if (iter == container->end()) {
373  return false;
374  } else {
375  delete iter->second;
376  container->erase(iter);
377  return true;
378  }
379 }
380 
391 template<typename T1>
392 typename T1::mapped_type STLLookupAndRemovePtr(
393  T1 *container,
394  const typename T1::key_type &key) {
395  typename T1::iterator iter = container->find(key);
396  if (iter == container->end()) {
397  return NULL;
398  } else {
399  typename T1::mapped_type value = iter->second;
400  container->erase(iter);
401  return value;
402  }
403 }
404 } // namespace ola
405 #endif // INCLUDE_OLA_STL_STLUTILS_H_
406