Pixie
Loading...
Searching...
No Matches
rank_select.h
Go to the documentation of this file.
1#pragma once
2
11
12#include <concepts>
13#include <cstddef>
14#include <cstdint>
15#include <string>
16
17namespace pixie {
18
31template <class Impl>
33 public:
39 std::size_t size() const { return impl().size_impl(); }
40
45 bool empty() const { return size() == 0; }
46
55 int operator[](std::size_t position) const {
56 return impl().bit_impl(position);
57 }
58
66 std::uint64_t rank(std::size_t end_position) const {
67 return impl().rank_impl(end_position);
68 }
69
78 std::uint64_t rank0(std::size_t end_position) const {
79 if constexpr (requires { impl().rank0_impl(end_position); }) {
80 return impl().rank0_impl(end_position);
81 } else {
82 return end_position >= size() ? size() - rank(size())
83 : end_position - rank(end_position);
84 }
85 }
86
95 std::uint64_t select(std::size_t rank) const {
96 return impl().select_impl(rank);
97 }
98
107 std::uint64_t select0(std::size_t rank) const {
108 return impl().select0_impl(rank);
109 }
110
117 bool supports_select1() const { return impl().supports_select1_impl(); }
118
125 bool supports_select0() const { return impl().supports_select0_impl(); }
126
135 std::size_t memory_usage_bytes() const
136 requires requires(const Impl& concrete) {
137 {
138 concrete.memory_usage_bytes_impl()
139 } -> std::convertible_to<std::size_t>;
140 }
141 {
142 return impl().memory_usage_bytes_impl();
143 }
144
149 std::string to_string() const {
150 std::string result;
151 result.reserve(size());
152 for (std::size_t i = 0; i < size(); ++i) {
153 result.push_back((*this)[i] ? '1' : '0');
154 }
155 return result;
156 }
157
158 private:
160 const Impl& impl() const { return static_cast<const Impl&>(*this); }
161};
162
163} // namespace pixie
CRTP facade for immutable rank/select queries and bit access.
Definition rank_select.h:32
bool empty() const
Check whether the bit vector is empty.
Definition rank_select.h:45
std::uint64_t select(std::size_t rank) const
Return the position of the rank-th one bit.
Definition rank_select.h:95
std::size_t memory_usage_bytes() const
Return owned index memory usage when provided by the implementation.
Definition rank_select.h:135
std::uint64_t select0(std::size_t rank) const
Return the position of the rank-th zero bit.
Definition rank_select.h:107
bool supports_select1() const
Check whether select queries for one bits are enabled.
Definition rank_select.h:117
std::uint64_t rank0(std::size_t end_position) const
Count zero bits in the prefix [0, end_position).
Definition rank_select.h:78
std::size_t size() const
Return the number of valid bits.
Definition rank_select.h:39
std::string to_string() const
Convert the logical bits to a binary string.
Definition rank_select.h:149
bool supports_select0() const
Check whether select queries for zero bits are enabled.
Definition rank_select.h:125
int operator[](std::size_t position) const
Read a bit by zero-based position.
Definition rank_select.h:55
std::uint64_t rank(std::size_t end_position) const
Count one bits in the prefix [0, end_position).
Definition rank_select.h:66