HighMap library (C++)
Loading...
Searching...
No Matches
radix_heap.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
12#pragma once
13
14#include <algorithm>
15#include <cstddef>
16#include <cstdint>
17#include <cstring>
18#include <utility>
19#include <vector>
20
21#include <bit>
22
23namespace hmap
24{
25
26// order-preserving mapping between IEEE-754 float and uint32_t for radix
27// sorting
28inline uint32_t float_key(float f)
29{
30 uint32_t u = 0;
31 std::memcpy(&u, &f, 4);
32 return (u & 0x80000000u) ? ~u : (u | 0x80000000u);
33}
34
35inline float key_float(uint32_t k)
36{
37 uint32_t u = (k & 0x80000000u) ? (k & 0x7fffffffu) : ~k;
38 float f = 0.f;
39 std::memcpy(&f, &u, 4);
40 return f;
41}
42
43inline float next_up(float f)
44{
45 return key_float(float_key(f) + 1u);
46}
47
48// monotone priority queue (radix heap) for fast exact flood / Dijkstra
49template <typename T = int> struct RadixHeap
50{
51 std::vector<std::pair<uint32_t, T>> bucket[33];
52 uint32_t last = 0;
53 size_t count = 0;
54
55 bool empty() const
56 {
57 return count == 0;
58 }
59
60 size_t size() const
61 {
62 return count;
63 }
64
65 void clear()
66 {
67 for (auto &b : bucket)
68 b.clear();
69
70 last = 0;
71 count = 0;
72 }
73
74 static int slot(uint32_t x)
75 {
76 return (int)std::bit_width(x);
77 }
78
79 void push(uint32_t key, const T &val)
80 {
81 bucket[slot(key ^ last)].emplace_back(key, val);
82 ++count;
83 }
84
85 void push(uint32_t key, T &&val)
86 {
87 bucket[slot(key ^ last)].emplace_back(key, std::move(val));
88 ++count;
89 }
90
91 std::pair<uint32_t, T> pop()
92 {
93 if (bucket[0].empty())
94 {
95 int i = 1;
96 while (bucket[i].empty())
97 ++i;
98
99 uint32_t mn = 0xffffffffu;
100 for (const auto &e : bucket[i])
101 mn = std::min(mn, e.first);
102
103 last = mn;
104 for (auto &e : bucket[i])
105 bucket[slot(e.first ^ last)].push_back(std::move(e));
106
107 bucket[i].clear();
108 }
109
110 auto e = std::move(bucket[0].back());
111 bucket[0].pop_back();
112 --count;
113 return e;
114 }
115};
116
117} // namespace hmap
float f(int i, float gi)
Definition distance_transform.cpp:14
Definition opencv.hpp:20
float key_float(uint32_t k)
Definition radix_heap.hpp:35
float next_up(float f)
Definition radix_heap.hpp:43
uint32_t float_key(float f)
Definition radix_heap.hpp:28
Definition radix_heap.hpp:50
static int slot(uint32_t x)
Definition radix_heap.hpp:74
size_t count
Definition radix_heap.hpp:53
bool empty() const
Definition radix_heap.hpp:55
std::vector< std::pair< uint32_t, T > > bucket[33]
Definition radix_heap.hpp:51
void clear()
Definition radix_heap.hpp:65
size_t size() const
Definition radix_heap.hpp:60
uint32_t last
Definition radix_heap.hpp:52
void push(uint32_t key, T &&val)
Definition radix_heap.hpp:85
void push(uint32_t key, const T &val)
Definition radix_heap.hpp:79
std::pair< uint32_t, T > pop()
Definition radix_heap.hpp:91