lower_bound.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef BOOST_MPL_LOWER_BOUND_HPP_INCLUDED
  2. #define BOOST_MPL_LOWER_BOUND_HPP_INCLUDED
  3. // Copyright Aleksey Gurtovoy 2001-2004
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/mpl for documentation.
  10. // $Id$
  11. // $Date$
  12. // $Revision$
  13. #include <boost/mpl/less.hpp>
  14. #include <boost/mpl/lambda.hpp>
  15. #include <boost/mpl/aux_/na_spec.hpp>
  16. #include <boost/mpl/aux_/config/workaround.hpp>
  17. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610))
  18. # define BOOST_MPL_CFG_STRIPPED_DOWN_LOWER_BOUND_IMPL
  19. #endif
  20. #if !defined(BOOST_MPL_CFG_STRIPPED_DOWN_LOWER_BOUND_IMPL)
  21. # include <boost/mpl/minus.hpp>
  22. # include <boost/mpl/divides.hpp>
  23. # include <boost/mpl/size.hpp>
  24. # include <boost/mpl/advance.hpp>
  25. # include <boost/mpl/begin_end.hpp>
  26. # include <boost/mpl/long.hpp>
  27. # include <boost/mpl/eval_if.hpp>
  28. # include <boost/mpl/prior.hpp>
  29. # include <boost/mpl/deref.hpp>
  30. # include <boost/mpl/apply.hpp>
  31. # include <boost/mpl/aux_/value_wknd.hpp>
  32. #else
  33. # include <boost/mpl/not.hpp>
  34. # include <boost/mpl/find.hpp>
  35. # include <boost/mpl/bind.hpp>
  36. #endif
  37. #include <boost/config.hpp>
  38. namespace boost { namespace mpl {
  39. #if defined(BOOST_MPL_CFG_STRIPPED_DOWN_LOWER_BOUND_IMPL)
  40. // agurt 23/oct/02: has a wrong complexity etc., but at least it works
  41. // feel free to contribute a better implementation!
  42. template<
  43. typename BOOST_MPL_AUX_NA_PARAM(Sequence)
  44. , typename BOOST_MPL_AUX_NA_PARAM(T)
  45. , typename Predicate = less<>
  46. , typename pred_ = typename lambda<Predicate>::type
  47. >
  48. struct lower_bound
  49. : find_if< Sequence, bind1< not_<>, bind2<pred_,_,T> > >
  50. {
  51. };
  52. #else
  53. namespace aux {
  54. template<
  55. typename Distance
  56. , typename Predicate
  57. , typename T
  58. , typename DeferredIterator
  59. >
  60. struct lower_bound_step_impl;
  61. template<
  62. typename Distance
  63. , typename Predicate
  64. , typename T
  65. , typename DeferredIterator
  66. >
  67. struct lower_bound_step
  68. {
  69. typedef typename eval_if<
  70. Distance
  71. , lower_bound_step_impl<Distance,Predicate,T,DeferredIterator>
  72. , DeferredIterator
  73. >::type type;
  74. };
  75. template<
  76. typename Distance
  77. , typename Predicate
  78. , typename T
  79. , typename DeferredIterator
  80. >
  81. struct lower_bound_step_impl
  82. {
  83. typedef typename divides< Distance, long_<2> >::type offset_;
  84. typedef typename DeferredIterator::type iter_;
  85. typedef typename advance< iter_,offset_ >::type middle_;
  86. typedef typename apply2<
  87. Predicate
  88. , typename deref<middle_>::type
  89. , T
  90. >::type cond_;
  91. typedef typename prior< minus< Distance, offset_> >::type step_;
  92. typedef lower_bound_step< offset_,Predicate,T,DeferredIterator > step_forward_;
  93. typedef lower_bound_step< step_,Predicate,T,next<middle_> > step_backward_;
  94. typedef typename eval_if<
  95. cond_
  96. , step_backward_
  97. , step_forward_
  98. >::type type;
  99. };
  100. } // namespace aux
  101. template<
  102. typename BOOST_MPL_AUX_NA_PARAM(Sequence)
  103. , typename BOOST_MPL_AUX_NA_PARAM(T)
  104. , typename Predicate = less<>
  105. >
  106. struct lower_bound
  107. {
  108. private:
  109. typedef typename lambda<Predicate>::type pred_;
  110. typedef typename size<Sequence>::type size_;
  111. public:
  112. typedef typename aux::lower_bound_step<
  113. size_,pred_,T,begin<Sequence>
  114. >::type type;
  115. };
  116. #endif // BOOST_MPL_CFG_STRIPPED_DOWN_LOWER_BOUND_IMPL
  117. BOOST_MPL_AUX_NA_SPEC(2, lower_bound)
  118. }}
  119. #endif // BOOST_MPL_LOWER_BOUND_HPP_INCLUDED