gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Doc improvements and source unification in radix_sort (#72)
0 2 0
default
2 files changed with 37 insertions and 35 deletions:
↑ Collapse diff ↑
Show white space 64 line context
1
/* -*- C++ -*-
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef RADIX_SORT_H
20 20
#define RADIX_SORT_H
21 21

	
22 22
/// \ingroup auxalg
23 23
/// \file
24 24
/// \brief Radix sort
25 25
///
26 26
/// Linear time sorting algorithms
27 27

	
28 28
#include <vector>
29 29
#include <limits>
30 30
#include <iterator>
31 31
#include <algorithm>
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  namespace _radix_sort_bits {
... ...
@@ -166,84 +166,87 @@
166 166
	while ((mask | functor(*it)) != mask) {
167 167
	  ++max_digit;
168 168
	  mask <<= 1; mask |= 1;
169 169
	}
170 170
      }
171 171
      radixIntroSort(first, last, functor, 1 << max_digit);
172 172
    }
173 173

	
174 174

	
175 175
    template <typename Value, 
176 176
	      bool sign = std::numeric_limits<Value>::is_signed >
177 177
    struct RadixSortSelector {
178 178
      template <typename Iterator, typename Functor>
179 179
      static void sort(Iterator first, Iterator last, Functor functor) {
180 180
	radixSignedSort<Value>(first, last, functor);
181 181
      }
182 182
    };
183 183

	
184 184
    template <typename Value>
185 185
    struct RadixSortSelector<Value, false> {
186 186
      template <typename Iterator, typename Functor>
187 187
      static void sort(Iterator first, Iterator last, Functor functor) {
188 188
	radixUnsignedSort<Value>(first, last, functor);
189 189
      }
190 190
    };
191 191

	
192 192
  }
193 193

	
194 194
  /// \ingroup auxalg
195 195
  ///
196 196
  /// \brief Sorts the STL compatible range into ascending order.
197 197
  ///
198
  /// The \c radixSort sorts the STL compatible range into ascending
199
  /// order.  The radix sort algorithm can sort the items which mapped
200
  /// to an integer with an adaptable unary function \c functor and the
201
  /// order will be ascending by these mapped values. As function
202
  /// specialization it is possible to use a normal function instead
203
  /// of the functor object or if the functor is not given it will use
204
  /// an identity function instead.
198
  /// The \c radixSort sorts an STL compatible range into ascending
199
  /// order.  The radix sort algorithm can sort items which are mapped
200
  /// to integers with an adaptable unary function \c functor and the
201
  /// order will be ascending according to these mapped values.
205 202
  ///
206
  /// This implemented radix sort is a special quick sort which pivot
207
  /// value is choosen to partite the items on the next
208
  /// bit. Therefore, let be \c c the maximal capacity and \c n the
209
  /// number of the items in the container, the time complexity of the
210
  /// algorithm is \f$ O(\log(c)n) \f$ and the additional space
211
  /// complexity is \f$ O(\log(c)) \f$.
203
  /// It is also possible to use a normal function instead
204
  /// of the functor object. If the functor is not given it will use
205
  /// the identity function instead.
206
  ///
207
  /// This is a special quick sort algorithm where the pivot
208
  /// values to split the items are choosen to be \f$ 2^k \f$ for each \c k.
209
  /// Therefore, the time complexity of the
210
  /// algorithm is \f$ O(\log(c)n) \f$ and it uses \f$ O(\log(c)) \f$,
211
  /// additional space, where \c c is the maximal value and \c n is the
212
  /// number of the items in the container.
212 213
  ///
213 214
  /// \param first The begin of the given range.
214 215
  /// \param last The end of the given range.
215 216
  /// \param functor An adaptible unary function or a normal function
216 217
  /// which maps the items to any integer type which can be either
217 218
  /// signed or unsigned.
219
  ///
220
  /// \sa counterSort()
218 221
  template <typename Iterator, typename Functor>
219 222
  void radixSort(Iterator first, Iterator last, Functor functor) {
220 223
    using namespace _radix_sort_bits;
221 224
    typedef typename Functor::result_type Value;
222 225
    RadixSortSelector<Value>::sort(first, last, functor);
223 226
  }
224 227

	
225 228
  template <typename Iterator, typename Value, typename Key>
226 229
  void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
227 230
    using namespace _radix_sort_bits;
228 231
    RadixSortSelector<Value>::sort(first, last, functor);
229 232
  }
230 233

	
231 234
  template <typename Iterator, typename Value, typename Key>
232 235
  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
233 236
    using namespace _radix_sort_bits;
234 237
    RadixSortSelector<Value>::sort(first, last, functor);
235 238
  }
236 239

	
237 240
  template <typename Iterator, typename Value, typename Key>
238 241
  void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
239 242
    using namespace _radix_sort_bits;
240 243
    RadixSortSelector<Value>::sort(first, last, functor);
241 244
  }
242 245

	
243 246
  template <typename Iterator, typename Value, typename Key>
244 247
  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
245 248
    using namespace _radix_sort_bits;
246 249
    RadixSortSelector<Value>::sort(first, last, functor);
247 250
  }
248 251

	
249 252
  template <typename Iterator>
... ...
@@ -384,92 +387,91 @@
384 387
	  std::copy(buffer + length, buffer + 2 * length, first);
385 388
	}
386 389
      } catch (...) {
387 390
	allocator.deallocate(buffer, 2 * length);
388 391
	throw;
389 392
      }
390 393
      allocator.deallocate(buffer, 2 * length);
391 394
    }
392 395

	
393 396

	
394 397

	
395 398
    template <typename Value, 
396 399
	      bool sign = std::numeric_limits<Value>::is_signed >
397 400
    struct CounterSortSelector {
398 401
      template <typename Iterator, typename Functor>
399 402
      static void sort(Iterator first, Iterator last, Functor functor) {
400 403
	counterSignedSort<Value>(first, last, functor);
401 404
      }
402 405
    };
403 406

	
404 407
    template <typename Value>
405 408
    struct CounterSortSelector<Value, false> {
406 409
      template <typename Iterator, typename Functor>
407 410
      static void sort(Iterator first, Iterator last, Functor functor) {
408 411
	counterUnsignedSort<Value>(first, last, functor);
409 412
      }
410 413
    };
411 414

	
412 415
  }
413 416

	
414 417
  /// \ingroup auxalg
415 418
  ///
416
  /// \brief Sorts stable the STL compatible range into ascending order.
419
  /// \brief Sorts the STL compatible range into ascending order in a stable
420
  /// way.
417 421
  ///
418
  /// The \c counterSort sorts the STL compatible range into ascending
419
  /// order.  The counter sort algorithm can sort the items which
420
  /// mapped to an integer with an adaptable unary function \c functor
421
  /// and the order will be ascending by these mapped values. As
422
  /// function specialization it is possible to use a normal function
423
  /// instead of the functor object or if the functor is not given it
424
  /// will use an identity function instead.
422
  /// This function sorts an STL compatible range into ascending
423
  /// order according to an integer mapping in the same as radixSort() does.
425 424
  ///
426
  /// The implemented counter sort use a radix forward sort on the
425
  /// This sorting algorithm is stable, i.e. the order of two equal
426
  /// element remains the same after the sorting.
427
  ///
428
  /// This sort algorithm  use a radix forward sort on the
427 429
  /// bytes of the integer number. The algorithm sorts the items
428
  /// byte-by-byte, first it counts how many times occurs a byte value
429
  /// in the containerm, and with the occurence number the container
430
  /// can be copied to an other in asceding order in \c O(n) time.
431
  /// Let be \c c the maximal capacity of the integer type and \c n
432
  /// the number of the items in the container, the time complexity of
433
  /// the algorithm is \f$ O(\log(c)n) \f$ and the additional space
434
  /// complexity is \f$ O(n) \f$.
430
  /// byte-by-byte. First, it counts how many times a byte value occurs
431
  /// in the container, then it copies the corresponding items to
432
  /// another container in asceding order in \c O(n) time.
435 433
  ///
436
  /// The sorting algorithm is stable, i.e. the order of two equal
437
  /// element remains the same.
434
  /// The time complexity of the algorithm is \f$ O(\log(c)n) \f$ and
435
  /// it uses \f$ O(n) \f$, additional space, where \c c is the
436
  /// maximal value and \c n is the number of the items in the
437
  /// container.
438 438
  ///
439

	
439 440
  /// \param first The begin of the given range.
440 441
  /// \param last The end of the given range.
441 442
  /// \param functor An adaptible unary function or a normal function
442 443
  /// which maps the items to any integer type which can be either
443 444
  /// signed or unsigned.
445
  /// \sa radixSort()
444 446
  template <typename Iterator, typename Functor>
445 447
  void counterSort(Iterator first, Iterator last, Functor functor) {
446 448
    using namespace _radix_sort_bits;
447 449
    typedef typename Functor::result_type Value;
448 450
    CounterSortSelector<Value>::sort(first, last, functor);
449 451
  }
450 452

	
451 453
  template <typename Iterator, typename Value, typename Key>
452 454
  void counterSort(Iterator first, Iterator last, Value (*functor)(Key)) {
453 455
    using namespace _radix_sort_bits;
454 456
    CounterSortSelector<Value>::sort(first, last, functor);
455 457
  }
456 458

	
457 459
  template <typename Iterator, typename Value, typename Key>
458 460
  void counterSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
459 461
    using namespace _radix_sort_bits;
460 462
    CounterSortSelector<Value>::sort(first, last, functor);
461 463
  }
462 464

	
463 465
  template <typename Iterator, typename Value, typename Key>
464 466
  void counterSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
465 467
    using namespace _radix_sort_bits;
466 468
    CounterSortSelector<Value>::sort(first, last, functor);
467 469
  }
468 470

	
469 471
  template <typename Iterator, typename Value, typename Key>
470 472
  void counterSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
471 473
    using namespace _radix_sort_bits;
472 474
    CounterSortSelector<Value>::sort(first, last, functor);
473 475
  }
474 476

	
475 477
  template <typename Iterator>
Show white space 64 line context
1
/* -*- C++ -*-
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/time_measure.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/maps.h>
22 22
#include <lemon/radix_sort.h>
23 23
#include <lemon/math.h>
24 24

	
25 25
#include "test_tools.h"
26 26

	
27 27
#include <vector>
28 28
#include <algorithm>
29 29

	
30 30
using namespace lemon;
31 31

	
32 32
static const int n = 10000;
33 33

	
34 34
struct Negate {
35 35
  typedef int argument_type;
0 comments (0 inline)