1.1 --- a/lemon/Makefile.am Sun Oct 12 19:35:48 2008 +0100
1.2 +++ b/lemon/Makefile.am Fri Oct 17 23:55:18 2008 +0200
1.3 @@ -36,6 +36,7 @@
1.4 lemon/maps.h \
1.5 lemon/math.h \
1.6 lemon/path.h \
1.7 + lemon/radix_sort.h \
1.8 lemon/random.h \
1.9 lemon/smart_graph.h \
1.10 lemon/time_measure.h \
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/lemon/radix_sort.h Fri Oct 17 23:55:18 2008 +0200
2.3 @@ -0,0 +1,484 @@
2.4 +/* -*- C++ -*-
2.5 + *
2.6 + * This file is a part of LEMON, a generic C++ optimization library
2.7 + *
2.8 + * Copyright (C) 2003-2008
2.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
2.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
2.11 + *
2.12 + * Permission to use, modify and distribute this software is granted
2.13 + * provided that this copyright notice appears in all copies. For
2.14 + * precise terms see the accompanying LICENSE file.
2.15 + *
2.16 + * This software is provided "AS IS" with no warranty of any kind,
2.17 + * express or implied, and with no claim as to its suitability for any
2.18 + * purpose.
2.19 + *
2.20 + */
2.21 +
2.22 +#ifndef RADIX_SORT_H
2.23 +#define RADIX_SORT_H
2.24 +
2.25 +/// \ingroup auxalg
2.26 +/// \file
2.27 +/// \brief Radix sort
2.28 +///
2.29 +/// Linear time sorting algorithms
2.30 +
2.31 +#include <vector>
2.32 +#include <limits>
2.33 +#include <iterator>
2.34 +#include <algorithm>
2.35 +
2.36 +namespace lemon {
2.37 +
2.38 + namespace _radix_sort_bits {
2.39 +
2.40 + template <typename Value>
2.41 + struct Identity {
2.42 + const Value& operator()(const Value& val) {
2.43 + return val;
2.44 + }
2.45 + };
2.46 +
2.47 +
2.48 + template <typename Value, typename Iterator, typename Functor>
2.49 + Iterator radixSortPartition(Iterator first, Iterator last,
2.50 + Functor functor, Value mask) {
2.51 + while (first != last && !(functor(*first) & mask)) {
2.52 + ++first;
2.53 + }
2.54 + if (first == last) {
2.55 + return first;
2.56 + }
2.57 + --last;
2.58 + while (first != last && (functor(*last) & mask)) {
2.59 + --last;
2.60 + }
2.61 + if (first == last) {
2.62 + return first;
2.63 + }
2.64 + std::iter_swap(first, last);
2.65 + ++first;
2.66 + if (!(first < last)) {
2.67 + return first;
2.68 + }
2.69 + while (true) {
2.70 + while (!(functor(*first) & mask)) {
2.71 + ++first;
2.72 + }
2.73 + --last;
2.74 + while (functor(*last) & mask) {
2.75 + --last;
2.76 + }
2.77 + if (!(first < last)) {
2.78 + return first;
2.79 + }
2.80 + std::iter_swap(first, last);
2.81 + ++first;
2.82 + }
2.83 + }
2.84 +
2.85 + template <typename Iterator, typename Functor>
2.86 + Iterator radixSortSignPartition(Iterator first, Iterator last,
2.87 + Functor functor) {
2.88 + while (first != last && functor(*first) < 0) {
2.89 + ++first;
2.90 + }
2.91 + if (first == last) {
2.92 + return first;
2.93 + }
2.94 + --last;
2.95 + while (first != last && functor(*last) >= 0) {
2.96 + --last;
2.97 + }
2.98 + if (first == last) {
2.99 + return first;
2.100 + }
2.101 + std::iter_swap(first, last);
2.102 + ++first;
2.103 + if (!(first < last)) {
2.104 + return first;
2.105 + }
2.106 + while (true) {
2.107 + while (functor(*first) < 0) {
2.108 + ++first;
2.109 + }
2.110 + --last;
2.111 + while (functor(*last) >= 0) {
2.112 + --last;
2.113 + }
2.114 + if (!(first < last)) {
2.115 + return first;
2.116 + }
2.117 + std::iter_swap(first, last);
2.118 + ++first;
2.119 + }
2.120 + }
2.121 +
2.122 + template <typename Value, typename Iterator, typename Functor>
2.123 + void radixIntroSort(Iterator first, Iterator last,
2.124 + Functor functor, Value mask) {
2.125 + while (mask != 0 && last - first > 1) {
2.126 + Iterator cut = radixSortPartition(first, last, functor, mask);
2.127 + mask >>= 1;
2.128 + radixIntroSort(first, cut, functor, mask);
2.129 + first = cut;
2.130 + }
2.131 + }
2.132 +
2.133 + template <typename Value, typename Iterator, typename Functor>
2.134 + void radixSignedSort(Iterator first, Iterator last, Functor functor) {
2.135 +
2.136 + Iterator cut = radixSortSignPartition(first, last, functor);
2.137 +
2.138 + Value mask;
2.139 + int max_digit;
2.140 + Iterator it;
2.141 +
2.142 + mask = ~0; max_digit = 0;
2.143 + for (it = first; it != cut; ++it) {
2.144 + while ((mask & functor(*it)) != mask) {
2.145 + ++max_digit;
2.146 + mask <<= 1;
2.147 + }
2.148 + }
2.149 + radixIntroSort(first, cut, functor, 1 << max_digit);
2.150 +
2.151 + mask = 0; max_digit = 0;
2.152 + for (it = cut; it != last; ++it) {
2.153 + while ((mask | functor(*it)) != mask) {
2.154 + ++max_digit;
2.155 + mask <<= 1; mask |= 1;
2.156 + }
2.157 + }
2.158 + radixIntroSort(cut, last, functor, 1 << max_digit);
2.159 + }
2.160 +
2.161 + template <typename Value, typename Iterator, typename Functor>
2.162 + void radixUnsignedSort(Iterator first, Iterator last, Functor functor) {
2.163 +
2.164 + Value mask = 0;
2.165 + int max_digit = 0;
2.166 +
2.167 + Iterator it;
2.168 + for (it = first; it != last; ++it) {
2.169 + while ((mask | functor(*it)) != mask) {
2.170 + ++max_digit;
2.171 + mask <<= 1; mask |= 1;
2.172 + }
2.173 + }
2.174 + radixIntroSort(first, last, functor, 1 << max_digit);
2.175 + }
2.176 +
2.177 +
2.178 + template <typename Value,
2.179 + bool sign = std::numeric_limits<Value>::is_signed >
2.180 + struct RadixSortSelector {
2.181 + template <typename Iterator, typename Functor>
2.182 + static void sort(Iterator first, Iterator last, Functor functor) {
2.183 + radixSignedSort<Value>(first, last, functor);
2.184 + }
2.185 + };
2.186 +
2.187 + template <typename Value>
2.188 + struct RadixSortSelector<Value, false> {
2.189 + template <typename Iterator, typename Functor>
2.190 + static void sort(Iterator first, Iterator last, Functor functor) {
2.191 + radixUnsignedSort<Value>(first, last, functor);
2.192 + }
2.193 + };
2.194 +
2.195 + }
2.196 +
2.197 + /// \ingroup auxalg
2.198 + ///
2.199 + /// \brief Sorts the STL compatible range into ascending order.
2.200 + ///
2.201 + /// The \c radixSort sorts the STL compatible range into ascending
2.202 + /// order. The radix sort algorithm can sort the items which mapped
2.203 + /// to an integer with an adaptable unary function \c functor and the
2.204 + /// order will be ascending by these mapped values. As function
2.205 + /// specialization it is possible to use a normal function instead
2.206 + /// of the functor object or if the functor is not given it will use
2.207 + /// an identity function instead.
2.208 + ///
2.209 + /// This implemented radix sort is a special quick sort which pivot
2.210 + /// value is choosen to partite the items on the next
2.211 + /// bit. Therefore, let be \c c the maximal capacity and \c n the
2.212 + /// number of the items in the container, the time complexity of the
2.213 + /// algorithm is \f$ O(\log(c)n) \f$ and the additional space
2.214 + /// complexity is \f$ O(\log(c)) \f$.
2.215 + ///
2.216 + /// \param first The begin of the given range.
2.217 + /// \param last The end of the given range.
2.218 + /// \param functor An adaptible unary function or a normal function
2.219 + /// which maps the items to any integer type which can be either
2.220 + /// signed or unsigned.
2.221 + template <typename Iterator, typename Functor>
2.222 + void radixSort(Iterator first, Iterator last, Functor functor) {
2.223 + using namespace _radix_sort_bits;
2.224 + typedef typename Functor::result_type Value;
2.225 + RadixSortSelector<Value>::sort(first, last, functor);
2.226 + }
2.227 +
2.228 + template <typename Iterator, typename Value, typename Key>
2.229 + void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
2.230 + using namespace _radix_sort_bits;
2.231 + RadixSortSelector<Value>::sort(first, last, functor);
2.232 + }
2.233 +
2.234 + template <typename Iterator, typename Value, typename Key>
2.235 + void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
2.236 + using namespace _radix_sort_bits;
2.237 + RadixSortSelector<Value>::sort(first, last, functor);
2.238 + }
2.239 +
2.240 + template <typename Iterator, typename Value, typename Key>
2.241 + void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
2.242 + using namespace _radix_sort_bits;
2.243 + RadixSortSelector<Value>::sort(first, last, functor);
2.244 + }
2.245 +
2.246 + template <typename Iterator, typename Value, typename Key>
2.247 + void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
2.248 + using namespace _radix_sort_bits;
2.249 + RadixSortSelector<Value>::sort(first, last, functor);
2.250 + }
2.251 +
2.252 + template <typename Iterator>
2.253 + void radixSort(Iterator first, Iterator last) {
2.254 + using namespace _radix_sort_bits;
2.255 + typedef typename std::iterator_traits<Iterator>::value_type Value;
2.256 + RadixSortSelector<Value>::sort(first, last, Identity<Value>());
2.257 + }
2.258 +
2.259 + namespace _radix_sort_bits {
2.260 +
2.261 + template <typename Value>
2.262 + unsigned char valueByte(Value value, int byte) {
2.263 + return value >> (std::numeric_limits<unsigned char>::digits * byte);
2.264 + }
2.265 +
2.266 + template <typename Functor, typename Key>
2.267 + void counterIntroSort(Key *first, Key *last, Key *target,
2.268 + int byte, Functor functor) {
2.269 + const int size =
2.270 + unsigned(std::numeric_limits<unsigned char>::max()) + 1;
2.271 + std::vector<int> counter(size);
2.272 + for (int i = 0; i < size; ++i) {
2.273 + counter[i] = 0;
2.274 + }
2.275 + Key *it = first;
2.276 + while (first != last) {
2.277 + ++counter[valueByte(functor(*first), byte)];
2.278 + ++first;
2.279 + }
2.280 + int prev, num = 0;
2.281 + for (int i = 0; i < size; ++i) {
2.282 + prev = num;
2.283 + num += counter[i];
2.284 + counter[i] = prev;
2.285 + }
2.286 + while (it != last) {
2.287 + target[counter[valueByte(functor(*it), byte)]++] = *it;
2.288 + ++it;
2.289 + }
2.290 + }
2.291 +
2.292 + template <typename Functor, typename Key>
2.293 + void signedCounterIntroSort(Key *first, Key *last, Key *target,
2.294 + int byte, Functor functor) {
2.295 + const int size =
2.296 + unsigned(std::numeric_limits<unsigned char>::max()) + 1;
2.297 + std::vector<int> counter(size);
2.298 + for (int i = 0; i < size; ++i) {
2.299 + counter[i] = 0;
2.300 + }
2.301 + Key *it = first;
2.302 + while (first != last) {
2.303 + counter[valueByte(functor(*first), byte)]++;
2.304 + ++first;
2.305 + }
2.306 + int prev, num = 0;
2.307 + for (int i = size / 2; i < size; ++i) {
2.308 + prev = num;
2.309 + num += counter[i];
2.310 + counter[i] = prev;
2.311 + }
2.312 + for (int i = 0; i < size / 2; ++i) {
2.313 + prev = num;
2.314 + num += counter[i];
2.315 + counter[i] = prev;
2.316 + }
2.317 + while (it != last) {
2.318 + target[counter[valueByte(functor(*it), byte)]++] = *it;
2.319 + ++it;
2.320 + }
2.321 + }
2.322 +
2.323 +
2.324 + template <typename Value, typename Iterator, typename Functor>
2.325 + void counterSignedSort(Iterator first, Iterator last, Functor functor) {
2.326 + if (first == last) return;
2.327 + typedef typename std::iterator_traits<Iterator>::value_type Key;
2.328 + typedef std::allocator<Key> Allocator;
2.329 + Allocator allocator;
2.330 +
2.331 + int length = std::distance(first, last);
2.332 + Key* buffer = allocator.allocate(2 * length);
2.333 + try {
2.334 + bool dir = true;
2.335 + std::copy(first, last, buffer);
2.336 + for (int i = 0; i < int(sizeof(Value)) - 1; ++i) {
2.337 + if (dir) {
2.338 + counterIntroSort(buffer, buffer + length, buffer + length,
2.339 + i, functor);
2.340 + } else {
2.341 + counterIntroSort(buffer + length, buffer + 2 * length, buffer,
2.342 + i, functor);
2.343 + }
2.344 + dir = !dir;
2.345 + }
2.346 + if (dir) {
2.347 + signedCounterIntroSort(buffer, buffer + length, buffer + length,
2.348 + sizeof(Value) - 1, functor);
2.349 + std::copy(buffer + length, buffer + 2 * length, first);
2.350 + } else {
2.351 + signedCounterIntroSort(buffer + length, buffer + 2 * length, buffer,
2.352 + sizeof(Value) - 1, functor);
2.353 + std::copy(buffer, buffer + length, first);
2.354 + }
2.355 + } catch (...) {
2.356 + allocator.deallocate(buffer, 2 * length);
2.357 + throw;
2.358 + }
2.359 + allocator.deallocate(buffer, 2 * length);
2.360 + }
2.361 +
2.362 + template <typename Value, typename Iterator, typename Functor>
2.363 + void counterUnsignedSort(Iterator first, Iterator last, Functor functor) {
2.364 + if (first == last) return;
2.365 + typedef typename std::iterator_traits<Iterator>::value_type Key;
2.366 + typedef std::allocator<Key> Allocator;
2.367 + Allocator allocator;
2.368 +
2.369 + int length = std::distance(first, last);
2.370 + Key *buffer = allocator.allocate(2 * length);
2.371 + try {
2.372 + bool dir = true;
2.373 + std::copy(first, last, buffer);
2.374 + for (int i = 0; i < int(sizeof(Value)); ++i) {
2.375 + if (dir) {
2.376 + counterIntroSort(buffer, buffer + length,
2.377 + buffer + length, i, functor);
2.378 + } else {
2.379 + counterIntroSort(buffer + length, buffer + 2 * length,
2.380 + buffer, i, functor);
2.381 + }
2.382 + dir = !dir;
2.383 + }
2.384 + if (dir) {
2.385 + std::copy(buffer, buffer + length, first);
2.386 + } else {
2.387 + std::copy(buffer + length, buffer + 2 * length, first);
2.388 + }
2.389 + } catch (...) {
2.390 + allocator.deallocate(buffer, 2 * length);
2.391 + throw;
2.392 + }
2.393 + allocator.deallocate(buffer, 2 * length);
2.394 + }
2.395 +
2.396 +
2.397 +
2.398 + template <typename Value,
2.399 + bool sign = std::numeric_limits<Value>::is_signed >
2.400 + struct CounterSortSelector {
2.401 + template <typename Iterator, typename Functor>
2.402 + static void sort(Iterator first, Iterator last, Functor functor) {
2.403 + counterSignedSort<Value>(first, last, functor);
2.404 + }
2.405 + };
2.406 +
2.407 + template <typename Value>
2.408 + struct CounterSortSelector<Value, false> {
2.409 + template <typename Iterator, typename Functor>
2.410 + static void sort(Iterator first, Iterator last, Functor functor) {
2.411 + counterUnsignedSort<Value>(first, last, functor);
2.412 + }
2.413 + };
2.414 +
2.415 + }
2.416 +
2.417 + /// \ingroup auxalg
2.418 + ///
2.419 + /// \brief Sorts stable the STL compatible range into ascending order.
2.420 + ///
2.421 + /// The \c counterSort sorts the STL compatible range into ascending
2.422 + /// order. The counter sort algorithm can sort the items which
2.423 + /// mapped to an integer with an adaptable unary function \c functor
2.424 + /// and the order will be ascending by these mapped values. As
2.425 + /// function specialization it is possible to use a normal function
2.426 + /// instead of the functor object or if the functor is not given it
2.427 + /// will use an identity function instead.
2.428 + ///
2.429 + /// The implemented counter sort use a radix forward sort on the
2.430 + /// bytes of the integer number. The algorithm sorts the items
2.431 + /// byte-by-byte, first it counts how many times occurs a byte value
2.432 + /// in the containerm, and with the occurence number the container
2.433 + /// can be copied to an other in asceding order in \c O(n) time.
2.434 + /// Let be \c c the maximal capacity of the integer type and \c n
2.435 + /// the number of the items in the container, the time complexity of
2.436 + /// the algorithm is \f$ O(\log(c)n) \f$ and the additional space
2.437 + /// complexity is \f$ O(n) \f$.
2.438 + ///
2.439 + /// The sorting algorithm is stable, i.e. the order of two equal
2.440 + /// element remains the same.
2.441 + ///
2.442 + /// \param first The begin of the given range.
2.443 + /// \param last The end of the given range.
2.444 + /// \param functor An adaptible unary function or a normal function
2.445 + /// which maps the items to any integer type which can be either
2.446 + /// signed or unsigned.
2.447 + template <typename Iterator, typename Functor>
2.448 + void counterSort(Iterator first, Iterator last, Functor functor) {
2.449 + using namespace _radix_sort_bits;
2.450 + typedef typename Functor::result_type Value;
2.451 + CounterSortSelector<Value>::sort(first, last, functor);
2.452 + }
2.453 +
2.454 + template <typename Iterator, typename Value, typename Key>
2.455 + void counterSort(Iterator first, Iterator last, Value (*functor)(Key)) {
2.456 + using namespace _radix_sort_bits;
2.457 + CounterSortSelector<Value>::sort(first, last, functor);
2.458 + }
2.459 +
2.460 + template <typename Iterator, typename Value, typename Key>
2.461 + void counterSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
2.462 + using namespace _radix_sort_bits;
2.463 + CounterSortSelector<Value>::sort(first, last, functor);
2.464 + }
2.465 +
2.466 + template <typename Iterator, typename Value, typename Key>
2.467 + void counterSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
2.468 + using namespace _radix_sort_bits;
2.469 + CounterSortSelector<Value>::sort(first, last, functor);
2.470 + }
2.471 +
2.472 + template <typename Iterator, typename Value, typename Key>
2.473 + void counterSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
2.474 + using namespace _radix_sort_bits;
2.475 + CounterSortSelector<Value>::sort(first, last, functor);
2.476 + }
2.477 +
2.478 + template <typename Iterator>
2.479 + void counterSort(Iterator first, Iterator last) {
2.480 + using namespace _radix_sort_bits;
2.481 + typedef typename std::iterator_traits<Iterator>::value_type Value;
2.482 + CounterSortSelector<Value>::sort(first, last, Identity<Value>());
2.483 + }
2.484 +
2.485 +}
2.486 +
2.487 +#endif
3.1 --- a/test/CMakeLists.txt Sun Oct 12 19:35:48 2008 +0100
3.2 +++ b/test/CMakeLists.txt Fri Oct 17 23:55:18 2008 +0200
3.3 @@ -16,6 +16,7 @@
3.4 heap_test
3.5 kruskal_test
3.6 maps_test
3.7 + radix_sort_test
3.8 random_test
3.9 path_test
3.10 time_measure_test
4.1 --- a/test/Makefile.am Sun Oct 12 19:35:48 2008 +0100
4.2 +++ b/test/Makefile.am Fri Oct 17 23:55:18 2008 +0200
4.3 @@ -19,6 +19,7 @@
4.4 test/heap_test \
4.5 test/kruskal_test \
4.6 test/maps_test \
4.7 + test/radix_sort_test \
4.8 test/random_test \
4.9 test/path_test \
4.10 test/test_tools_fail \
4.11 @@ -43,6 +44,7 @@
4.12 test_kruskal_test_SOURCES = test/kruskal_test.cc
4.13 test_maps_test_SOURCES = test/maps_test.cc
4.14 test_path_test_SOURCES = test/path_test.cc
4.15 +test_radix_sort_test_SOURCES = test/radix_sort_test.cc
4.16 test_random_test_SOURCES = test/random_test.cc
4.17 test_test_tools_fail_SOURCES = test/test_tools_fail.cc
4.18 test_test_tools_pass_SOURCES = test/test_tools_pass.cc
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/test/radix_sort_test.cc Fri Oct 17 23:55:18 2008 +0200
5.3 @@ -0,0 +1,147 @@
5.4 +/* -*- C++ -*-
5.5 + *
5.6 + * This file is a part of LEMON, a generic C++ optimization library
5.7 + *
5.8 + * Copyright (C) 2003-2008
5.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
5.11 + *
5.12 + * Permission to use, modify and distribute this software is granted
5.13 + * provided that this copyright notice appears in all copies. For
5.14 + * precise terms see the accompanying LICENSE file.
5.15 + *
5.16 + * This software is provided "AS IS" with no warranty of any kind,
5.17 + * express or implied, and with no claim as to its suitability for any
5.18 + * purpose.
5.19 + *
5.20 + */
5.21 +
5.22 +#include <lemon/time_measure.h>
5.23 +#include <lemon/smart_graph.h>
5.24 +#include <lemon/maps.h>
5.25 +#include <lemon/radix_sort.h>
5.26 +#include <lemon/math.h>
5.27 +
5.28 +#include "test_tools.h"
5.29 +
5.30 +#include <vector>
5.31 +#include <algorithm>
5.32 +
5.33 +using namespace lemon;
5.34 +
5.35 +static const int n = 10000;
5.36 +
5.37 +struct Negate {
5.38 + typedef int argument_type;
5.39 + typedef int result_type;
5.40 + int operator()(int a) { return - a; }
5.41 +};
5.42 +
5.43 +int negate(int a) { return - a; }
5.44 +
5.45 +
5.46 +void generateIntSequence(int n, std::vector<int>& data) {
5.47 + int prime = 9973;
5.48 + int root = 136, value = 1;
5.49 + for (int i = 0; i < n; ++i) {
5.50 + data.push_back(value - prime / 2);
5.51 + value = (value * root) % prime;
5.52 + }
5.53 +}
5.54 +
5.55 +void generateCharSequence(int n, std::vector<unsigned char>& data) {
5.56 + int prime = 251;
5.57 + int root = 3, value = root;
5.58 + for (int i = 0; i < n; ++i) {
5.59 + data.push_back(static_cast<unsigned char>(value));
5.60 + value = (value * root) % prime;
5.61 + }
5.62 +}
5.63 +
5.64 +void checkRadixSort() {
5.65 + {
5.66 + std::vector<int> data1;
5.67 + generateIntSequence(n, data1);
5.68 +
5.69 + std::vector<int> data2(data1);
5.70 + std::sort(data1.begin(), data1.end());
5.71 +
5.72 + radixSort(data2.begin(), data2.end());
5.73 + for (int i = 0; i < n; ++i) {
5.74 + check(data1[i] == data2[i], "Test failed");
5.75 + }
5.76 +
5.77 + radixSort(data2.begin(), data2.end(), Negate());
5.78 + for (int i = 0; i < n; ++i) {
5.79 + check(data1[i] == data2[n - 1 - i], "Test failed");
5.80 + }
5.81 +
5.82 + radixSort(data2.begin(), data2.end(), negate);
5.83 + for (int i = 0; i < n; ++i) {
5.84 + check(data1[i] == data2[n - 1 - i], "Test failed");
5.85 + }
5.86 +
5.87 + }
5.88 +
5.89 + {
5.90 + std::vector<unsigned char> data1(n);
5.91 + generateCharSequence(n, data1);
5.92 +
5.93 + std::vector<unsigned char> data2(data1);
5.94 + std::sort(data1.begin(), data1.end());
5.95 +
5.96 + radixSort(data2.begin(), data2.end());
5.97 + for (int i = 0; i < n; ++i) {
5.98 + check(data1[i] == data2[i], "Test failed");
5.99 + }
5.100 +
5.101 + }
5.102 +}
5.103 +
5.104 +
5.105 +void checkCounterSort() {
5.106 + {
5.107 + std::vector<int> data1;
5.108 + generateIntSequence(n, data1);
5.109 +
5.110 + std::vector<int> data2(data1);
5.111 + std::sort(data1.begin(), data1.end());
5.112 +
5.113 + counterSort(data2.begin(), data2.end());
5.114 + for (int i = 0; i < n; ++i) {
5.115 + check(data1[i] == data2[i], "Test failed");
5.116 + }
5.117 +
5.118 + counterSort(data2.begin(), data2.end(), Negate());
5.119 + for (int i = 0; i < n; ++i) {
5.120 + check(data1[i] == data2[n - 1 - i], "Test failed");
5.121 + }
5.122 +
5.123 + counterSort(data2.begin(), data2.end(), negate);
5.124 + for (int i = 0; i < n; ++i) {
5.125 + check(data1[i] == data2[n - 1 - i], "Test failed");
5.126 + }
5.127 + }
5.128 +
5.129 + {
5.130 + std::vector<unsigned char> data1(n);
5.131 + generateCharSequence(n, data1);
5.132 +
5.133 + std::vector<unsigned char> data2(data1);
5.134 + std::sort(data1.begin(), data1.end());
5.135 +
5.136 + radixSort(data2.begin(), data2.end());
5.137 + for (int i = 0; i < n; ++i) {
5.138 + check(data1[i] == data2[i], "Test failed");
5.139 + }
5.140 +
5.141 + }
5.142 +}
5.143 +
5.144 +int main() {
5.145 +
5.146 + checkRadixSort();
5.147 + checkCounterSort();
5.148 +
5.149 + return 0;
5.150 +}