summaryrefslogtreecommitdiff
path: root/boost/sort/common/range.hpp
blob: 072d98a938a2b3e06e65cc60163f46e5027fa20a (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//----------------------------------------------------------------------------
/// @file range.hpp
/// @brief Define a range [first, last), and the associated operations
///
/// @author Copyright (c) 2016 Francisco José Tapia (fjtapia@gmail.com )\n
///         Distributed under the Boost Software License, Version 1.0.\n
///         ( See accompanyingfile LICENSE_1_0.txt or copy at
///           http://www.boost.org/LICENSE_1_0.txt  )
/// @version 0.1
///
/// @remarks
//-----------------------------------------------------------------------------
#ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_RANGE_HPP
#define __BOOST_SORT_PARALLEL_DETAIL_UTIL_RANGE_HPP

#include <boost/sort/common/util/algorithm.hpp>
#include <boost/sort/common/util/merge.hpp>
#include <boost/sort/common/util/traits.hpp>
#include <cassert>
#include <functional>
#include <memory>
#include <vector>

namespace boost
{
namespace sort
{
namespace common
{

///---------------------------------------------------------------------------
/// @struct range
/// @brief this represent a range between two iterators
/// @remarks
//----------------------------------------------------------------------------
template <class Iter_t>
struct range
{
    Iter_t first, last;
    //
    //------------------------------------------------------------------------
    //  function : range
    /// @brief  empty constructor
    //------------------------------------------------------------------------
    range(void) { };
    //
    //------------------------------------------------------------------------
    //  function : range
    /// @brief  constructor with two parameters
    /// @param frs : iterator to the first element
    /// @param lst : iterator to the last element
    //-----------------------------------------------------------------------
    range(const Iter_t &frs, const Iter_t &lst): first(frs), last(lst) { };
    //
    //-----------------------------------------------------------------------
    //  function : empty
    /// @brief indicate if the range is empty
    /// @return  true : empty false : not empty
    //-----------------------------------------------------------------------
    bool empty(void) const { return (first == last); };
    //
    //-----------------------------------------------------------------------
    //  function : not_empty
    /// @brief indicate if the range is not empty
    /// @return  true : not empty false : empty
    //-----------------------------------------------------------------------
    bool not_empty(void) const {return (first != last); };
    //
    //-----------------------------------------------------------------------
    //  function : valid
    /// @brief  Indicate if the range is well constructed, and valid
    /// @return true : valid,  false : not valid
    //-----------------------------------------------------------------------
    bool valid(void) const { return ((last - first) >= 0); };
    //
    //-----------------------------------------------------------------------
    //  function : size
    /// @brief  return the size of the range
    /// @return size
    //-----------------------------------------------------------------------
    size_t size(void) const { return (last - first); };
    //
    //------------------------------------------------------------------------
    //  function : front
    /// @brief return an iterator to the first element of the range
    /// @return iterator
    //-----------------------------------------------------------------------
    Iter_t front(void) const { return first; };
    //
    //-------------------------------------------------------------------------
    //  function : back
    /// @brief return an iterator to the last element of the range
    /// @return iterator
    //-------------------------------------------------------------------------
    Iter_t back(void) const {return (last - 1); };
};

//
//-----------------------------------------------------------------------------
//  function : concat
/// @brief concatenate two contiguous ranges
/// @param it1 : first range
/// @param it2 : second range
/// @return  range resulting of the concatenation
//-----------------------------------------------------------------------------
template<class Iter_t>
inline range<Iter_t> concat(const range<Iter_t> &it1, const range<Iter_t> &it2)
{
    return range<Iter_t>(it1.first, it2.last);
}
;
//
//-----------------------------------------------------------------------------
//  function : move_forward
/// @brief Move initialized objets from the range src to dest
/// @param dest : range where move the objects
/// @param src : range from where move the objects
/// @return range with the objects moved and the size adjusted
//-----------------------------------------------------------------------------
template <class Iter1_t, class Iter2_t>
inline range<Iter2_t> move_forward(const range<Iter2_t> &dest,
                                   const range<Iter1_t> &src)
{
    assert(dest.size() >= src.size());
    Iter2_t it_aux = util::move_forward(dest.first, src.first, src.last);
    return range<Iter2_t>(dest.first, it_aux);
};
//
//-----------------------------------------------------------------------------
//  function : move_backward
/// @brief Move initialized objets from the range src to dest
/// @param dest : range where move the objects
/// @param src : range from where move the objects
/// @return range with the objects moved and the size adjusted
//-----------------------------------------------------------------------------
template <class Iter1_t, class Iter2_t>
inline range<Iter2_t> move_backward(const range<Iter2_t> &dest,
                                    const range<Iter1_t> &src)
{
    assert(dest.size() >= src.size());
    Iter2_t it_aux = util::move_backward(dest.first + src.size(), src.first,
                    src.last);
    return range<Iter2_t>(dest.first, dest.src.size());
};

//-----------------------------------------------------------------------------
//  function : uninit_move
/// @brief Move uninitialized objets from the range src creating them in  dest
///
/// @param dest : range where move and create the objects
/// @param src : range from where move the objects
/// @return range with the objects moved and the size adjusted
//-----------------------------------------------------------------------------
template<class Iter_t, class Value_t = util::value_iter<Iter_t> >
inline range<Value_t*> move_construct(const range<Value_t*> &dest,
                                      const range<Iter_t> &src)
{
    Value_t *ptr_aux = util::move_construct(dest.first, src.first, src.last);
    return range<Value_t*>(dest.first, ptr_aux);
};
//
//-----------------------------------------------------------------------------
//  function : destroy
/// @brief destroy a range of objects
/// @param rng : range to destroy
//-----------------------------------------------------------------------------
template<class Iter_t>
inline void destroy(range<Iter_t> rng)
{
    util::destroy(rng.first, rng.last);
};
//
//-----------------------------------------------------------------------------
//  function : initialize
/// @brief initialize a range of objects with the object val moving across them
/// @param rng : range of elements not initialized
/// @param val : object used for the initialization
/// @return range initialized
//-----------------------------------------------------------------------------
template<class Iter_t, class Value_t = util::value_iter<Iter_t> >
inline range<Iter_t> initialize(const range<Iter_t> &rng, Value_t &val)
{
    util::initialize(rng.first, rng.last, val);
    return rng;
};
//
//-----------------------------------------------------------------------------
//  function : is_mergeable
/// @brief : indicate if two ranges have a possible merge
/// @param src1 : first range
/// @param src2 : second range
/// @param comp : object for to compare elements
/// @return true : they can be merged
///         false : they can't be merged
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Compare>
inline bool is_mergeable(const range<Iter1_t> &src1, const range<Iter2_t> &src2,
                         Compare comp)
{
    //------------------------------------------------------------------------
    //                  Metaprogramming
    //------------------------------------------------------------------------
    typedef util::value_iter<Iter1_t> type1;
    typedef util::value_iter<Iter2_t> type2;
    static_assert (std::is_same< type1, type2 >::value,
                    "Incompatible iterators\n");
    //------------------------------------------------------------------------
    //                 Code
    //------------------------------------------------------------------------
    return comp(*(src2.front()), *(src1.back()));
};
//
//-----------------------------------------------------------------------------
//  function : is_mergeable_stable
/// @brief : indicate if two ranges have a possible merge
/// @param src1 : first range
/// @param src2 : second range
/// @param comp : object for to compare elements
/// @return true : they can be merged
///         false : they can't be merged
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Compare>
inline bool is_mergeable_stable(const range<Iter1_t> &src1,
                                const range<Iter2_t> &src2, Compare comp)
{
    //------------------------------------------------------------------------
    //                  Metaprogramming
    //------------------------------------------------------------------------
    typedef util::value_iter<Iter1_t> type1;
    typedef util::value_iter<Iter2_t> type2;
    static_assert (std::is_same< type1, type2 >::value,
                    "Incompatible iterators\n");
    //------------------------------------------------------------------------
    //                 Code
    //------------------------------------------------------------------------
    return not comp(*(src1.back()), *(src2.front()));
};
//
//-----------------------------------------------------------------------------
//  function : merge
/// @brief Merge two contiguous ranges src1 and src2, and put the result in
///        the range dest, returning the range merged
///
/// @param dest : range where locate the lements merged. the size of dest
///               must be  greater or equal than the sum of the sizes of
///               src1 and src2
/// @param src1 : first range to merge
/// @param src2 : second range to merge
/// @param comp : comparison object
/// @return range with the elements merged and the size adjusted
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Iter3_t, class Compare>
inline range<Iter3_t> merge(const range<Iter3_t> &dest,
                            const range<Iter1_t> &src1,
                            const range<Iter2_t> &src2, Compare comp)
{
    Iter3_t it_aux = util::merge(src1.first, src1.last, src2.first, src2.last,
                    dest.first, comp);
    return range<Iter3_t>(dest.first, it_aux);
};

//-----------------------------------------------------------------------------
//  function : merge_construct
/// @brief Merge two contiguous uninitialized ranges src1 and src2, and create
///        and move the result in the uninitialized range dest, returning the
///        range merged
//
/// @param dest : range where locate the elements merged. the size of dest
///               must be  greater or equal than the sum of the sizes of
///               src1 and src2. Initially is uninitialize memory
/// @param src1 : first range to merge
/// @param src2 : second range to merge
/// @param comp : comparison object
/// @return range with the elements merged and the size adjusted
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Value_t, class Compare>
inline range<Value_t *> merge_construct(const range<Value_t *> &dest,
                                        const range<Iter1_t> &src1,
                                        const range<Iter2_t> &src2,
                                        Compare comp)
{
    Value_t * ptr_aux = util::merge_construct(src1.first, src1.last, src2.first,
                    src2.last, dest.first, comp);
    return range<Value_t*>(dest.first, ptr_aux);
};
//
//---------------------------------------------------------------------------
//  function : half_merge
/// @brief : Merge two initialized buffers. The first buffer is in a separate
///          memory
//
/// @param dest : range where finish the two buffers merged
/// @param src1 : first range to merge in a separate memory
/// @param src2 : second range to merge, in the final part of the
///               range where deposit the final results
/// @param comp : object for compare two elements of the type pointed
///               by the Iter1_t and Iter2_t
/// @return : range with the two buffers merged
//---------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Compare>
inline range<Iter2_t> merge_half(const range<Iter2_t> &dest,
                                 const range<Iter1_t> &src1,
                                 const range<Iter2_t> &src2, Compare comp)
{
    Iter2_t it_aux = util::merge_half(src1.first, src1.last, src2.first,
                    src2.last, dest.first, comp);
    return range<Iter2_t>(dest.first, it_aux);
};
//
//-----------------------------------------------------------------------------
//  function : merge_uncontiguous
/// @brief : merge two non contiguous ranges src1, src2, using the range
///          aux as auxiliary memory. The results are in the original ranges
//
/// @param src1 : first range to merge
/// @param src2 : second range to merge
/// @param aux : auxiliary range used in the merge
/// @param comp : object for to compare elements
/// @return true : not changes done, false : changes in the buffers
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Iter3_t, class Compare>
inline bool merge_uncontiguous(const range<Iter1_t> &src1,
                               const range<Iter2_t> &src2,
                               const range<Iter3_t> &aux, Compare comp)
{
    return util::merge_uncontiguous(src1.first, src1.last, src2.first,
                    src2.last, aux.first, comp);
};
//
//-----------------------------------------------------------------------------
//  function : merge_contiguous
/// @brief : merge two contiguous ranges ( src1, src2) using buf as
///          auxiliary memory. The results are in the same ranges
/// @param src1 : first range to merge
/// @param src1 : second range to merge
/// @param buf : auxiliary memory used in the merge
/// @param comp : object for to compare elements
/// @return true : not changes done,   false : changes in the buffers
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Compare>
inline range<Iter1_t> merge_contiguous(const range<Iter1_t> &src1,
                                       const range<Iter1_t> &src2,
                                       const range<Iter2_t> &buf, Compare comp)
{
    util::merge_contiguous(src1.first, src1.last, src2.last, buf.first, comp);
    return concat(src1, src2);
};
//
//-----------------------------------------------------------------------------
//  function : merge_flow
/// @brief : merge two ranges, as part of a merge the ranges in a list. This
///         function reduce the number of movements compared with inplace_merge
///         when you need to merge a sequence of ranges.
///         This function merge the ranges rbuf and rng2, and the results
///          are in rng1 and rbuf
//
/// @param rng1 : range where locate the first elements of the merge
/// @param rbuf : range which provide the first elements, and where store
///               the last results of the merge
/// @param rng2 : range which provide the last elements to merge
/// @param comp : object for to compare elements
/// @return true : not changes done,  false : changes in the buffers
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Compare>
static void merge_flow(range<Iter1_t> rng1, range<Iter2_t> rbuf,
                       range<Iter1_t> rng2, Compare cmp)
{
    //-------------------------------------------------------------------------
    //                       Metaprogramming
    //-------------------------------------------------------------------------
    typedef util::value_iter<Iter1_t> type1;
    typedef util::value_iter<Iter2_t> type2;
    static_assert (std::is_same< type1, type2 >::value,
                    "Incompatible iterators\n");

    //-------------------------------------------------------------------------
    //                       Code
    //-------------------------------------------------------------------------
    range<Iter2_t> rbx(rbuf);
    range<Iter1_t> rx1(rng1), rx2(rng2);
    assert(rbx.size() == rx1.size() and rx1.size() == rx2.size());
    while (rx1.first != rx1.last)
    {
        *(rx1.first++) = (cmp(*rbx.first, *rx2.first)) ?
                                                    std::move(*(rbx.first++)):
                                                    std::move(*(rx2.first++));
    };
    if (rx2.first == rx2.last) return;
    if (rbx.first == rbx.last) move_forward(rbuf, rng2);
    else                       merge_half(rbuf, rx2, rbx, cmp);
};

//****************************************************************************
};//    End namespace common
};//    End namespace sort
};//    End namespace boost
//****************************************************************************
//
#endif