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