Pixie
Loading...
Searching...
No Matches
sdsl.h
1#pragma once
2
3#ifndef SDSL_SUPPORT
4#error "SdslRankSelectSupport requires PIXIE_THIRD_PARTY_BACKENDS"
5#endif
6
7#include <pixie/rank_select.h>
8
9#include <algorithm>
10#include <cstddef>
11#include <cstdint>
12#include <limits>
13// SDSL undefines ALIGNMENT; preserve the optional 3-star configuration.
14#pragma push_macro("ALIGNMENT")
15#include <sdsl/int_vector.hpp>
16#include <sdsl/rank_support_v.hpp>
17#include <sdsl/select_support_mcl.hpp>
18#pragma pop_macro("ALIGNMENT")
19#include <span>
20
21namespace pixie {
22
30class SdslRankSelectSupport : public RankSelectBase<SdslRankSelectSupport> {
31 public:
38 explicit SdslRankSelectSupport(std::span<const std::uint64_t> source_words,
39 std::size_t num_bits)
40 : bits_(copy_source(source_words, num_bits)),
41 rank_(&bits_),
42 select1_(&bits_),
43 select0_(&bits_),
44 one_count_(bits_.empty() ? 0 : rank_(bits_.size())) {}
45
47 SdslRankSelectSupport& operator=(const SdslRankSelectSupport&) = delete;
49 SdslRankSelectSupport& operator=(SdslRankSelectSupport&&) = delete;
50
52 std::size_t size_impl() const { return bits_.size(); }
53
55 int bit_impl(std::size_t position) const { return bits_[position]; }
56
58 std::uint64_t rank_impl(std::size_t end) const {
59 return end >= size() ? one_count_ : rank_(end);
60 }
61
63 std::uint64_t select_impl(std::size_t occurrence) const {
64 return occurrence == 0 ? 0
65 : occurrence > one_count_ ? size()
66 : select1_(occurrence);
67 }
68
70 std::uint64_t select0_impl(std::size_t occurrence) const {
71 return occurrence == 0 ? 0
72 : occurrence > size() - one_count_ ? size()
73 : select0_(occurrence);
74 }
75
77 bool supports_select1_impl() const { return true; }
78
80 bool supports_select0_impl() const { return true; }
81
83 std::size_t source_copy_bytes() const { return bits_.bit_capacity() / 8; }
84
91 std::size_t index_logical_bytes() const {
92 return sizeof(*this) + sdsl::size_in_bytes(rank_) +
93 sdsl::size_in_bytes(select1_) + sdsl::size_in_bytes(select0_);
94 }
95
97 std::size_t memory_usage_bytes_impl() const {
99 }
100
101 private:
102 static sdsl::bit_vector copy_source(
103 std::span<const std::uint64_t> source_words,
104 std::size_t num_bits) {
105 if (source_words.size() <= std::numeric_limits<std::size_t>::max() / 64) {
106 num_bits = std::min(num_bits, source_words.size() * 64);
107 }
108 sdsl::bit_vector result(num_bits, 0);
109 const std::size_t words = num_bits / 64 + (num_bits % 64 != 0);
110 std::copy_n(source_words.begin(), words, result.data());
111 if (num_bits % 64 != 0) {
112 result.data()[words - 1] &= (std::uint64_t{1} << (num_bits % 64)) - 1;
113 }
114 return result;
115 }
116
117 sdsl::bit_vector bits_;
118 sdsl::rank_support_v<> rank_;
119 sdsl::select_support_mcl<1> select1_;
120 sdsl::select_support_mcl<0> select0_;
121 std::size_t one_count_;
122};
123
124} // namespace pixie
CRTP facade for immutable rank/select queries and bit access.
Definition rank_select.h:32
bool empty() const
Definition rank_select.h:45
std::size_t size() const
Definition rank_select.h:39
Optional SDSL rank-v index with MCL select in both directions.
Definition sdsl.h:30
std::size_t size_impl() const
Return the logical bit count.
Definition sdsl.h:52
bool supports_select1_impl() const
SDSL constructs one-select support unconditionally.
Definition sdsl.h:77
bool supports_select0_impl() const
SDSL constructs zero-select support unconditionally.
Definition sdsl.h:80
SdslRankSelectSupport(std::span< const std::uint64_t > source_words, std::size_t num_bits)
Copy packed source words and build rank1, select1 and select0.
Definition sdsl.h:38
std::uint64_t select_impl(std::size_t occurrence) const
Locate a one-based one occurrence, or size() if absent.
Definition sdsl.h:63
int bit_impl(std::size_t position) const
Read a zero-based bit; position must be below size().
Definition sdsl.h:55
std::uint64_t rank_impl(std::size_t end) const
Count ones in [0, end), clamping end to size().
Definition sdsl.h:58
std::size_t memory_usage_bytes_impl() const
Return the reported index footprint and owned source bytes.
Definition sdsl.h:97
std::size_t index_logical_bytes() const
Return object bytes plus SDSL's serialized logical directory sizes.
Definition sdsl.h:91
std::size_t source_copy_bytes() const
Return allocated bytes in the owned source buffer.
Definition sdsl.h:83
std::uint64_t select0_impl(std::size_t occurrence) const
Locate a one-based zero occurrence, or size() if absent.
Definition sdsl.h:70
Common interface for rank/select support over packed bit sequences.