sort_basic.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //----------------------------------------------------------------------------
  2. /// @file sort_basic.hpp
  3. /// @brief Spin Sort algorithm
  4. ///
  5. /// @author Copyright (c) 2016 Francisco José Tapia ([email protected] )\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_SORT_BASIC_HPP
  14. #define __BOOST_SORT_COMMON_SORT_BASIC_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/insert_sort/insert_sort.hpp>
  24. #include <boost/sort/common/util/traits.hpp>
  25. #include <boost/sort/common/range.hpp>
  26. namespace boost
  27. {
  28. namespace sort
  29. {
  30. namespace common
  31. {
  32. //----------------------------------------------------------------------------
  33. // USING SENTENCES
  34. //----------------------------------------------------------------------------
  35. using boost::sort::insert_sort;
  36. //-----------------------------------------------------------------------------
  37. // function : is_stable_sorted_forward
  38. /// @brief examine the elements in the range first, last if they are stable
  39. /// sorted, and return an iterator to the first element not sorted
  40. /// @param first : iterator to the first element in the range
  41. /// @param last : ierator after the last element of the range
  42. /// @param comp : object for to compare two elements
  43. /// @return iterator to the first element not stable sorted. The number of
  44. /// elements sorted is the iterator returned minus first
  45. //-----------------------------------------------------------------------------
  46. template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
  47. inline Iter_t is_stable_sorted_forward (Iter_t first, Iter_t last,
  48. Compare comp = Compare())
  49. {
  50. #ifdef __BS_DEBUG
  51. assert ( (last- first) >= 0);
  52. #endif
  53. if ((last - first) < 2) return first;
  54. Iter_t it2 = first + 1;
  55. for (Iter_t it1 = first; it2 != last and not comp(*it2, *it1); it1 = it2++);
  56. return it2;
  57. }
  58. //-----------------------------------------------------------------------------
  59. // function : is_reverse_stable_sorted_forward
  60. /// @brief examine the elements in the range first, last if they are reverse
  61. /// stable sorted, and return an iterator to the first element not
  62. /// reverse stable sorted
  63. /// @param first : iterator to the first element in the range
  64. /// @param last : ierator after the last element of the range
  65. /// @param comp : object for to compare two elements
  66. /// @return iterator to the first element not reverse stable sorted. The number
  67. /// of elements sorted is the iterator returned minus first
  68. //-----------------------------------------------------------------------------
  69. template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
  70. inline Iter_t is_reverse_stable_sorted_forward(Iter_t first, Iter_t last,
  71. Compare comp = Compare())
  72. {
  73. #ifdef __BS_DEBUG
  74. assert ( (last- first) >= 0);
  75. #endif
  76. if ((last - first) < 2) return first;
  77. Iter_t it2 = first + 1;
  78. for (Iter_t it1 = first; it2 != last and comp(*it2, *it1); it1 = it2++);
  79. return it2;
  80. };
  81. //-----------------------------------------------------------------------------
  82. // function : number_stable_sorted_forward
  83. /// @brief examine the elements in the range first, last if they are stable
  84. /// sorted, and return the number of elements sorted
  85. /// @param first : iterator to the first element in the range
  86. /// @param last : ierator after the last element of the range
  87. /// @param comp : object for to compare two elements
  88. /// @param min_process : minimal number of elements to be consideer
  89. /// @return number of element sorted. I f the number is lower than min_process
  90. /// return 0
  91. //-----------------------------------------------------------------------------
  92. template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
  93. size_t number_stable_sorted_forward (Iter_t first, Iter_t last,
  94. size_t min_process,
  95. Compare comp = Compare())
  96. {
  97. #ifdef __BS_DEBUG
  98. assert ( (last- first) >= 0);
  99. #endif
  100. if ((last - first) < 2) return 0;
  101. // sorted elements
  102. Iter_t it2 = first + 1;
  103. for (Iter_t it1 = first; it2 != last and not comp(*it2, *it1); it1 = it2++);
  104. size_t nsorted = size_t ( it2 - first);
  105. if ( nsorted != 1)
  106. return (nsorted >= min_process) ? nsorted: 0;
  107. // reverse sorted elements
  108. it2 = first + 1;
  109. for (Iter_t it1 = first; it2 != last and comp(*it2, *it1); it1 = it2++);
  110. nsorted = size_t ( it2 - first);
  111. if ( nsorted < min_process) return 0 ;
  112. util::reverse ( first , it2);
  113. return nsorted;
  114. };
  115. //-----------------------------------------------------------------------------
  116. // function : is_stable_sorted_backward
  117. /// @brief examine the elements in the range first, last beginning at end, and
  118. /// if they are stablesorted, and return an iterator to the last element
  119. /// sorted
  120. /// @param first : iterator to the first element in the range
  121. /// @param last : ierator after the last element of the range
  122. /// @param comp : object for to compare two elements
  123. /// @return iterator to the last element stable sorted. The number of
  124. /// elements sorted is the last minus the iterator returned
  125. //-----------------------------------------------------------------------------
  126. template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
  127. inline Iter_t is_stable_sorted_backward(Iter_t first, Iter_t last,
  128. Compare comp = Compare())
  129. {
  130. #ifdef __BS_DEBUG
  131. assert ( (last- first) >= 0);
  132. #endif
  133. if ((last - first) < 2) return first;
  134. Iter_t itaux = last - 1;
  135. while (itaux != first and not comp(*itaux, *(itaux - 1))) {--itaux; };
  136. return itaux;
  137. }
  138. //-----------------------------------------------------------------------------
  139. // function : is_reverse_stable_sorted_backward
  140. /// @brief examine the elements in the range first, last beginning at end, and
  141. /// if they are stablesorted, and return an iterator to the last element
  142. /// sorted
  143. /// @param first : iterator to the first element in the range
  144. /// @param last : ierator after the last element of the range
  145. /// @param comp : object for to compare two elements
  146. /// @return iterator to the last element stable sorted. The number of
  147. /// elements sorted is the last minus the iterator returned
  148. //-----------------------------------------------------------------------------
  149. template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
  150. inline Iter_t is_reverse_stable_sorted_backward (Iter_t first, Iter_t last,
  151. Compare comp = Compare())
  152. {
  153. #ifdef __BS_DEBUG
  154. assert ( (last- first) >= 0);
  155. #endif
  156. if ((last - first) < 2) return first;
  157. Iter_t itaux = last - 1;
  158. for (; itaux != first and comp(*itaux, *(itaux - 1)); --itaux);
  159. return itaux;
  160. }
  161. //-----------------------------------------------------------------------------
  162. // function : number_stable_sorted_backward
  163. /// @brief examine the elements in the range first, last if they are stable
  164. /// sorted, and return the number of elements sorted
  165. /// @param first : iterator to the first element in the range
  166. /// @param last : ierator after the last element of the range
  167. /// @param comp : object for to compare two elements
  168. /// @param min_process : minimal number of elements to be consideer
  169. /// @return number of element sorted. I f the number is lower than min_process
  170. /// return 0
  171. //-----------------------------------------------------------------------------
  172. template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
  173. size_t number_stable_sorted_backward (Iter_t first, Iter_t last,
  174. size_t min_process,
  175. Compare comp = Compare())
  176. {
  177. #ifdef __BS_DEBUG
  178. assert ( (last- first) >= 0);
  179. #endif
  180. if ((last - first) < 2) return 0;
  181. Iter_t itaux = last - 1;
  182. while (itaux != first and not comp(*itaux, *(itaux - 1))) {--itaux; };
  183. size_t nsorted = size_t ( last - itaux);
  184. if ( nsorted != 1)
  185. return ( nsorted >= min_process)?nsorted: 0 ;
  186. itaux = last - 1;
  187. for (; itaux != first and comp(*itaux, *(itaux - 1)); --itaux);
  188. nsorted = size_t ( last - itaux);
  189. if ( nsorted < min_process) return 0 ;
  190. util::reverse ( itaux, last );
  191. return nsorted;
  192. }
  193. //-----------------------------------------------------------------------------
  194. // function : internal_sort
  195. /// @brief this function divide r_input in two parts, sort it,and merge moving
  196. /// the elements to range_buf
  197. /// @param range_input : range with the elements to sort
  198. /// @param range_buffer : range with the elements sorted
  199. /// @param comp : object for to compare two elements
  200. /// @param level : when is 1, sort with the insertionsort algorithm
  201. /// if not make a recursive call splitting the ranges
  202. //
  203. //-----------------------------------------------------------------------------
  204. template <class Iter1_t, class Iter2_t, class Compare>
  205. inline void internal_sort (const range<Iter1_t> &rng1,
  206. const range<Iter2_t> &rng2,
  207. Compare comp, uint32_t level, bool even = true)
  208. {
  209. //-----------------------------------------------------------------------
  210. // metaprogram
  211. //-----------------------------------------------------------------------
  212. typedef value_iter<Iter1_t> value_t;
  213. typedef value_iter<Iter2_t> value2_t;
  214. static_assert (std::is_same< value_t, value2_t>::value,
  215. "Incompatible iterators\n");
  216. //-----------------------------------------------------------------------
  217. // program
  218. //-----------------------------------------------------------------------
  219. #ifdef __BS_DEBUG
  220. assert (rng1.size ( ) == rng2.size ( ) );
  221. #endif
  222. size_t nelem = (rng1.size() + 1) >> 1;
  223. range<Iter1_t> rng1_left(rng1.first, rng1.first + nelem),
  224. rng1_right(rng1.first + nelem, rng1.last);
  225. range<Iter2_t> rng2_left(rng2.first, rng2.first + nelem),
  226. rng2_right(rng2.first + nelem, rng2.last);
  227. if (nelem <= 32 and (level & 1) == even)
  228. {
  229. insert_sort(rng1_left.first, rng1_left.last, comp);
  230. insert_sort(rng1_right.first, rng1_right.last, comp);
  231. }
  232. else
  233. {
  234. internal_sort(rng2_left, rng1_left, comp, level + 1, even);
  235. internal_sort(rng2_right, rng1_right, comp, level + 1, even);
  236. };
  237. merge(rng2, rng1_left, rng1_right, comp);
  238. };
  239. //-----------------------------------------------------------------------------
  240. // function : range_sort_data
  241. /// @brief this sort elements using the range_sort function and receiving a
  242. /// buffer of initialized memory
  243. /// @param rng_data : range with the elements to sort
  244. /// @param rng_aux : range of at least the same memory than rng_data used as
  245. /// auxiliary memory in the sorting
  246. /// @param comp : object for to compare two elements
  247. //-----------------------------------------------------------------------------
  248. template<class Iter1_t, class Iter2_t, class Compare>
  249. static void range_sort_data (const range<Iter1_t> & rng_data,
  250. const range<Iter2_t> & rng_aux, Compare comp)
  251. {
  252. //-----------------------------------------------------------------------
  253. // metaprogram
  254. //-----------------------------------------------------------------------
  255. typedef value_iter<Iter1_t> value_t;
  256. typedef value_iter<Iter2_t> value2_t;
  257. static_assert (std::is_same< value_t, value2_t>::value,
  258. "Incompatible iterators\n");
  259. //------------------------------------------------------------------------
  260. // program
  261. //------------------------------------------------------------------------
  262. #ifdef __BS_DEBUG
  263. assert ( rng_data.size() == rng_aux.size());
  264. #endif
  265. // minimal number of element before to jump to insertionsort
  266. const uint32_t sort_min = 32;
  267. if (rng_data.size() <= sort_min)
  268. {
  269. insert_sort(rng_data.first, rng_data.last, comp);
  270. return;
  271. };
  272. internal_sort(rng_aux, rng_data, comp, 0, true);
  273. };
  274. //-----------------------------------------------------------------------------
  275. // function : range_sort_buffer
  276. /// @brief this sort elements using the range_sort function and receiving a
  277. /// buffer of initialized memory
  278. /// @param rng_data : range with the elements to sort
  279. /// @param rng_aux : range of at least the same memory than rng_data used as
  280. /// auxiliary memory in the sorting
  281. /// @param comp : object for to compare two elements
  282. //-----------------------------------------------------------------------------
  283. template<class Iter1_t, class Iter2_t, class Compare>
  284. static void range_sort_buffer(const range<Iter1_t> & rng_data,
  285. const range<Iter2_t> & rng_aux, Compare comp)
  286. {
  287. //-----------------------------------------------------------------------
  288. // metaprogram
  289. //-----------------------------------------------------------------------
  290. typedef value_iter<Iter1_t> value_t;
  291. typedef value_iter<Iter2_t> value2_t;
  292. static_assert (std::is_same< value_t, value2_t>::value,
  293. "Incompatible iterators\n");
  294. //------------------------------------------------------------------------
  295. // program
  296. //------------------------------------------------------------------------
  297. #ifdef __BS_DEBUG
  298. assert ( rng_data.size() == rng_aux.size());
  299. #endif
  300. // minimal number of element before to jump to insertionsort
  301. const uint32_t sort_min = 32;
  302. if (rng_data.size() <= sort_min)
  303. {
  304. insert_sort(rng_data.first, rng_data.last, comp);
  305. move_forward(rng_aux, rng_data);
  306. return;
  307. };
  308. internal_sort(rng_data, rng_aux, comp, 0, false);
  309. };
  310. //****************************************************************************
  311. };// End namespace common
  312. };// End namespace sort
  313. };// End namepspace boost
  314. //****************************************************************************
  315. //
  316. #endif