aimstil  5.0.5
loop.h
Go to the documentation of this file.
1 #ifndef TIL_LOOP_H
2 #define TIL_LOOP_H
3 
4 // includes from BOOST
5 #include <boost/call_traits.hpp>
6 
7 // includes from TIL
8 #include "til/is_traits.h"
9 #include "til/stditerator.h"
10 
11 namespace til {
12 
13 namespace detail
14 {
15  template < class TContainer, typename T, class TBinaryFunctor >
16  void
17  loop_cv(TContainer & c, T v, TBinaryFunctor f)
18  {
19  for (typename stditerator<TContainer>::type iC = c.begin(); iC != c.end(); ++iC)
20  {
21  loop(*iC, v, f);
22  }
23  }
24 }
25 
27 template < class TContainer, typename T, class TBinaryFunctor >
28 typename boost::enable_if_c<
29  is_container<TContainer>::value &&
30  !is_container<T>::value
31 >::type
32 loop(TContainer & c, T v, TBinaryFunctor f)
33 {
34  detail::loop_cv(c, v, f);
35 }
36 
37 namespace detail
38 {
39  template < typename T1, typename T2, class TBinaryFunctor >
40  inline void
41  loop_vv(T1 & x, T2 & y, TBinaryFunctor f)
42  {
43  f(x,y);
44  }
45 }
46 
47 template < typename T1, typename T2, class TBinaryFunctor >
48 inline
49 typename boost::enable_if_c<
50  !is_container<T1>::value &&
51  !is_container<T2>::value
52 >::type
53 loop(T1 & x, T2 & y, TBinaryFunctor f)
54 {
55  detail::loop_vv(x, y, f);
56 }
57 
58 namespace detail
59 {
60  template < class TContainer1, class TContainer2, typename TBinaryFunctor >
61  void
62  loop_cc(TContainer1 & c1, TContainer2 &res, TBinaryFunctor f)
63  {
64  assert(size(c1) == size(res));
65  typename stditerator<TContainer1>::type iC1 = c1.begin();
66  typename stditerator<TContainer2>::type iRes = res.begin();
67  for (; iC1 != c1.end(); ++iC1, ++iRes)
68  {
69  loop(*iC1, *iRes, f);
70  }
71  }
72 }
73 
75 template < class TContainer1, class TContainer2, typename TBinaryFunctor >
76 typename boost::enable_if_c<
77  is_container<TContainer1>::value &&
78  is_container<TContainer2>::value
79 >::type
80 loop(const TContainer1 & c1, TContainer2 &res, TBinaryFunctor f)
81 {
82  detail::loop_cc(c1, res, f);
83 }
84 
85 
86 namespace detail
87 {
88  template < typename T1, typename T2, typename T3, class TBinaryFunctor >
89  inline void
90  loop_vvv(T1 x, T2 y, T3 & z, TBinaryFunctor f)
91  {
92  f(x,y,z);
93  }
94 }
95 
96 template < typename T1, typename T2, typename T3, class TBinaryFunctor >
97 inline
98 typename boost::enable_if_c<
99  !is_container<T1>::value &&
100  !is_container<T2>::value &&
101  !is_container<T3>::value
102 >::type
103 loop(T1 x, T2 y, T3 & z, TBinaryFunctor f)
104 {
105  detail::loop_vvv(x, y, z, f);
106 }
107 
108 namespace detail
109 {
110  template < class TContainer1, class TContainer2, class TContainerRes, class TBinaryFunctor >
111  void
112  loop_ccc(const TContainer1 & c1, const TContainer2 &c2, TContainerRes &res, TBinaryFunctor f)
113  {
114  assert(size(c1) == size(c2));
115  assert(size(c1) == size(res));
116  typename TContainer1::const_iterator iC1 = c1.begin();
117  typename TContainer1::const_iterator cEnd = c1.end();
118  typename TContainer2::const_iterator iC2 = c2.begin();
119  typename TContainerRes::iterator iRes = res.begin();
120  for (; iC1 != cEnd; ++iC1, ++iC2, ++iRes)
121  {
122  loop(*iC1, *iC2, *iRes, f);
123  }
124  }
125 }
126 
127 template < class TContainer1, class TContainer2, class TContainerRes, class TBinaryFunctor >
128 typename boost::enable_if_c<
129  is_container<TContainer1>::value &&
130  is_container<TContainer2>::value &&
131  is_container<TContainerRes>::value
132 >::type
133 loop(const TContainer1 & c1, const TContainer2 &c2, const TContainerRes &res, TBinaryFunctor f)
134 {
135  detail::loop_ccc(c1, c2, res, f);
136 }
137 
138 namespace detail
139 {
140  template < class TContainer, class TBinaryFunctor >
141  void
142  loop_cvc(const TContainer & c1, typename boost::call_traits<typename TContainer::value_type>::param_type v, TContainer & res, TBinaryFunctor f)
143  {
144  assert(size(c1) == size(res));
145  typename TContainer::const_iterator iC1 = c1.begin();
146  typename TContainer::iterator iRes = res.begin();
147  for (; iC1 != c1.end(); ++iC1, ++iRes)
148  {
149  loop(*iC1, v, *iRes, f);
150  }
151  //std::transform(c1.begin(), c1.end(), res.begin(), std::bind2nd(std::minus<typename TContainer::value_type>(), v));
152  }
153 }
154 
155 template < class TContainer, typename T, class TBinaryFunctor >
156 typename boost::enable_if_c<
157  is_container<TContainer>::value &&
158  !is_container<T>::value
159 >::type
160 loop(const TContainer & c1, T v, TContainer &res, TBinaryFunctor f)
161 {
162  detail::loop_cvc(c1, v, res, f);
163 }
164 
165 
166 
167 
168 
169 
170 
171 /*
172 
173 
174 
175  namespace label
176  {
177  template < std::size_t D >
178  struct ecd_fixed {};
179  struct ecd_dense {};
180  struct ecd_sparse {};
181  struct ecd_rle {};
182  };
183 
184 
185  template < typename T >
186  struct loop_label {};
187 
188  template < typename T, typename D >
189  struct loop_label<std::pair<numeric_array<T,D>::iterator,numeric_array<T,D>::iterator> > : public ecd_fixed<D> {};
190 
191  template < typename T, typename D >
192  struct loop_label<numeric_array<T,D>::const_iterator> : public ecd_fixed<D> {};
193 
194  namespace detail
195  {
196 
197  template < typename TFunctor, typename TIterator >
198  void loop_standard(TFunctor f, std::pair<TIterator, TIterator> range)
199  {
200  for (; range.first != range.second; ++range.first)
201  {
202  f(range.first);
203  }
204  }
205 
206  template < typename TFunctor, typename TIterator >
207  void newloop(TFunctor f, std::pair<TIterator,TIterator> range, ::til::label::ecd_dense)
208  {
209  detail::loop_standard(f, range);
210  }
211 
212 
213  template < typename TFunctor, typename TIterator1, typename TIterator2, std::size_t D >
214  void loop(TFunctor f, TIterator i, ecd_fixed<D>, TIterator2 j, ecd_fixed<D>)
215  {
216  for (std::size_t i = 0; i < D; ++i)
217  {
218  f(i,j);
219  }
220  }
221  }
222 
223 */
224 
225 
226 } // namespace til
227 
228 #endif
229 
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
void loop_vv(T1 &x, T2 &y, TBinaryFunctor f)
Definition: loop.h:41
numeric_array< T, D > size(const Box< T, D > &box)
Return the size of a box.
Definition: boxTools.h:56
void loop_ccc(const TContainer1 &c1, const TContainer2 &c2, TContainerRes &res, TBinaryFunctor f)
Definition: loop.h:112
void loop_cv(TContainer &c, T v, TBinaryFunctor f)
Definition: loop.h:17
void loop_cc(TContainer1 &c1, TContainer2 &res, TBinaryFunctor f)
Definition: loop.h:62
boost::enable_if_c< is_container< TContainer >::value &&!is_container< T >::value >::type loop(TContainer &c, T v, TBinaryFunctor f)
Apply a binary functor to a collection and a constant.
Definition: loop.h:32
void loop_vvv(T1 x, T2 y, T3 &z, TBinaryFunctor f)
Definition: loop.h:90
void loop_cvc(const TContainer &c1, typename boost::call_traits< typename TContainer::value_type >::param_type v, TContainer &res, TBinaryFunctor f)
Definition: loop.h:142