UStl.h
Go to the documentation of this file.
00001 // Taken from UtiLite library r185 [www.utilite.googlecode.com]
00002 
00003 /*
00004 *  utilite is a cross-platform library with
00005 *  useful utilities for fast and small developing.
00006 *  Copyright (C) 2010  Mathieu Labbe
00007 *
00008 *  utilite is free library: you can redistribute it and/or modify
00009 *  it under the terms of the GNU Lesser General Public License as published by
00010 *  the Free Software Foundation, either version 3 of the License, or
00011 *  (at your option) any later version.
00012 *
00013 *  utilite is distributed in the hope that it will be useful,
00014 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 *  GNU Lesser General Public License for more details.
00017 *
00018 *  You should have received a copy of the GNU Lesser General Public License
00019 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef USTL_H
00023 #define USTL_H
00024 
00025 #include <list>
00026 #include <map>
00027 #include <set>
00028 #include <vector>
00029 #include <string>
00030 #include <algorithm>
00031 
00046 template<class K, class V>
00047 inline std::list<K> uUniqueKeys(const std::multimap<K, V> & mm)
00048 {
00049         std::list<K> l;
00050         typename std::list<K>::reverse_iterator lastValue;
00051         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00052         {
00053                 if(iter == mm.begin() || (iter != mm.begin() && *lastValue != iter->first))
00054                 {
00055                         l.push_back(iter->first);
00056                         lastValue = l.rbegin();
00057                 }
00058         }
00059         return l;
00060 }
00061 
00067 template<class K, class V>
00068 inline std::list<K> uKeys(const std::multimap<K, V> & mm)
00069 {
00070         std::list<K> l;
00071         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00072         {
00073                 l.push_back(iter->first);
00074         }
00075         return l;
00076 }
00077 
00083 template<class K, class V>
00084 inline std::list<V> uValues(const std::multimap<K, V> & mm)
00085 {
00086         std::list<V> l;
00087         for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
00088         {
00089                 l.push_back(iter->second);
00090         }
00091         return l;
00092 }
00093 
00100 template<class K, class V>
00101 inline std::list<V> uValues(const std::multimap<K, V> & mm, const K & key)
00102 {
00103         std::list<V> l;
00104         std::pair<typename std::multimap<K, V>::const_iterator, typename std::multimap<K, V>::const_iterator> range;
00105         range = mm.equal_range(key);
00106         for(typename std::multimap<K, V>::const_iterator iter = range.first; iter!=range.second; ++iter)
00107         {
00108                 l.push_back(iter->second);
00109         }
00110         return l;
00111 }
00112 
00118 template<class K, class V>
00119 inline std::vector<K> uKeys(const std::map<K, V> & m)
00120 {
00121         std::vector<K> v(m.size());
00122         int i=0;
00123         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00124         {
00125                 v[i] = iter->first;
00126                 ++i;
00127         }
00128         return v;
00129 }
00130 
00136 template<class K, class V>
00137 inline std::list<K> uKeysList(const std::map<K, V> & m)
00138 {
00139         std::list<K> l;
00140         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00141         {
00142                 l.push_back(iter->first);
00143         }
00144         return l;
00145 }
00146 
00152 template<class K, class V>
00153 inline std::set<K> uKeysSet(const std::map<K, V> & m)
00154 {
00155         std::set<K> s;
00156         int i=0;
00157         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00158         {
00159                 s.insert(s.end(), iter->first);
00160                 ++i;
00161         }
00162         return s;
00163 }
00164 
00170 template<class K, class V>
00171 inline std::vector<V> uValues(const std::map<K, V> & m)
00172 {
00173         std::vector<V> v(m.size());
00174         int i=0;
00175         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00176         {
00177                 v[i] = iter->second;
00178                 ++i;
00179         }
00180         return v;
00181 }
00182 
00188 template<class K, class V>
00189 inline std::list<V> uValuesList(const std::map<K, V> & m)
00190 {
00191         std::list<V> l;
00192         for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
00193         {
00194                 l.push_back(iter->second);
00195         }
00196         return l;
00197 }
00198 
00206 template<class K, class V>
00207 inline V uValue(const std::map<K, V> & m, const K & key, const V & defaultValue = V())
00208 {
00209         V v = defaultValue;
00210         typename std::map<K, V>::const_iterator i = m.find(key);
00211         if(i != m.end())
00212         {
00213                 v = i->second;
00214         }
00215         return v;
00216 }
00217 
00226 template<class K, class V>
00227 inline V uTake(std::map<K, V> & m, const K & key, const V & defaultValue = V())
00228 {
00229         V v;
00230         typename std::map<K, V>::iterator i = m.find(key);
00231         if(i != m.end())
00232         {
00233                 v = i->second;
00234                 m.erase(i);
00235         }
00236         else
00237         {
00238                 v = defaultValue;
00239         }
00240         return v;
00241 }
00242 
00250 template<class V>
00251 inline typename std::list<V>::iterator uIteratorAt(std::list<V> & list, const unsigned int & pos)
00252 {
00253         typename std::list<V>::iterator iter = list.begin();
00254         for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
00255         {
00256                 ++iter;
00257         }
00258         return iter;
00259 }
00260 
00268 template<class V>
00269 inline typename std::list<V>::const_iterator uIteratorAt(const std::list<V> & list, const unsigned int & pos)
00270 {
00271         typename std::list<V>::const_iterator iter = list.begin();
00272         for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
00273         {
00274                 ++iter;
00275         }
00276         return iter;
00277 }
00278 
00286 template<class V>
00287 inline typename std::vector<V>::iterator uIteratorAt(std::vector<V> & v, const unsigned int & pos)
00288 {
00289         return v.begin() + pos;
00290 }
00291 
00299 template<class V>
00300 inline V & uValueAt(std::list<V> & list, const unsigned int & pos)
00301 {
00302         typename std::list<V>::iterator iter = uIteratorAt(list, pos);
00303         return *iter;
00304 }
00305 
00313 template<class V>
00314 inline const V & uValueAt(const std::list<V> & list, const unsigned int & pos)
00315 {
00316         typename std::list<V>::const_iterator iter = uIteratorAt(list, pos);
00317         return *iter;
00318 }
00319 
00326 template<class V>
00327 inline bool uContains(const std::list<V> & list, const V & value)
00328 {
00329         return std::find(list.begin(), list.end(), value) != list.end();
00330 }
00331 
00338 template<class K, class V>
00339 inline bool uContains(const std::map<K, V> & map, const K & key)
00340 {
00341         return map.find(key) != map.end();
00342 }
00343 
00350 template<class K, class V>
00351 inline bool uContains(const std::multimap<K, V> & map, const K & key)
00352 {
00353         return map.find(key) != map.end();
00354 }
00355 
00360 template<class K, class V>
00361 inline void uInsert(std::map<K, V> & map, const std::pair<K, V> & pair)
00362 {
00363         std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(pair);
00364         if(inserted.second == false)
00365         {
00366                 inserted.first->second = pair.second;
00367         }
00368 }
00369 
00375 template<class V>
00376 inline std::vector<V> uListToVector(const std::list<V> & list)
00377 {
00378         return std::vector<V>(list.begin(), list.end());
00379 }
00380 
00386 template<class V>
00387 inline std::list<V> uVectorToList(const std::vector<V> & v)
00388 {
00389         return std::list<V>(v.begin(), v.end());
00390 }
00391 
00397 template<class V>
00398 inline void uAppend(std::list<V> & list, const std::list<V> & newItems)
00399 {
00400         list.insert(list.end(), newItems.begin(), newItems.end());
00401 }
00402 
00410 template<class V>
00411 inline int uIndexOf(const std::vector<V> & list, const V & value)
00412 {
00413         int index=-1;
00414         int i=0;
00415         for(typename std::vector<V>::const_iterator iter = list.begin(); iter!=list.end(); ++iter)
00416         {
00417                 if(*iter == value)
00418                 {
00419                         index = i;
00420                         break;
00421                 }
00422                 ++i;
00423         }
00424         return index;
00425 }
00426 
00438 inline std::list<std::string> uSplit(const std::string & str, char separator = ' ')
00439 {
00440         std::list<std::string> v;
00441         std::string buf;
00442         for(unsigned int i=0; i<str.size(); ++i)
00443         {
00444                 if(str[i] != separator)
00445                 {
00446                         buf += str[i];
00447                 }
00448                 else if(buf.size())
00449                 {
00450                         v.push_back(buf);
00451                         buf = "";
00452                 }
00453         }
00454         if(buf.size())
00455         {
00456                 v.push_back(buf);
00457         }
00458         return v;
00459 }
00460 
00466 inline bool uIsDigit(const char c)
00467 {
00468         return c >= '0' && c <= '9';
00469 }
00470 
00481 inline std::list<std::string> uSplitNumChar(const std::string & str)
00482 {
00483         std::list<std::string> list;
00484         std::string buf;
00485         bool num = false;
00486         for(unsigned int i=0; i<str.size(); ++i)
00487         {
00488                 if(uIsDigit(str[i]))
00489                 {
00490                         if(!num && buf.size())
00491                         {
00492                                 list.push_back(buf);
00493                                 buf.clear();
00494                         }
00495                         buf += str[i];
00496                         num = true;
00497                 }
00498                 else
00499                 {
00500                         if(num)
00501                         {
00502                                 list.push_back(buf);
00503                                 buf.clear();
00504                         }
00505                         buf += str[i];
00506                         num = false;
00507                 }
00508         }
00509         if(buf.size())
00510         {
00511                 list.push_back(buf);
00512         }
00513         return list;
00514 }
00515 
00528 inline int uStrNumCmp(const std::string & a, const std::string & b)
00529 {
00530         std::vector<std::string> listA;
00531         std::vector<std::string> listB;
00532 
00533         listA = uListToVector(uSplitNumChar(a));
00534         listB = uListToVector(uSplitNumChar(b));
00535 
00536         unsigned int i;
00537         int result = 0;
00538         for(i=0; i<listA.size() && i<listB.size(); ++i)
00539         {
00540                 if(uIsDigit(listA[i].at(0)) && uIsDigit(listB[i].at(0)))
00541                 {
00542                         //padding if zeros at the beginning
00543                         if(listA[i].at(0) == '0' && listB[i].size() < listA[i].size())
00544                         {
00545                                 while(listB[i].size() < listA[i].size())
00546                                 {
00547                                         listB[i] += '0';
00548                                 }
00549                         }
00550                         else if(listB[i].at(0) == '0' && listA[i].size() < listB[i].size())
00551                         {
00552                                 while(listA[i].size() < listB[i].size())
00553                                 {
00554                                         listA[i] += '0';
00555                                 }
00556                         }
00557 
00558                         if(listB[i].size() < listA[i].size())
00559                         {
00560                                 result = 1;
00561                         }
00562                         else if(listB[i].size() > listA[i].size())
00563                         {
00564                                 result = -1;
00565                         }
00566                         else
00567                         {
00568                                 result = listA[i].compare(listB[i]);
00569                         }
00570                 }
00571                 else if(uIsDigit(listA[i].at(0)))
00572                 {
00573                         result = -1;
00574                 }
00575                 else if(uIsDigit(listB[i].at(0)))
00576                 {
00577                         result = 1;
00578                 }
00579                 else
00580                 {
00581                         result = listA[i].compare(listB[i]);
00582                 }
00583 
00584                 if(result != 0)
00585                 {
00586                         break;
00587                 }
00588         }
00589 
00590         return result;
00591 }
00592 
00593 #endif /* USTL_H */


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Aug 27 2015 13:00:33