Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stl_adds.h
Go to the documentation of this file.
1 #ifndef __STL_ADDS_H
2 #define __STL_ADDS_H
3 
4 #include<list>
5 #include<string>
6 #include<algorithm>
7 #include<vector>
8 #include<functional>
9 
10 // iterators for stl containers. Do not use for removal because the end is determined at the beginning.
11 // use C++11 syntax now: for( auto &val : coll)
12 #define FOREACH(colltype, coll, it) for( colltype::iterator it = (coll).begin(), __end=(coll).end(); it!= __end; ++it)
13 
14 // Iteration with index
15 // unfortunatelly we cannot initialize the index within the for loop (different type than iterator)
16 #define FOREACHI(colltype, coll, it, index) int index=0; for( colltype::iterator it = (coll).begin(), __end=(coll).end(); it!= __end; ++it, ++index)
17 // using C++11 auto typing!
18 #define FOREACHIa(coll, it, index) int index=0; for( auto it = (coll).begin(), __end=(coll).end(); it!= __end; ++it, ++index)
19 
20 
21 #define FOREACHC(colltype, coll, it) for( colltype::const_iterator it = (coll).begin(), __end=(coll).end(); it!= __end ; ++it )
22 #define FOREACHCI(colltype, coll, it, index) int index=0;for( colltype::const_iterator it = (coll).begin(), __end=(coll).end(); it!= __end; ++it, ++index)
23 
24 // using C++11 auto typing!
25 #define FOREACH2(coll1,coll2, it1,it2) \
26  for( auto it1 = (coll1).begin(), __end1=(coll1).end(), it2 = (coll2).begin(), __end2=(coll2).end(); it1!= __end1 && it2!= __end2; ++it1 , ++it2)
27 
28 
29 
30 /// contains some additions to the standard template library
31 namespace std {
32 
33  /// absolute function for all types
34  template<typename T>
35  inline T abs(T v)
36  { return ((v>0)?v:-v); }
37 
38  /// += operators for list (list concat)
39  template <class T, class A>
40  list<T,A>& operator += (list<T,A>& l1, const list<T,A>& l2) {
41  l1.insert(l1.end(), l2.begin(), l2.end());
42  return l1;
43  }
44 
45  /// += operators for list (append)
46  template <class T, class A>
47  list<T,A>& operator += (list<T,A>& l1, const T& v) {
48  l1.push_back(v);
49  return l1;
50  }
51 
52  /// + operators for lists (list concat)
53  template <class T, class A>
54  list<T,A> operator + (const list<T,A>& l1, const list<T,A>& l2) {
55  list<T,A> rv(l1.begin(),l1.end());
56  rv += l2;
57  return rv;
58  }
59 
60  // These initializer are obsolete with C11 use list initializers {a1,a2,...}
61  // returns a list with a single element
62  // template <typename T>
63  // std::list<T> _1tolist(T a){ std::list<T> l; l.push_back(a); return l; }
64  // ...
65 
66 
67  /// integer to string with default formating
68  string itos(int i);
69  /// integer to string with printf formating string
70  string itos(int i, const char *);
71  /// integer to string with default formating
72  string ftos(double i);
73  /// integer to string with printf formating string
74  string ftos(double i, const char *);
75 
76 
77  template<typename Col, typename T>
78  bool removeElement(Col& col, const T& elem){
79  // search for element
80  typename Col::iterator i = find(col.begin(), col.end(), elem);
81  if(i != col.end()){
82  col.erase(i);
83  return true;
84  }else{
85  return false;
86  }
87  }
88 
89 
90  template <class T>
91  struct join : public unary_function<T, void>
92  {
93  join(const T& delimit) : delimit(delimit), count(0) {}
94  void operator() (const T& s) {
95  if(count==0){
96  joined=s;
97  }else{
98  joined += delimit + s;
99  }
100  ++count;
101  }
104  int count;
105  };
106 
107  template <typename A, typename E>
108  A reduceList(std::list<E> list, A acc, std::function<A(A,E)> f){
109  for (auto i: list){
110  acc = f(acc,i);
111  }
112  return acc;
113  }
114 
115  template <typename O, typename I>
116  std::list<O> mapList(const std::list<I>& l, std::function<O (const I&)> fun){
117  std::list<O> res;
118  for (auto& p : l)
119  res.push_back(fun(p));
120  return res;
121  }
122 
123 }
124 
125 #endif
T joined
Definition: stl_adds.h:103
string ftos(double i)
integer to string with default formating
Definition: stl_adds.cpp:42
list< T, A > operator+(const list< T, A > &l1, const list< T, A > &l2)
Definition: stl_adds.h:54
int count
Definition: stl_adds.h:104
unsigned int I
type for matrix indices
Definition: matrix.h:36
T abs(T v)
absolute function for all types
Definition: stl_adds.h:35
list< T, A > & operator+=(list< T, A > &l1, const list< T, A > &l2)
+= operators for list (list concat)
Definition: stl_adds.h:40
join(const T &delimit)
Definition: stl_adds.h:93
A reduceList(std::list< E > list, A acc, std::function< A(A, E)> f)
Definition: stl_adds.h:108
void operator()(const T &s)
Definition: stl_adds.h:94
string itos(int i)
integer to string with default formating
Definition: stl_adds.cpp:30
std::list< O > mapList(const std::list< I > &l, std::function< O(const I &)> fun)
Definition: stl_adds.h:116
const int T
integer constant for use with exp function and (^) operator to transpose the matrix ...
Definition: matrix.cpp:21
Definition: stl_adds.h:91
bool removeElement(Col &col, const T &elem)
Definition: stl_adds.h:78
T delimit
Definition: stl_adds.h:102