aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvcore/RadixSort.h
blob: 82325ebb2494d80e7807e5943588bf900614f43a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#ifndef NV_CORE_RADIXSORT_H
#define NV_CORE_RADIXSORT_H

// Based on Pierre Terdiman's and Michael Herf's source code.
// http://www.codercorner.com/RadixSortRevisited.htm
// http://www.stereopsis.com/radix.html

#include "nvcore.h"
#include "Array.h"

namespace nv
{

    class NVCORE_CLASS RadixSort
    {
        NV_FORBID_COPY(RadixSort);
    public:
        // Constructor/Destructor
        RadixSort();
        RadixSort(uint reserve_count);
        ~RadixSort();

        // Invalidate ranks.
        RadixSort & reset() { m_validRanks = false; return *this; }

        // Sorting methods.
        RadixSort & sort(const uint32 * input, uint count);
        RadixSort & sort(const uint64 * input, uint count);
        RadixSort & sort(const float * input, uint count);

        // Helpers.
        RadixSort & sort(const Array<uint32> & input);
        RadixSort & sort(const Array<uint64> & input);
        RadixSort & sort(const Array<float> & input);

        // Access to results. m_ranks is a list of indices in sorted order, i.e. in the order you may further process your data
        inline const uint * ranks() const { nvDebugCheck(m_validRanks); return m_ranks; }
        inline uint * ranks() { nvDebugCheck(m_validRanks); return m_ranks; }
        inline uint rank(uint i) const { nvDebugCheck(m_validRanks); return m_ranks[i]; }

        // query whether the sort has been performed
        inline bool valid() const { return m_validRanks; }

    private:
        uint m_size;
        uint * m_ranks;
        uint * m_ranks2;
        bool m_validRanks;

        // Internal methods
        template <typename T> void insertionSort(const T * input, uint count);
        template <typename T> void radixSort(const T * input, uint count);

        void checkResize(uint nb);
        void resize(uint nb);
    };

    inline RadixSort & RadixSort::sort(const Array<uint32> & input) {
        return sort(input.buffer(), input.count());
    }

    inline RadixSort & RadixSort::sort(const Array<uint64> & input) {
        return sort(input.buffer(), input.count());
    }

    inline RadixSort & RadixSort::sort(const Array<float> & input) {
        return sort(input.buffer(), input.count());
    }

} // nv namespace



#endif // NV_CORE_RADIXSORT_H