UStl.h
Go to the documentation of this file.
1 // Taken from UtiLite library r185 [www.utilite.googlecode.com]
2 
3 /*
4 * utilite is a cross-platform library with
5 * useful utilities for fast and small developing.
6 * Copyright (C) 2010 Mathieu Labbe
7 *
8 * utilite is free library: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * utilite is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef USTL_H
23 #define USTL_H
24 
25 #include <list>
26 #include <map>
27 #include <set>
28 #include <vector>
29 #include <string>
30 #include <algorithm>
31 
46 template<class K, class V>
47 inline std::list<K> uUniqueKeys(const std::multimap<K, V> & mm)
48 {
49  std::list<K> l;
50  typename std::list<K>::reverse_iterator lastValue;
51  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
52  {
53  if(iter == mm.begin() || (iter != mm.begin() && *lastValue != iter->first))
54  {
55  l.push_back(iter->first);
56  lastValue = l.rbegin();
57  }
58  }
59  return l;
60 }
61 
67 template<class K, class V>
68 inline std::list<K> uKeys(const std::multimap<K, V> & mm)
69 {
70  std::list<K> l;
71  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
72  {
73  l.push_back(iter->first);
74  }
75  return l;
76 }
77 
83 template<class K, class V>
84 inline std::list<V> uValues(const std::multimap<K, V> & mm)
85 {
86  std::list<V> l;
87  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
88  {
89  l.push_back(iter->second);
90  }
91  return l;
92 }
93 
100 template<class K, class V>
101 inline std::list<V> uValues(const std::multimap<K, V> & mm, const K & key)
102 {
103  std::list<V> l;
104  std::pair<typename std::multimap<K, V>::const_iterator, typename std::multimap<K, V>::const_iterator> range;
105  range = mm.equal_range(key);
106  for(typename std::multimap<K, V>::const_iterator iter = range.first; iter!=range.second; ++iter)
107  {
108  l.push_back(iter->second);
109  }
110  return l;
111 }
112 
118 template<class K, class V>
119 inline std::vector<K> uKeys(const std::map<K, V> & m)
120 {
121  std::vector<K> v(m.size());
122  int i=0;
123  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
124  {
125  v[i] = iter->first;
126  ++i;
127  }
128  return v;
129 }
130 
136 template<class K, class V>
137 inline std::list<K> uKeysList(const std::map<K, V> & m)
138 {
139  std::list<K> l;
140  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
141  {
142  l.push_back(iter->first);
143  }
144  return l;
145 }
146 
152 template<class K, class V>
153 inline std::set<K> uKeysSet(const std::map<K, V> & m)
154 {
155  std::set<K> s;
156  int i=0;
157  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
158  {
159  s.insert(s.end(), iter->first);
160  ++i;
161  }
162  return s;
163 }
164 
170 template<class K, class V>
171 inline std::vector<V> uValues(const std::map<K, V> & m)
172 {
173  std::vector<V> v(m.size());
174  int i=0;
175  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
176  {
177  v[i] = iter->second;
178  ++i;
179  }
180  return v;
181 }
182 
188 template<class K, class V>
189 inline std::list<V> uValuesList(const std::map<K, V> & m)
190 {
191  std::list<V> l;
192  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
193  {
194  l.push_back(iter->second);
195  }
196  return l;
197 }
198 
206 template<class K, class V>
207 inline V uValue(const std::map<K, V> & m, const K & key, const V & defaultValue = V())
208 {
209  V v = defaultValue;
210  typename std::map<K, V>::const_iterator i = m.find(key);
211  if(i != m.end())
212  {
213  v = i->second;
214  }
215  return v;
216 }
217 
226 template<class K, class V>
227 inline V uTake(std::map<K, V> & m, const K & key, const V & defaultValue = V())
228 {
229  V v;
230  typename std::map<K, V>::iterator i = m.find(key);
231  if(i != m.end())
232  {
233  v = i->second;
234  m.erase(i);
235  }
236  else
237  {
238  v = defaultValue;
239  }
240  return v;
241 }
242 
250 template<class V>
251 inline typename std::list<V>::iterator uIteratorAt(std::list<V> & list, const unsigned int & pos)
252 {
253  typename std::list<V>::iterator iter = list.begin();
254  for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
255  {
256  ++iter;
257  }
258  return iter;
259 }
260 
268 template<class V>
269 inline typename std::list<V>::const_iterator uIteratorAt(const std::list<V> & list, const unsigned int & pos)
270 {
271  typename std::list<V>::const_iterator iter = list.begin();
272  for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
273  {
274  ++iter;
275  }
276  return iter;
277 }
278 
286 template<class V>
287 inline typename std::vector<V>::iterator uIteratorAt(std::vector<V> & v, const unsigned int & pos)
288 {
289  return v.begin() + pos;
290 }
291 
299 template<class V>
300 inline V & uValueAt(std::list<V> & list, const unsigned int & pos)
301 {
302  typename std::list<V>::iterator iter = uIteratorAt(list, pos);
303  return *iter;
304 }
305 
313 template<class V>
314 inline const V & uValueAt(const std::list<V> & list, const unsigned int & pos)
315 {
316  typename std::list<V>::const_iterator iter = uIteratorAt(list, pos);
317  return *iter;
318 }
319 
326 template<class V>
327 inline bool uContains(const std::list<V> & list, const V & value)
328 {
329  return std::find(list.begin(), list.end(), value) != list.end();
330 }
331 
338 template<class K, class V>
339 inline bool uContains(const std::map<K, V> & map, const K & key)
340 {
341  return map.find(key) != map.end();
342 }
343 
350 template<class K, class V>
351 inline bool uContains(const std::multimap<K, V> & map, const K & key)
352 {
353  return map.find(key) != map.end();
354 }
355 
360 template<class K, class V>
361 inline void uInsert(std::map<K, V> & map, const std::pair<K, V> & pair)
362 {
363  std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(pair);
364  if(inserted.second == false)
365  {
366  inserted.first->second = pair.second;
367  }
368 }
369 
375 template<class V>
376 inline std::vector<V> uListToVector(const std::list<V> & list)
377 {
378  return std::vector<V>(list.begin(), list.end());
379 }
380 
386 template<class V>
387 inline std::list<V> uVectorToList(const std::vector<V> & v)
388 {
389  return std::list<V>(v.begin(), v.end());
390 }
391 
397 template<class V>
398 inline void uAppend(std::list<V> & list, const std::list<V> & newItems)
399 {
400  list.insert(list.end(), newItems.begin(), newItems.end());
401 }
402 
410 template<class V>
411 inline int uIndexOf(const std::vector<V> & list, const V & value)
412 {
413  int index=-1;
414  int i=0;
415  for(typename std::vector<V>::const_iterator iter = list.begin(); iter!=list.end(); ++iter)
416  {
417  if(*iter == value)
418  {
419  index = i;
420  break;
421  }
422  ++i;
423  }
424  return index;
425 }
426 
438 inline std::list<std::string> uSplit(const std::string & str, char separator = ' ')
439 {
440  std::list<std::string> v;
441  std::string buf;
442  for(unsigned int i=0; i<str.size(); ++i)
443  {
444  if(str[i] != separator)
445  {
446  buf += str[i];
447  }
448  else if(buf.size())
449  {
450  v.push_back(buf);
451  buf = "";
452  }
453  }
454  if(buf.size())
455  {
456  v.push_back(buf);
457  }
458  return v;
459 }
460 
466 inline bool uIsDigit(const char c)
467 {
468  return c >= '0' && c <= '9';
469 }
470 
481 inline std::list<std::string> uSplitNumChar(const std::string & str)
482 {
483  std::list<std::string> list;
484  std::string buf;
485  bool num = false;
486  for(unsigned int i=0; i<str.size(); ++i)
487  {
488  if(uIsDigit(str[i]))
489  {
490  if(!num && buf.size())
491  {
492  list.push_back(buf);
493  buf.clear();
494  }
495  buf += str[i];
496  num = true;
497  }
498  else
499  {
500  if(num)
501  {
502  list.push_back(buf);
503  buf.clear();
504  }
505  buf += str[i];
506  num = false;
507  }
508  }
509  if(buf.size())
510  {
511  list.push_back(buf);
512  }
513  return list;
514 }
515 
528 inline int uStrNumCmp(const std::string & a, const std::string & b)
529 {
530  std::vector<std::string> listA;
531  std::vector<std::string> listB;
532 
533  listA = uListToVector(uSplitNumChar(a));
534  listB = uListToVector(uSplitNumChar(b));
535 
536  unsigned int i;
537  int result = 0;
538  for(i=0; i<listA.size() && i<listB.size(); ++i)
539  {
540  if(uIsDigit(listA[i].at(0)) && uIsDigit(listB[i].at(0)))
541  {
542  //padding if zeros at the beginning
543  if(listA[i].at(0) == '0' && listB[i].size() < listA[i].size())
544  {
545  while(listB[i].size() < listA[i].size())
546  {
547  listB[i] += '0';
548  }
549  }
550  else if(listB[i].at(0) == '0' && listA[i].size() < listB[i].size())
551  {
552  while(listA[i].size() < listB[i].size())
553  {
554  listA[i] += '0';
555  }
556  }
557 
558  if(listB[i].size() < listA[i].size())
559  {
560  result = 1;
561  }
562  else if(listB[i].size() > listA[i].size())
563  {
564  result = -1;
565  }
566  else
567  {
568  result = listA[i].compare(listB[i]);
569  }
570  }
571  else if(uIsDigit(listA[i].at(0)))
572  {
573  result = -1;
574  }
575  else if(uIsDigit(listB[i].at(0)))
576  {
577  result = 1;
578  }
579  else
580  {
581  result = listA[i].compare(listB[i]);
582  }
583 
584  if(result != 0)
585  {
586  break;
587  }
588  }
589 
590  return result;
591 }
592 
593 #endif /* USTL_H */
std::list< K > uUniqueKeys(const std::multimap< K, V > &mm)
Definition: UStl.h:47
std::list< K > uKeysList(const std::map< K, V > &m)
Definition: UStl.h:137
bool uIsDigit(const char c)
Definition: UStl.h:466
V & uValueAt(std::list< V > &list, const unsigned int &pos)
Definition: UStl.h:300
int uStrNumCmp(const std::string &a, const std::string &b)
Definition: UStl.h:528
XmlRpcServer s
std::set< K > uKeysSet(const std::map< K, V > &m)
Definition: UStl.h:153
V uTake(std::map< K, V > &m, const K &key, const V &defaultValue=V())
Definition: UStl.h:227
std::list< std::string > uSplit(const std::string &str, char separator= ' ')
Definition: UStl.h:438
std::list< V > uVectorToList(const std::vector< V > &v)
Definition: UStl.h:387
std::list< std::string > uSplitNumChar(const std::string &str)
Definition: UStl.h:481
std::list< V > uValues(const std::multimap< K, V > &mm)
Definition: UStl.h:84
bool uContains(const std::list< V > &list, const V &value)
Definition: UStl.h:327
std::vector< V > uListToVector(const std::list< V > &list)
Definition: UStl.h:376
std::list< V >::iterator uIteratorAt(std::list< V > &list, const unsigned int &pos)
Definition: UStl.h:251
std::list< K > uKeys(const std::multimap< K, V > &mm)
Definition: UStl.h:68
std::list< V > uValuesList(const std::map< K, V > &m)
Definition: UStl.h:189
void uAppend(std::list< V > &list, const std::list< V > &newItems)
Definition: UStl.h:398
int uIndexOf(const std::vector< V > &list, const V &value)
Definition: UStl.h:411
V uValue(const std::map< K, V > &m, const K &key, const V &defaultValue=V())
Definition: UStl.h:207
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
Definition: UStl.h:361


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26