skarupke/ska_sort

Framework for custom "item comparators"

Opened this issue · 0 comments

I propose to make the following framework for custom "item comparators", and to implement built-in functionality on top of it.

Low-level API allows user to choose how many buckets are used on each pass:

template <class LowLevelCustomComparator>
class LowLevelSorter {...}  // Here we should perform all the real work!

template <typename T>
class LowLevelCustomComparator
{
    // Total number of radix-sort passes over data or 0 for variable amount
    static int passes;

    // How many buckets to use on this pass
    template <int pass>  static int buckets();

    // Compute bucket from the item
    template <int pass>  static int bucket (T item);

    // Bucket used for items that don't need any more sorting, i.e. 0 for strings
    template <int pass>  static int final_bucket();
}

High-level API just tells how many bits in the sort key. Library provides implementation of this API that calls into the low-level API using optimal (for this particular box) settings for each pass:

template <class HighLevelCustomComparator>
class HighLevelSorter {...}

template <typename T>
class HighLevelCustomComparator
{
    // Total number of bits in the key or 0 for variable amount
    static int bits;

    // Compute bucket from the item for some pass
    template<int first_bit, int last_bit>  static int bucket (T item);

    // Bucket used for items that don't need any more sorting, i.e. 0 for strings
    template<int first_bit, int last_bit>  static int final_bucket();
}

Finally, highest-level code should implement standard sorting of standard C++ types by constructing HighLevelCustomComparator from the type and provide user with tools to construct comparator f.e. for selected fields in the structure.