upper_bound.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef BOOST_MPL_UPPER_BOUND_HPP_INCLUDED
  2. #define BOOST_MPL_UPPER_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_UPPER_BOUND_IMPL
  19. #endif
  20. #if !defined(BOOST_MPL_CFG_STRIPPED_DOWN_UPPER_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/find.hpp>
  34. # include <boost/mpl/bind.hpp>
  35. #endif
  36. #include <boost/config.hpp>
  37. namespace boost { namespace mpl {
  38. #if defined(BOOST_MPL_CFG_STRIPPED_DOWN_UPPER_BOUND_IMPL)
  39. // agurt 23/oct/02: has a wrong complexity etc., but at least it works;
  40. // feel free to contribute a better implementation!
  41. template<
  42. typename BOOST_MPL_AUX_NA_PARAM(Sequence)
  43. , typename BOOST_MPL_AUX_NA_PARAM(T)
  44. , typename Predicate = less<>
  45. , typename pred_ = typename lambda<Predicate>::type
  46. >
  47. struct upper_bound
  48. : find_if< Sequence, bind2<pred_,T,_> >
  49. {
  50. };
  51. #else
  52. namespace aux {
  53. template<
  54. typename Distance
  55. , typename Predicate
  56. , typename T
  57. , typename DeferredIterator
  58. >
  59. struct upper_bound_step_impl;
  60. template<
  61. typename Distance
  62. , typename Predicate
  63. , typename T
  64. , typename DeferredIterator
  65. >
  66. struct upper_bound_step
  67. {
  68. typedef typename eval_if<
  69. Distance
  70. , upper_bound_step_impl<Distance,Predicate,T,DeferredIterator>
  71. , DeferredIterator
  72. >::type type;
  73. };
  74. template<
  75. typename Distance
  76. , typename Predicate
  77. , typename T
  78. , typename DeferredIterator
  79. >
  80. struct upper_bound_step_impl
  81. {
  82. typedef typename divides< Distance, long_<2> >::type offset_;
  83. typedef typename DeferredIterator::type iter_;
  84. typedef typename advance< iter_,offset_ >::type middle_;
  85. typedef typename apply2<
  86. Predicate
  87. , T
  88. , typename deref<middle_>::type
  89. >::type cond_;
  90. typedef typename prior< minus< Distance, offset_ > >::type step_;
  91. typedef upper_bound_step< offset_,Predicate,T,DeferredIterator > step_forward_;
  92. typedef upper_bound_step< step_,Predicate,T,next<middle_> > step_backward_;
  93. typedef typename eval_if<
  94. cond_
  95. , step_forward_
  96. , step_backward_
  97. >::type type;
  98. };
  99. } // namespace aux
  100. template<
  101. typename BOOST_MPL_AUX_NA_PARAM(Sequence)
  102. , typename BOOST_MPL_AUX_NA_PARAM(T)
  103. , typename Predicate = less<>
  104. >
  105. struct upper_bound
  106. {
  107. private:
  108. typedef typename lambda<Predicate>::type pred_;
  109. typedef typename size<Sequence>::type size_;
  110. public:
  111. typedef typename aux::upper_bound_step<
  112. size_,pred_,T,begin<Sequence>
  113. >::type type;
  114. };
  115. #endif // BOOST_MPL_CFG_STRIPPED_DOWN_UPPER_BOUND_IMPL
  116. BOOST_MPL_AUX_NA_SPEC(2, upper_bound)
  117. }}
  118. #endif // BOOST_MPL_UPPER_BOUND_HPP_INCLUDED