0
2
0
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2009 |
|
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 { |
36 | 36 |
|
37 | 37 |
template <typename Value> |
38 | 38 |
struct Identity { |
39 | 39 |
const Value& operator()(const Value& val) { |
40 | 40 |
return val; |
41 | 41 |
} |
42 | 42 |
}; |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
template <typename Value, typename Iterator, typename Functor> |
46 | 46 |
Iterator radixSortPartition(Iterator first, Iterator last, |
47 | 47 |
Functor functor, Value mask) { |
48 | 48 |
while (first != last && !(functor(*first) & mask)) { |
49 | 49 |
++first; |
50 | 50 |
} |
51 | 51 |
if (first == last) { |
52 | 52 |
return first; |
53 | 53 |
} |
54 | 54 |
--last; |
55 | 55 |
while (first != last && (functor(*last) & mask)) { |
56 | 56 |
--last; |
57 | 57 |
} |
58 | 58 |
if (first == last) { |
59 | 59 |
return first; |
60 | 60 |
} |
61 | 61 |
std::iter_swap(first, last); |
62 | 62 |
++first; |
63 | 63 |
if (!(first < last)) { |
64 | 64 |
return first; |
65 | 65 |
} |
66 | 66 |
while (true) { |
67 | 67 |
while (!(functor(*first) & mask)) { |
68 | 68 |
++first; |
69 | 69 |
} |
... | ... |
@@ -287,141 +287,141 @@ |
287 | 287 |
target[counter[valueByte(functor(*it), byte)]++] = *it; |
288 | 288 |
++it; |
289 | 289 |
} |
290 | 290 |
} |
291 | 291 |
|
292 | 292 |
template <typename Functor, typename Key> |
293 | 293 |
void signedStableRadixIntroSort(Key *first, Key *last, Key *target, |
294 | 294 |
int byte, Functor functor) { |
295 | 295 |
const int size = |
296 | 296 |
unsigned(std::numeric_limits<unsigned char>::max()) + 1; |
297 | 297 |
std::vector<int> counter(size); |
298 | 298 |
for (int i = 0; i < size; ++i) { |
299 | 299 |
counter[i] = 0; |
300 | 300 |
} |
301 | 301 |
Key *it = first; |
302 | 302 |
while (first != last) { |
303 | 303 |
counter[valueByte(functor(*first), byte)]++; |
304 | 304 |
++first; |
305 | 305 |
} |
306 | 306 |
int prev, num = 0; |
307 | 307 |
for (int i = size / 2; i < size; ++i) { |
308 | 308 |
prev = num; |
309 | 309 |
num += counter[i]; |
310 | 310 |
counter[i] = prev; |
311 | 311 |
} |
312 | 312 |
for (int i = 0; i < size / 2; ++i) { |
313 | 313 |
prev = num; |
314 | 314 |
num += counter[i]; |
315 | 315 |
counter[i] = prev; |
316 | 316 |
} |
317 | 317 |
while (it != last) { |
318 | 318 |
target[counter[valueByte(functor(*it), byte)]++] = *it; |
319 | 319 |
++it; |
320 | 320 |
} |
321 | 321 |
} |
322 | 322 |
|
323 | 323 |
|
324 | 324 |
template <typename Value, typename Iterator, typename Functor> |
325 | 325 |
void stableRadixSignedSort(Iterator first, Iterator last, Functor functor) { |
326 | 326 |
if (first == last) return; |
327 | 327 |
typedef typename std::iterator_traits<Iterator>::value_type Key; |
328 | 328 |
typedef std::allocator<Key> Allocator; |
329 | 329 |
Allocator allocator; |
330 | 330 |
|
331 | 331 |
int length = std::distance(first, last); |
332 | 332 |
Key* buffer = allocator.allocate(2 * length); |
333 | 333 |
try { |
334 | 334 |
bool dir = true; |
335 | 335 |
std::copy(first, last, buffer); |
336 | 336 |
for (int i = 0; i < int(sizeof(Value)) - 1; ++i) { |
337 | 337 |
if (dir) { |
338 | 338 |
stableRadixIntroSort(buffer, buffer + length, buffer + length, |
339 | 339 |
i, functor); |
340 | 340 |
} else { |
341 | 341 |
stableRadixIntroSort(buffer + length, buffer + 2 * length, buffer, |
342 | 342 |
i, functor); |
343 | 343 |
} |
344 | 344 |
dir = !dir; |
345 | 345 |
} |
346 | 346 |
if (dir) { |
347 | 347 |
signedStableRadixIntroSort(buffer, buffer + length, buffer + length, |
348 | 348 |
sizeof(Value) - 1, functor); |
349 | 349 |
std::copy(buffer + length, buffer + 2 * length, first); |
350 | 350 |
} else { |
351 |
signedStableRadixIntroSort(buffer + length, buffer + 2 * length, |
|
351 |
signedStableRadixIntroSort(buffer + length, buffer + 2 * length, |
|
352 | 352 |
buffer, sizeof(Value) - 1, functor); |
353 | 353 |
std::copy(buffer, buffer + length, first); |
354 | 354 |
} |
355 | 355 |
} catch (...) { |
356 | 356 |
allocator.deallocate(buffer, 2 * length); |
357 | 357 |
throw; |
358 | 358 |
} |
359 | 359 |
allocator.deallocate(buffer, 2 * length); |
360 | 360 |
} |
361 | 361 |
|
362 | 362 |
template <typename Value, typename Iterator, typename Functor> |
363 |
void stableRadixUnsignedSort(Iterator first, Iterator last, |
|
363 |
void stableRadixUnsignedSort(Iterator first, Iterator last, |
|
364 | 364 |
Functor functor) { |
365 | 365 |
if (first == last) return; |
366 | 366 |
typedef typename std::iterator_traits<Iterator>::value_type Key; |
367 | 367 |
typedef std::allocator<Key> Allocator; |
368 | 368 |
Allocator allocator; |
369 | 369 |
|
370 | 370 |
int length = std::distance(first, last); |
371 | 371 |
Key *buffer = allocator.allocate(2 * length); |
372 | 372 |
try { |
373 | 373 |
bool dir = true; |
374 | 374 |
std::copy(first, last, buffer); |
375 | 375 |
for (int i = 0; i < int(sizeof(Value)); ++i) { |
376 | 376 |
if (dir) { |
377 | 377 |
stableRadixIntroSort(buffer, buffer + length, |
378 | 378 |
buffer + length, i, functor); |
379 | 379 |
} else { |
380 | 380 |
stableRadixIntroSort(buffer + length, buffer + 2 * length, |
381 | 381 |
buffer, i, functor); |
382 | 382 |
} |
383 | 383 |
dir = !dir; |
384 | 384 |
} |
385 | 385 |
if (dir) { |
386 | 386 |
std::copy(buffer, buffer + length, first); |
387 | 387 |
} else { |
388 | 388 |
std::copy(buffer + length, buffer + 2 * length, first); |
389 | 389 |
} |
390 | 390 |
} catch (...) { |
391 | 391 |
allocator.deallocate(buffer, 2 * length); |
392 | 392 |
throw; |
393 | 393 |
} |
394 | 394 |
allocator.deallocate(buffer, 2 * length); |
395 | 395 |
} |
396 | 396 |
|
397 | 397 |
|
398 | 398 |
|
399 | 399 |
template <typename Value, |
400 | 400 |
bool sign = std::numeric_limits<Value>::is_signed > |
401 | 401 |
struct StableRadixSortSelector { |
402 | 402 |
template <typename Iterator, typename Functor> |
403 | 403 |
static void sort(Iterator first, Iterator last, Functor functor) { |
404 | 404 |
stableRadixSignedSort<Value>(first, last, functor); |
405 | 405 |
} |
406 | 406 |
}; |
407 | 407 |
|
408 | 408 |
template <typename Value> |
409 | 409 |
struct StableRadixSortSelector<Value, false> { |
410 | 410 |
template <typename Iterator, typename Functor> |
411 | 411 |
static void sort(Iterator first, Iterator last, Functor functor) { |
412 | 412 |
stableRadixUnsignedSort<Value>(first, last, functor); |
413 | 413 |
} |
414 | 414 |
}; |
415 | 415 |
|
416 | 416 |
} |
417 | 417 |
|
418 | 418 |
/// \ingroup auxalg |
419 | 419 |
/// |
420 | 420 |
/// \brief Sorts the STL compatible range into ascending order in a stable |
421 | 421 |
/// way. |
422 | 422 |
/// |
423 | 423 |
/// This function sorts an STL compatible range into ascending |
424 | 424 |
/// order according to an integer mapping in the same as radixSort() does. |
425 | 425 |
/// |
426 | 426 |
/// This sorting algorithm is stable, i.e. the order of two equal |
427 | 427 |
/// elements remains the same after the sorting. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2009 |
|
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; |
36 | 36 |
typedef int result_type; |
37 | 37 |
int operator()(int a) { return - a; } |
38 | 38 |
}; |
39 | 39 |
|
40 | 40 |
int negate(int a) { return - a; } |
41 | 41 |
|
42 | 42 |
|
43 | 43 |
void generateIntSequence(int n, std::vector<int>& data) { |
44 | 44 |
int prime = 9973; |
45 | 45 |
int root = 136, value = 1; |
46 | 46 |
for (int i = 0; i < n; ++i) { |
47 | 47 |
data.push_back(value - prime / 2); |
48 | 48 |
value = (value * root) % prime; |
49 | 49 |
} |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
void generateCharSequence(int n, std::vector<unsigned char>& data) { |
53 | 53 |
int prime = 251; |
54 | 54 |
int root = 3, value = root; |
55 | 55 |
for (int i = 0; i < n; ++i) { |
56 | 56 |
data.push_back(static_cast<unsigned char>(value)); |
57 | 57 |
value = (value * root) % prime; |
58 | 58 |
} |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
void checkRadixSort() { |
62 | 62 |
{ |
63 | 63 |
std::vector<int> data1; |
64 | 64 |
generateIntSequence(n, data1); |
65 | 65 |
|
66 | 66 |
std::vector<int> data2(data1); |
67 | 67 |
std::sort(data1.begin(), data1.end()); |
68 | 68 |
|
69 | 69 |
radixSort(data2.begin(), data2.end()); |
0 comments (0 inline)