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 <ctime>
34 #include <list>
35 #include <map>
36 #include <set>
37 #include <string>
38 #include <vector>
39 
40 namespace te
41 {
42  namespace common
43  {
44  /*!
45  \brief This function can be applied to a vector of pointers. It will delete each pointer in the vector.
46 
47  \param v The vector of pointers to be cleaned.
48 
49  \note It will not clear the vector at the end of the cleanup.
50  */
51  template<class T> inline void FreeContents(const std::vector<T*>& v)
52  {
53  std::size_t size = v.size();
54 
55  for(std::size_t i = 0; i < size; ++i)
56  delete (v[i]);
57  }
58 
59  /*!
60  \brief This function can be applied to a list of pointers. It will delete each pointer in the vector.
61 
62  \param l The list of pointers to be cleaned.
63 
64  \note It will not clear the list at the end of the cleanup.
65  */
66  template<class T> inline void FreeContents(std::list<T*>& l)
67  {
68  for(typename std::list<T*>::const_iterator it = l.begin(); it != l.end(); ++it)
69  delete (*it);
70  }
71 
72  /*!
73  \brief This function can be applied to a map of pointers. It will delete each pointer in the map.
74 
75  \param m The map of pointers to be cleaned.
76 
77  \note It will not clear the map at the end of the cleanup.
78  */
79  template<class K, class V> inline void FreeContents(std::map<K, V*>& m)
80  {
81  typename std::map<K, V*>::const_iterator it = m.begin();
82  typename std::map<K, V*>::const_iterator itend = m.end();
83 
84  while(it != itend)
85  {
86  delete it->second;
87  ++it;
88  }
89  }
90 
91  /*!
92  \brief This function can be applied to a map of pointers. It will delete each pointer in the map.
93 
94  \param m The map of pointers to be cleaned.
95 
96  \note It will not clear the map at the end of the cleanup.
97  */
98  template<class V, class C> inline void FreeContents(std::set<V*, C>& m)
99  {
100  typename std::set<V*, C>::const_iterator it = m.begin();
101  typename std::set<V*, C>::const_iterator itend = m.end();
102 
103  while(it != itend)
104  {
105  delete *it;
106  ++it;
107  }
108  }
109 
110  /*!
111  \brief This function can be applied to a vector of pointers. It will delete each pointer in the vector.
112 
113  \param v The vector of pointers to be cleaned.
114 
115  \note It will not clear the vector at the end of the cleanup.
116  */
117  template<class IT> inline void FreeContents(IT it, IT itend)
118  {
119  while(it != itend)
120  {
121  delete *it;
122  ++it;
123  }
124  }
125 
126  /*!
127  \brief This function can be applied to a pointer to a vector of pointers.
128 
129  It will delete each pointer in the vector and then delete the pointer to the vector.
130 
131  \param v A pointer to a vector of pointers to be cleaned.
132  */
133  template<class T> inline void Free(std::vector<T*>* v)
134  {
135  if(v == 0)
136  return;
137 
138  std::size_t size = v->size();
139 
140  for(std::size_t i = 0; i < size; ++i)
141  delete ((*v)[i]);
142 
143  delete v;
144  }
145 
146  /*!
147  \brief It releases the char array.
148 
149  \param carray A NULL terminated array of characters pointers.
150  */
151  inline void Free(char** carray)
152  {
153  for(char** aux = carray; *aux; ++aux)
154  delete [] (*aux);
155 
156  delete [] carray;
157  }
158 
159  /*!
160  \brief It releases the given array.
161 
162  \param a The array that will be released.
163  \param s The array size.
164  */
165  template<class T> inline void Free(T** a, std::size_t s)
166  {
167  for(std::size_t i = 0; i < s; ++i)
168  delete [] (a[i]);
169 
170  delete [] a;
171  }
172 
173  /*!
174  \brief It finds for a given key in the map and returns a pointer if something is found or NULL otherwise.
175 
176  \param m The map of pointers where the key will be searched.
177  \param k The key.
178 
179  \return A pointer if something is found or NULL otherwise.
180  */
181  template<class K, class V> inline V* GetPValue(const std::map<K, V*>& m, const K& k)
182  {
183  typename std::map<K, V*>::const_iterator it = m.find(k);
184 
185  if(it != m.end())
186  return it->second;
187 
188  return 0;
189  }
190 
191  /*!
192  \brief It finds for a given key in the map and returns a pointer if something is found or NULL otherwise.
193 
194  \param m The map of pointers where the key will be searched.
195  \param k The key.
196 
197  \return A pointer if something is found or NULL otherwise.
198  */
199  template<class K, class C> inline K* GetPValue(const std::set<K*, C>& m, K* k)
200  {
201  typename std::set<K*, C>::const_iterator it = m.find(k);
202 
203  if(it != m.end())
204  return *it;
205 
206  return 0;
207  }
208 
209  /*!
210  \brief It finds for a given key in the map and returns a pointer if something is found or NULL otherwise.
211 
212  \param m The map of pointers where the key will be searched.
213  \param k The key.
214 
215  \return A pointer if something is found or NULL otherwise.
216 
217  \todo Make m parameter be a constant type!
218  */
219  template<class K, class V> inline V** GetPPValue(std::map<K, V*>& m, const K& k)
220  {
221  typename std::map<K, V*>::iterator it = m.find(k);
222 
223  if(it != m.end())
224  return &(it->second);
225 
226  return 0;
227  }
228 
229  /*!
230  \brief This function can be applied to a vector of pointers.
231 
232  It will clone element by element supposing a vector of pointers. It uses T different from T1 as developer could clone a std::vector<const te::gm::Geometry*> into a std::vector<te::gm::Geometry*>
233 
234  \param src The source vector with pointers to be cloned.
235  \param dst The target vector with cloned pointers.
236 
237  \note The type T must implement a method called clone().
238  */
239  template<class T, class T1> inline void Clone(const std::vector<T*>& src, std::vector<T1*>& dst)
240  {
241  std::size_t size = src.size();
242 
243  dst.reserve(size);
244 
245  for (std::size_t i = 0; i < size; ++i)
246  {
247  if (src[i] == nullptr)
248  {
249  dst.push_back(nullptr);
250  }
251  else
252  {
253  dst.push_back(static_cast<T1*>(src[i]->clone()));
254  }
255  }
256  }
257 
258  /*!
259  \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.
260 
261  \param src The source vector with pointers to be cloned.
262  \param dst The target vector with cloned pointers.
263 
264  \note The type T must implement the copy constructor method.
265  */
266  template<class T> inline void Copy(const std::vector<T*>& src, std::vector<T*>& dst)
267  {
268  std::size_t size = src.size();
269 
270  dst.reserve(size);
271 
272  for(std::size_t i = 0; i < size; ++i)
273  dst.push_back(new T(*src[i]));
274  }
275 
276  template<class T> inline bool Contains(const std::vector<T>& src, const T& value)
277  {
278  return std::find(src.begin(), src.end(), value) != src.end();
279  }
280 
281  inline std::string GetCurrentTime(const std::string& format = "%d.%m.%Y %H:%M:%S")
282  {
283  std::time_t now = std::time(NULL);
284  std::tm* ptm = std::localtime(&now);
285 
286  char buffer[32];
287 
288  // Format: 15.06.2009 20:20:00
289  std::strftime(buffer, 32, format.c_str(), ptm);
290 
291  std::string time(buffer);
292 
293  return time;
294  }
295 
296  } // end namespace common
297 } // end namespace te
298 
299 #endif // __TERRALIB_COMMON_INTERNAL_STLUTILS_H
300 
te::common::Contains
bool Contains(const std::vector< T > &src, const T &value)
Definition: STLUtils.h:276
te::common::Copy
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:266
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::common::Free
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:133
te::common::Clone
void Clone(const std::vector< T * > &src, std::vector< T1 * > &dst)
This function can be applied to a vector of pointers.
Definition: STLUtils.h:239
te::common::FreeContents
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
te::common::GetPPValue
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:219
te::common::GetPValue
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:181
te::common::GetCurrentTime
std::string GetCurrentTime(const std::string &format="%d.%m.%Y %H:%M:%S")
Definition: STLUtils.h:281