adaptive_merge.hpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2016.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #ifndef BOOST_MOVE_ADAPTIVE_MERGE_HPP
  12. #define BOOST_MOVE_ADAPTIVE_MERGE_HPP
  13. #include <boost/move/detail/config_begin.hpp>
  14. #include <boost/move/algo/detail/adaptive_sort_merge.hpp>
  15. namespace boost {
  16. namespace movelib {
  17. //! <b>Effects</b>: Merges two consecutive sorted ranges [first, middle) and [middle, last)
  18. //! into one sorted range [first, last) according to the given comparison function comp.
  19. //! The algorithm is stable (if there are equivalent elements in the original two ranges,
  20. //! the elements from the first range (preserving their original order) precede the elements
  21. //! from the second range (preserving their original order).
  22. //!
  23. //! <b>Requires</b>:
  24. //! - RandIt must meet the requirements of ValueSwappable and RandomAccessIterator.
  25. //! - The type of dereferenced RandIt must meet the requirements of MoveAssignable and MoveConstructible.
  26. //!
  27. //! <b>Parameters</b>:
  28. //! - first: the beginning of the first sorted range.
  29. //! - middle: the end of the first sorted range and the beginning of the second
  30. //! - last: the end of the second sorted range
  31. //! - comp: comparison function object which returns true if the first argument is is ordered before the second.
  32. //! - uninitialized, uninitialized_len: raw storage starting on "uninitialized", able to hold "uninitialized_len"
  33. //! elements of type iterator_traits<RandIt>::value_type. Maximum performance is achieved when uninitialized_len
  34. //! is min(std::distance(first, middle), std::distance(middle, last)).
  35. //!
  36. //! <b>Throws</b>: If comp throws or the move constructor, move assignment or swap of the type
  37. //! of dereferenced RandIt throws.
  38. //!
  39. //! <b>Complexity</b>: Always K x O(N) comparisons and move assignments/constructors/swaps.
  40. //! Constant factor for comparisons and data movement is minimized when uninitialized_len
  41. //! is min(std::distance(first, middle), std::distance(middle, last)).
  42. //! Pretty good enough performance is achieved when uninitialized_len is
  43. //! ceil(sqrt(std::distance(first, last)))*2.
  44. //!
  45. //! <b>Caution</b>: Experimental implementation, not production-ready.
  46. template<class RandIt, class Compare>
  47. void adaptive_merge( RandIt first, RandIt middle, RandIt last, Compare comp
  48. , typename iterator_traits<RandIt>::value_type* uninitialized = 0
  49. , std::size_t uninitialized_len = 0)
  50. {
  51. typedef typename iterator_traits<RandIt>::size_type size_type;
  52. typedef typename iterator_traits<RandIt>::value_type value_type;
  53. ::boost::movelib::detail_adaptive::adaptive_xbuf<value_type> xbuf(uninitialized, uninitialized_len);
  54. ::boost::movelib::detail_adaptive::adaptive_merge_impl(first, size_type(middle - first), size_type(last - middle), comp, xbuf);
  55. }
  56. } //namespace movelib {
  57. } //namespace boost {
  58. #include <boost/move/detail/config_end.hpp>
  59. #endif //#define BOOST_MOVE_ADAPTIVE_MERGE_HPP