lemon/radix_sort.h
changeset 464 4f7224faf3bd
child 465 31d224a3c0af
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/radix_sort.h	Fri Oct 17 23:55:18 2008 +0200
     1.3 @@ -0,0 +1,484 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     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 the STL compatible range into ascending
   1.202 +  /// order.  The radix sort algorithm can sort the items which mapped
   1.203 +  /// to an integer with an adaptable unary function \c functor and the
   1.204 +  /// order will be ascending by these mapped values. As function
   1.205 +  /// specialization it is possible to use a normal function instead
   1.206 +  /// of the functor object or if the functor is not given it will use
   1.207 +  /// an identity function instead.
   1.208 +  ///
   1.209 +  /// This implemented radix sort is a special quick sort which pivot
   1.210 +  /// value is choosen to partite the items on the next
   1.211 +  /// bit. Therefore, let be \c c the maximal capacity and \c n the
   1.212 +  /// number of the items in the container, the time complexity of the
   1.213 +  /// algorithm is \f$ O(\log(c)n) \f$ and the additional space
   1.214 +  /// complexity is \f$ O(\log(c)) \f$.
   1.215 +  ///
   1.216 +  /// \param first The begin of the given range.
   1.217 +  /// \param last The end of the given range.
   1.218 +  /// \param functor An adaptible unary function or a normal function
   1.219 +  /// which maps the items to any integer type which can be either
   1.220 +  /// signed or unsigned.
   1.221 +  template <typename Iterator, typename Functor>
   1.222 +  void radixSort(Iterator first, Iterator last, Functor functor) {
   1.223 +    using namespace _radix_sort_bits;
   1.224 +    typedef typename Functor::result_type Value;
   1.225 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.226 +  }
   1.227 +
   1.228 +  template <typename Iterator, typename Value, typename Key>
   1.229 +  void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
   1.230 +    using namespace _radix_sort_bits;
   1.231 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.232 +  }
   1.233 +
   1.234 +  template <typename Iterator, typename Value, typename Key>
   1.235 +  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
   1.236 +    using namespace _radix_sort_bits;
   1.237 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.238 +  }
   1.239 +
   1.240 +  template <typename Iterator, typename Value, typename Key>
   1.241 +  void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
   1.242 +    using namespace _radix_sort_bits;
   1.243 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.244 +  }
   1.245 +
   1.246 +  template <typename Iterator, typename Value, typename Key>
   1.247 +  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
   1.248 +    using namespace _radix_sort_bits;
   1.249 +    RadixSortSelector<Value>::sort(first, last, functor);
   1.250 +  }
   1.251 +
   1.252 +  template <typename Iterator>
   1.253 +  void radixSort(Iterator first, Iterator last) {
   1.254 +    using namespace _radix_sort_bits;
   1.255 +    typedef typename std::iterator_traits<Iterator>::value_type Value;
   1.256 +    RadixSortSelector<Value>::sort(first, last, Identity<Value>());
   1.257 +  }
   1.258 +
   1.259 +  namespace _radix_sort_bits {
   1.260 +
   1.261 +    template <typename Value>
   1.262 +    unsigned char valueByte(Value value, int byte) {
   1.263 +      return value >> (std::numeric_limits<unsigned char>::digits * byte);
   1.264 +    }
   1.265 +
   1.266 +    template <typename Functor, typename Key>
   1.267 +    void counterIntroSort(Key *first, Key *last, Key *target, 
   1.268 +			  int byte, Functor functor) {
   1.269 +      const int size = 
   1.270 +	unsigned(std::numeric_limits<unsigned char>::max()) + 1;
   1.271 +      std::vector<int> counter(size);
   1.272 +      for (int i = 0; i < size; ++i) {
   1.273 +	counter[i] = 0;
   1.274 +      }
   1.275 +      Key *it = first;
   1.276 +      while (first != last) {
   1.277 +	++counter[valueByte(functor(*first), byte)]; 
   1.278 +	++first;
   1.279 +      }
   1.280 +      int prev, num = 0;
   1.281 +      for (int i = 0; i < size; ++i) {
   1.282 +	prev = num;
   1.283 +	num += counter[i];
   1.284 +	counter[i] = prev;
   1.285 +      }
   1.286 +      while (it != last) {
   1.287 +	target[counter[valueByte(functor(*it), byte)]++] = *it;
   1.288 +	++it;
   1.289 +      }
   1.290 +    }
   1.291 +
   1.292 +    template <typename Functor, typename Key>
   1.293 +    void signedCounterIntroSort(Key *first, Key *last, Key *target, 
   1.294 +				int byte, Functor functor) {
   1.295 +      const int size = 
   1.296 +	unsigned(std::numeric_limits<unsigned char>::max()) + 1;
   1.297 +      std::vector<int> counter(size);
   1.298 +      for (int i = 0; i < size; ++i) {
   1.299 +	counter[i] = 0;
   1.300 +      }
   1.301 +      Key *it = first;
   1.302 +      while (first != last) {
   1.303 +	counter[valueByte(functor(*first), byte)]++;
   1.304 +	++first;
   1.305 +      }
   1.306 +      int prev, num = 0;
   1.307 +      for (int i = size / 2; i < size; ++i) {
   1.308 +	prev = num;
   1.309 +	num += counter[i];
   1.310 +	counter[i] = prev;
   1.311 +      }
   1.312 +      for (int i = 0; i < size / 2; ++i) {
   1.313 +	prev = num;
   1.314 +	num += counter[i];
   1.315 +	counter[i] = prev;
   1.316 +      }
   1.317 +      while (it != last) {
   1.318 +	target[counter[valueByte(functor(*it), byte)]++] = *it;
   1.319 +	++it;
   1.320 +      }
   1.321 +    }
   1.322 +
   1.323 +  
   1.324 +    template <typename Value, typename Iterator, typename Functor>
   1.325 +    void counterSignedSort(Iterator first, Iterator last, Functor functor) {
   1.326 +      if (first == last) return;
   1.327 +      typedef typename std::iterator_traits<Iterator>::value_type Key;
   1.328 +      typedef std::allocator<Key> Allocator;
   1.329 +      Allocator allocator;
   1.330 +
   1.331 +      int length = std::distance(first, last);
   1.332 +      Key* buffer = allocator.allocate(2 * length);
   1.333 +      try {
   1.334 +	bool dir = true;
   1.335 +	std::copy(first, last, buffer);
   1.336 +	for (int i = 0; i < int(sizeof(Value)) - 1; ++i) {
   1.337 +	  if (dir) {
   1.338 +	    counterIntroSort(buffer, buffer + length, buffer + length, 
   1.339 +			     i, functor);
   1.340 +	  } else {
   1.341 +	    counterIntroSort(buffer + length, buffer + 2 * length, buffer, 
   1.342 +			     i, functor);
   1.343 +	  }
   1.344 +	  dir = !dir;
   1.345 +	}
   1.346 +	if (dir) {
   1.347 +	  signedCounterIntroSort(buffer, buffer + length, buffer + length, 
   1.348 +				 sizeof(Value) - 1, functor);
   1.349 +	  std::copy(buffer + length, buffer + 2 * length, first);
   1.350 +	}	else {
   1.351 +	  signedCounterIntroSort(buffer + length, buffer + 2 * length, buffer, 
   1.352 +				 sizeof(Value) - 1, functor);
   1.353 +	  std::copy(buffer, buffer + length, first);
   1.354 +	}
   1.355 +      } catch (...) {
   1.356 +	allocator.deallocate(buffer, 2 * length);
   1.357 +	throw;
   1.358 +      }
   1.359 +      allocator.deallocate(buffer, 2 * length);
   1.360 +    }
   1.361 +
   1.362 +    template <typename Value, typename Iterator, typename Functor>
   1.363 +    void counterUnsignedSort(Iterator first, Iterator last, Functor functor) {
   1.364 +      if (first == last) return;
   1.365 +      typedef typename std::iterator_traits<Iterator>::value_type Key;
   1.366 +      typedef std::allocator<Key> Allocator;
   1.367 +      Allocator allocator;
   1.368 +
   1.369 +      int length = std::distance(first, last);
   1.370 +      Key *buffer = allocator.allocate(2 * length);
   1.371 +      try {
   1.372 +	bool dir = true;
   1.373 +	std::copy(first, last, buffer);
   1.374 +	for (int i = 0; i < int(sizeof(Value)); ++i) {
   1.375 +	  if (dir) {
   1.376 +	    counterIntroSort(buffer, buffer + length, 
   1.377 +			     buffer + length, i, functor);
   1.378 +	  } else {
   1.379 +	    counterIntroSort(buffer + length, buffer + 2 * length, 
   1.380 +			     buffer, i, functor);
   1.381 +	  }
   1.382 +	  dir = !dir;
   1.383 +	}
   1.384 +	if (dir) {
   1.385 +	  std::copy(buffer, buffer + length, first);
   1.386 +	}	else {
   1.387 +	  std::copy(buffer + length, buffer + 2 * length, first);
   1.388 +	}
   1.389 +      } catch (...) {
   1.390 +	allocator.deallocate(buffer, 2 * length);
   1.391 +	throw;
   1.392 +      }
   1.393 +      allocator.deallocate(buffer, 2 * length);
   1.394 +    }
   1.395 +
   1.396 +
   1.397 +
   1.398 +    template <typename Value, 
   1.399 +	      bool sign = std::numeric_limits<Value>::is_signed >
   1.400 +    struct CounterSortSelector {
   1.401 +      template <typename Iterator, typename Functor>
   1.402 +      static void sort(Iterator first, Iterator last, Functor functor) {
   1.403 +	counterSignedSort<Value>(first, last, functor);
   1.404 +      }
   1.405 +    };
   1.406 +
   1.407 +    template <typename Value>
   1.408 +    struct CounterSortSelector<Value, false> {
   1.409 +      template <typename Iterator, typename Functor>
   1.410 +      static void sort(Iterator first, Iterator last, Functor functor) {
   1.411 +	counterUnsignedSort<Value>(first, last, functor);
   1.412 +      }
   1.413 +    };
   1.414 +
   1.415 +  }
   1.416 +
   1.417 +  /// \ingroup auxalg
   1.418 +  ///
   1.419 +  /// \brief Sorts stable the STL compatible range into ascending order.
   1.420 +  ///
   1.421 +  /// The \c counterSort sorts the STL compatible range into ascending
   1.422 +  /// order.  The counter sort algorithm can sort the items which
   1.423 +  /// mapped to an integer with an adaptable unary function \c functor
   1.424 +  /// and the order will be ascending by these mapped values. As
   1.425 +  /// function specialization it is possible to use a normal function
   1.426 +  /// instead of the functor object or if the functor is not given it
   1.427 +  /// will use an identity function instead.
   1.428 +  ///
   1.429 +  /// The implemented counter sort use a radix forward sort on the
   1.430 +  /// bytes of the integer number. The algorithm sorts the items
   1.431 +  /// byte-by-byte, first it counts how many times occurs a byte value
   1.432 +  /// in the containerm, and with the occurence number the container
   1.433 +  /// can be copied to an other in asceding order in \c O(n) time.
   1.434 +  /// Let be \c c the maximal capacity of the integer type and \c n
   1.435 +  /// the number of the items in the container, the time complexity of
   1.436 +  /// the algorithm is \f$ O(\log(c)n) \f$ and the additional space
   1.437 +  /// complexity is \f$ O(n) \f$.
   1.438 +  ///
   1.439 +  /// The sorting algorithm is stable, i.e. the order of two equal
   1.440 +  /// element remains the same.
   1.441 +  ///
   1.442 +  /// \param first The begin of the given range.
   1.443 +  /// \param last The end of the given range.
   1.444 +  /// \param functor An adaptible unary function or a normal function
   1.445 +  /// which maps the items to any integer type which can be either
   1.446 +  /// signed or unsigned.
   1.447 +  template <typename Iterator, typename Functor>
   1.448 +  void counterSort(Iterator first, Iterator last, Functor functor) {
   1.449 +    using namespace _radix_sort_bits;
   1.450 +    typedef typename Functor::result_type Value;
   1.451 +    CounterSortSelector<Value>::sort(first, last, functor);
   1.452 +  }
   1.453 +
   1.454 +  template <typename Iterator, typename Value, typename Key>
   1.455 +  void counterSort(Iterator first, Iterator last, Value (*functor)(Key)) {
   1.456 +    using namespace _radix_sort_bits;
   1.457 +    CounterSortSelector<Value>::sort(first, last, functor);
   1.458 +  }
   1.459 +
   1.460 +  template <typename Iterator, typename Value, typename Key>
   1.461 +  void counterSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
   1.462 +    using namespace _radix_sort_bits;
   1.463 +    CounterSortSelector<Value>::sort(first, last, functor);
   1.464 +  }
   1.465 +
   1.466 +  template <typename Iterator, typename Value, typename Key>
   1.467 +  void counterSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
   1.468 +    using namespace _radix_sort_bits;
   1.469 +    CounterSortSelector<Value>::sort(first, last, functor);
   1.470 +  }
   1.471 +
   1.472 +  template <typename Iterator, typename Value, typename Key>
   1.473 +  void counterSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
   1.474 +    using namespace _radix_sort_bits;
   1.475 +    CounterSortSelector<Value>::sort(first, last, functor);
   1.476 +  }
   1.477 +
   1.478 +  template <typename Iterator>
   1.479 +  void counterSort(Iterator first, Iterator last) {
   1.480 +    using namespace _radix_sort_bits;
   1.481 +    typedef typename std::iterator_traits<Iterator>::value_type Value;
   1.482 +    CounterSortSelector<Value>::sort(first, last, Identity<Value>());
   1.483 +  }
   1.484 +
   1.485 +}
   1.486 +
   1.487 +#endif