gather.qbk 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [/ File gather.qbk]
  2. [section:gather gather]
  3. [/license
  4. Copyright (c) 2013 Marshall Clow
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. The header file 'boost/algorithm/gather.hpp' contains two variants of a single algorithm, `gather`.
  9. `gather()` takes a collection of elements defined by a pair of iterators and moves the ones satisfying a predicate to them to a position (called the pivot) within the sequence. The algorithm is stable. The result is a pair of iterators that contains the items that satisfy the predicate.
  10. [heading Interface]
  11. The function `gather` returns a `std::pair` of iterators that denote the elements that satisfy the predicate.
  12. There are two versions; one takes two iterators, and the other takes a range.
  13. ``
  14. namespace boost { namespace algorithm {
  15. template <typename BidirectionalIterator, typename Pred>
  16. std::pair<BidirectionalIterator,BidirectionalIterator>
  17. gather ( BidirectionalIterator first, BidirectionalIterator last, BidirectionalIterator pivot, Pred pred );
  18. template <typename BidirectionalRange, typename Pred>
  19. std::pair<typename boost::range_iterator<const BidirectionalRange>::type, typename boost::range_iterator<const BidirectionalRange>::type>
  20. gather ( const BidirectionalRange &range, typename boost::range_iterator<const BidirectionalRange>::type pivot, Pred pred );
  21. }}
  22. ``
  23. [heading Examples]
  24. Given an sequence containing:
  25. ``
  26. 0 1 2 3 4 5 6 7 8 9
  27. ``
  28. a call to gather ( arr, arr + 10, arr + 4, IsEven ) will result in:
  29. ``
  30. 1 3 0 2 4 6 8 5 7 9
  31. |---|-----|
  32. first | second
  33. pivot
  34. ``
  35. where `first` and `second` are the fields of the pair that is returned by the call.
  36. [heading Iterator Requirements]
  37. `gather` work on bidirectional iterators or better. This requirement comes from the usage of `stable_partition`, which requires bidirectional iterators. Some standard libraries (libstdc++ and libc++, for example) have implementations of `stable_partition` that work with forward iterators. If that is the case, then `gather` will work with forward iterators as well.
  38. [heading Storage Requirements]
  39. `gather` uses `stable_partition`, which will attempt to allocate temporary memory, but will work in-situ if there is none available.
  40. [heading Complexity]
  41. If there is sufficient memory available, the run time is linear: `O(N)`
  42. If there is not any memory available, then the run time is `O(N log N)`.
  43. [heading Exception Safety]
  44. [heading Notes]
  45. [endsect]
  46. [/ File gather.qbk
  47. Copyright 2013 Marshall Clow
  48. Distributed under the Boost Software License, Version 1.0.
  49. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
  50. ]