Robot Simulator of the Robotics Group for Self-Organization of Control: stl_adds.h Source File

# stl_adds.h

Go to the documentation of this file.

    00001 #ifndef __STL_ADDS_H
    00002 #define __STL_ADDS_H
    00003
    00004 #include<list>
    00005 #include<string>
    00006 #include<algorithm>
    00007 #include<vector>
    00008
    00009 // iterators for stl containers. Do not use for removal because the end is determined at the beginning.
    00010 #define FOREACH(colltype, coll, it) for( colltype::iterator it = (coll).begin(), __end=(coll).end(); it!= __end; it++)
    00011 // Iteration with index
    00012 // unfortunatelly we cannot initialize the index within the for loop (different type than iterator)
    00013 #define FOREACHI(colltype, coll, it, index) int index=0; for( colltype::iterator it = (coll).begin(), __end=(coll).end(); it!= __end; it++, index++)
    00014 #define FOREACHC(colltype, coll, it) for( colltype::const_iterator it = (coll).begin(), __end=(coll).end(); it!= __end ; it++ )
    00015 #define FOREACHCI(colltype, coll, it, index) int index=0;for( colltype::const_iterator it = (coll).begin(), __end=(coll).end(); it!= __end; it++, index++)
    00016
    00017
    00018 /// contains some additions to the standard template library
    00019 namespace std {
    00020
    00021   /// absolute function for all types
    00022   template<typename T>
    00023   inline T abs(T v)
    00024   { return ((v>0)?v:-v); }
    00025
    00026   /// += operators for list (list concat)
    00027   template <class T, class A>
    00028     list<T,A>& operator += (list<T,A>& l1, const list<T,A>& l2) {
    00029     l1.insert(l1.end(), l2.begin(), l2.end());
    00030     return l1;
    00031   }
    00032
    00033   /// += operators for list (append)
    00034   template <class T, class A>
    00035     list<T,A>& operator += (list<T,A>& l1, const T& v) {
    00036     l1.push_back(v);
    00037     return l1;
    00038   }
    00039
    00040   /// + operators for lists (list concat)
    00041   template <class T, class A>
    00042     list<T,A> operator + (const list<T,A>& l1, const list<T,A>& l2) {
    00043     list<T,A> rv(l1.begin(),l1.end());
    00044     rv += l2;
    00045     return rv;
    00046   }
    00047
    00048   /// integer to string with default formating
    00049   string itos(int i);
    00050   /// integer to string with printf formating string
    00051   string itos(int i, const char *);
    00052
    00053   template<typename Col, typename T>
    00054   bool removeElement(Col& col, const T& elem){
    00055     // search for element
    00056     typename Col::iterator i = find(col.begin(), col.end(), elem);
    00057     if(i != col.end()){
    00058       col.erase(i);
    00059       return true;
    00060     }else{
    00061       return false;
    00062     }
    00063   }
    00064
    00065
    00066   template <class T>
    00067   struct join : public unary_function<T, void>
    00068   {
    00069     join(const T& delimit) : delimit(delimit), count(0) {}
    00070     void operator() (const T& s) {
    00071       if(count==0){
    00072         joined=s;
    00073       }else{
    00074         joined += delimit + s;
    00075       }
    00076       ++count;
    00077     }
    00078     T delimit;
    00079     T joined;
    00080     int count;
    00081   };
    00082
    00083 }
    00084
    00085 #endif