00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef ADOBE_ALGORITHM_LOWER_BOUND_HPP
00010 #define ADOBE_ALGORITHM_LOWER_BOUND_HPP
00011
00012 #include <adobe/config.hpp>
00013
00014 #include <algorithm>
00015 #include <iterator>
00016
00017 #include <boost/range/begin.hpp>
00018 #include <boost/range/end.hpp>
00019 #include <boost/bind.hpp>
00020 #include <boost/next_prior.hpp>
00021 #include <boost/type_traits/is_same.hpp>
00022 #include <boost/utility/enable_if.hpp>
00023
00024 #include <adobe/functional/operator.hpp>
00025
00026
00027
00028 namespace adobe {
00029 namespace implementation {
00030
00031
00032
00033 template < typename I,
00034 typename N,
00035 typename T,
00036 typename C,
00037 typename P>
00038 I lower_bound_n_(I f, N n, const T& x, C c, P p)
00039 {
00040 while (n != 0)
00041 {
00042 N h = n >> 1;
00043 I m = boost::next(f, h);
00044
00045 if (c(p(*m), x)) { f = boost::next(m); n -= h + N(1); }
00046 else { n = h; }
00047 }
00048 return f;
00049 }
00050
00051
00052
00053 }
00054
00055
00056
00065
00066
00067 template < typename I,
00068 typename N,
00069 typename T>
00070 inline I lower_bound_n(I f, N n, const T& x)
00071 {
00072 return implementation::lower_bound_n_(f, n, x, less(), identity<T>());
00073 }
00074
00075
00076
00077 template < typename I,
00078 typename N,
00079 typename T,
00080 typename C>
00081 inline I lower_bound_n(I f, N n, const T& x, C c)
00082 {
00083 return implementation::lower_bound_n_(f, n, x, boost::bind(c, _1, _2), identity<T>());
00084 }
00085
00086
00087
00088 template < typename I,
00089 typename N,
00090 typename T,
00091 typename C,
00092 typename P>
00093 inline I lower_bound_n(I f, N n, const T& x, C c, P p)
00094 {
00095 return implementation::lower_bound_n_(f, n, x, boost::bind(c, _1, _2), boost::bind(p, _1));
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 namespace fn { } using namespace fn;
00107 namespace fn {
00108
00109
00110
00111 template < typename I,
00112 typename T>
00113 inline I lower_bound(I f, I l, const T& x)
00114 {
00115 return std::lower_bound(f, l, x);
00116 }
00117
00118
00119
00120 template < typename I,
00121 typename T,
00122 typename C>
00123 inline I lower_bound(I f, I l, const T& x, C c)
00124 {
00125 return std::lower_bound(f, l, x, boost::bind(c, _1, _2));
00126 }
00127
00128
00129
00130 template < typename I,
00131 typename T,
00132 typename C,
00133 typename P>
00134 inline I lower_bound(I f, I l, const T& x, C c, P p)
00135 {
00136 return lower_bound_n(f, std::distance(f, l), x, c, p);
00137 }
00138
00139
00140
00141 template < typename I,
00142 typename T,
00143 typename C,
00144 typename P>
00145 inline
00146 typename boost::lazy_disable_if<boost::is_same<I, T>, boost::range_iterator<I> >::type
00147 lower_bound(I& r, const T& x, C c, P p)
00148 { return adobe::lower_bound(boost::begin(r), boost::end(r), x, c, p); }
00149
00150
00151
00152 template < typename I,
00153 typename T,
00154 typename C,
00155 typename P>
00156 inline
00157 typename boost::lazy_disable_if<boost::is_same<I, T>, boost::range_const_iterator<I> >::type
00158 lower_bound(const I& r, const T& x, C c, P p)
00159 { return adobe::lower_bound(boost::begin(r), boost::end(r), x, c, p); }
00160
00161
00167 template <class ForwardRange, class T>
00168 inline typename boost::range_iterator<ForwardRange>::type lower_bound(ForwardRange& range, const T& value)
00169 {
00170 return std::lower_bound(boost::begin(range), boost::end(range), value);
00171 }
00172
00178 template <class ForwardRange, class T>
00179 inline typename boost::range_const_iterator<ForwardRange>::type lower_bound(const ForwardRange& range, const T& value)
00180 {
00181 return std::lower_bound(boost::begin(range), boost::end(range), value);
00182 }
00183
00194 template <typename I, class T, class Compare>
00195 inline typename boost::lazy_disable_if<boost::is_same<I, T>, boost::range_iterator<I> >::type
00196 lower_bound(I& range, const T& value, Compare comp)
00197 {
00198 return adobe::lower_bound(boost::begin(range), boost::end(range), value, comp);
00199 }
00200
00206 template <class I, class T, class Compare>
00207 inline typename boost::lazy_disable_if<boost::is_same<I, T>, boost::range_const_iterator<I> >::type
00208 lower_bound(const I& range, const T& value, Compare comp)
00209 {
00210 return adobe::lower_bound(boost::begin(range), boost::end(range), value, comp);
00211 }
00212
00213
00214
00215 }
00216 }
00217
00218
00219
00220 #endif
00221
00222