Open Lighting Architecture
 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 
45 using std::map;
46 using std::set;
47 using std::vector;
48 
77 template<typename T>
78 void STLDeleteElements(T *sequence) {
79  typename T::iterator iter = sequence->begin();
80  for (; iter != sequence->end(); ++iter)
81  delete *iter;
82  sequence->clear();
83 }
84 
109 template<typename T>
110 void STLDeleteValues(T *container) {
111  typename T::iterator iter = container->begin();
112  for (; iter != container->end(); ++iter)
113  delete iter->second;
114  container->clear();
115 }
116 
117 
125 template<typename T1, typename T2>
126 inline bool STLContains(const T1 &container, const T2 &value) {
127  return container.find(value) != container.end();
128 }
129 
136 template<typename T1>
137 void STLKeys(const T1 &container, vector<typename T1::key_type> *keys) {
138  keys->reserve(keys->size() + container.size());
139  typename T1::const_iterator iter = container.begin();
140  for (; iter != container.end(); ++iter)
141  keys->push_back(iter->first);
142 }
143 
151 template<typename T1, typename T2>
152 void STLValues(const T1 &container, vector<T2> *values) {
153  values->reserve(values->size() + container.size());
154  typename T1::const_iterator iter = container.begin();
155  for (; iter != container.end(); ++iter)
156  values->push_back(iter->second);
157 }
158 
166 template<typename T1>
167 typename T1::mapped_type* STLFind(T1 *container,
168  const typename T1::key_type &key) {
169  typename T1::iterator iter = container->find(key);
170  if (iter == container->end()) {
171  return NULL;
172  } else {
173  return &iter->second;
174  }
175 }
176 
187 template<typename T1>
188 typename T1::mapped_type STLFindOrNull(const T1 &container,
189  const typename T1::key_type &key) {
190  typename T1::const_iterator iter = container.find(key);
191  if (iter == container.end()) {
192  return NULL;
193  } else {
194  return iter->second;
195  }
196 }
197 
198 
214 template<typename T1>
215 bool STLReplace(T1 *container, const typename T1::key_type &key,
216  const typename T1::mapped_type &value) {
217  std::pair<typename T1::iterator, bool> p = container->insert(
218  typename T1::value_type(key, value));
219  if (!p.second) {
220  p.first->second = value;
221  return true;
222  }
223  return false;
224 }
225 
240 template<typename T1>
241 typename T1::mapped_type STLReplacePtr(T1 *container,
242  const typename T1::key_type &key,
243  const typename T1::mapped_type &value) {
244  std::pair<typename T1::iterator, bool> p = container->insert(
245  typename T1::value_type(key, value));
246  if (!p.second) {
247  typename T1::mapped_type old_value = p.first->second;
248  p.first->second = value;
249  return old_value;
250  }
251  return NULL;
252 }
253 
254 
264 template<typename T1>
265 bool STLReplaceAndDelete(T1 *container, const typename T1::key_type &key,
266  const typename T1::mapped_type &value) {
267  std::pair<typename T1::iterator, bool> p = container->insert(
268  typename T1::value_type(key, value));
269  if (!p.second) {
270  delete p.first->second;
271  p.first->second = value;
272  return true;
273  }
274  return false;
275 }
276 
277 
286 template<typename T1>
287 bool STLInsertIfNotPresent(T1 *container,
288  const typename T1::value_type &key_value) {
289  return container->insert(key_value).second;
290 }
291 
292 
303 template<typename T1>
304 bool STLInsertIfNotPresent(T1 *container, const typename T1::key_type &key,
305  const typename T1::mapped_type &value) {
306  return container->insert(typename T1::value_type(key, value)).second;
307 }
308 
309 
320 template<typename T1>
321 void STLInsertOrDie(T1 *container, const typename T1::key_type &key,
322  const typename T1::mapped_type &value) {
323  assert(container->insert(typename T1::value_type(key, value)).second);
324 }
325 
326 
334 template<typename T1>
335 bool STLRemove(T1 *container, const typename T1::key_type &key) {
336  return container->erase(key);
337 }
338 
351 template<typename T1>
352 bool STLLookupAndRemove(T1 *container,
353  const typename T1::key_type &key,
354  typename T1::mapped_type *value) {
355  typename T1::iterator iter = container->find(key);
356  if (iter == container->end()) {
357  return false;
358  } else {
359  *value = iter->second;
360  container->erase(iter);
361  return true;
362  }
363 }
364 
373 template<typename T1>
374 bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key) {
375  typename T1::iterator iter = container->find(key);
376  if (iter == container->end()) {
377  return false;
378  } else {
379  delete iter->second;
380  container->erase(iter);
381  return true;
382  }
383 }
384 
395 template<typename T1>
396 typename T1::mapped_type STLLookupAndRemovePtr(
397  T1 *container,
398  const typename T1::key_type &key) {
399  typename T1::iterator iter = container->find(key);
400  if (iter == container->end()) {
401  return NULL;
402  } else {
403  typename T1::mapped_type value = iter->second;
404  container->erase(iter);
405  return value;
406  }
407 }
408 } // namespace ola
409 #endif // INCLUDE_OLA_STL_STLUTILS_H_
410