[Lemon-commits] [lemon_svn] deba: r2403 - in hugo/trunk: lemon test
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:52:23 CET 2006
Author: deba
Date: Sat Dec 3 19:15:43 2005
New Revision: 2403
Modified:
hugo/trunk/lemon/radix_sort.h
hugo/trunk/test/radix_sort_test.cc
Log:
Changed implementation and bug fix
Modified: hugo/trunk/lemon/radix_sort.h
==============================================================================
--- hugo/trunk/lemon/radix_sort.h (original)
+++ hugo/trunk/lemon/radix_sort.h Sat Dec 3 19:15:43 2005
@@ -254,7 +254,7 @@
template <typename Value>
unsigned char valueByte(Value value, int byte) {
- return *((unsigned char *)(&value) + byte);
+ return value >> (std::numeric_limits<unsigned char>::digits * byte);
}
template <typename Functor, typename Key>
@@ -365,13 +365,11 @@
try {
bool dir = true;
std::copy(first, last, buffer);
- for (int i = 0; i < sizeof(Value); ++i) {
+ for (int i = 0; i < (int)sizeof(Value); ++i) {
if (dir) {
- counterIntroSort(buffer, buffer + length, buffer + length,
- i, functor);
+ counterIntroSort(buffer, buffer + length, buffer + length, i, functor);
} else {
- counterIntroSort(buffer + length, buffer + 2 * length, buffer,
- i, functor);
+ counterIntroSort(buffer + length, buffer + 2 * length, buffer, i, functor);
}
dir = !dir;
}
@@ -425,8 +423,8 @@
/// By the occurence number it is possible to copy the container
/// in the right order in \c O(n) time. The algorithm sorts the container
/// by each bytes in forward direction which sorts the container by the
- /// whole value. This way, let be
- /// \c c the maximal capacity and \c n the number of the items in
+ /// whole value. This way, let be \c c the maximal capacity of the integer
+ /// type and \c n the number of the items in
/// the container, the time complexity of the algorithm \c O(log(c)*n)
/// and the additional space complexity is \c O(n).
///
Modified: hugo/trunk/test/radix_sort_test.cc
==============================================================================
--- hugo/trunk/test/radix_sort_test.cc (original)
+++ hugo/trunk/test/radix_sort_test.cc Sat Dec 3 19:15:43 2005
@@ -15,29 +15,55 @@
using namespace lemon;
void checkRadixSort() {
- int n = 10000;
- vector<int> data1(n), data2(n);
- for (int i = 0; i < n; ++i) {
- data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
- }
- radixSort(data1.begin(), data1.end());
- sort(data2.begin(), data2.end());
- for (int i = 0; i < n; ++i) {
- check(data1[i] == data2[i], "Test failed");
+ {
+ int n = 10000;
+ vector<int> data1(n), data2(n);
+ for (int i = 0; i < n; ++i) {
+ data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
+ }
+ radixSort(data1.begin(), data1.end());
+ sort(data2.begin(), data2.end());
+ for (int i = 0; i < n; ++i) {
+ check(data1[i] == data2[i], "Test failed");
+ }
+ } {
+ int n = 10000;
+ vector<unsigned char> data1(n), data2(n);
+ for (int i = 0; i < n; ++i) {
+ data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0)));
+ }
+ radixSort(data1.begin(), data1.end());
+ sort(data2.begin(), data2.end());
+ for (int i = 0; i < n; ++i) {
+ check(data1[i] == data2[i], "Test failed");
+ }
}
}
void checkCounterSort() {
- int n = 10000;
- vector<int> data1(n), data2(n);
- for (int i = 0; i < n; ++i) {
- data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
- }
- counterSort(data1.begin(), data1.end());
- sort(data2.begin(), data2.end());
- for (int i = 0; i < n; ++i) {
- check(data1[i] == data2[i], "Test failed");
+ {
+ int n = 10000;
+ vector<int> data1(n), data2(n);
+ for (int i = 0; i < n; ++i) {
+ data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
+ }
+ counterSort(data1.begin(), data1.end());
+ sort(data2.begin(), data2.end());
+ for (int i = 0; i < n; ++i) {
+ check(data1[i] == data2[i], "Test failed");
+ }
+ } {
+ int n = 10000;
+ vector<unsigned char> data1(n), data2(n);
+ for (int i = 0; i < n; ++i) {
+ data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0)));
+ }
+ counterSort(data1.begin(), data1.end());
+ sort(data2.begin(), data2.end());
+ for (int i = 0; i < n; ++i) {
+ check(data1[i] == data2[i], "Test failed");
+ }
}
}
More information about the Lemon-commits
mailing list