pivot.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //----------------------------------------------------------------------------
  2. /// @file pivot.hpp
  3. /// @brief This file contains the description of several low level algorithms
  4. ///
  5. /// @author Copyright (c) 2010 2015 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_PIVOT_HPP
  14. #define __BOOST_SORT_COMMON_PIVOT_HPP
  15. #include <cstdint>
  16. namespace boost
  17. {
  18. namespace sort
  19. {
  20. namespace common
  21. {
  22. //
  23. //##########################################################################
  24. // ##
  25. // G L O B A L V A R I B L E S ##
  26. // ##
  27. //##########################################################################
  28. //
  29. //-----------------------------------------------------------------------------
  30. // function : mid3
  31. /// @brief : return the iterator to the mid value of the three values passsed
  32. /// as parameters
  33. //
  34. /// @param iter_1 : iterator to the first value
  35. /// @param iter_2 : iterator to the second value
  36. /// @param iter_3 : iterator to the third value
  37. /// @param comp : object for to compare two values
  38. /// @return iterator to mid value
  39. //-----------------------------------------------------------------------------
  40. template < typename Iter_t, typename Compare >
  41. inline Iter_t mid3 (Iter_t iter_1, Iter_t iter_2, Iter_t iter_3, Compare comp)
  42. {
  43. using std::swap;
  44. if (comp (*iter_2, *iter_1)) swap ( *iter_2, *iter_1);
  45. if (comp (*iter_3, *iter_2))
  46. { swap ( *iter_3, *iter_2);
  47. if (comp (*iter_2, *iter_1)) swap ( *iter_2, *iter_1);
  48. };
  49. return iter_2;
  50. }
  51. //
  52. //-----------------------------------------------------------------------------
  53. // function : pivot3
  54. /// @brief : receive a range between first and last, calcule the mid iterator
  55. /// with the first, the previous to the last, and the central
  56. /// position. With this mid iterator swap with the first position
  57. //
  58. /// @param first : iterator to the first element
  59. /// @param last : iterator to the last element
  60. /// @param comp : object for to compare two elements
  61. //-----------------------------------------------------------------------------
  62. template < class Iter_t, class Compare >
  63. inline void pivot3 (Iter_t first, Iter_t last, Compare comp)
  64. {
  65. using std::swap;
  66. auto N2 = (last - first) >> 1;
  67. Iter_t it_val = mid3 (first + 1, first + N2, last - 1, comp);
  68. swap (*first, *it_val);
  69. }
  70. //
  71. //-----------------------------------------------------------------------------
  72. // function : mid9
  73. /// @brief : return the iterator to the mid value of the nine values passsed
  74. /// as parameters
  75. //
  76. /// @param iter_1 : iterator to the first value
  77. /// @param iter_2 : iterator to the second value
  78. /// @param iter_3 : iterator to the third value
  79. /// @param iter_4 : iterator to the fourth value
  80. /// @param iter_5 : iterator to the fifth value
  81. /// @param iter_6 : iterator to the sixth value
  82. /// @param iter_7 : iterator to the seventh value
  83. /// @param iter_8 : iterator to the eighth value
  84. /// @param iter_9 : iterator to the ninth value
  85. /// @return iterator to the mid value
  86. //-----------------------------------------------------------------------------
  87. template < class Iter_t, class Compare >
  88. inline Iter_t mid9 (Iter_t iter_1, Iter_t iter_2, Iter_t iter_3, Iter_t iter_4,
  89. Iter_t iter_5, Iter_t iter_6, Iter_t iter_7, Iter_t iter_8,
  90. Iter_t iter_9, Compare comp)
  91. {
  92. return mid3 (mid3 (iter_1, iter_2, iter_3, comp),
  93. mid3 (iter_4, iter_5, iter_6, comp),
  94. mid3 (iter_7, iter_8, iter_9, comp), comp);
  95. }
  96. //
  97. //-----------------------------------------------------------------------------
  98. // function : pivot9
  99. /// @brief : receive a range between first and last, obtain 9 values between
  100. /// the elements including the first and the previous to the last.
  101. /// Obtain the iterator to the mid value and swap with the first
  102. /// position
  103. //
  104. /// @param first : iterator to the first element
  105. /// @param last : iterator to the last element
  106. /// @param comp : object for to compare two elements
  107. //-----------------------------------------------------------------------------
  108. template < class Iter_t, class Compare >
  109. inline void pivot9 (Iter_t first, Iter_t last, Compare comp)
  110. {
  111. using std::swap;
  112. size_t cupo = (last - first) >> 3;
  113. Iter_t itaux = mid9 (first + 1, first + cupo, first + 2 * cupo,
  114. first + 3 * cupo, first + 4 * cupo, first + 5 * cupo,
  115. first + 6 * cupo, first + 7 * cupo, last - 1, comp);
  116. swap (*first, *itaux);
  117. }
  118. //****************************************************************************
  119. }// End namespace common
  120. }// End namespace sort
  121. }// End namespace boost
  122. //****************************************************************************
  123. #endif