UStl.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef USTL_H
00021 #define USTL_H
00022 
00023 #include <list>
00024 #include <map>
00025 #include <set>
00026 #include <vector>
00027 #include <string>
00028 #include <algorithm>
00029 
00044 template<class K, class V>
00045 inline std::list<K> uUniqueKeys(const std::multimap<K, V> & mm)
00046 {
00047         std::list<K> l;
00048         typename std::list<K>::reverse_iterator lastValue;
00049         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00050         {
00051                 if(iter == mm.begin() || (iter != mm.begin() && *lastValue != iter->first))
00052                 {
00053                         l.push_back(iter->first);
00054                         lastValue = l.rbegin();
00055                 }
00056         }
00057         return l;
00058 }
00059 
00065 template<class K, class V>
00066 inline std::vector<K> uKeys(const std::multimap<K, V> & mm)
00067 {
00068         std::vector<K> v(mm.size());
00069         int i=0;
00070         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00071         {
00072                 v[i++] = iter->first;
00073         }
00074         return v;
00075 }
00076 
00082 template<class K, class V>
00083 inline std::list<K> uKeysList(const std::multimap<K, V> & mm)
00084 {
00085         std::list<K> l;
00086         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00087         {
00088                 l.push_back(iter->first);
00089         }
00090         return l;
00091 }
00092 
00098 template<class K, class V>
00099 inline std::vector<V> uValues(const std::multimap<K, V> & mm)
00100 {
00101         std::vector<V> v(mm.size());
00102         int i=0;
00103         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00104         {
00105                 v[i++] = iter->second;
00106         }
00107         return v;
00108 }
00109 
00115 template<class K, class V>
00116 inline std::list<V> uValuesList(const std::multimap<K, V> & mm)
00117 {
00118         std::list<V> l;
00119         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00120         {
00121                 l.push_back(iter->second);
00122         }
00123         return l;
00124 }
00125 
00132 template<class K, class V>
00133 inline std::list<V> uValues(const std::multimap<K, V> & mm, const K & key)
00134 {
00135         std::list<V> l;
00136         std::pair<typename std::multimap<K, V>::const_iterator, typename std::multimap<K, V>::const_iterator> range;
00137         range = mm.equal_range(key);
00138         for(typename std::multimap<K, V>::const_iterator iter = range.first; iter!=range.second; ++iter)
00139         {
00140                 l.push_back(iter->second);
00141         }
00142         return l;
00143 }
00144 
00150 template<class K, class V>
00151 inline std::vector<K> uKeys(const std::map<K, V> & m)
00152 {
00153         std::vector<K> v(m.size());
00154         int i=0;
00155         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00156         {
00157                 v[i] = iter->first;
00158                 ++i;
00159         }
00160         return v;
00161 }
00162 
00168 template<class K, class V>
00169 inline std::list<K> uKeysList(const std::map<K, V> & m)
00170 {
00171         std::list<K> l;
00172         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00173         {
00174                 l.push_back(iter->first);
00175         }
00176         return l;
00177 }
00178 
00184 template<class K, class V>
00185 inline std::set<K> uKeysSet(const std::map<K, V> & m)
00186 {
00187         std::set<K> s;
00188         int i=0;
00189         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00190         {
00191                 s.insert(s.end(), iter->first);
00192                 ++i;
00193         }
00194         return s;
00195 }
00196 
00202 template<class K, class V>
00203 inline std::vector<V> uValues(const std::map<K, V> & m)
00204 {
00205         std::vector<V> v(m.size());
00206         int i=0;
00207         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00208         {
00209                 v[i] = iter->second;
00210                 ++i;
00211         }
00212         return v;
00213 }
00214 
00220 template<class K, class V>
00221 inline std::list<V> uValuesList(const std::map<K, V> & m)
00222 {
00223         std::list<V> l;
00224         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00225         {
00226                 l.push_back(iter->second);
00227         }
00228         return l;
00229 }
00230 
00238 template<class K, class V>
00239 inline V uValue(const std::map<K, V> & m, const K & key, const V & defaultValue = V())
00240 {
00241         V v = defaultValue;
00242         typename std::map<K, V>::const_iterator i = m.find(key);
00243         if(i != m.end())
00244         {
00245                 v = i->second;
00246         }
00247         return v;
00248 }
00249 
00258 template<class K, class V>
00259 inline V uTake(std::map<K, V> & m, const K & key, const V & defaultValue = V())
00260 {
00261         V v;
00262         typename std::map<K, V>::iterator i = m.find(key);
00263         if(i != m.end())
00264         {
00265                 v = i->second;
00266                 m.erase(i);
00267         }
00268         else
00269         {
00270                 v = defaultValue;
00271         }
00272         return v;
00273 }
00274 
00282 template<class V>
00283 inline typename std::list<V>::iterator uIteratorAt(std::list<V> & list, const unsigned int & pos)
00284 {
00285         typename std::list<V>::iterator iter = list.begin();
00286         for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
00287         {
00288                 ++iter;
00289         }
00290         return iter;
00291 }
00292 
00300 template<class V>
00301 inline typename std::list<V>::const_iterator uIteratorAt(const std::list<V> & list, const unsigned int & pos)
00302 {
00303         typename std::list<V>::const_iterator iter = list.begin();
00304         for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
00305         {
00306                 ++iter;
00307         }
00308         return iter;
00309 }
00310 
00318 template<class V>
00319 inline typename std::set<V>::iterator uIteratorAt(std::set<V> & set, const unsigned int & pos)
00320 {
00321         typename std::set<V>::iterator iter = set.begin();
00322         for(unsigned int i = 0; i<pos && iter != set.end(); ++i )
00323         {
00324                 ++iter;
00325         }
00326         return iter;
00327 }
00328 
00336 template<class V>
00337 inline typename std::set<V>::const_iterator uIteratorAt(const std::set<V> & set, const unsigned int & pos)
00338 {
00339         typename std::set<V>::const_iterator iter = set.begin();
00340         for(unsigned int i = 0; i<pos && iter != set.end(); ++i )
00341         {
00342                 ++iter;
00343         }
00344         return iter;
00345 }
00346 
00354 template<class V>
00355 inline typename std::vector<V>::iterator uIteratorAt(std::vector<V> & v, const unsigned int & pos)
00356 {
00357         return v.begin() + pos;
00358 }
00359 
00367 template<class V>
00368 inline typename std::vector<V>::const_iterator uIteratorAt(const std::vector<V> & v, const unsigned int & pos)
00369 {
00370         return v.begin() + pos;
00371 }
00372 
00380 template<class V>
00381 inline V & uValueAt(std::list<V> & list, const unsigned int & pos)
00382 {
00383         typename std::list<V>::iterator iter = uIteratorAt(list, pos);
00384         return *iter;
00385 }
00386 
00394 template<class V>
00395 inline const V & uValueAt(const std::list<V> & list, const unsigned int & pos)
00396 {
00397         typename std::list<V>::const_iterator iter = uIteratorAt(list, pos);
00398         return *iter;
00399 }
00400 
00407 template<class V>
00408 inline bool uContains(const std::list<V> & list, const V & value)
00409 {
00410         return std::find(list.begin(), list.end(), value) != list.end();
00411 }
00412 
00419 template<class K, class V>
00420 inline bool uContains(const std::map<K, V> & map, const K & key)
00421 {
00422         return map.find(key) != map.end();
00423 }
00424 
00431 template<class K, class V>
00432 inline bool uContains(const std::multimap<K, V> & map, const K & key)
00433 {
00434         return map.find(key) != map.end();
00435 }
00436 
00441 template<class K, class V>
00442 inline void uInsert(std::map<K, V> & map, const std::pair<K, V> & pair)
00443 {
00444         std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(pair);
00445         if(inserted.second == false)
00446         {
00447                 inserted.first->second = pair.second;
00448         }
00449 }
00450 
00456 template<class V>
00457 inline std::vector<V> uListToVector(const std::list<V> & list)
00458 {
00459         return std::vector<V>(list.begin(), list.end());
00460 }
00461 
00467 template<class V>
00468 inline std::list<V> uVectorToList(const std::vector<V> & v)
00469 {
00470         return std::list<V>(v.begin(), v.end());
00471 }
00472 
00478 template<class V>
00479 inline void uAppend(std::list<V> & list, const std::list<V> & newItems)
00480 {
00481         list.insert(list.end(), newItems.begin(), newItems.end());
00482 }
00483 
00491 template<class V>
00492 inline int uIndexOf(const std::vector<V> & list, const V & value)
00493 {
00494         int index=-1;
00495         int i=0;
00496         for(typename std::vector<V>::const_iterator iter = list.begin(); iter!=list.end(); ++iter)
00497         {
00498                 if(*iter == value)
00499                 {
00500                         index = i;
00501                         break;
00502                 }
00503                 ++i;
00504         }
00505         return index;
00506 }
00507 
00519 inline std::list<std::string> uSplit(const std::string & str, char separator = ' ')
00520 {
00521         std::list<std::string> v;
00522         std::string buf;
00523         for(unsigned int i=0; i<str.size(); ++i)
00524         {
00525                 if(str[i] != separator)
00526                 {
00527                         buf += str[i];
00528                 }
00529                 else if(buf.size())
00530                 {
00531                         v.push_back(buf);
00532                         buf = "";
00533                 }
00534         }
00535         if(buf.size())
00536         {
00537                 v.push_back(buf);
00538         }
00539         return v;
00540 }
00541 
00547 inline bool uIsDigit(const char c)
00548 {
00549         return c >= '0' && c <= '9';
00550 }
00551 
00562 inline std::list<std::string> uSplitNumChar(const std::string & str)
00563 {
00564         std::list<std::string> list;
00565         std::string buf;
00566         bool num = false;
00567         for(unsigned int i=0; i<str.size(); ++i)
00568         {
00569                 if(uIsDigit(str[i]))
00570                 {
00571                         if(!num && buf.size())
00572                         {
00573                                 list.push_back(buf);
00574                                 buf.clear();
00575                         }
00576                         buf += str[i];
00577                         num = true;
00578                 }
00579                 else
00580                 {
00581                         if(num)
00582                         {
00583                                 list.push_back(buf);
00584                                 buf.clear();
00585                         }
00586                         buf += str[i];
00587                         num = false;
00588                 }
00589         }
00590         if(buf.size())
00591         {
00592                 list.push_back(buf);
00593         }
00594         return list;
00595 }
00596 
00609 inline int uStrNumCmp(const std::string & a, const std::string & b)
00610 {
00611         std::vector<std::string> listA;
00612         std::vector<std::string> listB;
00613 
00614         listA = uListToVector(uSplitNumChar(a));
00615         listB = uListToVector(uSplitNumChar(b));
00616 
00617         unsigned int i;
00618         int result = 0;
00619         for(i=0; i<listA.size() && i<listB.size(); ++i)
00620         {
00621                 if(uIsDigit(listA[i].at(0)) && uIsDigit(listB[i].at(0)))
00622                 {
00623                         //padding if zeros at the beginning
00624                         if(listA[i].at(0) == '0' && listB[i].size() < listA[i].size())
00625                         {
00626                                 while(listB[i].size() < listA[i].size())
00627                                 {
00628                                         listB[i] += '0';
00629                                 }
00630                         }
00631                         else if(listB[i].at(0) == '0' && listA[i].size() < listB[i].size())
00632                         {
00633                                 while(listA[i].size() < listB[i].size())
00634                                 {
00635                                         listA[i] += '0';
00636                                 }
00637                         }
00638 
00639                         if(listB[i].size() < listA[i].size())
00640                         {
00641                                 result = 1;
00642                         }
00643                         else if(listB[i].size() > listA[i].size())
00644                         {
00645                                 result = -1;
00646                         }
00647                         else
00648                         {
00649                                 result = listA[i].compare(listB[i]);
00650                         }
00651                 }
00652                 else if(uIsDigit(listA[i].at(0)))
00653                 {
00654                         result = -1;
00655                 }
00656                 else if(uIsDigit(listB[i].at(0)))
00657                 {
00658                         result = 1;
00659                 }
00660                 else
00661                 {
00662                         result = listA[i].compare(listB[i]);
00663                 }
00664 
00665                 if(result != 0)
00666                 {
00667                         break;
00668                 }
00669         }
00670 
00671         return result;
00672 }
00673 
00677 inline bool uStrContains(const std::string & string, const std::string & substring)
00678 {
00679         return string.find(substring) != std::string::npos;
00680 }
00681 
00682 #endif /* USTL_H */


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:42