Welcome to mirror list, hosted at ThFree Co, Russian Federation.

radix_sort.h « lemon « lemon-1.3.1 « 3rd « quadriflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d80875663b0b2e92ff1f0d208d22d5673c276d59 (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
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
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2013
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#ifndef RADIX_SORT_H
#define RADIX_SORT_H

/// \ingroup auxalg
/// \file
/// \brief Radix sort
///
/// Linear time sorting algorithms

#include <vector>
#include <limits>
#include <iterator>
#include <algorithm>

namespace lemon {

  namespace _radix_sort_bits {

    template <typename Iterator>
    bool unitRange(Iterator first, Iterator last) {
      ++first;
      return first == last;
    }

    template <typename Value>
    struct Identity {
      const Value& operator()(const Value& val) {
        return val;
      }
    };


    template <typename Value, typename Iterator, typename Functor>
    Iterator radixSortPartition(Iterator first, Iterator last,
                                Functor functor, Value mask) {
      while (first != last && !(functor(*first) & mask)) {
        ++first;
      }
      if (first == last) {
        return first;
      }
      --last;
      while (first != last && (functor(*last) & mask)) {
        --last;
      }
      if (first == last) {
        return first;
      }
      std::iter_swap(first, last);
      ++first;
      while (true) {
        while (!(functor(*first) & mask)) {
          ++first;
        }
        --last;
        while (functor(*last) & mask) {
          --last;
        }
        if (unitRange(last, first)) {
          return first;
        }
        std::iter_swap(first, last);
        ++first;
      }
    }

    template <typename Iterator, typename Functor>
    Iterator radixSortSignPartition(Iterator first, Iterator last,
                                    Functor functor) {
      while (first != last && functor(*first) < 0) {
        ++first;
      }
      if (first == last) {
        return first;
      }
      --last;
      while (first != last && functor(*last) >= 0) {
        --last;
      }
      if (first == last) {
        return first;
      }
      std::iter_swap(first, last);
      ++first;
      while (true) {
        while (functor(*first) < 0) {
          ++first;
        }
        --last;
        while (functor(*last) >= 0) {
          --last;
        }
        if (unitRange(last, first)) {
          return first;
        }
        std::iter_swap(first, last);
        ++first;
      }
    }

    template <typename Value, typename Iterator, typename Functor>
    void radixIntroSort(Iterator first, Iterator last,
                        Functor functor, Value mask) {
      while (mask != 0 && first != last && !unitRange(first, last)) {
        Iterator cut = radixSortPartition(first, last, functor, mask);
        mask >>= 1;
        radixIntroSort(first, cut, functor, mask);
        first = cut;
      }
    }

    template <typename Value, typename Iterator, typename Functor>
    void radixSignedSort(Iterator first, Iterator last, Functor functor) {

      Iterator cut = radixSortSignPartition(first, last, functor);

      Value mask;
      int max_digit;
      Iterator it;

      mask = ~0; max_digit = 0;
      for (it = first; it != cut; ++it) {
        while ((mask & functor(*it)) != mask) {
          ++max_digit;
          mask <<= 1;
        }
      }
      radixIntroSort(first, cut, functor, 1 << max_digit);

      mask = 0; max_digit = 0;
      for (it = cut; it != last; ++it) {
        while ((mask | functor(*it)) != mask) {
          ++max_digit;
          mask <<= 1; mask |= 1;
        }
      }
      radixIntroSort(cut, last, functor, 1 << max_digit);
    }

    template <typename Value, typename Iterator, typename Functor>
    void radixUnsignedSort(Iterator first, Iterator last, Functor functor) {

      Value mask = 0;
      int max_digit = 0;

      Iterator it;
      for (it = first; it != last; ++it) {
        while ((mask | functor(*it)) != mask) {
          ++max_digit;
          mask <<= 1; mask |= 1;
        }
      }
      radixIntroSort(first, last, functor, 1 << max_digit);
    }


    template <typename Value,
              bool sign = std::numeric_limits<Value>::is_signed >
    struct RadixSortSelector {
      template <typename Iterator, typename Functor>
      static void sort(Iterator first, Iterator last, Functor functor) {
        radixSignedSort<Value>(first, last, functor);
      }
    };

    template <typename Value>
    struct RadixSortSelector<Value, false> {
      template <typename Iterator, typename Functor>
      static void sort(Iterator first, Iterator last, Functor functor) {
        radixUnsignedSort<Value>(first, last, functor);
      }
    };

  }

  /// \ingroup auxalg
  ///
  /// \brief Sorts the STL compatible range into ascending order.
  ///
  /// The \c radixSort sorts an STL compatible range into ascending
  /// order.  The radix sort algorithm can sort items which are mapped
  /// to integers with an adaptable unary function \c functor and the
  /// order will be ascending according to these mapped values.
  ///
  /// It is also possible to use a normal function instead
  /// of the functor object. If the functor is not given it will use
  /// the identity function instead.
  ///
  /// This is a special quick sort algorithm where the pivot
  /// values to split the items are choosen to be 2<sup>k</sup>
  /// for each \c k.
  /// Therefore, the time complexity of the algorithm is O(log(c)*n) and
  /// it uses O(log(c)) additional space, where \c c is the maximal value
  /// and \c n is the number of the items in the container.
  ///
  /// \param first The begin of the given range.
  /// \param last The end of the given range.
  /// \param functor An adaptible unary function or a normal function
  /// which maps the items to any integer type which can be either
  /// signed or unsigned.
  ///
  /// \sa stableRadixSort()
  template <typename Iterator, typename Functor>
  void radixSort(Iterator first, Iterator last, Functor functor) {
    using namespace _radix_sort_bits;
    typedef typename Functor::result_type Value;
    RadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
    using namespace _radix_sort_bits;
    RadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
    using namespace _radix_sort_bits;
    RadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
    using namespace _radix_sort_bits;
    RadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
    using namespace _radix_sort_bits;
    RadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator>
  void radixSort(Iterator first, Iterator last) {
    using namespace _radix_sort_bits;
    typedef typename std::iterator_traits<Iterator>::value_type Value;
    RadixSortSelector<Value>::sort(first, last, Identity<Value>());
  }

  namespace _radix_sort_bits {

    template <typename Value>
    unsigned char valueByte(Value value, int byte) {
      return value >> (std::numeric_limits<unsigned char>::digits * byte);
    }

    template <typename Functor, typename Key>
    void stableRadixIntroSort(Key *first, Key *last, Key *target,
                              int byte, Functor functor) {
      const int size =
        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
      std::vector<int> counter(size);
      for (int i = 0; i < size; ++i) {
        counter[i] = 0;
      }
      Key *it = first;
      while (first != last) {
        ++counter[valueByte(functor(*first), byte)];
        ++first;
      }
      int prev, num = 0;
      for (int i = 0; i < size; ++i) {
        prev = num;
        num += counter[i];
        counter[i] = prev;
      }
      while (it != last) {
        target[counter[valueByte(functor(*it), byte)]++] = *it;
        ++it;
      }
    }

    template <typename Functor, typename Key>
    void signedStableRadixIntroSort(Key *first, Key *last, Key *target,
                                    int byte, Functor functor) {
      const int size =
        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
      std::vector<int> counter(size);
      for (int i = 0; i < size; ++i) {
        counter[i] = 0;
      }
      Key *it = first;
      while (first != last) {
        counter[valueByte(functor(*first), byte)]++;
        ++first;
      }
      int prev, num = 0;
      for (int i = size / 2; i < size; ++i) {
        prev = num;
        num += counter[i];
        counter[i] = prev;
      }
      for (int i = 0; i < size / 2; ++i) {
        prev = num;
        num += counter[i];
        counter[i] = prev;
      }
      while (it != last) {
        target[counter[valueByte(functor(*it), byte)]++] = *it;
        ++it;
      }
    }


    template <typename Value, typename Iterator, typename Functor>
    void stableRadixSignedSort(Iterator first, Iterator last, Functor functor) {
      if (first == last) return;
      typedef typename std::iterator_traits<Iterator>::value_type Key;
      typedef std::allocator<Key> Allocator;
      Allocator allocator;

      int length = std::distance(first, last);
      Key* buffer = allocator.allocate(2 * length);
      try {
        bool dir = true;
        std::copy(first, last, buffer);
        for (int i = 0; i < int(sizeof(Value)) - 1; ++i) {
          if (dir) {
            stableRadixIntroSort(buffer, buffer + length, buffer + length,
                                 i, functor);
          } else {
            stableRadixIntroSort(buffer + length, buffer + 2 * length, buffer,
                                 i, functor);
          }
          dir = !dir;
        }
        if (dir) {
          signedStableRadixIntroSort(buffer, buffer + length, buffer + length,
                                     sizeof(Value) - 1, functor);
          std::copy(buffer + length, buffer + 2 * length, first);
        }        else {
          signedStableRadixIntroSort(buffer + length, buffer + 2 * length,
                                     buffer, sizeof(Value) - 1, functor);
          std::copy(buffer, buffer + length, first);
        }
      } catch (...) {
        allocator.deallocate(buffer, 2 * length);
        throw;
      }
      allocator.deallocate(buffer, 2 * length);
    }

    template <typename Value, typename Iterator, typename Functor>
    void stableRadixUnsignedSort(Iterator first, Iterator last,
                                 Functor functor) {
      if (first == last) return;
      typedef typename std::iterator_traits<Iterator>::value_type Key;
      typedef std::allocator<Key> Allocator;
      Allocator allocator;

      int length = std::distance(first, last);
      Key *buffer = allocator.allocate(2 * length);
      try {
        bool dir = true;
        std::copy(first, last, buffer);
        for (int i = 0; i < int(sizeof(Value)); ++i) {
          if (dir) {
            stableRadixIntroSort(buffer, buffer + length,
                                 buffer + length, i, functor);
          } else {
            stableRadixIntroSort(buffer + length, buffer + 2 * length,
                                 buffer, i, functor);
          }
          dir = !dir;
        }
        if (dir) {
          std::copy(buffer, buffer + length, first);
        }        else {
          std::copy(buffer + length, buffer + 2 * length, first);
        }
      } catch (...) {
        allocator.deallocate(buffer, 2 * length);
        throw;
      }
      allocator.deallocate(buffer, 2 * length);
    }



    template <typename Value,
              bool sign = std::numeric_limits<Value>::is_signed >
    struct StableRadixSortSelector {
      template <typename Iterator, typename Functor>
      static void sort(Iterator first, Iterator last, Functor functor) {
        stableRadixSignedSort<Value>(first, last, functor);
      }
    };

    template <typename Value>
    struct StableRadixSortSelector<Value, false> {
      template <typename Iterator, typename Functor>
      static void sort(Iterator first, Iterator last, Functor functor) {
        stableRadixUnsignedSort<Value>(first, last, functor);
      }
    };

  }

  /// \ingroup auxalg
  ///
  /// \brief Sorts the STL compatible range into ascending order in a stable
  /// way.
  ///
  /// This function sorts an STL compatible range into ascending
  /// order according to an integer mapping in the same as radixSort() does.
  ///
  /// This sorting algorithm is stable, i.e. the order of two equal
  /// elements remains the same after the sorting.
  ///
  /// This sort algorithm  use a radix forward sort on the
  /// bytes of the integer number. The algorithm sorts the items
  /// byte-by-byte. First, it counts how many times a byte value occurs
  /// in the container, then it copies the corresponding items to
  /// another container in asceding order in O(n) time.
  ///
  /// The time complexity of the algorithm is O(log(c)*n) and
  /// it uses O(n) additional space, where \c c is the
  /// maximal value and \c n is the number of the items in the
  /// container.
  ///

  /// \param first The begin of the given range.
  /// \param last The end of the given range.
  /// \param functor An adaptible unary function or a normal function
  /// which maps the items to any integer type which can be either
  /// signed or unsigned.
  /// \sa radixSort()
  template <typename Iterator, typename Functor>
  void stableRadixSort(Iterator first, Iterator last, Functor functor) {
    using namespace _radix_sort_bits;
    typedef typename Functor::result_type Value;
    StableRadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
    using namespace _radix_sort_bits;
    StableRadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
    using namespace _radix_sort_bits;
    StableRadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
    using namespace _radix_sort_bits;
    StableRadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator, typename Value, typename Key>
  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
    using namespace _radix_sort_bits;
    StableRadixSortSelector<Value>::sort(first, last, functor);
  }

  template <typename Iterator>
  void stableRadixSort(Iterator first, Iterator last) {
    using namespace _radix_sort_bits;
    typedef typename std::iterator_traits<Iterator>::value_type Value;
    StableRadixSortSelector<Value>::sort(first, last, Identity<Value>());
  }

}

#endif