libhatchet
Loading...
Searching...
No Matches
hxradix_sort.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
8
9#include "libhatchet.h"
10
11// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
12#if HX_USE_MACROS_WITH_MODULE
13#error Header does not provide macros alone.
14#endif
15
16HX_NS_BEGIN_
17
21template<typename key_t_, typename value_t_>
23public:
25 hxradix_sort_key(key_t_ key_, value_t_ value_) { this->set_(key_, value_); }
26
28 void set(key_t_ key_, value_t_ value_) { this->set_(key_, value_); }
29
31 hxattr_nodiscard value_t_ get_value(void) const { return m_value_; }
32
34 hxattr_nodiscard value_t_ get_value(void) { return m_value_; }
35
38 bool operator<(hxradix_sort_key x_) const { return m_key_ < x_.m_key_; }
39
41 hxattr_nodiscard uint32_t get_modified_key(void) const { return m_key_; }
42
43private:
44 // Required by the implementation of hxradix_sort_void/hxradix_sort_void11.
45 // Use a pointer or small struct for value_t. Otherwise the implementation
46 // does too much copying.
47 static_assert(sizeof(void*) >= sizeof(value_t_), "value_t size too big");
48
49 // Internal. Possible conversion routines.
50 void set_(int8_t key_, value_t_ value_) { this->set_(static_cast<int32_t>(key_), value_); }
51 void set_(uint8_t key_, value_t_ value_) { m_key_=key_; m_value_=value_; }
52 void set_(int16_t key_, value_t_ value_) { this->set_(static_cast<int32_t>(key_), value_); }
53 void set_(uint16_t key_, value_t_ value_) { m_key_=key_; m_value_=value_; }
54 void set_(int32_t key_, value_t_ value_) {
55 m_key_ = static_cast<uint32_t>(key_) ^ 0x80000000u;
56 m_value_ = value_;
57 }
58 void set_(int64_t key_, value_t_ value_) = delete; // Not supported.
59 void set_(uint64_t key_, value_t_ value_) = delete; // Not supported.
60 void set_(uint32_t key_, value_t_ value_) { m_key_=key_; m_value_=value_; }
61 void set_(float key_, value_t_ value_) {
62 // Reinterpret a float as a signed int in order to use a sign extending
63 // right shift before switching to well-defined unsigned bit ops.
64 int32_t t_ = 0; ::memcpy(&t_, &key_, sizeof t_);
65 m_key_ = static_cast<uint32_t>(t_) ^ (static_cast<uint32_t>(t_ >> 31) | 0x80000000u);
66 m_value_ = value_;
67 }
68 void set_(double key_, value_t_ value_) = delete; // Not supported.
69
70 // The key used for sorting. May not be preserved in a usable form.
71 uint32_t m_key_;
72 union {
73 value_t_ m_value_;
74 // Forces m_value_ to occupy the same space as a void*.
76 };
77};
78
82
92 hxsystem_allocator_t allocator_);
93
103 hxsystem_allocator_t allocator_);
104
129template<typename key_t_, typename value_t_> hxattr_nonnull(1,2) hxattr_hot
130void hxradix_sort(hxradix_sort_key<key_t_, value_t_>* begin_, hxradix_sort_key<key_t_,
131 value_t_>* end_, hxsystem_allocator_t allocator_) {
132 hxradix_sort_void(reinterpret_cast<hxradix_sort_key_void*>(begin_),
133 reinterpret_cast<hxradix_sort_key_void*>(end_), allocator_);
134}
135
147template<typename key_t_, typename value_t_> hxattr_nonnull(1,2) hxattr_hot
148void hxradix_sort11(hxradix_sort_key<key_t_, value_t_>* begin_,
149 hxradix_sort_key<key_t_, value_t_>* end_, hxsystem_allocator_t allocator_) {
150 hxradix_sort_void11(reinterpret_cast<hxradix_sort_key_void*>(begin_),
151 reinterpret_cast<hxradix_sort_key_void*>(end_), allocator_);
152}
153
154HX_NS_END_
hxradix_sort_key - A key-value pair used with hxradix_sort.
Definition hxradix_sort.hpp:22
void * m_value_
Definition hxradix_sort.hpp:73
void set(key_t_ key_, value_t_ value_)
Sets from the required key_t type and value_t type.
Definition hxradix_sort.hpp:28
value_t_ get_value(void)
Returns the stored value_t.
Definition hxradix_sort.hpp:34
void * m_void_ptr_
Definition hxradix_sort.hpp:75
value_t_ get_value(void) const
Returns the stored value_t.
Definition hxradix_sort.hpp:31
hxradix_sort_key(key_t_ key_, value_t_ value_)
Constructs from the required key_t type and value_t type.
Definition hxradix_sort.hpp:25
bool operator<(hxradix_sort_key x_) const
Comparison operator for comparison sorting hxradix_sort_key objects by key as a fallback for short ar...
Definition hxradix_sort.hpp:38
uint32_t get_modified_key(void) const
A version of the key that may have been modified to work as a uint32_t.
Definition hxradix_sort.hpp:41
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:77
void hxradix_sort(hxradix_sort_key< key_t_, value_t_ > *begin_, hxradix_sort_key< key_t_, value_t_ > *end_, hxsystem_allocator_t allocator_)
hxradix_sort - Sorts an array of hxradix_sort_key<key_t, value_t> by key_t using 8-bit digits.
Definition hxradix_sort.hpp:130
hxradix_sort_key< uint32_t, void * > hxradix_sort_key_void
hxradix_sort_key_void - Internal.
Definition hxradix_sort.hpp:81
void hxradix_sort_void11(hxradix_sort_key_void *begin_, hxradix_sort_key_void *end_, hxsystem_allocator_t allocator_)
hxradix_sort_void11 - Internal.
void hxradix_sort_void(hxradix_sort_key_void *begin_, hxradix_sort_key_void *end_, hxsystem_allocator_t allocator_)
hxradix_sort_void - Internal.
void hxradix_sort11(hxradix_sort_key< key_t_, value_t_ > *begin_, hxradix_sort_key< key_t_, value_t_ > *end_, hxsystem_allocator_t allocator_)
hxradix_sort11 - Sorts an array of hxradix_sort_key<key_t, value_t> by key_t using 11-bit digits.
Definition hxradix_sort.hpp:148
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:97
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
#define hxattr_hot
hxattr_hot - Optimize a function more aggressively.
Definition hxsettings.h:86
Provides core macros, memory management and feature detection.