[Lemon-commits] Alpar Juttner: Doc improvements and source unifi...
Lemon HG
hg at lemon.cs.elte.hu
Thu Jan 8 18:32:49 CET 2009
details: http://lemon.cs.elte.hu/hg/lemon/rev/31d224a3c0af
changeset: 465:31d224a3c0af
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Tue Dec 02 10:17:30 2008 +0000
description:
Doc improvements and source unification in radix_sort (#72)
diffstat:
2 files changed, 176 insertions(+), 174 deletions(-)
lemon/radix_sort.h | 338 +++++++++++++++++++++++------------------------
test/radix_sort_test.cc | 12 -
diffs (truncated from 569 to 300 lines):
diff --git a/lemon/radix_sort.h b/lemon/radix_sort.h
--- a/lemon/radix_sort.h
+++ b/lemon/radix_sort.h
@@ -1,6 +1,6 @@
-/* -*- C++ -*-
+/* -*- mode: C++; indent-tabs-mode: nil; -*-
*
- * This file is a part of LEMON, a generic C++ optimization library
+ * This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
@@ -37,93 +37,93 @@
template <typename Value>
struct Identity {
const Value& operator()(const Value& val) {
- return val;
+ return val;
}
};
template <typename Value, typename Iterator, typename Functor>
- Iterator radixSortPartition(Iterator first, Iterator last,
- Functor functor, Value mask) {
+ Iterator radixSortPartition(Iterator first, Iterator last,
+ Functor functor, Value mask) {
while (first != last && !(functor(*first) & mask)) {
- ++first;
+ ++first;
}
if (first == last) {
- return first;
+ return first;
}
--last;
while (first != last && (functor(*last) & mask)) {
- --last;
+ --last;
}
if (first == last) {
- return first;
+ return first;
}
std::iter_swap(first, last);
++first;
if (!(first < last)) {
- return first;
+ return first;
}
while (true) {
- while (!(functor(*first) & mask)) {
- ++first;
- }
- --last;
- while (functor(*last) & mask) {
- --last;
- }
- if (!(first < last)) {
- return first;
- }
- std::iter_swap(first, last);
- ++first;
+ while (!(functor(*first) & mask)) {
+ ++first;
+ }
+ --last;
+ while (functor(*last) & mask) {
+ --last;
+ }
+ if (!(first < last)) {
+ return first;
+ }
+ std::iter_swap(first, last);
+ ++first;
}
}
template <typename Iterator, typename Functor>
- Iterator radixSortSignPartition(Iterator first, Iterator last,
- Functor functor) {
+ Iterator radixSortSignPartition(Iterator first, Iterator last,
+ Functor functor) {
while (first != last && functor(*first) < 0) {
- ++first;
+ ++first;
}
if (first == last) {
- return first;
+ return first;
}
--last;
while (first != last && functor(*last) >= 0) {
- --last;
+ --last;
}
if (first == last) {
- return first;
+ return first;
}
std::iter_swap(first, last);
++first;
if (!(first < last)) {
- return first;
+ return first;
}
while (true) {
- while (functor(*first) < 0) {
- ++first;
- }
- --last;
- while (functor(*last) >= 0) {
- --last;
- }
- if (!(first < last)) {
- return first;
- }
- std::iter_swap(first, last);
- ++first;
+ while (functor(*first) < 0) {
+ ++first;
+ }
+ --last;
+ while (functor(*last) >= 0) {
+ --last;
+ }
+ if (!(first < last)) {
+ return first;
+ }
+ std::iter_swap(first, last);
+ ++first;
}
}
template <typename Value, typename Iterator, typename Functor>
- void radixIntroSort(Iterator first, Iterator last,
- Functor functor, Value mask) {
+ void radixIntroSort(Iterator first, Iterator last,
+ Functor functor, Value mask) {
while (mask != 0 && last - first > 1) {
- Iterator cut = radixSortPartition(first, last, functor, mask);
- mask >>= 1;
- radixIntroSort(first, cut, functor, mask);
- first = cut;
+ Iterator cut = radixSortPartition(first, last, functor, mask);
+ mask >>= 1;
+ radixIntroSort(first, cut, functor, mask);
+ first = cut;
}
}
@@ -138,19 +138,19 @@
mask = ~0; max_digit = 0;
for (it = first; it != cut; ++it) {
- while ((mask & functor(*it)) != mask) {
- ++max_digit;
- mask <<= 1;
- }
+ while ((mask & functor(*it)) != mask) {
+ ++max_digit;
+ mask <<= 1;
+ }
}
radixIntroSort(first, cut, functor, 1 << max_digit);
mask = 0; max_digit = 0;
for (it = cut; it != last; ++it) {
- while ((mask | functor(*it)) != mask) {
- ++max_digit;
- mask <<= 1; mask |= 1;
- }
+ while ((mask | functor(*it)) != mask) {
+ ++max_digit;
+ mask <<= 1; mask |= 1;
+ }
}
radixIntroSort(cut, last, functor, 1 << max_digit);
}
@@ -163,21 +163,21 @@
Iterator it;
for (it = first; it != last; ++it) {
- while ((mask | functor(*it)) != mask) {
- ++max_digit;
- mask <<= 1; mask |= 1;
- }
+ while ((mask | functor(*it)) != mask) {
+ ++max_digit;
+ mask <<= 1; mask |= 1;
+ }
}
radixIntroSort(first, last, functor, 1 << max_digit);
}
- template <typename Value,
- bool sign = std::numeric_limits<Value>::is_signed >
+ template <typename Value,
+ bool sign = std::numeric_limits<Value>::is_signed >
struct RadixSortSelector {
template <typename Iterator, typename Functor>
static void sort(Iterator first, Iterator last, Functor functor) {
- radixSignedSort<Value>(first, last, functor);
+ radixSignedSort<Value>(first, last, functor);
}
};
@@ -185,7 +185,7 @@
struct RadixSortSelector<Value, false> {
template <typename Iterator, typename Functor>
static void sort(Iterator first, Iterator last, Functor functor) {
- radixUnsignedSort<Value>(first, last, functor);
+ radixUnsignedSort<Value>(first, last, functor);
}
};
@@ -195,26 +195,29 @@
///
/// \brief Sorts the STL compatible range into ascending order.
///
- /// The \c radixSort sorts the STL compatible range into ascending
- /// order. The radix sort algorithm can sort the items which mapped
- /// to an integer with an adaptable unary function \c functor and the
- /// order will be ascending by these mapped values. As function
- /// specialization it is possible to use a normal function instead
- /// of the functor object or if the functor is not given it will use
- /// an identity function instead.
+ /// The \c radixSort sorts an STL compatible range into ascending
+ /// order. The radix sort algorithm can sort items which are mapped
+ /// to integers with an adaptable unary function \c functor and the
+ /// order will be ascending according to these mapped values.
///
- /// This implemented radix sort is a special quick sort which pivot
- /// value is choosen to partite the items on the next
- /// bit. Therefore, let be \c c the maximal capacity and \c n the
- /// number of the items in the container, the time complexity of the
- /// algorithm is \f$ O(\log(c)n) \f$ and the additional space
- /// complexity is \f$ O(\log(c)) \f$.
+ /// It is also possible to use a normal function instead
+ /// of the functor object. If the functor is not given it will use
+ /// the identity function instead.
+ ///
+ /// This is a special quick sort algorithm where the pivot
+ /// values to split the items are choosen to be \f$ 2^k \f$ for each \c k.
+ /// Therefore, the time complexity of the
+ /// algorithm is \f$ O(\log(c)n) \f$ and it uses \f$ O(\log(c)) \f$,
+ /// additional space, where \c c is the maximal value and \c n is the
+ /// number of the items in the container.
///
/// \param first The begin of the given range.
/// \param last The end of the given range.
/// \param functor An adaptible unary function or a normal function
/// which maps the items to any integer type which can be either
/// signed or unsigned.
+ ///
+ /// \sa counterSort()
template <typename Iterator, typename Functor>
void radixSort(Iterator first, Iterator last, Functor functor) {
using namespace _radix_sort_bits;
@@ -261,63 +264,63 @@
}
template <typename Functor, typename Key>
- void counterIntroSort(Key *first, Key *last, Key *target,
- int byte, Functor functor) {
- const int size =
- unsigned(std::numeric_limits<unsigned char>::max()) + 1;
+ void counterIntroSort(Key *first, Key *last, Key *target,
+ int byte, Functor functor) {
+ const int size =
+ unsigned(std::numeric_limits<unsigned char>::max()) + 1;
std::vector<int> counter(size);
for (int i = 0; i < size; ++i) {
- counter[i] = 0;
+ counter[i] = 0;
}
Key *it = first;
while (first != last) {
- ++counter[valueByte(functor(*first), byte)];
- ++first;
+ ++counter[valueByte(functor(*first), byte)];
+ ++first;
}
int prev, num = 0;
for (int i = 0; i < size; ++i) {
- prev = num;
- num += counter[i];
- counter[i] = prev;
+ prev = num;
+ num += counter[i];
+ counter[i] = prev;
}
while (it != last) {
- target[counter[valueByte(functor(*it), byte)]++] = *it;
- ++it;
+ target[counter[valueByte(functor(*it), byte)]++] = *it;
+ ++it;
}
}
More information about the Lemon-commits
mailing list