gather.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2008 Adobe Systems Incorporated
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Revision history:
  6. January 2008 mtc Version for Adobe Source Library
  7. January 2013 mtc Version for Boost.Algorithm
  8. */
  9. /**************************************************************************************************/
  10. /*!
  11. \author Marshall Clow
  12. \date January 2008
  13. */
  14. #ifndef BOOST_ALGORITHM_GATHER_HPP
  15. #define BOOST_ALGORITHM_GATHER_HPP
  16. #include <algorithm> // for std::stable_partition
  17. #include <functional>
  18. #include <boost/config.hpp>
  19. #include <boost/bind.hpp> // for boost::bind
  20. #include <boost/range/begin.hpp> // for boost::begin(range)
  21. #include <boost/range/end.hpp> // for boost::end(range)
  22. /**************************************************************************************************/
  23. /*!
  24. \defgroup gather gather
  25. \ingroup mutating_algorithm
  26. \c gather() takes a collection of elements defined by a pair of iterators and moves
  27. the ones satisfying a predicate to them to a position (called the pivot) within
  28. the sequence. The algorithm is stable. The result is a pair of iterators that
  29. contains the items that satisfy the predicate.
  30. Given an sequence containing:
  31. <pre>
  32. 0 1 2 3 4 5 6 7 8 9
  33. </pre>
  34. a call to gather ( arr, arr + 10, arr + 4, IsEven ()) will result in:
  35. <pre>
  36. 1 3 0 2 4 6 8 5 7 9
  37. |---|-----|
  38. first | second
  39. pivot
  40. </pre>
  41. The problem is broken down into two basic steps, namely, moving the items before the pivot
  42. and then moving the items from the pivot to the end. These "moves" are done with calls to
  43. stable_partition.
  44. \par Storage Requirements:
  45. The algorithm uses stable_partition, which will attempt to allocate temporary memory,
  46. but will work in-situ if there is none available.
  47. \par Time Complexity:
  48. If there is sufficient memory available, the run time is linear in <code>N</code>.
  49. If there is not any memory available, then the run time is <code>O(N log N)</code>.
  50. */
  51. /**************************************************************************************************/
  52. namespace boost { namespace algorithm {
  53. /**************************************************************************************************/
  54. /*!
  55. \ingroup gather
  56. \brief iterator-based gather implementation
  57. */
  58. template <
  59. typename BidirectionalIterator, // Iter models BidirectionalIterator
  60. typename Pred> // Pred models UnaryPredicate
  61. std::pair<BidirectionalIterator, BidirectionalIterator> gather
  62. ( BidirectionalIterator first, BidirectionalIterator last, BidirectionalIterator pivot, Pred pred )
  63. {
  64. // The first call partitions everything up to (but not including) the pivot element,
  65. // while the second call partitions the rest of the sequence.
  66. return std::make_pair (
  67. std::stable_partition ( first, pivot, !boost::bind<bool> ( pred, _1 )),
  68. std::stable_partition ( pivot, last, boost::bind<bool> ( pred, _1 )));
  69. }
  70. /**************************************************************************************************/
  71. /*!
  72. \ingroup gather
  73. \brief range-based gather implementation
  74. */
  75. template <
  76. typename BidirectionalRange, //
  77. typename Pred> // Pred models UnaryPredicate
  78. std::pair<
  79. typename boost::range_iterator<const BidirectionalRange>::type,
  80. typename boost::range_iterator<const BidirectionalRange>::type>
  81. gather (
  82. const BidirectionalRange &range,
  83. typename boost::range_iterator<const BidirectionalRange>::type pivot,
  84. Pred pred )
  85. {
  86. return boost::algorithm::gather ( boost::begin ( range ), boost::end ( range ), pivot, pred );
  87. }
  88. /**************************************************************************************************/
  89. }} // namespace
  90. /**************************************************************************************************/
  91. #endif