zhangmeng
2021-07-02 056f71f24cefaf88f2a93714c6678c03ed5f1e0e
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//----------------------------------------------------------------------------
/// @file merge.hpp
/// @brief low level merge functions
///
/// @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_COMMON_UTIL_MERGE_HPP
#define __BOOST_SORT_COMMON_UTIL_MERGE_HPP
 
#include <algorithm>
#include <functional>
#include <iterator>
#include <memory>
 
#include <boost/sort/common/util/algorithm.hpp>
#include <boost/sort/common/util/traits.hpp>
#include <boost/sort/common/util/circular_buffer.hpp>
 
namespace boost
{
namespace sort
{
namespace common
{
namespace util
{
namespace here = boost::sort::common::util;
//----------------------------------------------------------------------------
//
//           F U N C T I O N S    I N    T H E     F I L E
//----------------------------------------------------------------------------
//
// template < class Iter1_t, class Iter2_t, class Compare >
// Iter2_t merge (Iter1_t buf1, const Iter1_t end_buf1, Iter1_t buf2,
//                const Iter1_t end_buf2, Iter2_t buf_out, Compare comp)
//
// template < class Iter_t, class Value_t, class Compare >
// Value_t *merge_construct (Iter_t first1, const Iter_t last1, Iter_t first2,
//                           const Iter_t last2, Value_t *it_out, Compare comp)
//
// template < class Iter1_t, class Iter2_t, class Compare >
// Iter2_t merge_half (Iter1_t buf1, const Iter1_t end_buf1, Iter2_t buf2,
//                     const Iter2_t end_buf2, Iter2_t buf_out, Compare comp)
//
// template < class Iter1_t, class Iter2_t, class Compare >
// Iter2_t merge_half_backward (Iter1_t buf1,  Iter1_t end_buf1,
//                              Iter2_t buf2, Iter2_t end_buf2,
//                              Iter1_t end_buf_out, Compare comp)
//
// template < class Iter1_t, class Iter2_t, class Iter3_t, class Compare >
// bool merge_uncontiguous (Iter1_t src1, const Iter1_t end_src1,
//                          Iter2_t src2, const Iter2_t end_src2,
//                          Iter3_t aux, Compare comp)
//
// template < class Iter1_t, class Iter2_t, class Compare >
// bool merge_contiguous (Iter1_t src1, Iter1_t src2, Iter1_t end_src2,
//                        Iter2_t buf, Compare comp)
//
// template < class Iter_t, class Circular ,class Compare >
// bool merge_circular  (Iter_t buf1, Iter_t end_buf1,
//                       Iter_t buf2, Iter_t end_buf2,
//                       Circular &circ, Compare comp, Iter_t &it_aux)
//
//----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
//  function : merge
/// @brief Merge two contiguous buffers pointed by buf1 and buf2, and put
///        in the buffer pointed by buf_out
///
/// @param buf1 : iterator to the first element in the first buffer
/// @param end_buf1 : final iterator of first buffer
/// @param buf2 : iterator to the first iterator to the second buffer
/// @param end_buf2 : final iterator of the second buffer
/// @param buf_out : buffer where move the elements merged
/// @param comp : comparison object
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Iter3_t, class Compare>
static Iter3_t merge(Iter1_t buf1, const Iter1_t end_buf1, Iter2_t buf2,
                     const Iter2_t end_buf2, Iter3_t buf_out, Compare comp)
{
    //-------------------------------------------------------------------------
    //                       Metaprogramming
    //------------------------------------------------------------------------- 
    typedef value_iter<Iter1_t> value1_t;
    typedef value_iter<Iter2_t> value2_t;
    typedef value_iter<Iter3_t> value3_t;
    static_assert (std::is_same< value1_t, value2_t >::value,
                    "Incompatible iterators\n");
    static_assert (std::is_same< value3_t, value2_t >::value,
                    "Incompatible iterators\n");
 
    //-------------------------------------------------------------------------
    //                       Code
    //-------------------------------------------------------------------------
    const size_t MIN_CHECK = 1024;
 
    if (size_t((end_buf1 - buf1) + (end_buf2 - buf2)) >= MIN_CHECK)
    {
        if (buf1 == end_buf1) return move_forward(buf_out, buf2, end_buf2);
        if (buf2 == end_buf2) return move_forward(buf_out, buf1, end_buf1);
 
        if (not comp(*buf2, *(end_buf1 - 1)))
        {
            Iter3_t mid = move_forward(buf_out, buf1, end_buf1);
            return move_forward(mid, buf2, end_buf2);
        };
 
        if (comp(*(end_buf2 - 1), *buf1))
        {
            Iter3_t mid = move_forward(buf_out, buf2, end_buf2);
            return move_forward(mid, buf1, end_buf1);
        };
    };
    while ((buf1 != end_buf1) and (buf2 != end_buf2))
    {
        *(buf_out++) = (not comp(*buf2, *buf1)) ?
                        std::move(*(buf1++)) : std::move(*(buf2++));
    };
 
    return (buf1 == end_buf1) ?
                    move_forward(buf_out, buf2, end_buf2) :
                    move_forward(buf_out, buf1, end_buf1);
}
;
//
//-----------------------------------------------------------------------------
//  function : merge_construct
/// @brief Merge two contiguous buffers pointed by first1 and first2, and put
///        in the uninitialized buffer pointed by it_out
///
/// @param first1 : iterator to the first element in the first buffer
/// @param last1 : last iterator of the first buffer
/// @param first2 : iterator to the first element to the second buffer
/// @param last2 : final iterator of the second buffer
/// @param it_out : uninitialized buffer where move the elements merged
/// @param comp : comparison object
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Value_t, class Compare>
static Value_t *merge_construct(Iter1_t first1, const Iter1_t last1,
                                Iter2_t first2, const Iter2_t last2,
                                Value_t *it_out, Compare comp)
{
    //-------------------------------------------------------------------------
    //                       Metaprogramming
    //------------------------------------------------------------------------- 
    typedef value_iter<Iter1_t> type1;
    typedef value_iter<Iter2_t> type2;
    static_assert (std::is_same< Value_t, type1 >::value,
                    "Incompatible iterators\n");
    static_assert (std::is_same< Value_t, type2 >::value,
                    "Incompatible iterators\n");
 
    //-------------------------------------------------------------------------
    //                       Code
    //-------------------------------------------------------------------------
    const size_t MIN_CHECK = 1024;
 
    if (size_t((last1 - first1) + (last2 - first2)) >= MIN_CHECK)
    {
        if (first1 == last1) return move_construct(it_out, first2, last2);
        if (first2 == last2) return move_construct(it_out, first1, last1);
 
        if (not comp(*first2, *(last1 - 1)))
        {
            Value_t* mid = move_construct(it_out, first1, last1);
            return move_construct(mid, first2, last2);
        };
 
        if (comp(*(last2 - 1), *first1))
        {
            Value_t* mid = move_construct(it_out, first2, last2);
            return move_construct(mid, first1, last1);
        };
    };
    while (first1 != last1 and first2 != last2)
    {
        construct_object((it_out++),
                        (not comp(*first2, *first1)) ?
                                        std::move(*(first1++)) :
                                        std::move(*(first2++)));
    };
    return (first1 == last1) ?
                    move_construct(it_out, first2, last2) :
                    move_construct(it_out, first1, last1);
};
//
//---------------------------------------------------------------------------
//  function : merge_half
/// @brief : Merge two buffers. The first buffer is in a separate memory.
///          The second buffer have a empty space before buf2 of the same size
///          than the (end_buf1 - buf1)
///
/// @param buf1 : iterator to the first element of the first buffer
/// @param end_buf1 : iterator to the last element of the first buffer
/// @param buf2 : iterator to the first element of the second buffer
/// @param end_buf2 : iterator to the last element of the second buffer
/// @param buf_out : iterator to the first element to the buffer where put
///                  the result
/// @param comp : object for Compare two elements of the type pointed
///                by the Iter1_t and Iter2_t
//---------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Compare>
static Iter2_t merge_half(Iter1_t buf1, const Iter1_t end_buf1, Iter2_t buf2,
                          const Iter2_t end_buf2, Iter2_t buf_out, Compare comp)
{
    //-------------------------------------------------------------------------
    //                         Metaprogramming
    //------------------------------------------------------------------------- 
    typedef value_iter<Iter1_t> value1_t;
    typedef value_iter<Iter2_t> value2_t;
    static_assert (std::is_same< value1_t, value2_t >::value,
                    "Incompatible iterators\n");
 
    //-------------------------------------------------------------------------
    //                         Code
    //-------------------------------------------------------------------------
#ifdef __BS_DEBUG
    assert ( (buf2 - buf_out) == ( end_buf1 - buf1));
#endif
    const size_t MIN_CHECK = 1024;
 
    if (size_t((end_buf1 - buf1) + (end_buf2 - buf2)) >= MIN_CHECK)
    {
        if (buf1 == end_buf1) return end_buf2;
        if (buf2 == end_buf2) return move_forward(buf_out, buf1, end_buf1);
 
        if (not comp(*buf2, *(end_buf1 - 1)))
        {
            move_forward(buf_out, buf1, end_buf1);
            return end_buf2;
        };
 
        if (comp(*(end_buf2 - 1), *buf1))
        {
            Iter2_t mid = move_forward(buf_out, buf2, end_buf2);
            return move_forward(mid, buf1, end_buf1);
        };
    };
    while ((buf1 != end_buf1) and (buf2 != end_buf2))
    {
        *(buf_out++) = (not comp(*buf2, *buf1)) ?
                        std::move(*(buf1++)) : std::move(*(buf2++));
    };
    return (buf2 == end_buf2)? move_forward(buf_out, buf1, end_buf1) : end_buf2;
};
 
//
//---------------------------------------------------------------------------
//  function : merge_half_backward
/// @brief : Merge two buffers. The first buffer is in a separate memory.
///          The second buffer have a empty space before buf2 of the same size
///          than the (end_buf1 - buf1)
///
/// @param buf1 : iterator to the first element of the first buffer
/// @param end_buf1 : iterator to the last element of the first buffer
/// @param buf2 : iterator to the first element of the second buffer
/// @param end_buf2 : iterator to the last element of the second buffer
/// @param buf_out : iterator to the first element to the buffer where put
///                  the result
/// @param comp : object for Compare two elements of the type pointed
///                by the Iter1_t and Iter2_t
//---------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Compare>
static Iter2_t merge_half_backward(Iter1_t buf1, Iter1_t end_buf1, Iter2_t buf2,
                                   Iter2_t end_buf2, Iter1_t end_buf_out,
                                   Compare comp)
{
    //-------------------------------------------------------------------------
    //                         Metaprogramming
    //-------------------------------------------------------------------------
    typedef value_iter<Iter1_t> value1_t;
    typedef value_iter<Iter2_t> value2_t;
    static_assert (std::is_same< value1_t, value2_t >::value,
                    "Incompatible iterators\n");
 
    //-------------------------------------------------------------------------
    //                         Code
    //-------------------------------------------------------------------------
#ifdef __BS_DEBUG
    assert ((end_buf_out - end_buf1) == (end_buf2 - buf2) );
#endif
    const size_t MIN_CHECK = 1024;
 
    if (size_t((end_buf1 - buf1) + (end_buf2 - buf2)) >= MIN_CHECK)
    {
        if (buf2 == end_buf2) return buf1;
        if (buf1 == end_buf1)
            return here::move_backward(end_buf_out, buf2, end_buf2);
 
        if (not comp(*buf2, *(end_buf1 - 1)))
        {
            here::move_backward(end_buf_out, buf2, end_buf2);
            return buf1;
        };
 
        if (comp(*(end_buf2 - 1), *buf1))
        {
            Iter1_t mid = here::move_backward(end_buf_out, buf1, end_buf1);
            return here::move_backward(mid, buf2, end_buf2);
        };
    };
    while ((buf1 != end_buf1) and (buf2 != end_buf2))
    {
        *(--end_buf_out) =
                        (not comp(*(end_buf2 - 1), *(end_buf1 - 1))) ?
                                        std::move(*(--end_buf2)):
                                        std::move(*(--end_buf1));
    };
    return (buf1 == end_buf1) ?
                    here::move_backward(end_buf_out, buf2, end_buf2) : buf1;
};
 
//
//-----------------------------------------------------------------------------
//  function : merge_uncontiguous
/// @brief : merge two uncontiguous buffers, placing the results in the buffers
///          Use an auxiliary buffer pointed by aux
///
/// @param src1 : iterator to the first element of the first buffer
/// @param end_src1 : last iterator  of the first buffer
/// @param src2 : iterator to the first element of the second buffer
/// @param end_src2 : last iterator  of the second buffer
/// @param aux  : iterator to the first element of the auxiliary buffer
/// @param comp : object for to Compare elements
/// @return true : not changes done,  false : changes in the buffers
/// @remarks
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Iter3_t, class Compare>
static bool merge_uncontiguous(Iter1_t src1, const Iter1_t end_src1,
                               Iter2_t src2, const Iter2_t end_src2,
                               Iter3_t aux, Compare comp)
{
    //-------------------------------------------------------------------------
    //                    Metaprogramming
    //------------------------------------------------------------------------- 
    typedef value_iter<Iter1_t> type1;
    typedef value_iter<Iter2_t> type2;
    typedef value_iter<Iter3_t> type3;
    static_assert (std::is_same< type1, type2 >::value,
                    "Incompatible iterators\n");
    static_assert (std::is_same< type3, type2 >::value,
                    "Incompatible iterators\n");
 
    //-------------------------------------------------------------------------
    //                    Code
    //-------------------------------------------------------------------------
    if (src1 == end_src1 or src2 == end_src2
                    or not comp(*src2, *(end_src1 - 1))) return true;
 
    while (src1 != end_src1 and not comp(*src2, *src1))
        ++src1;
 
    Iter3_t const end_aux = aux + (end_src1 - src1);
    Iter2_t src2_first = src2;
    move_forward(aux, src1, end_src1);
 
    while ((src1 != end_src1) and (src2 != end_src2))
    {
        *(src1++) = std::move((not comp(*src2, *aux)) ? *(aux++) : *(src2++));
    }
 
    if (src2 == end_src2)
    {
        while (src1 != end_src1)
            *(src1++) = std::move(*(aux++));
        move_forward(src2_first, aux, end_aux);
    }
    else
    {
        merge_half(aux, end_aux, src2, end_src2, src2_first, comp);
    };
    return false;
};
 
//
//-----------------------------------------------------------------------------
//  function : merge_contiguous
/// @brief : merge two contiguous buffers,using an auxiliary buffer pointed
///          by buf. The results are in src1 and src2
///
/// @param src1: iterator to the first position of the first buffer
/// @param src2: final iterator of the first buffer and first iterator
///              of the second buffer
/// @param end_src2 : final iterator of the second buffer
/// @param buf  : iterator to buffer used as auxiliary memory
/// @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 bool merge_contiguous(Iter1_t src1, Iter1_t src2, Iter1_t end_src2,
                             Iter2_t buf, Compare comp)
{
    //-------------------------------------------------------------------------
    //                      Metaprogramming
    //------------------------------------------------------------------------- 
    typedef value_iter<Iter1_t> type1;
    typedef value_iter<Iter2_t> type2;
    static_assert (std::is_same< type1, type2 >::value,
                    "Incompatible iterators\n");
 
    //-------------------------------------------------------------------------
    //                         Code
    //-------------------------------------------------------------------------
    if (src1 == src2 or src2 == end_src2 or not comp(*src2, *(src2 - 1)))
        return true;
 
    Iter1_t end_src1 = src2;
    while (src1 != end_src1 and not comp(*src2, *src1))
        ++src1;
 
    if (src1 == end_src1) return false;
 
    size_t nx = end_src1 - src1;
    move_forward(buf, src1, end_src1);
    merge_half(buf, buf + nx, src2, end_src2, src1, comp);
    return false;
};
//
//-----------------------------------------------------------------------------
//  function : merge_circular
/// @brief : merge two buffers,using a circular buffer
///          This function don't check the parameters
/// @param buf1: iterator to the first position of the first buffer
/// @param end_buf1: iterator after the last element of the first buffer
/// @param buf2: iterator to the first element of the secind buffer
/// @param end_buf2: iterator to the first element of the secind buffer
/// @param circ : circular buffer
/// @param comp : comparison object
/// @return true : finished buf1,  false : finished buf2
/// @comments : be carefully because the iterators buf1 and buf2 are modified
//-----------------------------------------------------------------------------
template<class Iter1_t, class Iter2_t, class Circular, class Compare>
static bool merge_circular(Iter1_t buf1, Iter1_t end_buf1, Iter2_t buf2,
                           Iter2_t end_buf2, Circular &circ, Compare comp,
                           Iter1_t &it1_out, Iter2_t &it2_out)
{
    //-------------------------------------------------------------------------
    //                      Metaprogramming
    //-------------------------------------------------------------------------
    typedef value_iter<Iter1_t> type1;
    typedef value_iter<Iter2_t> type2;
    static_assert (std::is_same< type1, type2 >::value,
                    "Incompatible iterators\n");
    typedef typename Circular::value_t type3;
    static_assert (std::is_same<type1, type3>::value,
                    "Incompatible iterators\n");
 
    //-------------------------------------------------------------------------
    //                      Code
    //-------------------------------------------------------------------------
#ifdef __BS_DEBUG
    assert ( circ.free_size() >= size_t ((end_buf1-buf1) + (end_buf2-buf2)));
#endif
 
    if (not comp(*buf2, *(end_buf1 - 1)))
    {
        circ.push_move_back(buf1, (end_buf1 - buf1));
        it1_out = end_buf1;
        it2_out = buf2;
        return true;
    };
    if (comp(*(end_buf2 - 1), *buf1))
    {
        circ.push_move_back(buf2, (end_buf2 - buf2));
        it1_out = buf1;
        it2_out = end_buf2;
        return false;
    }
    while (buf1 != end_buf1 and buf2 != end_buf2)
    {
        circ.push_back(comp(*buf2, *buf1) ? std::move(*(buf2++))
                                          : std::move(*(buf1++)));
    };
    it2_out = buf2;
    it1_out = buf1;
    bool ret = (buf1 == end_buf1);
    return ret;
};
//
//****************************************************************************
};//    End namespace util
};//    End namespace common
};//    End namespace sort
};//    End namespace boost
//****************************************************************************
//
#endif