lemon/radix_sort.h
changeset 802 994c7df296c9
parent 444 ba49101c9b07
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/radix_sort.h	Thu Dec 10 17:05:35 2009 +0100
     1.3 @@ -0,0 +1,487 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2009
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef RADIX_SORT_H
    1.23 +#define RADIX_SORT_H
    1.24 +
    1.25 +/// \ingroup auxalg
    1.26 +/// \file
    1.27 +/// \brief Radix sort
    1.28 +///
    1.29 +/// Linear time sorting algorithms
    1.30 +
    1.31 +#include <vector>
    1.32 +#include <limits>
    1.33 +#include <iterator>
    1.34 +#include <algorithm>
    1.35 +
    1.36 +namespace lemon {
    1.37 +
    1.38 +  namespace _radix_sort_bits {
    1.39 +
    1.40 +    template <typename Value>
    1.41 +    struct Identity {
    1.42 +      const Value& operator()(const Value& val) {
    1.43 +        return val;
    1.44 +      }
    1.45 +    };
    1.46 +
    1.47 +
    1.48 +    template <typename Value, typename Iterator, typename Functor>
    1.49 +    Iterator radixSortPartition(Iterator first, Iterator last,
    1.50 +                                Functor functor, Value mask) {
    1.51 +      while (first != last && !(functor(*first) & mask)) {
    1.52 +        ++first;
    1.53 +      }
    1.54 +      if (first == last) {
    1.55 +        return first;
    1.56 +      }
    1.57 +      --last;
    1.58 +      while (first != last && (functor(*last) & mask)) {
    1.59 +        --last;
    1.60 +      }
    1.61 +      if (first == last) {
    1.62 +        return first;
    1.63 +      }
    1.64 +      std::iter_swap(first, last);
    1.65 +      ++first;
    1.66 +      if (!(first < last)) {
    1.67 +        return first;
    1.68 +      }
    1.69 +      while (true) {
    1.70 +        while (!(functor(*first) & mask)) {
    1.71 +          ++first;
    1.72 +        }
    1.73 +        --last;
    1.74 +        while (functor(*last) & mask) {
    1.75 +          --last;
    1.76 +        }
    1.77 +        if (!(first < last)) {
    1.78 +          return first;
    1.79 +        }
    1.80 +        std::iter_swap(first, last);
    1.81 +        ++first;
    1.82 +      }
    1.83 +    }
    1.84 +
    1.85 +    template <typename Iterator, typename Functor>
    1.86 +    Iterator radixSortSignPartition(Iterator first, Iterator last,
    1.87 +                                    Functor functor) {
    1.88 +      while (first != last && functor(*first) < 0) {
    1.89 +        ++first;
    1.90 +      }
    1.91 +      if (first == last) {
    1.92 +        return first;
    1.93 +      }
    1.94 +      --last;
    1.95 +      while (first != last && functor(*last) >= 0) {
    1.96 +        --last;
    1.97 +      }
    1.98 +      if (first == last) {
    1.99 +        return first;
   1.100 +      }
   1.101 +      std::iter_swap(first, last);
   1.102 +      ++first;
   1.103 +      if (!(first < last)) {
   1.104 +        return first;
   1.105 +      }
   1.106 +      while (true) {
   1.107 +        while (functor(*first) < 0) {
   1.108 +          ++first;
   1.109 +        }
   1.110 +        --last;
   1.111 +        while (functor(*last) >= 0) {
   1.112 +          --last;
   1.113 +        }
   1.114 +        if (!(first < last)) {
   1.115 +          return first;
   1.116 +        }
   1.117 +        std::iter_swap(first, last);
   1.118 +        ++first;
   1.119 +      }
   1.120 +    }
   1.121 +
   1.122 +    template <typename Value, typename Iterator, typename Functor>
   1.123 +    void radixIntroSort(Iterator first, Iterator last,
   1.124 +                        Functor functor, Value mask) {
   1.125 +      while (mask != 0 && last - first > 1) {
   1.126 +        Iterator cut = radixSortPartition(first, last, functor, mask);
   1.127 +        mask >>= 1;
   1.128 +        radixIntroSort(first, cut, functor, mask);
   1.129 +        first = cut;
   1.130 +      }
   1.131 +    }
   1.132 +
   1.133 +    template <typename Value, typename Iterator, typename Functor>
   1.134 +    void radixSignedSort(Iterator first, Iterator last, Functor functor) {
   1.135 +
   1.136 +      Iterator cut = radixSortSignPartition(first, last, functor);
   1.137 +
   1.138 +      Value mask;
   1.139 +      int max_digit;
   1.140 +      Iterator it;
   1.141 +
   1.142 +      mask = ~0; max_digit = 0;
   1.143 +      for (it = first; it != cut; ++it) {
   1.144 +        while ((mask & functor(*it)) != mask) {
   1.145 +          ++max_digit;
   1.146 +          mask <<= 1;
   1.147 +        }
   1.148 +      }
   1.149 +      radixIntroSort(first, cut, functor, 1 << max_digit);
   1.150 +
   1.151 +      mask = 0; max_digit = 0;
   1.152 +      for (it = cut; it != last; ++it) {
   1.153 +        while ((mask | functor(*it)) != mask) {
   1.154 +          ++max_digit;
   1.155 +          mask <<= 1; mask |= 1;
   1.156 +        }
   1.157 +      }
   1.158 +      radixIntroSort(cut, last, functor, 1 << max_digit);
   1.159 +    }
   1.160 +
   1.161 +    template <typename Value, typename Iterator, typename Functor>
   1.162 +    void radixUnsignedSort(Iterator first, Iterator last, Functor functor) {
   1.163 +
   1.164 +      Value mask = 0;
   1.165 +      int max_digit = 0;
   1.166 +
   1.167 +      Iterator it;
   1.168 +      for (it = first; it != last; ++it) {
   1.169 +        while ((mask | functor(*it)) != mask) {
   1.170 +          ++max_digit;
   1.171 +          mask <<= 1; mask |= 1;
   1.172 +        }
   1.173 +      }
   1.174 +      radixIntroSort(first, last, functor, 1 << max_digit);
   1.175 +    }
   1.176 +
   1.177 +
   1.178 +    template <typename Value,
   1.179 +              bool sign = std::numeric_limits<Value>::is_signed >
   1.180 +    struct RadixSortSelector {
   1.181 +      template <typename Iterator, typename Functor>
   1.182 +      static void sort(Iterator first, Iterator last, Functor functor) {
   1.183 +        radixSignedSort<Value>(first, last, functor);
   1.184 +      }
   1.185 +    };
   1.186 +
   1.187 +    template <typename Value>
   1.188 +    struct RadixSortSelector<Value, false> {
   1.189 +      template <typename Iterator, typename Functor>
   1.190 +      static void sort(Iterator first, Iterator last, Functor functor) {
   1.191 +        radixUnsignedSort<Value>(first, last, functor);
   1.192 +      }
   1.193 +    };
   1.194 +
   1.195 +  }
   1.196 +
   1.197 +  /// \ingroup auxalg
   1.198 +  ///
   1.199 +  /// \brief Sorts the STL compatible range into ascending order.
   1.200 +  ///
   1.201 +  /// The \c radixSort sorts an STL compatible range into ascending
   1.202 +  /// order.  The radix sort algorithm can sort items which are mapped
   1.203 +  /// to integers with an adaptable unary function \c functor and the
   1.204 +  /// order will be ascending according to these mapped values.
   1.205 +  ///
   1.206 +  /// It is also possible to use a normal function instead
   1.207 +  /// of the functor object. If the functor is not given it will use
   1.208 +  /// the identity function instead.
   1.209 +  ///
   1.210 +  /// This is a special quick sort algorithm where the pivot
   1.211 +  /// values to split the items are choosen to be 2<sup>k</sup>
   1.212 +  /// for each \c k.
   1.213 +  /// Therefore, the time complexity of the algorithm is O(log(c)*n) and
   1.214 +  /// it uses O(log(c)) additional space, where \c c is the maximal value
   1.215 +  /// and \c n is the number of the items in the container.
   1.216 +  ///
   1.217 +  /// \param first The begin of the given range.
   1.218 +  /// \param last The end of the given range.
   1.219 +  /// \param functor An adaptible unary function or a normal function
   1.220 +  /// which maps the items to any integer type which can be either
   1.221 +  /// signed or unsigned.
   1.222 +  ///
   1.223 +  /// \sa stableRadixSort()
   1.224 +  template <typename Iterator, typename Functor>
   1.225 +  void radixSort(Iterator first, Iterator last, Functor functor) {
   1.226 +    using namespace _radix_sort_bits;
   1.227 +    typedef typename Functor::result_type Value;
   1.228 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.229 +  }
   1.230 +
   1.231 +  template <typename Iterator, typename Value, typename Key>
   1.232 +  void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
   1.233 +    using namespace _radix_sort_bits;
   1.234 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.235 +  }
   1.236 +
   1.237 +  template <typename Iterator, typename Value, typename Key>
   1.238 +  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
   1.239 +    using namespace _radix_sort_bits;
   1.240 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.241 +  }
   1.242 +
   1.243 +  template <typename Iterator, typename Value, typename Key>
   1.244 +  void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
   1.245 +    using namespace _radix_sort_bits;
   1.246 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.247 +  }
   1.248 +
   1.249 +  template <typename Iterator, typename Value, typename Key>
   1.250 +  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
   1.251 +    using namespace _radix_sort_bits;
   1.252 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.253 +  }
   1.254 +
   1.255 +  template <typename Iterator>
   1.256 +  void radixSort(Iterator first, Iterator last) {
   1.257 +    using namespace _radix_sort_bits;
   1.258 +    typedef typename std::iterator_traits<Iterator>::value_type Value;
   1.259 +    RadixSortSelector<Value>::sort(first, last, Identity<Value>());
   1.260 +  }
   1.261 +
   1.262 +  namespace _radix_sort_bits {
   1.263 +
   1.264 +    template <typename Value>
   1.265 +    unsigned char valueByte(Value value, int byte) {
   1.266 +      return value >> (std::numeric_limits<unsigned char>::digits * byte);
   1.267 +    }
   1.268 +
   1.269 +    template <typename Functor, typename Key>
   1.270 +    void stableRadixIntroSort(Key *first, Key *last, Key *target,
   1.271 +                              int byte, Functor functor) {
   1.272 +      const int size =
   1.273 +        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
   1.274 +      std::vector<int> counter(size);
   1.275 +      for (int i = 0; i < size; ++i) {
   1.276 +        counter[i] = 0;
   1.277 +      }
   1.278 +      Key *it = first;
   1.279 +      while (first != last) {
   1.280 +        ++counter[valueByte(functor(*first), byte)];
   1.281 +        ++first;
   1.282 +      }
   1.283 +      int prev, num = 0;
   1.284 +      for (int i = 0; i < size; ++i) {
   1.285 +        prev = num;
   1.286 +        num += counter[i];
   1.287 +        counter[i] = prev;
   1.288 +      }
   1.289 +      while (it != last) {
   1.290 +        target[counter[valueByte(functor(*it), byte)]++] = *it;
   1.291 +        ++it;
   1.292 +      }
   1.293 +    }
   1.294 +
   1.295 +    template <typename Functor, typename Key>
   1.296 +    void signedStableRadixIntroSort(Key *first, Key *last, Key *target,
   1.297 +                                    int byte, Functor functor) {
   1.298 +      const int size =
   1.299 +        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
   1.300 +      std::vector<int> counter(size);
   1.301 +      for (int i = 0; i < size; ++i) {
   1.302 +        counter[i] = 0;
   1.303 +      }
   1.304 +      Key *it = first;
   1.305 +      while (first != last) {
   1.306 +        counter[valueByte(functor(*first), byte)]++;
   1.307 +        ++first;
   1.308 +      }
   1.309 +      int prev, num = 0;
   1.310 +      for (int i = size / 2; i < size; ++i) {
   1.311 +        prev = num;
   1.312 +        num += counter[i];
   1.313 +        counter[i] = prev;
   1.314 +      }
   1.315 +      for (int i = 0; i < size / 2; ++i) {
   1.316 +        prev = num;
   1.317 +        num += counter[i];
   1.318 +        counter[i] = prev;
   1.319 +      }
   1.320 +      while (it != last) {
   1.321 +        target[counter[valueByte(functor(*it), byte)]++] = *it;
   1.322 +        ++it;
   1.323 +      }
   1.324 +    }
   1.325 +
   1.326 +
   1.327 +    template <typename Value, typename Iterator, typename Functor>
   1.328 +    void stableRadixSignedSort(Iterator first, Iterator last, Functor functor) {
   1.329 +      if (first == last) return;
   1.330 +      typedef typename std::iterator_traits<Iterator>::value_type Key;
   1.331 +      typedef std::allocator<Key> Allocator;
   1.332 +      Allocator allocator;
   1.333 +
   1.334 +      int length = std::distance(first, last);
   1.335 +      Key* buffer = allocator.allocate(2 * length);
   1.336 +      try {
   1.337 +        bool dir = true;
   1.338 +        std::copy(first, last, buffer);
   1.339 +        for (int i = 0; i < int(sizeof(Value)) - 1; ++i) {
   1.340 +          if (dir) {
   1.341 +            stableRadixIntroSort(buffer, buffer + length, buffer + length,
   1.342 +                                 i, functor);
   1.343 +          } else {
   1.344 +            stableRadixIntroSort(buffer + length, buffer + 2 * length, buffer,
   1.345 +                                 i, functor);
   1.346 +          }
   1.347 +          dir = !dir;
   1.348 +        }
   1.349 +        if (dir) {
   1.350 +          signedStableRadixIntroSort(buffer, buffer + length, buffer + length,
   1.351 +                                     sizeof(Value) - 1, functor);
   1.352 +          std::copy(buffer + length, buffer + 2 * length, first);
   1.353 +        }        else {
   1.354 +          signedStableRadixIntroSort(buffer + length, buffer + 2 * length,
   1.355 +                                     buffer, sizeof(Value) - 1, functor);
   1.356 +          std::copy(buffer, buffer + length, first);
   1.357 +        }
   1.358 +      } catch (...) {
   1.359 +        allocator.deallocate(buffer, 2 * length);
   1.360 +        throw;
   1.361 +      }
   1.362 +      allocator.deallocate(buffer, 2 * length);
   1.363 +    }
   1.364 +
   1.365 +    template <typename Value, typename Iterator, typename Functor>
   1.366 +    void stableRadixUnsignedSort(Iterator first, Iterator last,
   1.367 +                                 Functor functor) {
   1.368 +      if (first == last) return;
   1.369 +      typedef typename std::iterator_traits<Iterator>::value_type Key;
   1.370 +      typedef std::allocator<Key> Allocator;
   1.371 +      Allocator allocator;
   1.372 +
   1.373 +      int length = std::distance(first, last);
   1.374 +      Key *buffer = allocator.allocate(2 * length);
   1.375 +      try {
   1.376 +        bool dir = true;
   1.377 +        std::copy(first, last, buffer);
   1.378 +        for (int i = 0; i < int(sizeof(Value)); ++i) {
   1.379 +          if (dir) {
   1.380 +            stableRadixIntroSort(buffer, buffer + length,
   1.381 +                                 buffer + length, i, functor);
   1.382 +          } else {
   1.383 +            stableRadixIntroSort(buffer + length, buffer + 2 * length,
   1.384 +                                 buffer, i, functor);
   1.385 +          }
   1.386 +          dir = !dir;
   1.387 +        }
   1.388 +        if (dir) {
   1.389 +          std::copy(buffer, buffer + length, first);
   1.390 +        }        else {
   1.391 +          std::copy(buffer + length, buffer + 2 * length, first);
   1.392 +        }
   1.393 +      } catch (...) {
   1.394 +        allocator.deallocate(buffer, 2 * length);
   1.395 +        throw;
   1.396 +      }
   1.397 +      allocator.deallocate(buffer, 2 * length);
   1.398 +    }
   1.399 +
   1.400 +
   1.401 +
   1.402 +    template <typename Value,
   1.403 +              bool sign = std::numeric_limits<Value>::is_signed >
   1.404 +    struct StableRadixSortSelector {
   1.405 +      template <typename Iterator, typename Functor>
   1.406 +      static void sort(Iterator first, Iterator last, Functor functor) {
   1.407 +        stableRadixSignedSort<Value>(first, last, functor);
   1.408 +      }
   1.409 +    };
   1.410 +
   1.411 +    template <typename Value>
   1.412 +    struct StableRadixSortSelector<Value, false> {
   1.413 +      template <typename Iterator, typename Functor>
   1.414 +      static void sort(Iterator first, Iterator last, Functor functor) {
   1.415 +        stableRadixUnsignedSort<Value>(first, last, functor);
   1.416 +      }
   1.417 +    };
   1.418 +
   1.419 +  }
   1.420 +
   1.421 +  /// \ingroup auxalg
   1.422 +  ///
   1.423 +  /// \brief Sorts the STL compatible range into ascending order in a stable
   1.424 +  /// way.
   1.425 +  ///
   1.426 +  /// This function sorts an STL compatible range into ascending
   1.427 +  /// order according to an integer mapping in the same as radixSort() does.
   1.428 +  ///
   1.429 +  /// This sorting algorithm is stable, i.e. the order of two equal
   1.430 +  /// elements remains the same after the sorting.
   1.431 +  ///
   1.432 +  /// This sort algorithm  use a radix forward sort on the
   1.433 +  /// bytes of the integer number. The algorithm sorts the items
   1.434 +  /// byte-by-byte. First, it counts how many times a byte value occurs
   1.435 +  /// in the container, then it copies the corresponding items to
   1.436 +  /// another container in asceding order in O(n) time.
   1.437 +  ///
   1.438 +  /// The time complexity of the algorithm is O(log(c)*n) and
   1.439 +  /// it uses O(n) additional space, where \c c is the
   1.440 +  /// maximal value and \c n is the number of the items in the
   1.441 +  /// container.
   1.442 +  ///
   1.443 +
   1.444 +  /// \param first The begin of the given range.
   1.445 +  /// \param last The end of the given range.
   1.446 +  /// \param functor An adaptible unary function or a normal function
   1.447 +  /// which maps the items to any integer type which can be either
   1.448 +  /// signed or unsigned.
   1.449 +  /// \sa radixSort()
   1.450 +  template <typename Iterator, typename Functor>
   1.451 +  void stableRadixSort(Iterator first, Iterator last, Functor functor) {
   1.452 +    using namespace _radix_sort_bits;
   1.453 +    typedef typename Functor::result_type Value;
   1.454 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   1.455 +  }
   1.456 +
   1.457 +  template <typename Iterator, typename Value, typename Key>
   1.458 +  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
   1.459 +    using namespace _radix_sort_bits;
   1.460 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   1.461 +  }
   1.462 +
   1.463 +  template <typename Iterator, typename Value, typename Key>
   1.464 +  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
   1.465 +    using namespace _radix_sort_bits;
   1.466 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   1.467 +  }
   1.468 +
   1.469 +  template <typename Iterator, typename Value, typename Key>
   1.470 +  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
   1.471 +    using namespace _radix_sort_bits;
   1.472 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   1.473 +  }
   1.474 +
   1.475 +  template <typename Iterator, typename Value, typename Key>
   1.476 +  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
   1.477 +    using namespace _radix_sort_bits;
   1.478 +    StableRadixSortSelector<Value>::sort(first, last, functor);
   1.479 +  }
   1.480 +
   1.481 +  template <typename Iterator>
   1.482 +  void stableRadixSort(Iterator first, Iterator last) {
   1.483 +    using namespace _radix_sort_bits;
   1.484 +    typedef typename std::iterator_traits<Iterator>::value_type Value;
   1.485 +    StableRadixSortSelector<Value>::sort(first, last, Identity<Value>());
   1.486 +  }
   1.487 +
   1.488 +}
   1.489 +
   1.490 +#endif