COIN-OR::LEMON - Graph Library

Ticket #72: de16f1f2d228.patch

File de16f1f2d228.patch, 9.4 KB (added by Balazs Dezso, 16 years ago)

Rename counterSort

  • lemon/radix_sort.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1228256143 -3600
    # Node ID de16f1f2d228be03fed82674499f76eb72082431
    # Parent  31d224a3c0af0f0ae35ac7264d0a90eb1871648e
    Rename counterSort to stableRadixSort
    
    diff -r 31d224a3c0af -r de16f1f2d228 lemon/radix_sort.h
    a b  
    217217  /// which maps the items to any integer type which can be either
    218218  /// signed or unsigned.
    219219  ///
    220   /// \sa counterSort()
     220  /// \sa stableRadixSort()
    221221  template <typename Iterator, typename Functor>
    222222  void radixSort(Iterator first, Iterator last, Functor functor) {
    223223    using namespace _radix_sort_bits;
     
    264264    }
    265265
    266266    template <typename Functor, typename Key>
    267     void counterIntroSort(Key *first, Key *last, Key *target,
    268                           int byte, Functor functor) {
     267    void stableRadixIntroSort(Key *first, Key *last, Key *target,
     268                              int byte, Functor functor) {
    269269      const int size =
    270270        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
    271271      std::vector<int> counter(size);
     
    290290    }
    291291
    292292    template <typename Functor, typename Key>
    293     void signedCounterIntroSort(Key *first, Key *last, Key *target,
    294                                 int byte, Functor functor) {
     293    void signedStableRadixIntroSort(Key *first, Key *last, Key *target,
     294                                    int byte, Functor functor) {
    295295      const int size =
    296296        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
    297297      std::vector<int> counter(size);
     
    322322
    323323
    324324    template <typename Value, typename Iterator, typename Functor>
    325     void counterSignedSort(Iterator first, Iterator last, Functor functor) {
     325    void stableRadixSignedSort(Iterator first, Iterator last, Functor functor) {
    326326      if (first == last) return;
    327327      typedef typename std::iterator_traits<Iterator>::value_type Key;
    328328      typedef std::allocator<Key> Allocator;
     
    335335        std::copy(first, last, buffer);
    336336        for (int i = 0; i < int(sizeof(Value)) - 1; ++i) {
    337337          if (dir) {
    338             counterIntroSort(buffer, buffer + length, buffer + length,
    339                              i, functor);
     338            stableRadixIntroSort(buffer, buffer + length, buffer + length,
     339                                 i, functor);
    340340          } else {
    341             counterIntroSort(buffer + length, buffer + 2 * length, buffer,
    342                              i, functor);
     341            stableRadixIntroSort(buffer + length, buffer + 2 * length, buffer,
     342                                 i, functor);
    343343          }
    344344          dir = !dir;
    345345        }
    346346        if (dir) {
    347           signedCounterIntroSort(buffer, buffer + length, buffer + length,
    348                                  sizeof(Value) - 1, functor);
     347          signedStableRadixIntroSort(buffer, buffer + length, buffer + length,
     348                                     sizeof(Value) - 1, functor);
    349349          std::copy(buffer + length, buffer + 2 * length, first);
    350350        }        else {
    351           signedCounterIntroSort(buffer + length, buffer + 2 * length, buffer,
    352                                  sizeof(Value) - 1, functor);
     351          signedStableRadixIntroSort(buffer + length, buffer + 2 * length,
     352                                     buffer, sizeof(Value) - 1, functor);
    353353          std::copy(buffer, buffer + length, first);
    354354        }
    355355      } catch (...) {
     
    360360    }
    361361
    362362    template <typename Value, typename Iterator, typename Functor>
    363     void counterUnsignedSort(Iterator first, Iterator last, Functor functor) {
     363    void stableRadixUnsignedSort(Iterator first, Iterator last,
     364                                 Functor functor) {
    364365      if (first == last) return;
    365366      typedef typename std::iterator_traits<Iterator>::value_type Key;
    366367      typedef std::allocator<Key> Allocator;
     
    373374        std::copy(first, last, buffer);
    374375        for (int i = 0; i < int(sizeof(Value)); ++i) {
    375376          if (dir) {
    376             counterIntroSort(buffer, buffer + length,
    377                              buffer + length, i, functor);
     377            stableRadixIntroSort(buffer, buffer + length,
     378                                 buffer + length, i, functor);
    378379          } else {
    379             counterIntroSort(buffer + length, buffer + 2 * length,
    380                              buffer, i, functor);
     380            stableRadixIntroSort(buffer + length, buffer + 2 * length,
     381                                 buffer, i, functor);
    381382          }
    382383          dir = !dir;
    383384        }
     
    397398
    398399    template <typename Value,
    399400              bool sign = std::numeric_limits<Value>::is_signed >
    400     struct CounterSortSelector {
     401    struct StableRadixSortSelector {
    401402      template <typename Iterator, typename Functor>
    402403      static void sort(Iterator first, Iterator last, Functor functor) {
    403         counterSignedSort<Value>(first, last, functor);
     404        stableRadixSignedSort<Value>(first, last, functor);
    404405      }
    405406    };
    406407
    407408    template <typename Value>
    408     struct CounterSortSelector<Value, false> {
     409    struct StableRadixSortSelector<Value, false> {
    409410      template <typename Iterator, typename Functor>
    410411      static void sort(Iterator first, Iterator last, Functor functor) {
    411         counterUnsignedSort<Value>(first, last, functor);
     412        stableRadixUnsignedSort<Value>(first, last, functor);
    412413      }
    413414    };
    414415
     
    423424  /// order according to an integer mapping in the same as radixSort() does.
    424425  ///
    425426  /// This sorting algorithm is stable, i.e. the order of two equal
    426   /// element remains the same after the sorting.
     427  /// elements remains the same after the sorting.
    427428  ///
    428429  /// This sort algorithm  use a radix forward sort on the
    429430  /// bytes of the integer number. The algorithm sorts the items
     
    444445  /// signed or unsigned.
    445446  /// \sa radixSort()
    446447  template <typename Iterator, typename Functor>
    447   void counterSort(Iterator first, Iterator last, Functor functor) {
     448  void stableRadixSort(Iterator first, Iterator last, Functor functor) {
    448449    using namespace _radix_sort_bits;
    449450    typedef typename Functor::result_type Value;
    450     CounterSortSelector<Value>::sort(first, last, functor);
     451    StableRadixSortSelector<Value>::sort(first, last, functor);
    451452  }
    452453
    453454  template <typename Iterator, typename Value, typename Key>
    454   void counterSort(Iterator first, Iterator last, Value (*functor)(Key)) {
     455  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
    455456    using namespace _radix_sort_bits;
    456     CounterSortSelector<Value>::sort(first, last, functor);
     457    StableRadixSortSelector<Value>::sort(first, last, functor);
    457458  }
    458459
    459460  template <typename Iterator, typename Value, typename Key>
    460   void counterSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
     461  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
    461462    using namespace _radix_sort_bits;
    462     CounterSortSelector<Value>::sort(first, last, functor);
     463    StableRadixSortSelector<Value>::sort(first, last, functor);
    463464  }
    464465
    465466  template <typename Iterator, typename Value, typename Key>
    466   void counterSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
     467  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
    467468    using namespace _radix_sort_bits;
    468     CounterSortSelector<Value>::sort(first, last, functor);
     469    StableRadixSortSelector<Value>::sort(first, last, functor);
    469470  }
    470471
    471472  template <typename Iterator, typename Value, typename Key>
    472   void counterSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
     473  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
    473474    using namespace _radix_sort_bits;
    474     CounterSortSelector<Value>::sort(first, last, functor);
     475    StableRadixSortSelector<Value>::sort(first, last, functor);
    475476  }
    476477
    477478  template <typename Iterator>
    478   void counterSort(Iterator first, Iterator last) {
     479  void stableRadixSort(Iterator first, Iterator last) {
    479480    using namespace _radix_sort_bits;
    480481    typedef typename std::iterator_traits<Iterator>::value_type Value;
    481     CounterSortSelector<Value>::sort(first, last, Identity<Value>());
     482    StableRadixSortSelector<Value>::sort(first, last, Identity<Value>());
    482483  }
    483484
    484485}
  • test/radix_sort_test.cc

    diff -r 31d224a3c0af -r de16f1f2d228 test/radix_sort_test.cc
    a b  
    9999}
    100100
    101101
    102 void checkCounterSort() {
     102void checkStableRadixSort() {
    103103  {
    104104    std::vector<int> data1;
    105105    generateIntSequence(n, data1);
     
    107107    std::vector<int> data2(data1);
    108108    std::sort(data1.begin(), data1.end());
    109109
    110     counterSort(data2.begin(), data2.end());
     110    stableRadixSort(data2.begin(), data2.end());
    111111    for (int i = 0; i < n; ++i) {
    112112      check(data1[i] == data2[i], "Test failed");
    113113    }
    114114
    115     counterSort(data2.begin(), data2.end(), Negate());
     115    stableRadixSort(data2.begin(), data2.end(), Negate());
    116116    for (int i = 0; i < n; ++i) {
    117117      check(data1[i] == data2[n - 1 - i], "Test failed");
    118118    }
    119119
    120     counterSort(data2.begin(), data2.end(), negate);
     120    stableRadixSort(data2.begin(), data2.end(), negate);
    121121    for (int i = 0; i < n; ++i) {
    122122      check(data1[i] == data2[n - 1 - i], "Test failed");
    123123    }
     
    141141int main() {
    142142
    143143  checkRadixSort();
    144   checkCounterSort();
     144  checkStableRadixSort();
    145145
    146146  return 0;
    147147}