test/radix_sort_test.cc
changeset 1402 3c00344f49c9
parent 1270 dceba191c00d
     1.1 --- a/test/radix_sort_test.cc	Mon Jul 16 16:21:40 2018 +0200
     1.2 +++ b/test/radix_sort_test.cc	Wed Oct 17 19:14:07 2018 +0200
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2009
     1.8 + * Copyright (C) 2003-2013
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -16,6 +16,8 @@
    1.13   *
    1.14   */
    1.15  
    1.16 +#include <lemon/core.h>
    1.17 +
    1.18  #include <lemon/time_measure.h>
    1.19  #include <lemon/smart_graph.h>
    1.20  #include <lemon/maps.h>
    1.21 @@ -25,6 +27,7 @@
    1.22  #include "test_tools.h"
    1.23  
    1.24  #include <vector>
    1.25 +#include <list>
    1.26  #include <algorithm>
    1.27  
    1.28  using namespace lemon;
    1.29 @@ -39,8 +42,58 @@
    1.30  
    1.31  int negate(int a) { return - a; }
    1.32  
    1.33 +template<class T>
    1.34 +bool isTheSame(T &a, T&b)
    1.35 +{
    1.36 +  typename T::iterator ai=a.begin();
    1.37 +  typename T::iterator bi=b.begin();
    1.38 +  for(;ai!=a.end()||bi!=b.end();++ai,++bi)
    1.39 +    if(*ai!=*bi) return false;
    1.40 +  return ai==a.end()&&bi==b.end();
    1.41 +}
    1.42  
    1.43 -void generateIntSequence(int n, std::vector<int>& data) {
    1.44 +template<class T>
    1.45 +T listsort(typename T::iterator b, typename T::iterator e)
    1.46 +{
    1.47 +  if(b==e) return T();
    1.48 +  typename T::iterator bn=b;
    1.49 +  if(++bn==e) {
    1.50 +    T l;
    1.51 +    l.push_back(*b);
    1.52 +    return l;
    1.53 +  }
    1.54 +  typename T::iterator m=b;
    1.55 +  bool x=false;
    1.56 +  for(typename T::iterator i=b;i!=e;++i,x=!x)
    1.57 +    if(x) ++m;
    1.58 +  T l1(listsort<T>(b,m));
    1.59 +  T l2(listsort<T>(m,e));
    1.60 +  T l;
    1.61 +  while((!l1.empty())&&(!l2.empty()))
    1.62 +    if(l1.front()<=l2.front())
    1.63 +      {
    1.64 +        l.push_back(l1.front());
    1.65 +        l1.pop_front();
    1.66 +      }
    1.67 +    else {
    1.68 +      l.push_back(l2.front());
    1.69 +      l2.pop_front();
    1.70 +    }
    1.71 +  while(!l1.empty())
    1.72 +    {
    1.73 +      l.push_back(l1.front());
    1.74 +      l1.pop_front();
    1.75 +    }
    1.76 +  while(!l2.empty())
    1.77 +    {
    1.78 +      l.push_back(l2.front());
    1.79 +      l2.pop_front();
    1.80 +    }
    1.81 +  return l;
    1.82 +}
    1.83 +
    1.84 +template<class T>
    1.85 +void generateIntSequence(int n, T & data) {
    1.86    int prime = 9973;
    1.87    int root = 136, value = 1;
    1.88    for (int i = 0; i < n; ++i) {
    1.89 @@ -49,7 +102,8 @@
    1.90    }
    1.91  }
    1.92  
    1.93 -void generateCharSequence(int n, std::vector<unsigned char>& data) {
    1.94 +template<class T>
    1.95 +void generateCharSequence(int n, T & data) {
    1.96    int prime = 251;
    1.97    int root = 3, value = root;
    1.98    for (int i = 0; i < n; ++i) {
    1.99 @@ -71,15 +125,15 @@
   1.100        check(data1[i] == data2[i], "Test failed");
   1.101      }
   1.102  
   1.103 -    radixSort(data2.begin(), data2.end(), Negate());
   1.104 -    for (int i = 0; i < n; ++i) {
   1.105 -      check(data1[i] == data2[n - 1 - i], "Test failed");
   1.106 -    }
   1.107 +    // radixSort(data2.begin(), data2.end(), Negate());
   1.108 +    // for (int i = 0; i < n; ++i) {
   1.109 +    //   check(data1[i] == data2[n - 1 - i], "Test failed");
   1.110 +    // }
   1.111  
   1.112 -    radixSort(data2.begin(), data2.end(), negate);
   1.113 -    for (int i = 0; i < n; ++i) {
   1.114 -      check(data1[i] == data2[n - 1 - i], "Test failed");
   1.115 -    }
   1.116 +    // radixSort(data2.begin(), data2.end(), negate);
   1.117 +    // for (int i = 0; i < n; ++i) {
   1.118 +    //   check(data1[i] == data2[n - 1 - i], "Test failed");
   1.119 +    // }
   1.120  
   1.121    }
   1.122  
   1.123 @@ -96,6 +150,42 @@
   1.124      }
   1.125  
   1.126    }
   1.127 +  {
   1.128 +    std::list<int> data1;
   1.129 +    generateIntSequence(n, data1);
   1.130 +
   1.131 +    std::list<int> data2(listsort<std::list<int> >(data1.begin(), data1.end()));
   1.132 +
   1.133 +    radixSort(data1.begin(), data1.end());
   1.134 +
   1.135 +    check(isTheSame(data1,data2), "Test failed");
   1.136 +
   1.137 +
   1.138 +    // radixSort(data2.begin(), data2.end(), Negate());
   1.139 +    // check(isTheSame(data1,data2), "Test failed");
   1.140 +    // for (int i = 0; i < n; ++i) {
   1.141 +    //   check(data1[i] == data2[n - 1 - i], "Test failed");
   1.142 +    // }
   1.143 +
   1.144 +    // radixSort(data2.begin(), data2.end(), negate);
   1.145 +    // for (int i = 0; i < n; ++i) {
   1.146 +    //   check(data1[i] == data2[n - 1 - i], "Test failed");
   1.147 +    // }
   1.148 +
   1.149 +  }
   1.150 +
   1.151 +  {
   1.152 +    std::list<unsigned char> data1(n);
   1.153 +    generateCharSequence(n, data1);
   1.154 +
   1.155 +    std::list<unsigned char> data2(listsort<std::list<unsigned char> >
   1.156 +                                   (data1.begin(),
   1.157 +                                    data1.end()));
   1.158 +
   1.159 +    radixSort(data1.begin(), data1.end());
   1.160 +    check(isTheSame(data1,data2), "Test failed");
   1.161 +
   1.162 +  }
   1.163  }
   1.164  
   1.165  
   1.166 @@ -136,6 +226,37 @@
   1.167      }
   1.168  
   1.169    }
   1.170 +  {
   1.171 +    std::list<int> data1;
   1.172 +    generateIntSequence(n, data1);
   1.173 +
   1.174 +    std::list<int> data2(listsort<std::list<int> >(data1.begin(),
   1.175 +                                                   data1.end()));
   1.176 +    stableRadixSort(data1.begin(), data1.end());
   1.177 +    check(isTheSame(data1,data2), "Test failed");
   1.178 +
   1.179 +    // stableRadixSort(data2.begin(), data2.end(), Negate());
   1.180 +    // for (int i = 0; i < n; ++i) {
   1.181 +    //   check(data1[i] == data2[n - 1 - i], "Test failed");
   1.182 +    // }
   1.183 +
   1.184 +    // stableRadixSort(data2.begin(), data2.end(), negate);
   1.185 +    // for (int i = 0; i < n; ++i) {
   1.186 +    //   check(data1[i] == data2[n - 1 - i], "Test failed");
   1.187 +    // }
   1.188 +  }
   1.189 +
   1.190 +  {
   1.191 +    std::list<unsigned char> data1(n);
   1.192 +    generateCharSequence(n, data1);
   1.193 +
   1.194 +    std::list<unsigned char> data2(listsort<std::list<unsigned char> >
   1.195 +                                   (data1.begin(),
   1.196 +                                    data1.end()));
   1.197 +    radixSort(data1.begin(), data1.end());
   1.198 +    check(isTheSame(data1,data2), "Test failed");
   1.199 +
   1.200 +  }
   1.201  }
   1.202  
   1.203  int main() {