pivot.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if (comp (*iter_2, *iter_1)) std::swap ( *iter_2, *iter_1);
  44. if (comp (*iter_3, *iter_2))
  45. { std::swap ( *iter_3, *iter_2);
  46. if (comp (*iter_2, *iter_1)) std::swap ( *iter_2, *iter_1);
  47. };
  48. return iter_2;
  49. };
  50. //
  51. //-----------------------------------------------------------------------------
  52. // function : pivot3
  53. /// @brief : receive a range between first and last, calcule the mid iterator
  54. /// with the first, the previous to the last, and the central
  55. /// position. With this mid iterator swap with the first position
  56. //
  57. /// @param first : iterator to the first element
  58. /// @param last : iterator to the last element
  59. /// @param comp : object for to compare two elements
  60. //-----------------------------------------------------------------------------
  61. template < class Iter_t, class Compare >
  62. inline void pivot3 (Iter_t first, Iter_t last, Compare comp)
  63. {
  64. auto N2 = (last - first) >> 1;
  65. Iter_t it_val = mid3 (first + 1, first + N2, last - 1, comp);
  66. std::swap (*first, *it_val);
  67. };
  68. //
  69. //-----------------------------------------------------------------------------
  70. // function : mid9
  71. /// @brief : return the iterator to the mid value of the nine values passsed
  72. /// as parameters
  73. //
  74. /// @param iter_1 : iterator to the first value
  75. /// @param iter_2 : iterator to the second value
  76. /// @param iter_3 : iterator to the third value
  77. /// @param iter_4 : iterator to the fourth value
  78. /// @param iter_5 : iterator to the fifth value
  79. /// @param iter_6 : iterator to the sixth value
  80. /// @param iter_7 : iterator to the seventh value
  81. /// @param iter_8 : iterator to the eighth value
  82. /// @param iter_9 : iterator to the ninth value
  83. /// @return iterator to the mid value
  84. //-----------------------------------------------------------------------------
  85. template < class Iter_t, class Compare >
  86. inline Iter_t mid9 (Iter_t iter_1, Iter_t iter_2, Iter_t iter_3, Iter_t iter_4,
  87. Iter_t iter_5, Iter_t iter_6, Iter_t iter_7, Iter_t iter_8,
  88. Iter_t iter_9, Compare comp)
  89. {
  90. return mid3 (mid3 (iter_1, iter_2, iter_3, comp),
  91. mid3 (iter_4, iter_5, iter_6, comp),
  92. mid3 (iter_7, iter_8, iter_9, comp), comp);
  93. };
  94. //
  95. //-----------------------------------------------------------------------------
  96. // function : pivot9
  97. /// @brief : receive a range between first and last, obtain 9 values between
  98. /// the elements including the first and the previous to the last.
  99. /// Obtain the iterator to the mid value and swap with the first
  100. /// position
  101. //
  102. /// @param first : iterator to the first element
  103. /// @param last : iterator to the last element
  104. /// @param comp : object for to compare two elements
  105. //-----------------------------------------------------------------------------
  106. template < class Iter_t, class Compare >
  107. inline void pivot9 (Iter_t first, Iter_t last, Compare comp)
  108. {
  109. size_t cupo = (last - first) >> 3;
  110. Iter_t itaux = mid9 (first + 1, first + cupo, first + 2 * cupo,
  111. first + 3 * cupo, first + 4 * cupo, first + 5 * cupo,
  112. first + 6 * cupo, first + 7 * cupo, last - 1, comp);
  113. std::swap (*first, *itaux);
  114. };
  115. //****************************************************************************
  116. };// End namespace common
  117. };// End namespace sort
  118. };// End namespace boost
  119. //****************************************************************************
  120. #endif