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  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
190  {
191  s.insert(s.end(), iter->first);
192  }
193  return s;
194 }
195 
201 template<class K, class V>
202 inline std::vector<V> uValues(const std::map<K, V> & m)
203 {
204  std::vector<V> v(m.size());
205  int i=0;
206  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
207  {
208  v[i] = iter->second;
209  ++i;
210  }
211  return v;
212 }
213 
219 template<class K, class V>
220 inline std::list<V> uValuesList(const std::map<K, V> & m)
221 {
222  std::list<V> l;
223  for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
224  {
225  l.push_back(iter->second);
226  }
227  return l;
228 }
229 
237 template<class K, class V>
238 inline V uValue(const std::map<K, V> & m, const K & key, const V & defaultValue = V())
239 {
240  V v = defaultValue;
241  typename std::map<K, V>::const_iterator i = m.find(key);
242  if(i != m.end())
243  {
244  v = i->second;
245  }
246  return v;
247 }
248 
257 template<class K, class V>
258 inline V uTake(std::map<K, V> & m, const K & key, const V & defaultValue = V())
259 {
260  V v;
261  typename std::map<K, V>::iterator i = m.find(key);
262  if(i != m.end())
263  {
264  v = i->second;
265  m.erase(i);
266  }
267  else
268  {
269  v = defaultValue;
270  }
271  return v;
272 }
273 
281 template<class V>
282 inline typename std::list<V>::iterator uIteratorAt(std::list<V> & list, const unsigned int & pos)
283 {
284  typename std::list<V>::iterator iter = list.begin();
285  for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
286  {
287  ++iter;
288  }
289  return iter;
290 }
291 
299 template<class V>
300 inline typename std::list<V>::const_iterator uIteratorAt(const std::list<V> & list, const unsigned int & pos)
301 {
302  typename std::list<V>::const_iterator iter = list.begin();
303  for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
304  {
305  ++iter;
306  }
307  return iter;
308 }
309 
317 template<class V>
318 inline typename std::set<V>::iterator uIteratorAt(std::set<V> & set, const unsigned int & pos)
319 {
320  typename std::set<V>::iterator iter = set.begin();
321  for(unsigned int i = 0; i<pos && iter != set.end(); ++i )
322  {
323  ++iter;
324  }
325  return iter;
326 }
327 
335 template<class V>
336 inline typename std::set<V>::const_iterator uIteratorAt(const std::set<V> & set, const unsigned int & pos)
337 {
338  typename std::set<V>::const_iterator iter = set.begin();
339  for(unsigned int i = 0; i<pos && iter != set.end(); ++i )
340  {
341  ++iter;
342  }
343  return iter;
344 }
345 
353 template<class V>
354 inline typename std::vector<V>::iterator uIteratorAt(std::vector<V> & v, const unsigned int & pos)
355 {
356  return v.begin() + pos;
357 }
358 
366 template<class V>
367 inline typename std::vector<V>::const_iterator uIteratorAt(const std::vector<V> & v, const unsigned int & pos)
368 {
369  return v.begin() + pos;
370 }
371 
379 template<class V>
380 inline V & uValueAt(std::list<V> & list, const unsigned int & pos)
381 {
382  typename std::list<V>::iterator iter = uIteratorAt(list, pos);
383  return *iter;
384 }
385 
393 template<class V>
394 inline const V & uValueAt(const std::list<V> & list, const unsigned int & pos)
395 {
396  typename std::list<V>::const_iterator iter = uIteratorAt(list, pos);
397  return *iter;
398 }
399 
406 template<class V>
407 inline bool uContains(const std::list<V> & list, const V & value)
408 {
409  return std::find(list.begin(), list.end(), value) != list.end();
410 }
411 
418 template<class K, class V>
419 inline bool uContains(const std::map<K, V> & map, const K & key)
420 {
421  return map.find(key) != map.end();
422 }
423 
430 template<class K, class V>
431 inline bool uContains(const std::multimap<K, V> & map, const K & key)
432 {
433  return map.find(key) != map.end();
434 }
435 
440 template<class K, class V>
441 inline void uInsert(std::map<K, V> & map, const std::pair<K, V> & pair)
442 {
443  std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(pair);
444  if(inserted.second == false)
445  {
446  inserted.first->second = pair.second;
447  }
448 }
449 
454 template<class K, class V>
455 inline void uInsert(std::map<K, V> & map, const std::map<K, V> & items)
456 {
457  for(typename std::map<K, V>::const_iterator iter=items.begin(); iter!=items.end(); ++iter)
458  {
459  std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(*iter);
460  if(inserted.second == false)
461  {
462  inserted.first->second = iter->second;
463  }
464  }
465 }
466 
472 template<class V>
473 inline std::vector<V> uListToVector(const std::list<V> & list)
474 {
475  return std::vector<V>(list.begin(), list.end());
476 }
477 
483 template<class V>
484 inline std::list<V> uVectorToList(const std::vector<V> & v)
485 {
486  return std::list<V>(v.begin(), v.end());
487 }
488 
493 template<class K, class V>
494 inline std::map<K, V> uMultimapToMap(const std::multimap<K, V> & m)
495 {
496  return std::map<K, V>(m.begin(), m.end());
497 }
498 
502 template<class K, class V>
503 inline std::map<K, V> uMultimapToMapUnique(const std::multimap<K, V> & m)
504 {
505  std::map<K, V> mapOut;
506  std::list<K> uniqueKeys = uUniqueKeys(m);
507  for(typename std::list<K>::const_iterator iter = uniqueKeys.begin(); iter!=uniqueKeys.end(); ++iter)
508  {
509  if(m.count(*iter) == 1)
510  {
511  typename std::multimap<K, V>::const_iterator jter=m.find(*iter);
512  mapOut.insert(mapOut.end(), std::pair<K,V>(jter->first, jter->second));
513  }
514  }
515  return mapOut;
516 }
517 
523 template<class V>
524 inline void uAppend(std::list<V> & list, const std::list<V> & newItems)
525 {
526  list.insert(list.end(), newItems.begin(), newItems.end());
527 }
528 
536 template<class V>
537 inline int uIndexOf(const std::vector<V> & list, const V & value)
538 {
539  int index=-1;
540  int i=0;
541  for(typename std::vector<V>::const_iterator iter = list.begin(); iter!=list.end(); ++iter)
542  {
543  if(*iter == value)
544  {
545  index = i;
546  break;
547  }
548  ++i;
549  }
550  return index;
551 }
552 
564 inline std::list<std::string> uSplit(const std::string & str, char separator = ' ')
565 {
566  std::list<std::string> v;
567  std::string buf;
568  for(unsigned int i=0; i<str.size(); ++i)
569  {
570  if(str[i] != separator)
571  {
572  buf += str[i];
573  }
574  else if(buf.size())
575  {
576  v.push_back(buf);
577  buf = "";
578  }
579  }
580  if(buf.size())
581  {
582  v.push_back(buf);
583  }
584  return v;
585 }
586 
601 inline std::string uJoin(const std::list<std::string> & strings, const std::string & separator = "")
602 {
603  std::string out;
604  for(std::list<std::string>::const_iterator iter = strings.begin(); iter!=strings.end(); ++iter)
605  {
606  if(iter!=strings.begin() && !separator.empty())
607  {
608  out += separator;
609  }
610  out+=*iter;
611  }
612  return out;
613 }
614 
620 inline bool uIsDigit(const char c)
621 {
622  return c >= '0' && c <= '9';
623 }
624 
630 inline bool uIsInteger(const std::string & str, bool checkForSign = true)
631 {
632  bool isInteger = str.size()!=0;
633  for(unsigned int i=0; i<str.size() && isInteger; ++i)
634  {
635  isInteger = (checkForSign && i==0 && str[i]=='-') || uIsDigit(str[i]);
636  }
637  return isInteger;
638 }
639 
645 inline bool uIsNumber(const std::string & str)
646 {
647  std::list<std::string> list = uSplit(str, '.');
648  if(list.size() == 1)
649  {
650  return uIsInteger(str);
651  }
652  else if(list.size() == 2)
653  {
654  return uIsInteger(list.front()) && uIsInteger(list.back(), false);
655  }
656  return false;
657 }
658 
659 
670 inline std::list<std::string> uSplitNumChar(const std::string & str)
671 {
672  std::list<std::string> list;
673  std::string buf;
674  bool num = false;
675  for(unsigned int i=0; i<str.size(); ++i)
676  {
677  if(uIsDigit(str[i]))
678  {
679  if(!num && buf.size())
680  {
681  list.push_back(buf);
682  buf.clear();
683  }
684  buf += str[i];
685  num = true;
686  }
687  else
688  {
689  if(num)
690  {
691  list.push_back(buf);
692  buf.clear();
693  }
694  buf += str[i];
695  num = false;
696  }
697  }
698  if(buf.size())
699  {
700  list.push_back(buf);
701  }
702  return list;
703 }
704 
717 inline int uStrNumCmp(const std::string & a, const std::string & b)
718 {
719  std::vector<std::string> listA;
720  std::vector<std::string> listB;
721 
722  listA = uListToVector(uSplitNumChar(a));
723  listB = uListToVector(uSplitNumChar(b));
724 
725  unsigned int i;
726  int result = 0;
727  for(i=0; i<listA.size() && i<listB.size(); ++i)
728  {
729  if(uIsDigit(listA[i].at(0)) && uIsDigit(listB[i].at(0)))
730  {
731  //padding if zeros at the beginning
732  if(listA[i].at(0) == '0' && listB[i].size() < listA[i].size())
733  {
734  while(listB[i].size() < listA[i].size())
735  {
736  listB[i] += '0';
737  }
738  }
739  else if(listB[i].at(0) == '0' && listA[i].size() < listB[i].size())
740  {
741  while(listA[i].size() < listB[i].size())
742  {
743  listA[i] += '0';
744  }
745  }
746 
747  if(listB[i].size() < listA[i].size())
748  {
749  result = 1;
750  }
751  else if(listB[i].size() > listA[i].size())
752  {
753  result = -1;
754  }
755  else
756  {
757  result = listA[i].compare(listB[i]);
758  }
759  }
760  else if(uIsDigit(listA[i].at(0)))
761  {
762  result = -1;
763  }
764  else if(uIsDigit(listB[i].at(0)))
765  {
766  result = 1;
767  }
768  else
769  {
770  result = listA[i].compare(listB[i]);
771  }
772 
773  if(result != 0)
774  {
775  break;
776  }
777  }
778 
779  return result;
780 }
781 
785 inline bool uStrContains(const std::string & string, const std::string & substring)
786 {
787  return string.find(substring) != std::string::npos;
788 }
789 
790 inline int uCompareVersion(const std::string & version, int major, int minor=-1, int patch=-1)
791 {
792  std::vector<std::string> v = uListToVector(uSplit(version, '.'));
793  if(v.size() == 3)
794  {
795  int vMajor = atoi(v[0].c_str());
796  int vMinor = atoi(v[1].c_str());
797  int vPatch = atoi(v[2].c_str());
798  if(vMajor > major ||
799  (vMajor == major && minor!=-1 && vMinor > minor) ||
800  (vMajor == major && minor!=-1 && vMinor == minor && patch!=-1 && vPatch > patch))
801  {
802  return 1;
803  }
804  else if(vMajor == major && (minor == -1 || (vMinor == minor && (patch == -1 || vPatch == patch))))
805  {
806  return 0;
807  }
808  }
809  return -1;
810 }
811 
812 inline std::string uPad(const std::string & title, int padding = 20)
813 {
814  int emptySize = padding - (int)title.size();
815  if(emptySize>0)
816  {
817  return title + std::string(emptySize, ' ');
818  }
819  return title;
820 }
821 
822 #endif /* USTL_H */
int
int
uIsNumber
bool uIsNumber(const std::string &str)
Definition: UStl.h:645
list::end
detail::list_iterator end() const
uPad
std::string uPad(const std::string &title, int padding=20)
Definition: UStl.h:812
set
s
RealScalar s
b
Array< int, 3, 1 > b
list
c
Scalar Scalar * c
uJoin
std::string uJoin(const std::list< std::string > &strings, const std::string &separator="")
Definition: UStl.h:601
size
Index size
uUniqueKeys
std::list< K > uUniqueKeys(const std::multimap< K, V > &mm)
Definition: UStl.h:46
uVectorToList
std::list< V > uVectorToList(const std::vector< V > &v)
Definition: UStl.h:484
uIndexOf
int uIndexOf(const std::vector< V > &list, const V &value)
Definition: UStl.h:537
uMultimapToMapUnique
std::map< K, V > uMultimapToMapUnique(const std::multimap< K, V > &m)
Definition: UStl.h:503
uMultimapToMap
std::map< K, V > uMultimapToMap(const std::multimap< K, V > &m)
Definition: UStl.h:494
uIsInteger
bool uIsInteger(const std::string &str, bool checkForSign=true)
Definition: UStl.h:630
uListToVector
std::vector< V > uListToVector(const std::list< V > &list)
Definition: UStl.h:473
uSplitNumChar
std::list< std::string > uSplitNumChar(const std::string &str)
Definition: UStl.h:670
uKeysList
std::list< K > uKeysList(const std::multimap< K, V > &mm)
Definition: UStl.h:84
range
Double_ range(const Point2_ &p, const Point2_ &q)
uInsert
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
Definition: UStl.h:441
uContains
bool uContains(const std::list< V > &list, const V &value)
Definition: UStl.h:407
list::size
size_t size() const
uKeys
std::vector< K > uKeys(const std::multimap< K, V > &mm)
Definition: UStl.h:67
uStrNumCmp
int uStrNumCmp(const std::string &a, const std::string &b)
Definition: UStl.h:717
uIteratorAt
std::list< V >::iterator uIteratorAt(std::list< V > &list, const unsigned int &pos)
Definition: UStl.h:282
uTake
V uTake(std::map< K, V > &m, const K &key, const V &defaultValue=V())
Definition: UStl.h:258
list::insert
void insert(const IdxType &index, ValType &&val)
version
version
uValue
V uValue(const std::map< K, V > &m, const K &key, const V &defaultValue=V())
Definition: UStl.h:238
m
Matrix3f m
uStrContains
bool uStrContains(const std::string &string, const std::string &substring)
Definition: UStl.h:785
uAppend
void uAppend(std::list< V > &list, const std::list< V > &newItems)
Definition: UStl.h:524
uKeysSet
std::set< K > uKeysSet(const std::map< K, V > &m)
Definition: UStl.h:186
out
std::ofstream out("Result.txt")
str
key
const gtsam::Symbol key( 'X', 0)
a
ArrayXXi a
uSplit
std::list< std::string > uSplit(const std::string &str, char separator=' ')
Definition: UStl.h:564
uCompareVersion
int uCompareVersion(const std::string &version, int major, int minor=-1, int patch=-1)
Definition: UStl.h:790
iter
iterator iter(handle obj)
c_str
const char * c_str(Args &&...args)
v
Array< int, Dynamic, 1 > v
V
MatrixXcd V
uIsDigit
bool uIsDigit(const char c)
Definition: UStl.h:620
uValues
std::vector< V > uValues(const std::multimap< K, V > &mm)
Definition: UStl.h:100
pos
value
value
i
int i
result
RESULT & result
uValueAt
V & uValueAt(std::list< V > &list, const unsigned int &pos)
Definition: UStl.h:380
uValuesList
std::list< V > uValuesList(const std::multimap< K, V > &mm)
Definition: UStl.h:117
list::clear
void clear()
list::begin
detail::list_iterator begin() const


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:23