1 // Copyright (c) 2000 David Abrahams.
\r
2 // Distributed under the Boost Software License, Version 1.0.
\r
3 // (See accompanying file LICENSE_1_0.txt or copy at
\r
4 // http://www.boost.org/LICENSE_1_0.txt)
\r
6 // Copyright (c) 1994
\r
7 // Hewlett-Packard Company
\r
9 // Permission to use, copy, modify, distribute and sell this software
\r
10 // and its documentation for any purpose is hereby granted without fee,
\r
11 // provided that the above copyright notice appear in all copies and
\r
12 // that both that copyright notice and this permission notice appear
\r
13 // in supporting documentation. Hewlett-Packard Company makes no
\r
14 // representations about the suitability of this software for any
\r
15 // purpose. It is provided "as is" without express or implied warranty.
\r
17 // Copyright (c) 1996
\r
18 // Silicon Graphics Computer Systems, Inc.
\r
20 // Permission to use, copy, modify, distribute and sell this software
\r
21 // and its documentation for any purpose is hereby granted without fee,
\r
22 // provided that the above copyright notice appear in all copies and
\r
23 // that both that copyright notice and this permission notice appear
\r
24 // in supporting documentation. Silicon Graphics makes no
\r
25 // representations about the suitability of this software for any
\r
26 // purpose. It is provided "as is" without express or implied warranty.
\r
28 #ifndef BINARY_SEARCH_DWA_122600_H_
\r
29 # define BINARY_SEARCH_DWA_122600_H_
\r
31 # include <boost/detail/iterator.hpp>
\r
34 namespace boost { namespace detail {
\r
36 template <class ForwardIter, class Tp>
\r
37 ForwardIter lower_bound(ForwardIter first, ForwardIter last,
\r
40 typedef detail::iterator_traits<ForwardIter> traits;
\r
42 typename traits::difference_type len = boost::detail::distance(first, last);
\r
43 typename traits::difference_type half;
\r
49 std::advance(middle, half);
\r
50 if (*middle < val) {
\r
53 len = len - half - 1;
\r
61 template <class ForwardIter, class Tp, class Compare>
\r
62 ForwardIter lower_bound(ForwardIter first, ForwardIter last,
\r
63 const Tp& val, Compare comp)
\r
65 typedef detail::iterator_traits<ForwardIter> traits;
\r
67 typename traits::difference_type len = boost::detail::distance(first, last);
\r
68 typename traits::difference_type half;
\r
74 std::advance(middle, half);
\r
75 if (comp(*middle, val)) {
\r
78 len = len - half - 1;
\r
86 template <class ForwardIter, class Tp>
\r
87 ForwardIter upper_bound(ForwardIter first, ForwardIter last,
\r
90 typedef detail::iterator_traits<ForwardIter> traits;
\r
92 typename traits::difference_type len = boost::detail::distance(first, last);
\r
93 typename traits::difference_type half;
\r
99 std::advance(middle, half);
\r
105 len = len - half - 1;
\r
111 template <class ForwardIter, class Tp, class Compare>
\r
112 ForwardIter upper_bound(ForwardIter first, ForwardIter last,
\r
113 const Tp& val, Compare comp)
\r
115 typedef detail::iterator_traits<ForwardIter> traits;
\r
117 typename traits::difference_type len = boost::detail::distance(first, last);
\r
118 typename traits::difference_type half;
\r
119 ForwardIter middle;
\r
124 std::advance(middle, half);
\r
125 if (comp(val, *middle))
\r
130 len = len - half - 1;
\r
136 template <class ForwardIter, class Tp>
\r
137 std::pair<ForwardIter, ForwardIter>
\r
138 equal_range(ForwardIter first, ForwardIter last, const Tp& val)
\r
140 typedef detail::iterator_traits<ForwardIter> traits;
\r
142 typename traits::difference_type len = boost::detail::distance(first, last);
\r
143 typename traits::difference_type half;
\r
144 ForwardIter middle, left, right;
\r
149 std::advance(middle, half);
\r
150 if (*middle < val) {
\r
153 len = len - half - 1;
\r
155 else if (val < *middle)
\r
158 left = boost::detail::lower_bound(first, middle, val);
\r
159 std::advance(first, len);
\r
160 right = boost::detail::upper_bound(++middle, first, val);
\r
161 return std::pair<ForwardIter, ForwardIter>(left, right);
\r
164 return std::pair<ForwardIter, ForwardIter>(first, first);
\r
167 template <class ForwardIter, class Tp, class Compare>
\r
168 std::pair<ForwardIter, ForwardIter>
\r
169 equal_range(ForwardIter first, ForwardIter last, const Tp& val,
\r
172 typedef detail::iterator_traits<ForwardIter> traits;
\r
174 typename traits::difference_type len = boost::detail::distance(first, last);
\r
175 typename traits::difference_type half;
\r
176 ForwardIter middle, left, right;
\r
181 std::advance(middle, half);
\r
182 if (comp(*middle, val)) {
\r
185 len = len - half - 1;
\r
187 else if (comp(val, *middle))
\r
190 left = boost::detail::lower_bound(first, middle, val, comp);
\r
191 std::advance(first, len);
\r
192 right = boost::detail::upper_bound(++middle, first, val, comp);
\r
193 return std::pair<ForwardIter, ForwardIter>(left, right);
\r
196 return std::pair<ForwardIter, ForwardIter>(first, first);
\r
199 template <class ForwardIter, class Tp>
\r
200 bool binary_search(ForwardIter first, ForwardIter last,
\r
202 ForwardIter i = boost::detail::lower_bound(first, last, val);
\r
203 return i != last && !(val < *i);
\r
206 template <class ForwardIter, class Tp, class Compare>
\r
207 bool binary_search(ForwardIter first, ForwardIter last,
\r
210 ForwardIter i = boost::detail::lower_bound(first, last, val, comp);
\r
211 return i != last && !comp(val, *i);
\r
214 }} // namespace boost::detail
\r
216 #endif // BINARY_SEARCH_DWA_122600_H_
\r