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