Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:41

0001 // Copyright (c)  2000 David Abrahams.
0002 // Distributed under the Boost Software License, Version 1.0.
0003 // (See accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 //
0006 // Copyright (c) 1994
0007 // Hewlett-Packard Company
0008 //
0009 // Permission to use, copy, modify, distribute and sell this software
0010 // and its documentation for any purpose is hereby granted without fee,
0011 // provided that the above copyright notice appear in all copies and
0012 // that both that copyright notice and this permission notice appear
0013 // in supporting documentation.  Hewlett-Packard Company makes no
0014 // representations about the suitability of this software for any
0015 // purpose.  It is provided "as is" without express or implied warranty.
0016 //
0017 // Copyright (c) 1996
0018 // Silicon Graphics Computer Systems, Inc.
0019 //
0020 // Permission to use, copy, modify, distribute and sell this software
0021 // and its documentation for any purpose is hereby granted without fee,
0022 // provided that the above copyright notice appear in all copies and
0023 // that both that copyright notice and this permission notice appear
0024 // in supporting documentation.  Silicon Graphics makes no
0025 // representations about the suitability of this software for any
0026 // purpose.  It is provided "as is" without express or implied warranty.
0027 //
0028 #ifndef BINARY_SEARCH_DWA_122600_H_
0029 # define BINARY_SEARCH_DWA_122600_H_
0030 
0031 # include <utility>
0032 # include <iterator>
0033 
0034 namespace boost { namespace detail {
0035 
0036 template <class ForwardIter, class Tp>
0037 ForwardIter lower_bound(ForwardIter first, ForwardIter last,
0038                              const Tp& val)
0039 {
0040     typedef std::iterator_traits<ForwardIter> traits;
0041 
0042     typename traits::difference_type len = std::distance(first, last);
0043     typename traits::difference_type half;
0044     ForwardIter middle;
0045 
0046     while (len > 0) {
0047       half = len >> 1;
0048       middle = first;
0049       std::advance(middle, half);
0050       if (*middle < val) {
0051         first = middle;
0052         ++first;
0053         len = len - half - 1;
0054       }
0055       else
0056         len = half;
0057     }
0058     return first;
0059 }
0060 
0061 template <class ForwardIter, class Tp, class Compare>
0062 ForwardIter lower_bound(ForwardIter first, ForwardIter last,
0063                               const Tp& val, Compare comp)
0064 {
0065   typedef std::iterator_traits<ForwardIter> traits;
0066 
0067   typename traits::difference_type len = std::distance(first, last);
0068   typename traits::difference_type half;
0069   ForwardIter middle;
0070 
0071   while (len > 0) {
0072     half = len >> 1;
0073     middle = first;
0074     std::advance(middle, half);
0075     if (comp(*middle, val)) {
0076       first = middle;
0077       ++first;
0078       len = len - half - 1;
0079     }
0080     else
0081       len = half;
0082   }
0083   return first;
0084 }
0085 
0086 template <class ForwardIter, class Tp>
0087 ForwardIter upper_bound(ForwardIter first, ForwardIter last,
0088                            const Tp& val)
0089 {
0090   typedef std::iterator_traits<ForwardIter> traits;
0091 
0092   typename traits::difference_type len = std::distance(first, last);
0093   typename traits::difference_type half;
0094   ForwardIter middle;
0095 
0096   while (len > 0) {
0097     half = len >> 1;
0098     middle = first;
0099     std::advance(middle, half);
0100     if (val < *middle)
0101       len = half;
0102     else {
0103       first = middle;
0104       ++first;
0105       len = len - half - 1;
0106     }
0107   }
0108   return first;
0109 }
0110 
0111 template <class ForwardIter, class Tp, class Compare>
0112 ForwardIter upper_bound(ForwardIter first, ForwardIter last,
0113                            const Tp& val, Compare comp)
0114 {
0115   typedef std::iterator_traits<ForwardIter> traits;
0116 
0117   typename traits::difference_type len = std::distance(first, last);
0118   typename traits::difference_type half;
0119   ForwardIter middle;
0120 
0121   while (len > 0) {
0122     half = len >> 1;
0123     middle = first;
0124     std::advance(middle, half);
0125     if (comp(val, *middle))
0126       len = half;
0127     else {
0128       first = middle;
0129       ++first;
0130       len = len - half - 1;
0131     }
0132   }
0133   return first;
0134 }
0135 
0136 template <class ForwardIter, class Tp>
0137 std::pair<ForwardIter, ForwardIter>
0138 equal_range(ForwardIter first, ForwardIter last, const Tp& val)
0139 {
0140   typedef std::iterator_traits<ForwardIter> traits;
0141 
0142   typename traits::difference_type len = std::distance(first, last);
0143   typename traits::difference_type half;
0144   ForwardIter middle, left, right;
0145 
0146   while (len > 0) {
0147     half = len >> 1;
0148     middle = first;
0149     std::advance(middle, half);
0150     if (*middle < val) {
0151       first = middle;
0152       ++first;
0153       len = len - half - 1;
0154     }
0155     else if (val < *middle)
0156       len = half;
0157     else {
0158       left = boost::detail::lower_bound(first, middle, val);
0159       std::advance(first, len);
0160       right = boost::detail::upper_bound(++middle, first, val);
0161       return std::pair<ForwardIter, ForwardIter>(left, right);
0162     }
0163   }
0164   return std::pair<ForwardIter, ForwardIter>(first, first);
0165 }
0166 
0167 template <class ForwardIter, class Tp, class Compare>
0168 std::pair<ForwardIter, ForwardIter>
0169 equal_range(ForwardIter first, ForwardIter last, const Tp& val,
0170               Compare comp)
0171 {
0172   typedef std::iterator_traits<ForwardIter> traits;
0173 
0174   typename traits::difference_type len = std::distance(first, last);
0175   typename traits::difference_type half;
0176   ForwardIter middle, left, right;
0177 
0178   while (len > 0) {
0179     half = len >> 1;
0180     middle = first;
0181     std::advance(middle, half);
0182     if (comp(*middle, val)) {
0183       first = middle;
0184       ++first;
0185       len = len - half - 1;
0186     }
0187     else if (comp(val, *middle))
0188       len = half;
0189     else {
0190       left = boost::detail::lower_bound(first, middle, val, comp);
0191       std::advance(first, len);
0192       right = boost::detail::upper_bound(++middle, first, val, comp);
0193       return std::pair<ForwardIter, ForwardIter>(left, right);
0194     }
0195   }
0196   return std::pair<ForwardIter, ForwardIter>(first, first);
0197 }
0198 
0199 template <class ForwardIter, class Tp>
0200 bool binary_search(ForwardIter first, ForwardIter last,
0201                    const Tp& val) {
0202   ForwardIter i = boost::detail::lower_bound(first, last, val);
0203   return i != last && !(val < *i);
0204 }
0205 
0206 template <class ForwardIter, class Tp, class Compare>
0207 bool binary_search(ForwardIter first, ForwardIter last,
0208                    const Tp& val,
0209                    Compare comp) {
0210   ForwardIter i = boost::detail::lower_bound(first, last, val, comp);
0211   return i != last && !comp(val, *i);
0212 }
0213 
0214 }} // namespace boost::detail
0215 
0216 #endif // BINARY_SEARCH_DWA_122600_H_