Merge
authorAlpar Juttner <alpar@cs.elte.hu>
Thu, 08 Jan 2009 17:19:26 +0000
changeset 44575a5df083951
parent 440 88ed40ad0d4f
parent 444 ba49101c9b07
child 455 5a1e9fdcfd3a
Merge
lemon/Makefile.am
test/CMakeLists.txt
test/Makefile.am
     1.1 --- a/lemon/Makefile.am	Thu Jan 01 00:00:00 2009 +0100
     1.2 +++ b/lemon/Makefile.am	Thu Jan 08 17:19:26 2009 +0000
     1.3 @@ -47,6 +47,7 @@
     1.4  	lemon/nauty_reader.h \
     1.5  	lemon/path.h \
     1.6  	lemon/preflow.h \
     1.7 +	lemon/radix_sort.h \
     1.8  	lemon/random.h \
     1.9  	lemon/smart_graph.h \
    1.10  	lemon/suurballe.h \
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/lemon/radix_sort.h	Thu Jan 08 17:19:26 2009 +0000
     2.3 @@ -0,0 +1,487 @@
     2.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2.5 + *
     2.6 + * This file is a part of LEMON, a generic C++ optimization library.
     2.7 + *
     2.8 + * Copyright (C) 2003-2009
     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 an STL compatible range into ascending
   2.202 +  /// order.  The radix sort algorithm can sort items which are mapped
   2.203 +  /// to integers with an adaptable unary function \c functor and the
   2.204 +  /// order will be ascending according to these mapped values.
   2.205 +  ///
   2.206 +  /// It is also possible to use a normal function instead
   2.207 +  /// of the functor object. If the functor is not given it will use
   2.208 +  /// the identity function instead.
   2.209 +  ///
   2.210 +  /// This is a special quick sort algorithm where the pivot
   2.211 +  /// values to split the items are choosen to be \f$ 2^k \f$ for each \c k.
   2.212 +  /// Therefore, the time complexity of the
   2.213 +  /// algorithm is \f$ O(\log(c)n) \f$ and it uses \f$ O(\log(c)) \f$,
   2.214 +  /// additional space, where \c c is the maximal value and \c n is the
   2.215 +  /// number of the items in the container.
   2.216 +  ///
   2.217 +  /// \param first The begin of the given range.
   2.218 +  /// \param last The end of the given range.
   2.219 +  /// \param functor An adaptible unary function or a normal function
   2.220 +  /// which maps the items to any integer type which can be either
   2.221 +  /// signed or unsigned.
   2.222 +  ///
   2.223 +  /// \sa stableRadixSort()
   2.224 +  template <typename Iterator, typename Functor>
   2.225 +  void radixSort(Iterator first, Iterator last, Functor functor) {
   2.226 +    using namespace _radix_sort_bits;
   2.227 +    typedef typename Functor::result_type Value;
   2.228 +    RadixSortSelector<Value>::sort(first, last, functor);
   2.229 +  }
   2.230 +
   2.231 +  template <typename Iterator, typename Value, typename Key>
   2.232 +  void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
   2.233 +    using namespace _radix_sort_bits;
   2.234 +    RadixSortSelector<Value>::sort(first, last, functor);
   2.235 +  }
   2.236 +
   2.237 +  template <typename Iterator, typename Value, typename Key>
   2.238 +  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
   2.239 +    using namespace _radix_sort_bits;
   2.240 +    RadixSortSelector<Value>::sort(first, last, functor);
   2.241 +  }
   2.242 +
   2.243 +  template <typename Iterator, typename Value, typename Key>
   2.244 +  void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
   2.245 +    using namespace _radix_sort_bits;
   2.246 +    RadixSortSelector<Value>::sort(first, last, functor);
   2.247 +  }
   2.248 +
   2.249 +  template <typename Iterator, typename Value, typename Key>
   2.250 +  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
   2.251 +    using namespace _radix_sort_bits;
   2.252 +    RadixSortSelector<Value>::sort(first, last, functor);
   2.253 +  }
   2.254 +
   2.255 +  template <typename Iterator>
   2.256 +  void radixSort(Iterator first, Iterator last) {
   2.257 +    using namespace _radix_sort_bits;
   2.258 +    typedef typename std::iterator_traits<Iterator>::value_type Value;
   2.259 +    RadixSortSelector<Value>::sort(first, last, Identity<Value>());
   2.260 +  }
   2.261 +
   2.262 +  namespace _radix_sort_bits {
   2.263 +
   2.264 +    template <typename Value>
   2.265 +    unsigned char valueByte(Value value, int byte) {
   2.266 +      return value >> (std::numeric_limits<unsigned char>::digits * byte);
   2.267 +    }
   2.268 +
   2.269 +    template <typename Functor, typename Key>
   2.270 +    void stableRadixIntroSort(Key *first, Key *last, Key *target,
   2.271 +                              int byte, Functor functor) {
   2.272 +      const int size =
   2.273 +        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
   2.274 +      std::vector<int> counter(size);
   2.275 +      for (int i = 0; i < size; ++i) {
   2.276 +        counter[i] = 0;
   2.277 +      }
   2.278 +      Key *it = first;
   2.279 +      while (first != last) {
   2.280 +        ++counter[valueByte(functor(*first), byte)];
   2.281 +        ++first;
   2.282 +      }
   2.283 +      int prev, num = 0;
   2.284 +      for (int i = 0; i < size; ++i) {
   2.285 +        prev = num;
   2.286 +        num += counter[i];
   2.287 +        counter[i] = prev;
   2.288 +      }
   2.289 +      while (it != last) {
   2.290 +        target[counter[valueByte(functor(*it), byte)]++] = *it;
   2.291 +        ++it;
   2.292 +      }
   2.293 +    }
   2.294 +
   2.295 +    template <typename Functor, typename Key>
   2.296 +    void signedStableRadixIntroSort(Key *first, Key *last, Key *target,
   2.297 +                                    int byte, Functor functor) {
   2.298 +      const int size =
   2.299 +        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
   2.300 +      std::vector<int> counter(size);
   2.301 +      for (int i = 0; i < size; ++i) {
   2.302 +        counter[i] = 0;
   2.303 +      }
   2.304 +      Key *it = first;
   2.305 +      while (first != last) {
   2.306 +        counter[valueByte(functor(*first), byte)]++;
   2.307 +        ++first;
   2.308 +      }
   2.309 +      int prev, num = 0;
   2.310 +      for (int i = size / 2; i < size; ++i) {
   2.311 +        prev = num;
   2.312 +        num += counter[i];
   2.313 +        counter[i] = prev;
   2.314 +      }
   2.315 +      for (int i = 0; i < size / 2; ++i) {
   2.316 +        prev = num;
   2.317 +        num += counter[i];
   2.318 +        counter[i] = prev;
   2.319 +      }
   2.320 +      while (it != last) {
   2.321 +        target[counter[valueByte(functor(*it), byte)]++] = *it;
   2.322 +        ++it;
   2.323 +      }
   2.324 +    }
   2.325 +
   2.326 +
   2.327 +    template <typename Value, typename Iterator, typename Functor>
   2.328 +    void stableRadixSignedSort(Iterator first, Iterator last, Functor functor) {
   2.329 +      if (first == last) return;
   2.330 +      typedef typename std::iterator_traits<Iterator>::value_type Key;
   2.331 +      typedef std::allocator<Key> Allocator;
   2.332 +      Allocator allocator;
   2.333 +
   2.334 +      int length = std::distance(first, last);
   2.335 +      Key* buffer = allocator.allocate(2 * length);
   2.336 +      try {
   2.337 +        bool dir = true;
   2.338 +        std::copy(first, last, buffer);
   2.339 +        for (int i = 0; i < int(sizeof(Value)) - 1; ++i) {
   2.340 +          if (dir) {
   2.341 +            stableRadixIntroSort(buffer, buffer + length, buffer + length,
   2.342 +                                 i, functor);
   2.343 +          } else {
   2.344 +            stableRadixIntroSort(buffer + length, buffer + 2 * length, buffer,
   2.345 +                                 i, functor);
   2.346 +          }
   2.347 +          dir = !dir;
   2.348 +        }
   2.349 +        if (dir) {
   2.350 +          signedStableRadixIntroSort(buffer, buffer + length, buffer + length,
   2.351 +                                     sizeof(Value) - 1, functor);
   2.352 +          std::copy(buffer + length, buffer + 2 * length, first);
   2.353 +        }        else {
   2.354 +          signedStableRadixIntroSort(buffer + length, buffer + 2 * length,
   2.355 +                                     buffer, sizeof(Value) - 1, functor);
   2.356 +          std::copy(buffer, buffer + length, first);
   2.357 +        }
   2.358 +      } catch (...) {
   2.359 +        allocator.deallocate(buffer, 2 * length);
   2.360 +        throw;
   2.361 +      }
   2.362 +      allocator.deallocate(buffer, 2 * length);
   2.363 +    }
   2.364 +
   2.365 +    template <typename Value, typename Iterator, typename Functor>
   2.366 +    void stableRadixUnsignedSort(Iterator first, Iterator last,
   2.367 +                                 Functor functor) {
   2.368 +      if (first == last) return;
   2.369 +      typedef typename std::iterator_traits<Iterator>::value_type Key;
   2.370 +      typedef std::allocator<Key> Allocator;
   2.371 +      Allocator allocator;
   2.372 +
   2.373 +      int length = std::distance(first, last);
   2.374 +      Key *buffer = allocator.allocate(2 * length);
   2.375 +      try {
   2.376 +        bool dir = true;
   2.377 +        std::copy(first, last, buffer);
   2.378 +        for (int i = 0; i < int(sizeof(Value)); ++i) {
   2.379 +          if (dir) {
   2.380 +            stableRadixIntroSort(buffer, buffer + length,
   2.381 +                                 buffer + length, i, functor);
   2.382 +          } else {
   2.383 +            stableRadixIntroSort(buffer + length, buffer + 2 * length,
   2.384 +                                 buffer, i, functor);
   2.385 +          }
   2.386 +          dir = !dir;
   2.387 +        }
   2.388 +        if (dir) {
   2.389 +          std::copy(buffer, buffer + length, first);
   2.390 +        }        else {
   2.391 +          std::copy(buffer + length, buffer + 2 * length, first);
   2.392 +        }
   2.393 +      } catch (...) {
   2.394 +        allocator.deallocate(buffer, 2 * length);
   2.395 +        throw;
   2.396 +      }
   2.397 +      allocator.deallocate(buffer, 2 * length);
   2.398 +    }
   2.399 +
   2.400 +
   2.401 +
   2.402 +    template <typename Value,
   2.403 +              bool sign = std::numeric_limits<Value>::is_signed >
   2.404 +    struct StableRadixSortSelector {
   2.405 +      template <typename Iterator, typename Functor>
   2.406 +      static void sort(Iterator first, Iterator last, Functor functor) {
   2.407 +        stableRadixSignedSort<Value>(first, last, functor);
   2.408 +      }
   2.409 +    };
   2.410 +
   2.411 +    template <typename Value>
   2.412 +    struct StableRadixSortSelector<Value, false> {
   2.413 +      template <typename Iterator, typename Functor>
   2.414 +      static void sort(Iterator first, Iterator last, Functor functor) {
   2.415 +        stableRadixUnsignedSort<Value>(first, last, functor);
   2.416 +      }
   2.417 +    };
   2.418 +
   2.419 +  }
   2.420 +
   2.421 +  /// \ingroup auxalg
   2.422 +  ///
   2.423 +  /// \brief Sorts the STL compatible range into ascending order in a stable
   2.424 +  /// way.
   2.425 +  ///
   2.426 +  /// This function sorts an STL compatible range into ascending
   2.427 +  /// order according to an integer mapping in the same as radixSort() does.
   2.428 +  ///
   2.429 +  /// This sorting algorithm is stable, i.e. the order of two equal
   2.430 +  /// elements remains the same after the sorting.
   2.431 +  ///
   2.432 +  /// This sort algorithm  use a radix forward sort on the
   2.433 +  /// bytes of the integer number. The algorithm sorts the items
   2.434 +  /// byte-by-byte. First, it counts how many times a byte value occurs
   2.435 +  /// in the container, then it copies the corresponding items to
   2.436 +  /// another container in asceding order in \c O(n) time.
   2.437 +  ///
   2.438 +  /// The time complexity of the algorithm is \f$ O(\log(c)n) \f$ and
   2.439 +  /// it uses \f$ O(n) \f$, additional space, where \c c is the
   2.440 +  /// maximal value and \c n is the number of the items in the
   2.441 +  /// container.
   2.442 +  ///
   2.443 +
   2.444 +  /// \param first The begin of the given range.
   2.445 +  /// \param last The end of the given range.
   2.446 +  /// \param functor An adaptible unary function or a normal function
   2.447 +  /// which maps the items to any integer type which can be either
   2.448 +  /// signed or unsigned.
   2.449 +  /// \sa radixSort()
   2.450 +  template <typename Iterator, typename Functor>
   2.451 +  void stableRadixSort(Iterator first, Iterator last, Functor functor) {
   2.452 +    using namespace _radix_sort_bits;
   2.453 +    typedef typename Functor::result_type Value;
   2.454 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   2.455 +  }
   2.456 +
   2.457 +  template <typename Iterator, typename Value, typename Key>
   2.458 +  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
   2.459 +    using namespace _radix_sort_bits;
   2.460 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   2.461 +  }
   2.462 +
   2.463 +  template <typename Iterator, typename Value, typename Key>
   2.464 +  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
   2.465 +    using namespace _radix_sort_bits;
   2.466 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   2.467 +  }
   2.468 +
   2.469 +  template <typename Iterator, typename Value, typename Key>
   2.470 +  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
   2.471 +    using namespace _radix_sort_bits;
   2.472 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   2.473 +  }
   2.474 +
   2.475 +  template <typename Iterator, typename Value, typename Key>
   2.476 +  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
   2.477 +    using namespace _radix_sort_bits;
   2.478 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   2.479 +  }
   2.480 +
   2.481 +  template <typename Iterator>
   2.482 +  void stableRadixSort(Iterator first, Iterator last) {
   2.483 +    using namespace _radix_sort_bits;
   2.484 +    typedef typename std::iterator_traits<Iterator>::value_type Value;
   2.485 +    StableRadixSortSelector<Value>::sort(first, last, Identity<Value>());
   2.486 +  }
   2.487 +
   2.488 +}
   2.489 +
   2.490 +#endif
     3.1 --- a/test/CMakeLists.txt	Thu Jan 01 00:00:00 2009 +0100
     3.2 +++ b/test/CMakeLists.txt	Thu Jan 08 17:19:26 2009 +0000
     3.3 @@ -20,6 +20,7 @@
     3.4    kruskal_test
     3.5    maps_test
     3.6    max_matching_test
     3.7 +  radix_sort_test
     3.8    path_test
     3.9    preflow_test
    3.10    random_test
     4.1 --- a/test/Makefile.am	Thu Jan 01 00:00:00 2009 +0100
     4.2 +++ b/test/Makefile.am	Thu Jan 08 17:19:26 2009 +0000
     4.3 @@ -25,6 +25,7 @@
     4.4  	test/max_matching_test \
     4.5  	test/path_test \
     4.6  	test/preflow_test \
     4.7 +	test/radix_sort_test \
     4.8  	test/random_test \
     4.9  	test/suurballe_test \
    4.10  	test/test_tools_fail \
    4.11 @@ -54,6 +55,7 @@
    4.12  test_max_matching_test_SOURCES = test/max_matching_test.cc
    4.13  test_path_test_SOURCES = test/path_test.cc
    4.14  test_preflow_test_SOURCES = test/preflow_test.cc
    4.15 +test_radix_sort_test_SOURCES = test/radix_sort_test.cc
    4.16  test_suurballe_test_SOURCES = test/suurballe_test.cc
    4.17  test_random_test_SOURCES = test/random_test.cc
    4.18  test_test_tools_fail_SOURCES = test/test_tools_fail.cc
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/radix_sort_test.cc	Thu Jan 08 17:19:26 2009 +0000
     5.3 @@ -0,0 +1,147 @@
     5.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     5.5 + *
     5.6 + * This file is a part of LEMON, a generic C++ optimization library.
     5.7 + *
     5.8 + * Copyright (C) 2003-2009
     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 checkStableRadixSort() {
   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 +    stableRadixSort(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 +    stableRadixSort(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 +    stableRadixSort(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 +  checkStableRadixSort();
   5.148 +
   5.149 +  return 0;
   5.150 +}