| 1 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // algorithm.hpp | 
|---|
| 3 | // | 
|---|
| 4 | //  Copyright 2004 Eric Niebler. Distributed under the Boost | 
|---|
| 5 | //  Software License, Version 1.0. (See accompanying file | 
|---|
| 6 | //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005 | 
|---|
| 9 | #define BOOST_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005 | 
|---|
| 10 |  | 
|---|
| 11 | // MS compatible compilers support #pragma once | 
|---|
| 12 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) | 
|---|
| 13 | # pragma once | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | #include <climits> | 
|---|
| 17 | #include <algorithm> | 
|---|
| 18 | #include <boost/iterator/iterator_traits.hpp> | 
|---|
| 19 |  | 
|---|
| 20 | namespace boost { namespace xpressive { namespace detail | 
|---|
| 21 | { | 
|---|
| 22 |  | 
|---|
| 23 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 24 | // any | 
|---|
| 25 | // | 
|---|
| 26 | template<typename InIter, typename Pred> | 
|---|
| 27 | inline bool any(InIter begin, InIter end, Pred pred) | 
|---|
| 28 | { | 
|---|
| 29 |     return end != std::find_if(begin, end, pred); | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 33 | // find_nth_if | 
|---|
| 34 | // | 
|---|
| 35 | template<typename FwdIter, typename Diff, typename Pred> | 
|---|
| 36 | FwdIter find_nth_if(FwdIter begin, FwdIter end, Diff count, Pred pred) | 
|---|
| 37 | { | 
|---|
| 38 |     for(; begin != end; ++begin) | 
|---|
| 39 |     { | 
|---|
| 40 |         if(pred(*begin) && 0 == count--) | 
|---|
| 41 |         { | 
|---|
| 42 |             return begin; | 
|---|
| 43 |         } | 
|---|
| 44 |     } | 
|---|
| 45 |  | 
|---|
| 46 |     return end; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 50 | // toi | 
|---|
| 51 | // | 
|---|
| 52 | template<typename InIter, typename Traits> | 
|---|
| 53 | int toi(InIter &begin, InIter end, Traits const &traits, int radix = 10, int max = INT_MAX) | 
|---|
| 54 | { | 
|---|
| 55 |     int i = 0, c = 0; | 
|---|
| 56 |     for(; begin != end && -1 != (c = traits.value(*begin, radix)); ++begin) | 
|---|
| 57 |     { | 
|---|
| 58 |         if(max < ((i *= radix) += c)) | 
|---|
| 59 |             return i / radix; | 
|---|
| 60 |     } | 
|---|
| 61 |     return i; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 65 | // advance_to | 
|---|
| 66 | // | 
|---|
| 67 | template<typename BidiIter, typename Diff> | 
|---|
| 68 | inline bool advance_to_impl(BidiIter & iter, Diff diff, BidiIter end, std::bidirectional_iterator_tag) | 
|---|
| 69 | { | 
|---|
| 70 |     for(; 0 < diff && iter != end; --diff) | 
|---|
| 71 |         ++iter; | 
|---|
| 72 |     for(; 0 > diff && iter != end; ++diff) | 
|---|
| 73 |         --iter; | 
|---|
| 74 |     return 0 == diff; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | template<typename RandIter, typename Diff> | 
|---|
| 78 | inline bool advance_to_impl(RandIter & iter, Diff diff, RandIter end, std::random_access_iterator_tag) | 
|---|
| 79 | { | 
|---|
| 80 |     if(0 < diff) | 
|---|
| 81 |     { | 
|---|
| 82 |         if((end - iter) < diff) | 
|---|
| 83 |             return false; | 
|---|
| 84 |     } | 
|---|
| 85 |     else if(0 > diff) | 
|---|
| 86 |     { | 
|---|
| 87 |         if((iter - end) < -diff) | 
|---|
| 88 |             return false; | 
|---|
| 89 |     } | 
|---|
| 90 |     iter += diff; | 
|---|
| 91 |     return true; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | template<typename Iter, typename Diff> | 
|---|
| 95 | inline bool advance_to(Iter & iter, Diff diff, Iter end) | 
|---|
| 96 | { | 
|---|
| 97 |     return detail::advance_to_impl(iter, diff, end, typename iterator_category<Iter>::type()); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | }}} | 
|---|
| 101 |  | 
|---|
| 102 | #endif | 
|---|