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