block.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //----------------------------------------------------------------------------
  2. /// @file block.hpp
  3. /// @brief This file contains the internal data structures used in the
  4. /// block_indirect_sort algorithm
  5. ///
  6. /// @author Copyright (c) 2016 Francisco Jose Tapia (fjtapia@gmail.com )\n
  7. /// Distributed under the Boost Software License, Version 1.0.\n
  8. /// ( See accompanying file LICENSE_1_0.txt or copy at
  9. /// http://www.boost.org/LICENSE_1_0.txt )
  10. /// @version 0.1
  11. ///
  12. /// @remarks
  13. //-----------------------------------------------------------------------------
  14. #ifndef __BOOST_SORT_PARALLEL_DETAIL_BLOCK_HPP
  15. #define __BOOST_SORT_PARALLEL_DETAIL_BLOCK_HPP
  16. #include <boost/sort/common/range.hpp>
  17. namespace boost
  18. {
  19. namespace sort
  20. {
  21. namespace blk_detail
  22. {
  23. //---------------------------------------------------------------------------
  24. // USING SENTENCES
  25. //---------------------------------------------------------------------------
  26. using namespace boost::sort::common;
  27. //
  28. //---------------------------------------------------------------------------
  29. /// @struct block_pos
  30. /// @brief represent a pair of values, a position represented as an unsigned
  31. /// variable ( position ), and a bool variable ( side ). They are packed
  32. /// in a size_t variable. The Least Significant Bit is the bool variable,
  33. /// and the others bits are the position
  34. //----------------------------------------------------------------------------
  35. class block_pos
  36. {
  37. //------------------------------------------------------------------------
  38. // VARIABLES
  39. //-----------------------------------------------------------------------
  40. size_t num; // number which store a position and a bool side
  41. public:
  42. //----------------------------- FUNCTIONS ------------------------------
  43. block_pos (void) : num (0){};
  44. //
  45. //-------------------------------------------------------------------------
  46. // function : block_pos
  47. /// @brief constructor from a position and a side
  48. /// @param position : position to sotre
  49. /// @param side : side to store
  50. //-------------------------------------------------------------------------
  51. block_pos (size_t position, bool side = false)
  52. {
  53. num = (position << 1) + ((side) ? 1 : 0);
  54. };
  55. //
  56. //-------------------------------------------------------------------------
  57. // function : pos
  58. /// @brief obtain the position stored inside the block_pos
  59. /// @return position
  60. //-------------------------------------------------------------------------
  61. size_t pos (void) const { return (num >> 1); };
  62. //
  63. //-------------------------------------------------------------------------
  64. // function : pos
  65. /// @brief store a position inside the block_pos
  66. /// @param position : value to store
  67. //-------------------------------------------------------------------------
  68. void set_pos (size_t position) { num = (position << 1) + (num & 1); };
  69. //
  70. //-------------------------------------------------------------------------
  71. // function : side
  72. /// @brief obtain the side stored inside the block_pos
  73. /// @return bool value
  74. //-------------------------------------------------------------------------
  75. bool side (void) const { return ((num & 1) != 0); };
  76. //
  77. //-------------------------------------------------------------------------
  78. // function : side
  79. /// @brief store a bool value the block_pos
  80. /// @param sd : bool value to store
  81. //-------------------------------------------------------------------------
  82. void set_side (bool sd) { num = (num & ~1) + ((sd) ? 1 : 0); };
  83. }; // end struct block_pos
  84. //
  85. //---------------------------------------------------------------------------
  86. /// @struct block
  87. /// @brief represent a group of Block_size contiguous elements, beginning
  88. /// with the pointed by first
  89. //----------------------------------------------------------------------------
  90. template < uint32_t Block_size, class Iter_t >
  91. struct block
  92. {
  93. //----------------------------------------------------------------------
  94. // VARIABLES
  95. //----------------------------------------------------------------------
  96. Iter_t first; // iterator to the first element of the block
  97. //-------------------------------------------------------------------------
  98. // function : block
  99. /// @brief constructor from an iterator to the first element of the block
  100. /// @param it : iterator to the first element of the block
  101. //-------------------------------------------------------------------------
  102. block (Iter_t it) : first (it){};
  103. //-------------------------------------------------------------------------
  104. // function : get_range
  105. /// @brief convert a block in a range
  106. /// @return range
  107. //-------------------------------------------------------------------------
  108. range< Iter_t > get_range (void)
  109. {
  110. return range_it (first, first + Block_size);
  111. };
  112. }; // end struct block
  113. //
  114. //-------------------------------------------------------------------------
  115. // function : compare_block
  116. /// @brief compare two blocks using the content of the pointed by first
  117. /// @param block1 : first block to compare
  118. /// @param block2 : second block to compare
  119. /// @param cmp : comparison operator
  120. //-------------------------------------------------------------------------
  121. template < uint32_t Block_size, class Iter_t, class Compare >
  122. bool compare_block (block< Block_size, Iter_t > block1,
  123. block< Block_size, Iter_t > block2,
  124. Compare cmp = Compare ( ))
  125. {
  126. return cmp (*block1.first, *block2.first);
  127. };
  128. //
  129. ///---------------------------------------------------------------------------
  130. /// @struct compare_block_pos
  131. /// @brief This is a object for to compare two block_pos objects
  132. //----------------------------------------------------------------------------
  133. template < uint32_t Block_size, class Iter_t, class Compare >
  134. struct compare_block_pos
  135. {
  136. //-----------------------------------------------------------------------
  137. // VARIABLES
  138. //-----------------------------------------------------------------------
  139. Iter_t global_first; // iterator to the first element to sort
  140. Compare comp; // comparison object for to compare two elements
  141. //-------------------------------------------------------------------------
  142. // function : compare_block_pos
  143. /// @brief constructor
  144. /// @param g_first : itertor to the first element to sort
  145. /// @param cmp : comparison operator
  146. //-------------------------------------------------------------------------
  147. compare_block_pos (Iter_t g_first, Compare cmp)
  148. : global_first (g_first), comp (cmp){};
  149. //
  150. //-------------------------------------------------------------------------
  151. // function : operator ()
  152. /// @brief compare two blocks using the content of the pointed by
  153. /// global_first
  154. /// @param block_pos1 : first block to compare
  155. /// @param block_pos2 : second block to compare
  156. //-------------------------------------------------------------------------
  157. bool operator( ) (block_pos block_pos1, block_pos block_pos2) const
  158. {
  159. return comp (*(global_first + (block_pos1.pos ( ) * Block_size)),
  160. *(global_first + (block_pos2.pos ( ) * Block_size)));
  161. };
  162. }; // end struct compare_block_pos
  163. //****************************************************************************
  164. }; // End namespace blk_detail
  165. }; // End namespace sort
  166. }; // End namespace boost
  167. //****************************************************************************
  168. //
  169. #endif