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