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