STLUtils.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/STLUtils.h
22 
23  \brief This file contains several utility functions for dealing with STL containers.
24 
25  \ingroup common
26 */
27 
28 #ifndef __TERRALIB_COMMON_INTERNAL_STLUTILS_H
29 #define __TERRALIB_COMMON_INTERNAL_STLUTILS_H
30 
31 // STL
32 #include <algorithm>
33 #include <list>
34 #include <map>
35 #include <set>
36 #include <vector>
37 
38 namespace te
39 {
40  namespace common
41  {
42  /*!
43  \brief This function can be applied to a vector of pointers. It will delete each pointer in the vector.
44 
45  \param v The vector of pointers to be cleaned.
46 
47  \note It will not clear the vector at the end of the cleanup.
48  */
49  template<class T> inline void FreeContents(const std::vector<T*>& v)
50  {
51  std::size_t size = v.size();
52 
53  for(std::size_t i = 0; i < size; ++i)
54  delete (v[i]);
55  }
56 
57  /*!
58  \brief This function can be applied to a list of pointers. It will delete each pointer in the vector.
59 
60  \param l The list of pointers to be cleaned.
61 
62  \note It will not clear the list at the end of the cleanup.
63  */
64  template<class T> inline void FreeContents(std::list<T*>& l)
65  {
66  for(typename std::list<T*>::const_iterator it = l.begin(); it != l.end(); ++it)
67  delete (*it);
68  }
69 
70  /*!
71  \brief This function can be applied to a map of pointers. It will delete each pointer in the map.
72 
73  \param m The map of pointers to be cleaned.
74 
75  \note It will not clear the map at the end of the cleanup.
76  */
77  template<class K, class V> inline void FreeContents(std::map<K, V*>& m)
78  {
79  typename std::map<K, V*>::const_iterator it = m.begin();
80  typename std::map<K, V*>::const_iterator itend = m.end();
81 
82  while(it != itend)
83  {
84  delete it->second;
85  ++it;
86  }
87  }
88 
89  /*!
90  \brief This function can be applied to a map of pointers. It will delete each pointer in the map.
91 
92  \param m The map of pointers to be cleaned.
93 
94  \note It will not clear the map at the end of the cleanup.
95  */
96  template<class V, class C> inline void FreeContents(std::set<V*, C>& m)
97  {
98  typename std::set<V*, C>::const_iterator it = m.begin();
99  typename std::set<V*, C>::const_iterator itend = m.end();
100 
101  while(it != itend)
102  {
103  delete *it;
104  ++it;
105  }
106  }
107 
108  /*!
109  \brief This function can be applied to a vector of pointers. It will delete each pointer in the vector.
110 
111  \param v The vector of pointers to be cleaned.
112 
113  \note It will not clear the vector at the end of the cleanup.
114  */
115  template<class IT> inline void FreeContents(IT it, IT itend)
116  {
117  while(it != itend)
118  {
119  delete *it;
120  ++it;
121  }
122  }
123 
124  /*!
125  \brief This function can be applied to a pointer to a vector of pointers.
126 
127  It will delete each pointer in the vector and then delete the pointer to the vector.
128 
129  \param v A pointer to a vector of pointers to be cleaned.
130  */
131  template<class T> inline void Free(std::vector<T*>* v)
132  {
133  if(v == 0)
134  return;
135 
136  std::size_t size = v->size();
137 
138  for(std::size_t i = 0; i < size; ++i)
139  delete ((*v)[i]);
140 
141  delete v;
142  }
143 
144  /*!
145  \brief It releases the char array.
146 
147  \param carray A NULL terminated array of characters pointers.
148  */
149  inline void Free(char** carray)
150  {
151  for(char** aux = carray; *aux; ++aux)
152  delete [] (*aux);
153 
154  delete [] carray;
155  }
156 
157  /*!
158  \brief It releases the given array.
159 
160  \param a The array that will be released.
161  \param s The array size.
162  */
163  template<class T> inline void Free(T** a, std::size_t s)
164  {
165  for(std::size_t i = 0; i < s; ++i)
166  delete [] (a[i]);
167 
168  delete [] a;
169  }
170 
171  /*!
172  \brief It finds for a given key in the map and returns a pointer if something is found or NULL otherwise.
173 
174  \param m The map of pointers where the key will be searched.
175  \param k The key.
176 
177  \return A pointer if something is found or NULL otherwise.
178  */
179  template<class K, class V> inline V* GetPValue(const std::map<K, V*>& m, const K& k)
180  {
181  typename std::map<K, V*>::const_iterator it = m.find(k);
182 
183  if(it != m.end())
184  return it->second;
185 
186  return 0;
187  }
188 
189  /*!
190  \brief It finds for a given key in the map and returns a pointer if something is found or NULL otherwise.
191 
192  \param m The map of pointers where the key will be searched.
193  \param k The key.
194 
195  \return A pointer if something is found or NULL otherwise.
196  */
197  template<class K, class C> inline K* GetPValue(const std::set<K*, C>& m, K* k)
198  {
199  typename std::set<K*, C>::const_iterator it = m.find(k);
200 
201  if(it != m.end())
202  return *it;
203 
204  return 0;
205  }
206 
207  /*!
208  \brief It finds for a given key in the map and returns a pointer if something is found or NULL otherwise.
209 
210  \param m The map of pointers where the key will be searched.
211  \param k The key.
212 
213  \return A pointer if something is found or NULL otherwise.
214 
215  \todo Make m parameter be a constant type!
216  */
217  template<class K, class V> inline V** GetPPValue(std::map<K, V*>& m, const K& k)
218  {
219  typename std::map<K, V*>::iterator it = m.find(k);
220 
221  if(it != m.end())
222  return &(it->second);
223 
224  return 0;
225  }
226 
227  /*!
228  \brief This function can be applied to a vector of pointers.
229 
230  It will clone element by element supposing a vector of pointers.
231 
232  \param src The source vector with pointers to be cloned.
233  \param dst The target vector with cloned pointers.
234 
235  \note The type T must implement a method called clone().
236  */
237  template<class T> inline void Clone(const std::vector<T*>& src, std::vector<T*>& dst)
238  {
239  std::size_t size = src.size();
240 
241  dst.reserve(size);
242 
243  for(std::size_t i = 0; i < size; ++i)
244  dst.push_back(static_cast<T*>(src[i]->clone()));
245  }
246 
247  /*!
248  \brief This function can be applied to a vector of pointers. It will copy element by element through its copy constructor supposing a vector of pointers.
249 
250  \param src The source vector with pointers to be cloned.
251  \param dst The target vector with cloned pointers.
252 
253  \note The type T must implement the copy constructor method.
254  */
255  template<class T> inline void Copy(const std::vector<T*>& src, std::vector<T*>& dst)
256  {
257  std::size_t size = src.size();
258 
259  dst.reserve(size);
260 
261  for(std::size_t i = 0; i < size; ++i)
262  dst.push_back(new T(*src[i]));
263  }
264 
265  template<class T> inline bool Contains(const std::vector<T>& src, const T& value)
266  {
267  return std::find(src.begin(), src.end(), value) != src.end();
268  }
269 
270  } // end namespace common
271 } // end namespace te
272 
273 #endif // __TERRALIB_COMMON_INTERNAL_STLUTILS_H
274 
V * GetPValue(const std::map< K, V * > &m, const K &k)
It finds for a given key in the map and returns a pointer if something is found or NULL otherwise...
Definition: STLUtils.h:179
bool Contains(const std::vector< T > &src, const T &value)
Definition: STLUtils.h:265
void Copy(const std::vector< T * > &src, std::vector< T * > &dst)
This function can be applied to a vector of pointers. It will copy element by element through its cop...
Definition: STLUtils.h:255
V ** GetPPValue(std::map< K, V * > &m, const K &k)
It finds for a given key in the map and returns a pointer if something is found or NULL otherwise...
Definition: STLUtils.h:217
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
URI C++ Library.
void Clone(const std::vector< T * > &src, std::vector< T * > &dst)
This function can be applied to a vector of pointers.
Definition: STLUtils.h:237
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55