UStl.h
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef USTL_H
21 #define USTL_H
22 
23 #include <list>
24 #include <map>
25 #include <set>
26 #include <vector>
27 #include <string>
28 #include <algorithm>
29 #include <stdlib.h>
30 
45 template<class K, class V>
46 inline std::list<K> uUniqueKeys(const std::multimap<K, V> & mm)
47 {
48  std::list<K> l;
49  typename std::list<K>::reverse_iterator lastValue;
50  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
51  {
52  if(iter == mm.begin() || (iter != mm.begin() && *lastValue != iter->first))
53  {
54  l.push_back(iter->first);
55  lastValue = l.rbegin();
56  }
57  }
58  return l;
59 }
60 
66 template<class K, class V>
67 inline std::vector<K> uKeys(const std::multimap<K, V> & mm)
68 {
69  std::vector<K> v(mm.size());
70  int i=0;
71  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
72  {
73  v[i++] = iter->first;
74  }
75  return v;
76 }
77 
83 template<class K, class V>
84 inline std::list<K> uKeysList(const std::multimap<K, V> & mm)
85 {
86  std::list<K> l;
87  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
88  {
89  l.push_back(iter->first);
90  }
91  return l;
92 }
93 
99 template<class K, class V>
100 inline std::vector<V> uValues(const std::multimap<K, V> & mm)
101 {
102  std::vector<V> v(mm.size());
103  int i=0;
104  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
105  {
106  v[i++] = iter->second;
107  }
108  return v;
109 }
110 
116 template<class K, class V>
117 inline std::list<V> uValuesList(const std::multimap<K, V> & mm)
118 {
119  std::list<V> l;
120  for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
121  {
122  l.push_back(iter->second);
123  }
124  return l;
125 }
126 
133 template<class K, class V>
134 inline std::list<V> uValues(const std::multimap<K, V> & mm, const K & key)
135 {
136  std::list<V> l;
137  std::pair<typename std::multimap<K, V>::const_iterator, typename std::multimap<K, V>::const_iterator> range;
138  range = mm.equal_range(key);
139  for(typename std::multimap<K, V>::const_iterator iter = range.first; iter!=range.second; ++iter)
140  {
141  l.push_back(iter->second);
142  }
143  return l;
144 }
145 
151 template<class K, class V>
152 inline std::vector<K> uKeys(const std::map<K, V> & m)
153 {
154  std::vector<K> v(m.size());
155  int i=0;
156  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
157  {
158  v[i] = iter->first;
159  ++i;
160  }
161  return v;
162 }
163 
169 template<class K, class V>
170 inline std::list<K> uKeysList(const std::map<K, V> & m)
171 {
172  std::list<K> l;
173  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
174  {
175  l.push_back(iter->first);
176  }
177  return l;
178 }
179 
185 template<class K, class V>
186 inline std::set<K> uKeysSet(const std::map<K, V> & m)
187 {
188  std::set<K> s;
189  int i=0;
190  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
191  {
192  s.insert(s.end(), iter->first);
193  ++i;
194  }
195  return s;
196 }
197 
203 template<class K, class V>
204 inline std::vector<V> uValues(const std::map<K, V> & m)
205 {
206  std::vector<V> v(m.size());
207  int i=0;
208  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
209  {
210  v[i] = iter->second;
211  ++i;
212  }
213  return v;
214 }
215 
221 template<class K, class V>
222 inline std::list<V> uValuesList(const std::map<K, V> & m)
223 {
224  std::list<V> l;
225  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
226  {
227  l.push_back(iter->second);
228  }
229  return l;
230 }
231 
239 template<class K, class V>
240 inline V uValue(const std::map<K, V> & m, const K & key, const V & defaultValue = V())
241 {
242  V v = defaultValue;
243  typename std::map<K, V>::const_iterator i = m.find(key);
244  if(i != m.end())
245  {
246  v = i->second;
247  }
248  return v;
249 }
250 
259 template<class K, class V>
260 inline V uTake(std::map<K, V> & m, const K & key, const V & defaultValue = V())
261 {
262  V v;
263  typename std::map<K, V>::iterator i = m.find(key);
264  if(i != m.end())
265  {
266  v = i->second;
267  m.erase(i);
268  }
269  else
270  {
271  v = defaultValue;
272  }
273  return v;
274 }
275 
283 template<class V>
284 inline typename std::list<V>::iterator uIteratorAt(std::list<V> & list, const unsigned int & pos)
285 {
286  typename std::list<V>::iterator iter = list.begin();
287  for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
288  {
289  ++iter;
290  }
291  return iter;
292 }
293 
301 template<class V>
302 inline typename std::list<V>::const_iterator uIteratorAt(const std::list<V> & list, const unsigned int & pos)
303 {
304  typename std::list<V>::const_iterator iter = list.begin();
305  for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
306  {
307  ++iter;
308  }
309  return iter;
310 }
311 
319 template<class V>
320 inline typename std::set<V>::iterator uIteratorAt(std::set<V> & set, const unsigned int & pos)
321 {
322  typename std::set<V>::iterator iter = set.begin();
323  for(unsigned int i = 0; i<pos && iter != set.end(); ++i )
324  {
325  ++iter;
326  }
327  return iter;
328 }
329 
337 template<class V>
338 inline typename std::set<V>::const_iterator uIteratorAt(const std::set<V> & set, const unsigned int & pos)
339 {
340  typename std::set<V>::const_iterator iter = set.begin();
341  for(unsigned int i = 0; i<pos && iter != set.end(); ++i )
342  {
343  ++iter;
344  }
345  return iter;
346 }
347 
355 template<class V>
356 inline typename std::vector<V>::iterator uIteratorAt(std::vector<V> & v, const unsigned int & pos)
357 {
358  return v.begin() + pos;
359 }
360 
368 template<class V>
369 inline typename std::vector<V>::const_iterator uIteratorAt(const std::vector<V> & v, const unsigned int & pos)
370 {
371  return v.begin() + pos;
372 }
373 
381 template<class V>
382 inline V & uValueAt(std::list<V> & list, const unsigned int & pos)
383 {
384  typename std::list<V>::iterator iter = uIteratorAt(list, pos);
385  return *iter;
386 }
387 
395 template<class V>
396 inline const V & uValueAt(const std::list<V> & list, const unsigned int & pos)
397 {
398  typename std::list<V>::const_iterator iter = uIteratorAt(list, pos);
399  return *iter;
400 }
401 
408 template<class V>
409 inline bool uContains(const std::list<V> & list, const V & value)
410 {
411  return std::find(list.begin(), list.end(), value) != list.end();
412 }
413 
420 template<class K, class V>
421 inline bool uContains(const std::map<K, V> & map, const K & key)
422 {
423  return map.find(key) != map.end();
424 }
425 
432 template<class K, class V>
433 inline bool uContains(const std::multimap<K, V> & map, const K & key)
434 {
435  return map.find(key) != map.end();
436 }
437 
442 template<class K, class V>
443 inline void uInsert(std::map<K, V> & map, const std::pair<K, V> & pair)
444 {
445  std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(pair);
446  if(inserted.second == false)
447  {
448  inserted.first->second = pair.second;
449  }
450 }
451 
456 template<class K, class V>
457 inline void uInsert(std::map<K, V> & map, const std::map<K, V> & items)
458 {
459  for(typename std::map<K, V>::const_iterator iter=items.begin(); iter!=items.end(); ++iter)
460  {
461  std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(*iter);
462  if(inserted.second == false)
463  {
464  inserted.first->second = iter->second;
465  }
466  }
467 }
468 
474 template<class V>
475 inline std::vector<V> uListToVector(const std::list<V> & list)
476 {
477  return std::vector<V>(list.begin(), list.end());
478 }
479 
485 template<class V>
486 inline std::list<V> uVectorToList(const std::vector<V> & v)
487 {
488  return std::list<V>(v.begin(), v.end());
489 }
490 
495 template<class K, class V>
496 inline std::map<K, V> uMultimapToMap(const std::multimap<K, V> & m)
497 {
498  return std::map<K, V>(m.begin(), m.end());
499 }
500 
504 template<class K, class V>
505 inline std::map<K, V> uMultimapToMapUnique(const std::multimap<K, V> & m)
506 {
507  std::map<K, V> mapOut;
508  std::list<K> uniqueKeys = uUniqueKeys(m);
509  for(typename std::list<K>::const_iterator iter = uniqueKeys.begin(); iter!=uniqueKeys.end(); ++iter)
510  {
511  if(m.count(*iter) == 1)
512  {
513  typename std::multimap<K, V>::const_iterator jter=m.find(*iter);
514  mapOut.insert(mapOut.end(), std::pair<K,V>(jter->first, jter->second));
515  }
516  }
517  return mapOut;
518 }
519 
525 template<class V>
526 inline void uAppend(std::list<V> & list, const std::list<V> & newItems)
527 {
528  list.insert(list.end(), newItems.begin(), newItems.end());
529 }
530 
538 template<class V>
539 inline int uIndexOf(const std::vector<V> & list, const V & value)
540 {
541  int index=-1;
542  int i=0;
543  for(typename std::vector<V>::const_iterator iter = list.begin(); iter!=list.end(); ++iter)
544  {
545  if(*iter == value)
546  {
547  index = i;
548  break;
549  }
550  ++i;
551  }
552  return index;
553 }
554 
566 inline std::list<std::string> uSplit(const std::string & str, char separator = ' ')
567 {
568  std::list<std::string> v;
569  std::string buf;
570  for(unsigned int i=0; i<str.size(); ++i)
571  {
572  if(str[i] != separator)
573  {
574  buf += str[i];
575  }
576  else if(buf.size())
577  {
578  v.push_back(buf);
579  buf = "";
580  }
581  }
582  if(buf.size())
583  {
584  v.push_back(buf);
585  }
586  return v;
587 }
588 
603 inline std::string uJoin(const std::list<std::string> & strings, const std::string & separator = "")
604 {
605  std::string out;
606  for(std::list<std::string>::const_iterator iter = strings.begin(); iter!=strings.end(); ++iter)
607  {
608  if(iter!=strings.begin() && !separator.empty())
609  {
610  out += separator;
611  }
612  out+=*iter;
613  }
614  return out;
615 }
616 
622 inline bool uIsDigit(const char c)
623 {
624  return c >= '0' && c <= '9';
625 }
626 
632 inline bool uIsInteger(const std::string & str, bool checkForSign = true)
633 {
634  bool isInteger = str.size()!=0;
635  for(unsigned int i=0; i<str.size() && isInteger; ++i)
636  {
637  isInteger = (checkForSign && i==0 && str[i]=='-') || uIsDigit(str[i]);
638  }
639  return isInteger;
640 }
641 
647 inline bool uIsNumber(const std::string & str)
648 {
649  std::list<std::string> list = uSplit(str, '.');
650  if(list.size() == 1)
651  {
652  return uIsInteger(str);
653  }
654  else if(list.size() == 2)
655  {
656  return uIsInteger(list.front()) && uIsInteger(list.back(), false);
657  }
658  return false;
659 }
660 
661 
672 inline std::list<std::string> uSplitNumChar(const std::string & str)
673 {
674  std::list<std::string> list;
675  std::string buf;
676  bool num = false;
677  for(unsigned int i=0; i<str.size(); ++i)
678  {
679  if(uIsDigit(str[i]))
680  {
681  if(!num && buf.size())
682  {
683  list.push_back(buf);
684  buf.clear();
685  }
686  buf += str[i];
687  num = true;
688  }
689  else
690  {
691  if(num)
692  {
693  list.push_back(buf);
694  buf.clear();
695  }
696  buf += str[i];
697  num = false;
698  }
699  }
700  if(buf.size())
701  {
702  list.push_back(buf);
703  }
704  return list;
705 }
706 
719 inline int uStrNumCmp(const std::string & a, const std::string & b)
720 {
721  std::vector<std::string> listA;
722  std::vector<std::string> listB;
723 
724  listA = uListToVector(uSplitNumChar(a));
725  listB = uListToVector(uSplitNumChar(b));
726 
727  unsigned int i;
728  int result = 0;
729  for(i=0; i<listA.size() && i<listB.size(); ++i)
730  {
731  if(uIsDigit(listA[i].at(0)) && uIsDigit(listB[i].at(0)))
732  {
733  //padding if zeros at the beginning
734  if(listA[i].at(0) == '0' && listB[i].size() < listA[i].size())
735  {
736  while(listB[i].size() < listA[i].size())
737  {
738  listB[i] += '0';
739  }
740  }
741  else if(listB[i].at(0) == '0' && listA[i].size() < listB[i].size())
742  {
743  while(listA[i].size() < listB[i].size())
744  {
745  listA[i] += '0';
746  }
747  }
748 
749  if(listB[i].size() < listA[i].size())
750  {
751  result = 1;
752  }
753  else if(listB[i].size() > listA[i].size())
754  {
755  result = -1;
756  }
757  else
758  {
759  result = listA[i].compare(listB[i]);
760  }
761  }
762  else if(uIsDigit(listA[i].at(0)))
763  {
764  result = -1;
765  }
766  else if(uIsDigit(listB[i].at(0)))
767  {
768  result = 1;
769  }
770  else
771  {
772  result = listA[i].compare(listB[i]);
773  }
774 
775  if(result != 0)
776  {
777  break;
778  }
779  }
780 
781  return result;
782 }
783 
787 inline bool uStrContains(const std::string & string, const std::string & substring)
788 {
789  return string.find(substring) != std::string::npos;
790 }
791 
792 inline int uCompareVersion(const std::string & version, int major, int minor=-1, int patch=-1)
793 {
794  std::vector<std::string> v = uListToVector(uSplit(version, '.'));
795  if(v.size() == 3)
796  {
797  int vMajor = atoi(v[0].c_str());
798  int vMinor = atoi(v[1].c_str());
799  int vPatch = atoi(v[2].c_str());
800  if(vMajor > major ||
801  (vMajor == major && minor!=-1 && vMinor > minor) ||
802  (vMajor == major && minor!=-1 && vMinor == minor && patch!=-1 && vPatch > patch))
803  {
804  return 1;
805  }
806  else if(vMajor == major && (minor == -1 || (vMinor == minor && (patch == -1 || vPatch == patch))))
807  {
808  return 0;
809  }
810  }
811  return -1;
812 }
813 
814 #endif /* USTL_H */
bool uIsInteger(const std::string &str, bool checkForSign=true)
Definition: UStl.h:632
std::list< K > uUniqueKeys(const std::multimap< K, V > &mm)
Definition: UStl.h:46
std::map< K, V > uMultimapToMapUnique(const std::multimap< K, V > &m)
Definition: UStl.h:505
std::vector< K > uKeys(const std::multimap< K, V > &mm)
Definition: UStl.h:67
bool uIsDigit(const char c)
Definition: UStl.h:622
V & uValueAt(std::list< V > &list, const unsigned int &pos)
Definition: UStl.h:382
int uStrNumCmp(const std::string &a, const std::string &b)
Definition: UStl.h:719
std::set< K > uKeysSet(const std::map< K, V > &m)
Definition: UStl.h:186
std::list< V > uValuesList(const std::multimap< K, V > &mm)
Definition: UStl.h:117
V uTake(std::map< K, V > &m, const K &key, const V &defaultValue=V())
Definition: UStl.h:260
bool uStrContains(const std::string &string, const std::string &substring)
Definition: UStl.h:787
std::list< std::string > uSplit(const std::string &str, char separator= ' ')
Definition: UStl.h:566
std::string uJoin(const std::list< std::string > &strings, const std::string &separator="")
Definition: UStl.h:603
int uCompareVersion(const std::string &version, int major, int minor=-1, int patch=-1)
Definition: UStl.h:792
std::map< K, V > uMultimapToMap(const std::multimap< K, V > &m)
Definition: UStl.h:496
std::list< V > uVectorToList(const std::vector< V > &v)
Definition: UStl.h:486
std::list< std::string > uSplitNumChar(const std::string &str)
Definition: UStl.h:672
bool uIsNumber(const std::string &str)
Definition: UStl.h:647
bool uContains(const std::list< V > &list, const V &value)
Definition: UStl.h:409
std::vector< V > uListToVector(const std::list< V > &list)
Definition: UStl.h:475
std::vector< V > uValues(const std::multimap< K, V > &mm)
Definition: UStl.h:100
std::list< V >::iterator uIteratorAt(std::list< V > &list, const unsigned int &pos)
Definition: UStl.h:284
std::list< K > uKeysList(const std::multimap< K, V > &mm)
Definition: UStl.h:84
void uAppend(std::list< V > &list, const std::list< V > &newItems)
Definition: UStl.h:526
int uIndexOf(const std::vector< V > &list, const V &value)
Definition: UStl.h:539
V uValue(const std::map< K, V > &m, const K &key, const V &defaultValue=V())
Definition: UStl.h:240
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
Definition: UStl.h:443


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06