stlab.adobe.com Adobe Systems Incorporated
find.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_ALGORITHM_FIND_HPP
10 #define ADOBE_ALGORITHM_FIND_HPP
11 
12 #include <adobe/config.hpp>
13 
14 #include <algorithm>
15 #include <utility>
16 
17 #include <boost/range/begin.hpp>
18 #include <boost/range/end.hpp>
19 #include <boost/next_prior.hpp>
20 #include <boost/bind.hpp>
21 
22 /*************************************************************************************************/
23 
24 namespace adobe {
25 
26 /*************************************************************************************************/
39 /*************************************************************************************************/
40 
56 /*************************************************************************************************/
57 
58 /*************************************************************************************************/
73 /*************************************************************************************************/
79 template <class InputIterator, class Predicate>
80 inline InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred)
81 {
82  return std::find_if(first, last, !boost::bind(pred, _1));
83 }
84 
90 template <class InputRange, class Predicate>
91 inline typename boost::range_iterator<InputRange>::type
92 find_if_not(InputRange& range, Predicate pred)
93 {
94  return adobe::find_if_not(boost::begin(range), boost::end(range), pred);
95 }
96 
102 template <class InputRange, class Predicate>
103 inline typename boost::range_const_iterator<InputRange>::type
104 find_if_not(const InputRange& range, Predicate pred)
105 {
106  return adobe::find_if_not(boost::begin(range), boost::end(range), pred);
107 }
108 
109 /*************************************************************************************************/
110 
114 template <class InputIterator, class T>
115 inline InputIterator find_not(InputIterator first, InputIterator last, const T& value)
116 {
117  return std::find_if(first, last, boost::bind(std::not_equal_to<T>(), value, _1));
118 }
119 
125 template <class InputRange, class T>
126 inline typename boost::range_iterator<InputRange>::type
127 find_not(InputRange& range, const T& value)
128 {
129  return adobe::find_not(boost::begin(range), boost::end(range), value);
130 }
131 
137 template <class InputRange, class T>
138 inline typename boost::range_const_iterator<InputRange>::type
139 find_not(const InputRange& range, const T& value)
140 {
141  return adobe::find_not(boost::begin(range), boost::end(range), value);
142 }
143 
144 
149 template <class InputRange, class T>
150 inline typename boost::range_iterator<InputRange>::type
151 find(InputRange& range, const T& value)
152 {
153  return std::find(boost::begin(range), boost::end(range), value);
154 }
155 
161 template <class InputRange, class T>
162 inline typename boost::range_const_iterator<InputRange>::type
163 find(const InputRange& range, const T& value)
164 {
165  return std::find(boost::begin(range), boost::end(range), value);
166 }
167 
173 template <class InputIterator, class Predicate>
174 inline InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)
175 {
176  return std::find_if(first, last, boost::bind(pred, _1));
177 }
178 
184 template <class InputRange, class Predicate>
185 inline typename boost::range_iterator<InputRange>::type
186 find_if(InputRange& range, Predicate pred)
187 {
188  return adobe::find_if(boost::begin(range), boost::end(range), pred);
189 }
190 
196 template <class InputRange, class Predicate>
197 inline typename boost::range_const_iterator<InputRange>::type
198 find_if(const InputRange& range, Predicate pred)
199 {
200  return adobe::find_if(boost::begin(range), boost::end(range), pred);
201 }
202 
207 template < typename I, // I models ForwardIterator
208  typename T> // T is value_type(I)
209 std::pair<I, I> find_range(I f, I l, const T& x)
210 {
211  f = std::find(f, l, x);
212  if (f != l) l = find_not(boost::next(f), l, x);
213  return std::make_pair(f, l);
214 }
215 
220 template < typename I, // I models ForwardIterator
221  typename P> // P models UnaryPredicate(value_type(I))
222 std::pair<I, I> find_range_if(I f, I l, P p)
223 {
224  f = adobe::find_if(f, l, p);
225  if (f != l) l = adobe::find_if_not(boost::next(f), l, p);
226  return std::make_pair(f, l);
227 }
228 
234 template <class ForwardRange1, class ForwardRange2>
235 inline typename boost::range_iterator<ForwardRange1>::type
236 find_end(ForwardRange1& range1, const ForwardRange2& range2)
237 {
238  return std::find_end(boost::begin(range1), boost::end(range1),
239  boost::begin(range2), boost::end(range2));
240 }
241 
247 template <class ForwardRange1, class ForwardRange2>
248 inline typename boost::range_const_iterator<ForwardRange1>::type
249 find_end(const ForwardRange1& range1, const ForwardRange2& range2)
250 {
251  return std::find_end(boost::begin(range1), boost::end(range1),
252  boost::begin(range2), boost::end(range2));
253 }
254 
260 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
261 inline ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
262  ForwardIterator2 first2, ForwardIterator2 last2,
263  BinaryPredicate comp)
264 {
265  return std::find_end(first1, last1, first2, last2, boost::bind(comp, _1, _2));
266 }
267 
273 template <class ForwardRange1, class ForwardRange2, class BinaryPredicate>
274 inline typename boost::range_iterator<ForwardRange1>::type
275 find_end(ForwardRange1& range1, const ForwardRange2& range2, BinaryPredicate comp)
276 {
277  return adobe::find_end(boost::begin(range1), boost::end(range1),
278  boost::begin(range2), boost::end(range2),
279  comp);
280 }
281 
287 template <class ForwardRange1, class ForwardRange2, class BinaryPredicate>
288 inline typename boost::range_const_iterator<ForwardRange1>::type
289 find_end(const ForwardRange1& range1, const ForwardRange2& range2, BinaryPredicate comp)
290 {
291  return adobe::find_end(boost::begin(range1), boost::end(range1),
292  boost::begin(range2), boost::end(range2),
293  comp);
294 }
295 
296 #if 0
297 
298 // find_first_of is a bad algorithm. Use find_first_of_set until we provide a better predicate.
299 
305 template <class InputRange, class ForwardRange>
306 inline typename boost::range_iterator<InputRange>::type
307 find_first_of(InputRange& range1, const ForwardRange& range2)
308 {
309  return std::find_first_of(boost::begin(range1), boost::end(range1),
310  boost::begin(range2), boost::end(range2));
311 }
312 
318 template <class InputRange, class ForwardRange>
319 inline typename boost::range_const_iterator<InputRange>::type
320 find_first_of(const InputRange& range1, const ForwardRange& range2)
321 {
322  return std::find_first_of(boost::begin(range1), boost::end(range1),
323  boost::begin(range2), boost::end(range2));
324 }
325 
331 template <class InputIterator, class ForwardIterator, class BinaryPredicate>
332 inline InputIterator find_first_of(InputIterator first1, InputIterator last1,
333  ForwardIterator first2, ForwardIterator last2,
334  BinaryPredicate comp)
335 
336 {
337  return std::find_first_of(first1, last1, first2, last2, boost::bind(comp, _1, _2));
338 }
339 
345 template <class InputRange, class ForwardRange, class BinaryPredicate>
346 inline typename boost::range_iterator<InputRange>::type
347 find_first_of(InputRange& range1, const ForwardRange& range2, BinaryPredicate comp)
348 {
349  return adobe::find_first_of(boost::begin(range1), boost::end(range1),
350  boost::begin(range2), boost::end(range2),
351  comp);
352 }
353 
359 template <class InputRange, class ForwardRange, class BinaryPredicate>
360 inline typename boost::range_const_iterator<InputRange>::type
361 find_first_of(const InputRange& range1, const ForwardRange& range2, BinaryPredicate comp)
362 {
363  return adobe::find_first_of(boost::begin(range1), boost::end(range1),
364  boost::begin(range2), boost::end(range2),
365  comp);
366 }
367 
368 #endif
369 
375 template <class ForwardRange>
376 inline typename boost::range_iterator<ForwardRange>::type adjacent_find(ForwardRange& range)
377 {
378  return std::adjacent_find(boost::begin(range), boost::end(range));
379 }
380 
386 template <class ForwardRange>
387 inline typename boost::range_const_iterator<ForwardRange>::type
388 adjacent_find(const ForwardRange& range)
389 {
390  return std::adjacent_find(boost::begin(range), boost::end(range));
391 }
392 
398 template <class ForwardIterator, class BinaryPredicate>
399 inline ForwardIterator
400 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
401 {
402  return std::adjacent_find(first, last, boost::bind(pred, _1, _2));
403 }
404 
410 template <class ForwardRange, class BinaryPredicate>
411 inline typename boost::range_iterator<ForwardRange>::type
412 adjacent_find(ForwardRange& range, BinaryPredicate pred)
413 {
414  return adobe::adjacent_find(boost::begin(range), boost::end(range), pred);
415 }
416 
422 template <class ForwardRange, class BinaryPredicate>
423 inline typename boost::range_const_iterator<ForwardRange>::type
424 adjacent_find(const ForwardRange& range, BinaryPredicate pred)
425 {
426  return adobe::adjacent_find(boost::begin(range), boost::end(range), pred);
427 }
428 
429 /*************************************************************************************************/
430 
431 } // namespace adobe
432 
433 /*************************************************************************************************/
434 
435 #endif
436 
437 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google