Simo 0.0.1
Loading...
Searching...
No Matches
RadixHeap.h
1// Copyright 2026 Matteo Fusi and Contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SIMO_RADIXHEAP_HH
16#define SIMO_RADIXHEAP_HH
17
18#include <algorithm>
19#include <array>
20#include <cstdint>
21#include <limits>
22#include <vector>
23
24namespace Simo::Internal {
25class RadixHeap {
26 public:
27 constexpr RadixHeap() : bucket_mins() {
28 std::ranges::fill(bucket_mins, std::numeric_limits<uint64_t>::max());
29 }
30
31 void push(uint64_t val);
32
33 [[nodiscard]]
34 uint64_t peek() const;
35
36 void pop();
37
38 [[nodiscard]]
39 bool empty() const;
40
41 [[nodiscard]]
42 size_t size() const;
43
44 private:
45 static size_t get_bucket_index(const uint64_t min_val, const uint64_t val) {
46 const uint64_t xor_op = val ^ min_val;
47 const int leading_zeroes = std::countl_zero<uint64_t>(xor_op);
48 return NUM_BITS - leading_zeroes;
49 }
50
51 static constexpr size_t NUM_BITS = sizeof(uint64_t) * 8U;
52 static constexpr size_t NUM_BUCKETS = NUM_BITS + 1U;
53
54 std::array<std::vector<uint64_t>, NUM_BUCKETS> buckets{};
55 std::array<uint64_t, NUM_BUCKETS> bucket_mins;
56 size_t size_val{0};
57};
58} // namespace Simo::Internal
59
60#endif // SIMO_RADIXHEAP_HH