Pixie
Loading...
Searching...
No Matches
succinct_bitvec.h
1#pragma once
2
3#ifndef PIXIE_SUCCINCT_BITVEC_SUPPORT
4#error "SuccinctFastRankSelectSupport requires PIXIE_THIRD_PARTY_BACKENDS"
5#endif
6
7#include <pixie/rank_select.h>
8
9#include <algorithm>
10#include <bitvec.hpp>
11#include <cstddef>
12#include <cstdint>
13#include <limits>
14#include <span>
15#include <stdexcept>
16
17namespace pixie {
18
31 : public RankSelectBase<SuccinctFastRankSelectSupport> {
32 public:
34 static constexpr std::uint64_t kMaxBits = std::uint64_t{1} << 32;
35
44 std::span<const std::uint64_t> source_words,
45 std::size_t num_bits)
46 : bits_(checked_size(source_words, num_bits)),
47 support_(copy_source(bits_, source_words)) {}
48
51 const SuccinctFastRankSelectSupport&) = delete;
54 delete;
55
57 std::size_t size_impl() const { return bits_.len; }
58
60 int bit_impl(std::size_t position) const { return bits_.get(position); }
61
63 std::uint64_t rank_impl(std::size_t end) const {
64 if (end >= size()) {
65 return support_.get_total_ones();
66 }
67 return end == 0 ? 0 : support_.rank1(end - 1);
68 }
69
72 std::uint64_t select_impl(std::size_t occurrence) const {
73 if (occurrence == 0) {
74 return 0;
75 }
76 if (occurrence > support_.get_total_ones()) {
77 return size();
78 }
79 return static_cast<std::uint64_t>(support_.select1(occurrence - 1));
80 }
81
83 bool supports_select1_impl() const { return true; }
85 bool supports_select0_impl() const { return false; }
87 std::uint64_t select0_impl(std::size_t occurrence) const {
88 return occurrence == 0 ? 0 : size();
89 }
90
92 std::size_t source_copy_bytes() const {
93 return bits_.words.capacity() * sizeof(std::uint64_t);
94 }
95
100 std::size_t index_logical_bytes() const {
101 return sizeof(*this) + support_.overhead_bytes();
102 }
103
105 std::size_t memory_usage_bytes_impl() const {
107 }
108
109 private:
110 static std::size_t checked_size(std::span<const std::uint64_t> source,
111 std::size_t num_bits) {
112 if (source.size() <= std::numeric_limits<std::size_t>::max() / 64) {
113 num_bits = std::min(num_bits, source.size() * 64);
114 }
115 if (num_bits > kMaxBits) {
116 throw std::length_error(
117 "FastBitVec select samples support at most 2^32 bits");
118 }
119 return num_bits;
120 }
121
122 static const succinct::RawBits& copy_source(
123 succinct::RawBits& bits,
124 std::span<const std::uint64_t> source) {
125 const std::size_t word_count = std::min(bits.num_words, source.size());
126 std::copy_n(source.begin(), word_count, bits.words.begin());
127 if (bits.len % 64 != 0) {
128 bits.words.back() &= (std::uint64_t{1} << (bits.len % 64)) - 1;
129 }
130 return bits;
131 }
132
133 succinct::RawBits bits_;
134 succinct::FastBitVec support_;
135};
136
137} // namespace pixie
CRTP facade for immutable rank/select queries and bit access.
Definition rank_select.h:32
std::size_t size() const
Definition rank_select.h:39
Optional rank/select implementation over succinct-bitvec-cpp's FastBitVec.
Definition succinct_bitvec.h:31
std::uint64_t select_impl(std::size_t occurrence) const
Return a one-based one's position, zero for zero, size() if absent.
Definition succinct_bitvec.h:72
std::size_t memory_usage_bytes_impl() const
Return reported index bytes plus the allocated source copy.
Definition succinct_bitvec.h:105
std::size_t source_copy_bytes() const
Return allocated bytes in the normalized source copy.
Definition succinct_bitvec.h:92
static constexpr std::uint64_t kMaxBits
Largest supported source; its last bit fits a uint32_t sample.
Definition succinct_bitvec.h:34
int bit_impl(std::size_t position) const
Return a source bit; position must be below size().
Definition succinct_bitvec.h:60
std::size_t index_logical_bytes() const
Return object plus upstream-reported logical index bytes.
Definition succinct_bitvec.h:100
bool supports_select0_impl() const
The pinned upstream implementation has no zero-select API.
Definition succinct_bitvec.h:85
std::uint64_t select0_impl(std::size_t occurrence) const
Return zero for occurrence zero, otherwise size(): disabled.
Definition succinct_bitvec.h:87
std::uint64_t rank_impl(std::size_t end) const
Count ones in [0, end), clamping end to size().
Definition succinct_bitvec.h:63
std::size_t size_impl() const
Return the logical source length.
Definition succinct_bitvec.h:57
bool supports_select1_impl() const
FastBitVec always builds one-select metadata.
Definition succinct_bitvec.h:83
SuccinctFastRankSelectSupport(std::span< const std::uint64_t > source_words, std::size_t num_bits)
Build FastBitVec over an owned least-significant-bit-first source.
Definition succinct_bitvec.h:43
Common interface for rank/select support over packed bit sequences.