test/radix_sort_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Sun, 17 Aug 2008 15:02:03 +0200
changeset 1418 15282595e6f4
parent 1270 dceba191c00d
permissions -rw-r--r--
Using Arc instead of ArcIt in Dfs (#32)
alpar@465
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@464
     2
 *
alpar@465
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@464
     4
 *
alpar@1270
     5
 * Copyright (C) 2003-2013
deba@464
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@464
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@464
     8
 *
deba@464
     9
 * Permission to use, modify and distribute this software is granted
deba@464
    10
 * provided that this copyright notice appears in all copies. For
deba@464
    11
 * precise terms see the accompanying LICENSE file.
deba@464
    12
 *
deba@464
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@464
    14
 * express or implied, and with no claim as to its suitability for any
deba@464
    15
 * purpose.
deba@464
    16
 *
deba@464
    17
 */
deba@464
    18
alpar@1341
    19
#include <lemon/core.h>
alpar@1341
    20
deba@464
    21
#include <lemon/time_measure.h>
deba@464
    22
#include <lemon/smart_graph.h>
deba@464
    23
#include <lemon/maps.h>
deba@464
    24
#include <lemon/radix_sort.h>
deba@464
    25
#include <lemon/math.h>
deba@464
    26
deba@464
    27
#include "test_tools.h"
deba@464
    28
deba@464
    29
#include <vector>
alpar@1211
    30
#include <list>
deba@464
    31
#include <algorithm>
deba@464
    32
deba@464
    33
using namespace lemon;
deba@464
    34
deba@464
    35
static const int n = 10000;
deba@464
    36
deba@464
    37
struct Negate {
deba@464
    38
  typedef int argument_type;
deba@464
    39
  typedef int result_type;
deba@464
    40
  int operator()(int a) { return - a; }
deba@464
    41
};
deba@464
    42
deba@464
    43
int negate(int a) { return - a; }
deba@464
    44
alpar@1211
    45
template<class T>
alpar@1211
    46
bool isTheSame(T &a, T&b)
alpar@1211
    47
{
alpar@1211
    48
  typename T::iterator ai=a.begin();
alpar@1211
    49
  typename T::iterator bi=b.begin();
alpar@1211
    50
  for(;ai!=a.end()||bi!=b.end();++ai,++bi)
alpar@1211
    51
    if(*ai!=*bi) return false;
alpar@1211
    52
  return ai==a.end()&&bi==b.end();
alpar@1211
    53
}
deba@464
    54
alpar@1211
    55
template<class T>
alpar@1211
    56
T listsort(typename T::iterator b, typename T::iterator e)
alpar@1270
    57
{
alpar@1211
    58
  if(b==e) return T();
alpar@1211
    59
  typename T::iterator bn=b;
alpar@1211
    60
  if(++bn==e) {
alpar@1211
    61
    T l;
alpar@1211
    62
    l.push_back(*b);
alpar@1211
    63
    return l;
alpar@1211
    64
  }
alpar@1211
    65
  typename T::iterator m=b;
alpar@1211
    66
  bool x=false;
alpar@1211
    67
  for(typename T::iterator i=b;i!=e;++i,x=!x)
alpar@1211
    68
    if(x) ++m;
alpar@1211
    69
  T l1(listsort<T>(b,m));
alpar@1211
    70
  T l2(listsort<T>(m,e));
alpar@1211
    71
  T l;
alpar@1211
    72
  while((!l1.empty())&&(!l2.empty()))
alpar@1211
    73
    if(l1.front()<=l2.front())
alpar@1211
    74
      {
alpar@1211
    75
        l.push_back(l1.front());
alpar@1211
    76
        l1.pop_front();
alpar@1211
    77
      }
alpar@1211
    78
    else {
alpar@1211
    79
      l.push_back(l2.front());
alpar@1211
    80
      l2.pop_front();
alpar@1211
    81
    }
alpar@1211
    82
  while(!l1.empty())
alpar@1211
    83
    {
alpar@1211
    84
      l.push_back(l1.front());
alpar@1211
    85
      l1.pop_front();
alpar@1211
    86
    }
alpar@1211
    87
  while(!l2.empty())
alpar@1211
    88
    {
alpar@1211
    89
      l.push_back(l2.front());
alpar@1211
    90
      l2.pop_front();
alpar@1211
    91
    }
alpar@1211
    92
  return l;
alpar@1211
    93
}
alpar@1211
    94
alpar@1211
    95
template<class T>
alpar@1211
    96
void generateIntSequence(int n, T & data) {
deba@464
    97
  int prime = 9973;
deba@464
    98
  int root = 136, value = 1;
deba@464
    99
  for (int i = 0; i < n; ++i) {
deba@464
   100
    data.push_back(value - prime / 2);
deba@464
   101
    value = (value * root) % prime;
deba@464
   102
  }
deba@464
   103
}
deba@464
   104
alpar@1211
   105
template<class T>
alpar@1211
   106
void generateCharSequence(int n, T & data) {
deba@464
   107
  int prime = 251;
deba@464
   108
  int root = 3, value = root;
deba@464
   109
  for (int i = 0; i < n; ++i) {
deba@464
   110
    data.push_back(static_cast<unsigned char>(value));
deba@464
   111
    value = (value * root) % prime;
deba@464
   112
  }
deba@464
   113
}
deba@464
   114
deba@464
   115
void checkRadixSort() {
deba@464
   116
  {
deba@464
   117
    std::vector<int> data1;
deba@464
   118
    generateIntSequence(n, data1);
deba@464
   119
deba@464
   120
    std::vector<int> data2(data1);
deba@464
   121
    std::sort(data1.begin(), data1.end());
deba@464
   122
deba@464
   123
    radixSort(data2.begin(), data2.end());
deba@464
   124
    for (int i = 0; i < n; ++i) {
deba@464
   125
      check(data1[i] == data2[i], "Test failed");
deba@464
   126
    }
deba@464
   127
alpar@1211
   128
    // radixSort(data2.begin(), data2.end(), Negate());
alpar@1211
   129
    // for (int i = 0; i < n; ++i) {
alpar@1211
   130
    //   check(data1[i] == data2[n - 1 - i], "Test failed");
alpar@1211
   131
    // }
deba@464
   132
alpar@1211
   133
    // radixSort(data2.begin(), data2.end(), negate);
alpar@1211
   134
    // for (int i = 0; i < n; ++i) {
alpar@1211
   135
    //   check(data1[i] == data2[n - 1 - i], "Test failed");
alpar@1211
   136
    // }
deba@464
   137
alpar@465
   138
  }
alpar@465
   139
deba@464
   140
  {
deba@464
   141
    std::vector<unsigned char> data1(n);
deba@464
   142
    generateCharSequence(n, data1);
deba@464
   143
deba@464
   144
    std::vector<unsigned char> data2(data1);
deba@464
   145
    std::sort(data1.begin(), data1.end());
deba@464
   146
deba@464
   147
    radixSort(data2.begin(), data2.end());
deba@464
   148
    for (int i = 0; i < n; ++i) {
deba@464
   149
      check(data1[i] == data2[i], "Test failed");
deba@464
   150
    }
deba@464
   151
deba@464
   152
  }
alpar@1211
   153
  {
alpar@1211
   154
    std::list<int> data1;
alpar@1211
   155
    generateIntSequence(n, data1);
alpar@1211
   156
alpar@1211
   157
    std::list<int> data2(listsort<std::list<int> >(data1.begin(), data1.end()));
alpar@1211
   158
alpar@1211
   159
    radixSort(data1.begin(), data1.end());
alpar@1211
   160
alpar@1211
   161
    check(isTheSame(data1,data2), "Test failed");
alpar@1211
   162
alpar@1211
   163
alpar@1211
   164
    // radixSort(data2.begin(), data2.end(), Negate());
alpar@1211
   165
    // check(isTheSame(data1,data2), "Test failed");
alpar@1211
   166
    // for (int i = 0; i < n; ++i) {
alpar@1211
   167
    //   check(data1[i] == data2[n - 1 - i], "Test failed");
alpar@1211
   168
    // }
alpar@1211
   169
alpar@1211
   170
    // radixSort(data2.begin(), data2.end(), negate);
alpar@1211
   171
    // for (int i = 0; i < n; ++i) {
alpar@1211
   172
    //   check(data1[i] == data2[n - 1 - i], "Test failed");
alpar@1211
   173
    // }
alpar@1211
   174
alpar@1211
   175
  }
alpar@1211
   176
alpar@1211
   177
  {
alpar@1211
   178
    std::list<unsigned char> data1(n);
alpar@1211
   179
    generateCharSequence(n, data1);
alpar@1211
   180
alpar@1211
   181
    std::list<unsigned char> data2(listsort<std::list<unsigned char> >
alpar@1211
   182
                                   (data1.begin(),
alpar@1211
   183
                                    data1.end()));
alpar@1211
   184
alpar@1211
   185
    radixSort(data1.begin(), data1.end());
alpar@1211
   186
    check(isTheSame(data1,data2), "Test failed");
alpar@1211
   187
alpar@1211
   188
  }
deba@464
   189
}
deba@464
   190
deba@464
   191
deba@466
   192
void checkStableRadixSort() {
deba@464
   193
  {
deba@464
   194
    std::vector<int> data1;
deba@464
   195
    generateIntSequence(n, data1);
deba@464
   196
deba@464
   197
    std::vector<int> data2(data1);
deba@464
   198
    std::sort(data1.begin(), data1.end());
deba@464
   199
deba@466
   200
    stableRadixSort(data2.begin(), data2.end());
deba@464
   201
    for (int i = 0; i < n; ++i) {
deba@464
   202
      check(data1[i] == data2[i], "Test failed");
deba@464
   203
    }
deba@464
   204
deba@466
   205
    stableRadixSort(data2.begin(), data2.end(), Negate());
deba@464
   206
    for (int i = 0; i < n; ++i) {
deba@464
   207
      check(data1[i] == data2[n - 1 - i], "Test failed");
deba@464
   208
    }
deba@464
   209
deba@466
   210
    stableRadixSort(data2.begin(), data2.end(), negate);
deba@464
   211
    for (int i = 0; i < n; ++i) {
deba@464
   212
      check(data1[i] == data2[n - 1 - i], "Test failed");
deba@464
   213
    }
alpar@465
   214
  }
deba@464
   215
deba@464
   216
  {
deba@464
   217
    std::vector<unsigned char> data1(n);
deba@464
   218
    generateCharSequence(n, data1);
deba@464
   219
deba@464
   220
    std::vector<unsigned char> data2(data1);
deba@464
   221
    std::sort(data1.begin(), data1.end());
deba@464
   222
deba@464
   223
    radixSort(data2.begin(), data2.end());
deba@464
   224
    for (int i = 0; i < n; ++i) {
deba@464
   225
      check(data1[i] == data2[i], "Test failed");
deba@464
   226
    }
deba@464
   227
deba@464
   228
  }
alpar@1211
   229
  {
alpar@1211
   230
    std::list<int> data1;
alpar@1211
   231
    generateIntSequence(n, data1);
alpar@1211
   232
alpar@1211
   233
    std::list<int> data2(listsort<std::list<int> >(data1.begin(),
alpar@1211
   234
                                                   data1.end()));
alpar@1211
   235
    stableRadixSort(data1.begin(), data1.end());
alpar@1211
   236
    check(isTheSame(data1,data2), "Test failed");
alpar@1211
   237
alpar@1211
   238
    // stableRadixSort(data2.begin(), data2.end(), Negate());
alpar@1211
   239
    // for (int i = 0; i < n; ++i) {
alpar@1211
   240
    //   check(data1[i] == data2[n - 1 - i], "Test failed");
alpar@1211
   241
    // }
alpar@1211
   242
alpar@1211
   243
    // stableRadixSort(data2.begin(), data2.end(), negate);
alpar@1211
   244
    // for (int i = 0; i < n; ++i) {
alpar@1211
   245
    //   check(data1[i] == data2[n - 1 - i], "Test failed");
alpar@1211
   246
    // }
alpar@1211
   247
  }
alpar@1211
   248
alpar@1211
   249
  {
alpar@1211
   250
    std::list<unsigned char> data1(n);
alpar@1211
   251
    generateCharSequence(n, data1);
alpar@1211
   252
alpar@1211
   253
    std::list<unsigned char> data2(listsort<std::list<unsigned char> >
alpar@1211
   254
                                   (data1.begin(),
alpar@1211
   255
                                    data1.end()));
alpar@1211
   256
    radixSort(data1.begin(), data1.end());
alpar@1211
   257
    check(isTheSame(data1,data2), "Test failed");
alpar@1211
   258
alpar@1211
   259
  }
deba@464
   260
}
deba@464
   261
deba@464
   262
int main() {
deba@464
   263
alpar@465
   264
  checkRadixSort();
deba@466
   265
  checkStableRadixSort();
deba@464
   266
deba@464
   267
  return 0;
deba@464
   268
}