Pixie
Loading...
Searching...
No Matches
rank_select.h
Go to the documentation of this file.
1#pragma once
2
10
11#include <concepts>
12#include <cstddef>
13#include <cstdint>
14#include <string>
15
16namespace pixie {
17
24template <class Impl>
26 public:
31 std::size_t size() const { return impl().size_impl(); }
32
37 bool empty() const { return size() == 0; }
38
44 int operator[](std::size_t position) const {
45 return impl().bit_impl(position);
46 }
47
53 std::uint64_t rank(std::size_t end_position) const {
54 return impl().rank_impl(end_position);
55 }
56
62 std::uint64_t rank0(std::size_t end_position) const {
63 return end_position >= size() ? size() - rank(size())
64 : end_position - rank(end_position);
65 }
66
72 std::uint64_t select(std::size_t rank) const {
73 return impl().select_impl(rank);
74 }
75
81 std::uint64_t select0(std::size_t rank) const {
82 return impl().select0_impl(rank);
83 }
84
89 bool supports_select1() const { return impl().supports_select1_impl(); }
90
95 bool supports_select0() const { return impl().supports_select0_impl(); }
96
101 std::size_t memory_usage_bytes() const
102 requires requires(const Impl& concrete) {
103 {
104 concrete.memory_usage_bytes_impl()
105 } -> std::convertible_to<std::size_t>;
106 }
107 {
108 return impl().memory_usage_bytes_impl();
109 }
110
115 std::string to_string() const {
116 std::string result;
117 result.reserve(size());
118 for (std::size_t i = 0; i < size(); ++i) {
119 result.push_back((*this)[i] ? '1' : '0');
120 }
121 return result;
122 }
123
124 private:
126 const Impl& impl() const { return static_cast<const Impl&>(*this); }
127};
128
129} // namespace pixie
CRTP facade for immutable rank/select queries and bit access.
Definition rank_select.h:25
bool empty() const
Check whether the bit vector is empty.
Definition rank_select.h:37
std::uint64_t select(std::size_t rank) const
Return the position of the rank-th one bit.
Definition rank_select.h:72
std::size_t memory_usage_bytes() const
Return owned index memory usage when provided by the implementation.
Definition rank_select.h:101
std::uint64_t select0(std::size_t rank) const
Return the position of the rank-th zero bit.
Definition rank_select.h:81
bool supports_select1() const
Check whether select queries for one bits are enabled.
Definition rank_select.h:89
std::uint64_t rank0(std::size_t end_position) const
Count zero bits in the prefix [0, end_position).
Definition rank_select.h:62
std::size_t size() const
Return the number of valid bits.
Definition rank_select.h:31
std::string to_string() const
Convert the logical bits to a binary string.
Definition rank_select.h:115
bool supports_select0() const
Check whether select queries for zero bits are enabled.
Definition rank_select.h:95
int operator[](std::size_t position) const
Read a bit by zero-based position.
Definition rank_select.h:44
std::uint64_t rank(std::size_t end_position) const
Count one bits in the prefix [0, end_position).
Definition rank_select.h:53