2015-11-21 12:47:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string.h>
|
2016-12-24 01:03:10 +00:00
|
|
|
#if !defined(__APPLE__) && !defined(__FreeBSD__)
|
2015-11-21 12:47:41 +00:00
|
|
|
#include <malloc.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
|
|
|
#include <cstdlib>
|
2015-11-21 12:47:41 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/bit_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <Core/Defines.h>
|
2015-11-21 12:47:41 +00:00
|
|
|
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
/** Radix sort, has the following functionality:
|
2017-05-07 20:25:26 +00:00
|
|
|
* Can sort unsigned, signed numbers, and floats.
|
|
|
|
* Can sort an array of fixed length elements that contain something else besides the key.
|
2017-05-10 04:00:19 +00:00
|
|
|
* Customizable radix size.
|
2015-11-23 20:24:54 +00:00
|
|
|
*
|
|
|
|
* LSB, stable.
|
2017-05-07 20:25:26 +00:00
|
|
|
* NOTE For some applications it makes sense to add MSB-radix-sort,
|
|
|
|
* as well as radix-select, radix-partial-sort, radix-get-permutation algorithms based on it.
|
2015-11-21 12:47:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/** Used as a template parameter. See below.
|
2015-11-21 12:47:41 +00:00
|
|
|
*/
|
|
|
|
struct RadixSortMallocAllocator
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
void * allocate(size_t size)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
2017-12-01 18:36:55 +00:00
|
|
|
void deallocate(void * ptr, size_t /*size*/)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return free(ptr);
|
|
|
|
}
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/** A transformation that transforms the bit representation of a key into an unsigned integer number,
|
|
|
|
* that the order relation over the keys will match the order relation over the obtained unsigned numbers.
|
|
|
|
* For floats this conversion does the following:
|
|
|
|
* if the signed bit is set, it flips all other bits.
|
|
|
|
* In this case, NaN-s are bigger than all normal numbers.
|
2015-11-21 12:47:41 +00:00
|
|
|
*/
|
|
|
|
template <typename KeyBits>
|
|
|
|
struct RadixSortFloatTransform
|
|
|
|
{
|
2017-05-10 04:00:19 +00:00
|
|
|
/// Is it worth writing the result in memory, or is it better to do calculation every time again?
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr bool transform_is_simple = false;
|
|
|
|
|
|
|
|
static KeyBits forward(KeyBits x)
|
|
|
|
{
|
2018-06-25 07:29:53 +00:00
|
|
|
return x ^ ((-(x >> (sizeof(KeyBits) * 8 - 1))) | (KeyBits(1) << (sizeof(KeyBits) * 8 - 1)));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static KeyBits backward(KeyBits x)
|
|
|
|
{
|
|
|
|
return x ^ (((x >> (sizeof(KeyBits) * 8 - 1)) - 1) | (KeyBits(1) << (sizeof(KeyBits) * 8 - 1)));
|
|
|
|
}
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-04-12 12:48:00 +00:00
|
|
|
template <typename _Element, typename _Key = _Element>
|
2015-11-21 12:47:41 +00:00
|
|
|
struct RadixSortFloatTraits
|
|
|
|
{
|
2019-04-12 12:48:00 +00:00
|
|
|
using Element = _Element; /// The type of the element. It can be a structure with a key and some other payload. Or just a key.
|
|
|
|
using Key = _Key; /// The key to sort.
|
2017-05-07 20:25:26 +00:00
|
|
|
using CountType = uint32_t; /// Type for calculating histograms. In the case of a known small number of elements, it can be less than size_t.
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// The type to which the key is transformed to do bit operations. This UInt is the same size as the key.
|
2019-04-12 12:48:00 +00:00
|
|
|
using KeyBits = std::conditional_t<sizeof(_Key) == 8, uint64_t, uint32_t>;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
static constexpr size_t PART_SIZE_BITS = 8; /// With what pieces of the key, in bits, to do one pass - reshuffle of the array.
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Converting a key into KeyBits is such that the order relation over the key corresponds to the order relation over KeyBits.
|
2017-04-01 07:20:54 +00:00
|
|
|
using Transform = RadixSortFloatTransform<KeyBits>;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// An object with the functions allocate and deallocate.
|
|
|
|
/// Can be used, for example, to allocate memory for a temporary array on the stack.
|
|
|
|
/// To do this, the allocator itself is created on the stack.
|
2017-04-01 07:20:54 +00:00
|
|
|
using Allocator = RadixSortMallocAllocator;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// The function to get the key from an array element.
|
2019-04-12 12:48:00 +00:00
|
|
|
static Key & extractKey(Element & elem)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<Element, Key>)
|
|
|
|
return elem;
|
|
|
|
else
|
|
|
|
return *reinterpret_cast<Key *>(&elem);
|
|
|
|
}
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
|
|
|
|
2019-02-18 16:29:37 +00:00
|
|
|
template <typename Float>
|
2019-03-06 21:58:40 +00:00
|
|
|
struct RadixSortPairFloatKeyTraits
|
2019-02-18 16:29:37 +00:00
|
|
|
{
|
|
|
|
using Element = std::pair<Float, size_t>;
|
|
|
|
using Key = Float;
|
|
|
|
using CountType = uint32_t;
|
|
|
|
using KeyBits = std::conditional_t<sizeof(Float) == 8, uint64_t, uint32_t>;
|
|
|
|
|
|
|
|
static constexpr size_t PART_SIZE_BITS = 8;
|
|
|
|
|
|
|
|
using Transform = RadixSortFloatTransform<KeyBits>;
|
|
|
|
using Allocator = RadixSortMallocAllocator;
|
|
|
|
|
|
|
|
/// The function to get the key from an array element.
|
|
|
|
static Key & extractKey(Element & elem) { return elem.first; }
|
|
|
|
};
|
2015-11-21 12:47:41 +00:00
|
|
|
|
|
|
|
template <typename KeyBits>
|
|
|
|
struct RadixSortIdentityTransform
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr bool transform_is_simple = true;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static KeyBits forward(KeyBits x) { return x; }
|
2017-05-10 04:00:19 +00:00
|
|
|
static KeyBits backward(KeyBits x) { return x; }
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename KeyBits>
|
|
|
|
struct RadixSortSignedTransform
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr bool transform_is_simple = true;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static KeyBits forward(KeyBits x) { return x ^ (KeyBits(1) << (sizeof(KeyBits) * 8 - 1)); }
|
2017-05-10 04:00:19 +00:00
|
|
|
static KeyBits backward(KeyBits x) { return x ^ (KeyBits(1) << (sizeof(KeyBits) * 8 - 1)); }
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-04-12 12:48:00 +00:00
|
|
|
template <typename _Element, typename _Key = _Element>
|
2015-11-21 12:47:41 +00:00
|
|
|
struct RadixSortUIntTraits
|
|
|
|
{
|
2019-04-12 12:48:00 +00:00
|
|
|
using Element = _Element;
|
|
|
|
using Key = _Key;
|
2017-04-01 07:20:54 +00:00
|
|
|
using CountType = uint32_t;
|
2019-04-12 12:48:00 +00:00
|
|
|
using KeyBits = _Key;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr size_t PART_SIZE_BITS = 8;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using Transform = RadixSortIdentityTransform<KeyBits>;
|
|
|
|
using Allocator = RadixSortMallocAllocator;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// The function to get the key from an array element.
|
2019-04-12 12:48:00 +00:00
|
|
|
static Key & extractKey(Element & elem)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<Element, Key>)
|
|
|
|
return elem;
|
|
|
|
else
|
|
|
|
return *reinterpret_cast<Key *>(&elem);
|
|
|
|
}
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
|
|
|
|
2019-04-12 12:48:00 +00:00
|
|
|
template <typename _Element, typename _Key = _Element>
|
2015-11-21 12:47:41 +00:00
|
|
|
struct RadixSortIntTraits
|
|
|
|
{
|
2019-04-12 12:48:00 +00:00
|
|
|
using Element = _Element;
|
|
|
|
using Key = _Key;
|
2017-04-01 07:20:54 +00:00
|
|
|
using CountType = uint32_t;
|
2019-04-12 12:48:00 +00:00
|
|
|
using KeyBits = std::make_unsigned_t<_Key>;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr size_t PART_SIZE_BITS = 8;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using Transform = RadixSortSignedTransform<KeyBits>;
|
|
|
|
using Allocator = RadixSortMallocAllocator;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// The function to get the key from an array element.
|
2019-04-12 12:48:00 +00:00
|
|
|
static Key & extractKey(Element & elem)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<Element, Key>)
|
|
|
|
return elem;
|
|
|
|
else
|
|
|
|
return *reinterpret_cast<Key *>(&elem);
|
|
|
|
}
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
|
|
|
|
2019-02-18 16:29:37 +00:00
|
|
|
template <typename Int>
|
2019-03-06 21:58:40 +00:00
|
|
|
struct RadixSortPairIntKeyTraits
|
2019-02-18 16:29:37 +00:00
|
|
|
{
|
|
|
|
using Element = std::pair<Int, size_t>;
|
|
|
|
using Key = Int;
|
|
|
|
using CountType = uint32_t;
|
|
|
|
using KeyBits = std::make_unsigned_t<Int>;
|
|
|
|
|
|
|
|
static constexpr size_t PART_SIZE_BITS = 8;
|
|
|
|
|
|
|
|
using Transform = RadixSortSignedTransform<KeyBits>;
|
|
|
|
using Allocator = RadixSortMallocAllocator;
|
|
|
|
|
|
|
|
/// The function to get the key from an array element.
|
|
|
|
static Key & extractKey(Element & elem) { return elem.first; }
|
|
|
|
};
|
|
|
|
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2019-03-26 21:55:37 +00:00
|
|
|
// Allow std::pair copying
|
|
|
|
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ >= 8)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wclass-memaccess"
|
|
|
|
#endif
|
2015-11-21 12:47:41 +00:00
|
|
|
template <typename Traits>
|
|
|
|
struct RadixSort
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Element = typename Traits::Element;
|
|
|
|
using Key = typename Traits::Key;
|
2017-05-10 04:00:19 +00:00
|
|
|
using CountType = typename Traits::CountType;
|
2017-04-01 07:20:54 +00:00
|
|
|
using KeyBits = typename Traits::KeyBits;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr size_t HISTOGRAM_SIZE = 1 << Traits::PART_SIZE_BITS;
|
|
|
|
static constexpr size_t PART_BITMASK = HISTOGRAM_SIZE - 1;
|
|
|
|
static constexpr size_t KEY_BITS = sizeof(Key) * 8;
|
|
|
|
static constexpr size_t NUM_PASSES = (KEY_BITS + (Traits::PART_SIZE_BITS - 1)) / Traits::PART_SIZE_BITS;
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static ALWAYS_INLINE KeyBits getPart(size_t N, KeyBits x)
|
|
|
|
{
|
|
|
|
if (Traits::Transform::transform_is_simple)
|
|
|
|
x = Traits::Transform::forward(x);
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (x >> (N * Traits::PART_SIZE_BITS)) & PART_BITMASK;
|
|
|
|
}
|
2015-11-21 12:47:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static KeyBits keyToBits(Key x) { return ext::bit_cast<KeyBits>(x); }
|
|
|
|
static Key bitsToKey(KeyBits x) { return ext::bit_cast<Key>(x); }
|
2015-11-21 12:47:41 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static void execute(Element * arr, size_t size)
|
|
|
|
{
|
2017-05-07 20:25:26 +00:00
|
|
|
/// If the array is smaller than 256, then it is better to use another algorithm.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
/// There are loops of NUM_PASSES. It is very important that they are unfolded at compile-time.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
/// For each of the NUM_PASSES bit ranges of the key, consider how many times each value of this bit range met.
|
2017-04-01 07:20:54 +00:00
|
|
|
CountType histograms[HISTOGRAM_SIZE * NUM_PASSES] = {0};
|
|
|
|
|
|
|
|
typename Traits::Allocator allocator;
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// We will do several passes through the array. On each pass, the data is transferred to another array. Let's allocate this temporary array.
|
2017-04-01 07:20:54 +00:00
|
|
|
Element * swap_buffer = reinterpret_cast<Element *>(allocator.allocate(size * sizeof(Element)));
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Transform the array and calculate the histogram.
|
2017-09-12 21:06:11 +00:00
|
|
|
/// NOTE This is slightly suboptimal. Look at https://github.com/powturbo/TurboHist
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
if (!Traits::Transform::transform_is_simple)
|
|
|
|
Traits::extractKey(arr[i]) = bitsToKey(Traits::Transform::forward(keyToBits(Traits::extractKey(arr[i]))));
|
|
|
|
|
|
|
|
for (size_t j = 0; j < NUM_PASSES; ++j)
|
|
|
|
++histograms[j * HISTOGRAM_SIZE + getPart(j, keyToBits(Traits::extractKey(arr[i])))];
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Replace the histograms with the accumulated sums: the value in position i is the sum of the previous positions minus one.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t sums[NUM_PASSES] = {0};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < HISTOGRAM_SIZE; ++i)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < NUM_PASSES; ++j)
|
|
|
|
{
|
|
|
|
size_t tmp = histograms[j * HISTOGRAM_SIZE + i] + sums[j];
|
|
|
|
histograms[j * HISTOGRAM_SIZE + i] = sums[j] - 1;
|
|
|
|
sums[j] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Move the elements in the order starting from the least bit piece, and then do a few passes on the number of pieces.
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t j = 0; j < NUM_PASSES; ++j)
|
|
|
|
{
|
|
|
|
Element * writer = j % 2 ? arr : swap_buffer;
|
|
|
|
Element * reader = j % 2 ? swap_buffer : arr;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
size_t pos = getPart(j, keyToBits(Traits::extractKey(reader[i])));
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Place the element on the next free position.
|
2017-04-01 07:20:54 +00:00
|
|
|
auto & dest = writer[++histograms[j * HISTOGRAM_SIZE + pos]];
|
|
|
|
dest = reader[i];
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// On the last pass, we do the reverse transformation.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!Traits::Transform::transform_is_simple && j == NUM_PASSES - 1)
|
|
|
|
Traits::extractKey(dest) = bitsToKey(Traits::Transform::backward(keyToBits(Traits::extractKey(reader[i]))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// If the number of passes is odd, the result array is in a temporary buffer. Copy it to the place of the original array.
|
2017-05-10 04:00:19 +00:00
|
|
|
/// NOTE Sometimes it will be more optimal to provide non-destructive interface, that will not modify original array.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (NUM_PASSES % 2)
|
|
|
|
memcpy(arr, swap_buffer, size * sizeof(Element));
|
|
|
|
|
|
|
|
allocator.deallocate(swap_buffer, size * sizeof(Element));
|
|
|
|
}
|
2015-11-21 12:47:41 +00:00
|
|
|
};
|
2019-03-26 21:55:37 +00:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ >= 8)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2015-11-21 12:47:41 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2017-12-25 04:01:46 +00:00
|
|
|
std::enable_if_t<std::is_unsigned_v<T> && std::is_integral_v<T>, void>
|
2015-11-21 12:47:41 +00:00
|
|
|
radixSort(T * arr, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return RadixSort<RadixSortUIntTraits<T>>::execute(arr, size);
|
2015-11-21 12:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-12-25 04:01:46 +00:00
|
|
|
std::enable_if_t<std::is_signed_v<T> && std::is_integral_v<T>, void>
|
2015-11-21 12:47:41 +00:00
|
|
|
radixSort(T * arr, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return RadixSort<RadixSortIntTraits<T>>::execute(arr, size);
|
2015-11-21 12:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-12-25 04:01:46 +00:00
|
|
|
std::enable_if_t<std::is_floating_point_v<T>, void>
|
2015-11-21 12:47:41 +00:00
|
|
|
radixSort(T * arr, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return RadixSort<RadixSortFloatTraits<T>>::execute(arr, size);
|
2015-11-21 12:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 12:48:00 +00:00
|
|
|
template <typename _Element, typename _Key>
|
|
|
|
std::enable_if_t<std::is_integral_v<_Key>, void>
|
|
|
|
radixSort(_Element * arr, size_t size)
|
|
|
|
{
|
|
|
|
return RadixSort<RadixSortUIntTraits<_Element, _Key>>::execute(arr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename _Element, typename _Key>
|
|
|
|
std::enable_if_t<std::is_floating_point_v<_Key>, void>
|
|
|
|
radixSort(_Element * arr, size_t size)
|
|
|
|
{
|
|
|
|
return RadixSort<RadixSortFloatTraits<_Element, _Key>>::execute(arr, size);
|
|
|
|
}
|