Commit 28c1bb40 authored by Johannes Braun's avatar Johannes Braun
Browse files

Update sort.h

parent 8733c893
......@@ -5,39 +5,39 @@
#include <functional>
#include <vector>
#include <util/templates.h>
namespace sort
{
template<bool Predicate, typename A, typename B> struct type_select { };
template<typename A, typename B> struct type_select<true, A, B> { using type = A; };
template<typename A, typename B> struct type_select<false, A, B> { using type = B; };
template<typename A> struct sized_uint {
using type = typename type_select<
sizeof(A) == sizeof(uint8_t), uint8_t, typename type_select<
sizeof(A) == sizeof(uint16_t), uint16_t, typename type_select<
sizeof(A) == sizeof(uint32_t), uint32_t, typename type_select<
sizeof(A) == sizeof(uint64_t), uint64_t, void
//For any type smaller or with equal size to uint64_t: type is the unsigned int equivalent sized equally
template<typename T> struct sized_uint {
using type = typename templates::decide_on<
sizeof(T) == sizeof(uint8_t), uint8_t, typename templates::decide_on<
sizeof(T) == sizeof(uint16_t), uint16_t, typename templates::decide_on<
sizeof(T) == sizeof(uint32_t), uint32_t, typename templates::decide_on<
sizeof(T) == sizeof(uint64_t), uint64_t, void
>::type
>::type
>::type
>::type;
};
template<typename A, typename B = sized_uint<A>::type> B valueOf(A a)
template<typename TIn, typename TOut = sized_uint<TIn>::type> TOut valueOf(TIn in)
{
return *reinterpret_cast<B*>(&a);
return *reinterpret_cast<TOut*>(&in);
}
template<typename Gen, typename BaseType>
inline void radix(std::vector<Gen> &data, std::function<BaseType(const Gen&)> get)
template<typename TData, typename TBase>
inline void radix(std::vector<TData> &data, std::function<TBase(const TData&)> get)
{
const size_t m_2DMaxZ = data.size();
Gen* sorted = data.data();
Gen* countSort = new Gen[m_2DMaxZ];
TData* sorted = data.data();
TData* countSort = new TData[m_2DMaxZ];
unsigned counting[16];
size_t size = data.size();
const unsigned d = static_cast<unsigned>(2 * sizeof(BaseType));
for (sized_uint<BaseType>::type a = 0xf, b = 0; a <= 0xf << (4 * (d - 1)) && a != 0; a <<= 4, ++b)
const unsigned d = static_cast<unsigned>(2 * sizeof(TBase));
for (sized_uint<TBase>::type a = 0xf, b = 0; a <= 0xf << (4 * (d - 1)) && a != 0; a <<= 4, ++b)
{
for (size_t c = 0; c < 16; ++c)
{
......@@ -67,10 +67,10 @@ namespace sort
delete countSort;
}
template<typename Base>
inline void radix(std::vector<Base> &data)
template<typename TBase>
inline void radix(std::vector<TBase> &data)
{
advancedSort<Base, Base>(data, [](const Base &data)
advancedSort<TBase, TBase>(data, [](const Base &data)
{
return data;
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment