insert.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //----------------------------------------------------------------------------
  2. /// @file insert.hpp
  3. /// @brief
  4. ///
  5. /// @author Copyright (c) 2016 Francisco José Tapia (fjtapia@gmail.com )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanying file LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. /// @version 0.1
  10. ///
  11. /// @remarks
  12. //-----------------------------------------------------------------------------
  13. #ifndef __BOOST_SORT_COMMON_UTIL_INSERT_HPP
  14. #define __BOOST_SORT_COMMON_UTIL_INSERT_HPP
  15. //#include <boost/sort/spinsort/util/indirect.hpp>
  16. #include <boost/sort/common/util/insert.hpp>
  17. #include <boost/sort/common/util/traits.hpp>
  18. #include <boost/sort/common/util/algorithm.hpp>
  19. #include <cstdlib>
  20. #include <functional>
  21. #include <iterator>
  22. #include <memory>
  23. #include <type_traits>
  24. #include <vector>
  25. #include <cstddef>
  26. namespace boost
  27. {
  28. namespace sort
  29. {
  30. namespace common
  31. {
  32. namespace util
  33. {
  34. namespace here = boost::sort::common::util;
  35. //
  36. //############################################################################
  37. //
  38. // D E F I N I T I O N S O F F U N C T I O N S
  39. //
  40. // template < class Iter1_t, class Iter2_t, typename Compare>
  41. // void insert_sorted (Iter1_t first, Iter1_t mid, Iter1_t last,
  42. // Compare comp, Iter2_t it_aux)
  43. //
  44. //############################################################################
  45. //
  46. //-----------------------------------------------------------------------------
  47. // function : insert_sorted
  48. /// @brief : Insertion sort of elements sorted
  49. /// @param first: iterator to the first element of the range
  50. /// @param mid : last pointer of the sorted data, and first pointer to the
  51. /// elements to insert
  52. /// @param last : iterator to the next element of the last in the range
  53. /// @param comp :
  54. /// @comments : the two ranges are sorted and in it_aux there is spave for
  55. /// to store temporally the elements to insert
  56. //-----------------------------------------------------------------------------
  57. template<class Iter1_t, class Iter2_t, typename Compare>
  58. static void insert_sorted(Iter1_t first, Iter1_t mid, Iter1_t last,
  59. Compare comp, Iter2_t it_aux)
  60. {
  61. //------------------------------------------------------------------------
  62. // metaprogram
  63. //------------------------------------------------------------------------
  64. typedef value_iter<Iter1_t> value_t;
  65. typedef value_iter<Iter2_t> value2_t;
  66. static_assert (std::is_same< value_t, value2_t>::value,
  67. "Incompatible iterators\n");
  68. //--------------------------------------------------------------------
  69. // program
  70. //--------------------------------------------------------------------
  71. if (mid == last) return;
  72. if (first == mid) return;
  73. //------------------------------------------------------------------------
  74. // creation of the vector of elements to insert and their position in the
  75. // sorted part
  76. // the data are inserted in it_aux
  77. //-----------------------------------------------------------------------
  78. move_forward(it_aux, mid, last);
  79. // search of the iterators where insert the new elements
  80. size_t ndata = last - mid;
  81. Iter1_t mv_first = mid, mv_last = mid;
  82. for (size_t i = ndata; i > 0; --i)
  83. {
  84. mv_last = mv_first;
  85. mv_first = std::upper_bound(first, mv_last, it_aux[i - 1], comp);
  86. Iter1_t it1 = here::move_backward(mv_last + i, mv_first, mv_last);
  87. *(it1 - 1) = std::move(it_aux[i - 1]);
  88. };
  89. };
  90. template<class Iter1_t, class Iter2_t, typename Compare>
  91. static void insert_sorted_backward(Iter1_t first, Iter1_t mid, Iter1_t last,
  92. Compare comp, Iter2_t it_aux)
  93. {
  94. //------------------------------------------------------------------------
  95. // metaprogram
  96. //------------------------------------------------------------------------
  97. typedef value_iter<Iter1_t> value_t;
  98. typedef value_iter<Iter2_t> value2_t;
  99. static_assert (std::is_same< value_t, value2_t>::value,
  100. "Incompatible iterators\n");
  101. //--------------------------------------------------------------------
  102. // program
  103. //--------------------------------------------------------------------
  104. if (mid == last) return;
  105. if (first == mid) return;
  106. //------------------------------------------------------------------------
  107. // creation of the vector of elements to insert and their position in the
  108. // sorted part
  109. // the data are inserted in it_aux
  110. //-----------------------------------------------------------------------
  111. move_forward(it_aux, first, mid);
  112. // search of the iterators where insert the new elements
  113. size_t ndata = mid - first;
  114. Iter1_t mv_first = mid, mv_last = mid;
  115. for (size_t i = 0; i < ndata; ++i)
  116. {
  117. mv_first = mv_last;
  118. mv_last = std::lower_bound(mv_first, last, it_aux[i], comp);
  119. Iter1_t it1 = move_forward(mv_first - (ndata - i), mv_first, mv_last);
  120. *(it1) = std::move(it_aux[i]);
  121. };
  122. };
  123. //
  124. //****************************************************************************
  125. };// End namespace util
  126. };// End namepspace common
  127. };// End namespace sort
  128. };// End namepspace boost
  129. //****************************************************************************
  130. //
  131. #endif