9#ifndef INCLUDED_SDSL_RRR_VECTOR_15
10#define INCLUDED_SDSL_RRR_VECTOR_15
39template <
typename T =
void>
49 static int const n = 15;
50 static int const MAX_SIZE = 32;
51 uint8_t m_space_for_bt[16];
52 uint8_t m_space_for_bt_pair[256];
53 uint64_t m_C[MAX_SIZE];
59 m_nr_to_bin.
resize(1 << n);
60 m_bin_to_nr.
resize(1 << n);
61 for (
int i = 0, cnt = 0, class_cnt = 0; i <= n; ++i)
65 std::vector<bool> b(n, 0);
66 for (
int j = 0; j < i; ++j)
71 for (
int k = 0; k < n; ++k)
72 x |= ((uint32_t)b[n - k - 1]) << (n - 1 - k);
74 m_bin_to_nr[x] = class_cnt;
78 while (next_permutation(b.begin(), b.end()));
80 m_space_for_bt[i] = 0;
82 m_space_for_bt[i] =
bits::hi(class_cnt) + 1;
86 for (
int x = 0; x < 256; ++x)
88 m_space_for_bt_pair[x] = m_space_for_bt[x >> 4] + m_space_for_bt[x & 0x0F];
97 return iii.m_space_for_bt[i];
100 static inline uint32_t
nr_to_bin(uint8_t k, uint32_t nr)
102 return iii.m_nr_to_bin[iii.m_C[k] + nr];
107 return iii.m_bin_to_nr[bin];
112 return iii.m_space_for_bt_pair[x];
118typename binomial15_impl<T>::impl binomial15_impl<T>::iii;
134template <
class t_rac, u
int16_t t_k>
190 *
this = std::move(v);
199 *
this = std::move(tmp);
210 m_bt = std::move(v.m_bt);
211 m_btnr = std::move(v.m_btnr);
212 m_btnrp = std::move(v.m_btnrp);
213 m_rank = std::move(v.m_rank);
237 btnr_pos += bi_type::space_for_bt(x);
244 btnr_pos += bi_type::space_for_bt(x);
259 btnr_pos = 0, sum_rank = 0;
264 m_btnrp[i / t_k] = btnr_pos;
265 m_rank[i / t_k] = sum_rank;
267 uint16_t space_for_bt = bi_type::space_for_bt(x = bt_array[i++]);
273 btnr_pos += space_for_bt;
280 m_btnrp[i / t_k] = btnr_pos;
281 m_rank[i / t_k] = sum_rank;
283 uint16_t space_for_bt = bi_type::space_for_bt(x = bt_array[i++]);
287 m_btnr.
set_int(btnr_pos, bi_type::bin_to_nr(bv.
get_int(pos, m_size - pos)), space_for_bt);
289 btnr_pos += space_for_bt;
290 assert(m_rank.
size() - 1 == ((i + t_k - 1) / t_k));
294 assert(m_rank.
size() - 1 == ((i + t_k - 1) / t_k));
297 m_rank[m_rank.
size() - 1] = sum_rank;
298 m_bt =
rac_type(std::move(bt_array));
308 uint8_t *
bt = (uint8_t *)(m_bt.data());
309 uint32_t i_bt = *(
bt + (bt_idx / 2));
326 if (j % 2 == 1 and j < bt_idx)
328 btnrp += bi_type::space_for_bt((*
bt++) >> 4);
331 while (j + 1 < bt_idx)
333 btnrp += bi_type::space_for_bt_pair(*(
bt++));
338 btnrp += bi_type::space_for_bt((*
bt) & 0x0F);
341 uint32_t
btnr = m_btnr.
get_int(btnrp, bi_type::space_for_bt(i_bt));
344 return (bi_type::nr_to_bin(i_bt,
btnr) >> off) & (uint32_t)1;
360 uint16_t
bt = m_bt[bb_idx];
363 if (bb_idx == eb_idx)
376 for (
size_type j = sample_pos * t_k; j < bb_idx; ++j)
378 btnrp += bi_type::space_for_bt(m_bt[j]);
380 uint32_t
btnr = m_btnr.
get_int(btnrp, bi_type::space_for_bt(
bt));
387 uint8_t b_len_sum = 0;
390 res |=
get_int(idx, b_len) << b_len_sum;
395 b_len = std::min(len, b_len);
413 written_bytes +=
write_member(m_size, out, child,
"size");
414 written_bytes += m_bt.
serialize(out, child,
"bt");
415 written_bytes += m_btnr.
serialize(out, child,
"btnr");
416 written_bytes += m_btnrp.
serialize(out, child,
"btnrp");
417 written_bytes += m_rank.
serialize(out, child,
"rank_samples");
419 return written_bytes;
423 void load(std::istream & in)
432 template <
typename archive_t>
442 template <
typename archive_t>
464 return m_size == v.m_size && m_bt == v.m_bt && m_btnr == v.m_btnr && m_btnrp == v.m_btnrp && m_rank == v.m_rank;
469 return !(*
this == v);
476template <u
int8_t t_b,
class t_rac, u
int16_t t_k>
479 static_assert(t_b == 1u or t_b == 0u,
"rank_support_rrr: bit pattern must be `0` or `1`");
484 typedef typename bit_vector_type::bi_type
bi_type;
518 size_type bt_idx = i / bit_vector_type::block_size;
520 size_type btnrp = m_v->m_btnrp[sample_pos];
522 if (sample_pos + 1 < m_v->m_rank.size())
529 else if (diff_rank == (
size_type)bit_vector_type::block_size * t_k)
532 - sample_pos * t_k * bit_vector_type::block_size,
536 uint8_t * bt = (uint8_t *)(m_v->m_bt.data());
538 uint8_t last_bt = *(bt + (bt_idx / 2));
549 if (last_bt == 0 or last_bt == 15)
552 rank += i % bit_vector_type::block_size;
554 bt_idx = bt_idx << 2;
558 uint64_t
const * bt64 = m_v->m_bt.data() + (j >> 6);
559 uint8_t bt64_off = j & 0x3F;
560 uint64_t
const * bt64_end = m_v->m_bt.data() + (bt_idx >> 6);
561 uint8_t bt64_end_off = bt_idx & 0x3F;
563 if (bt64 == bt64_end)
565 uint64_t w = ((*bt64) >> bt64_off) &
bits::lo_set[bt64_end_off - bt64_off];
566 w = (w & 0x0f0f0f0f0f0f0f0full) + ((w >> 4) & 0x0f0f0f0f0f0f0f0full);
567 rank += ((0x0101010101010101ull * w) >> 56);
571 uint64_t w = ((*bt64) >> bt64_off);
572 w = (w & 0x0f0f0f0f0f0f0f0full) + ((w >> 4) & 0x0f0f0f0f0f0f0f0full);
573 rank += ((0x0101010101010101ull * w) >> 56);
574 while ((++bt64) != bt64_end)
577 w = (w & 0x0f0f0f0f0f0f0f0full) + ((w >> 4) & 0x0f0f0f0f0f0f0f0full);
578 rank += ((0x0101010101010101ull * w) >> 56);
581 if (bt64_end_off > 0)
583 w = *bt64 << (64 - bt64_end_off);
584 w = (w & 0x0f0f0f0f0f0f0f0full) + ((w >> 4) & 0x0f0f0f0f0f0f0f0full);
585 rank += ((0x0101010101010101ull * w) >> 56);
592 if (j % 2 == 1 and j < bt_idx)
594 const uint8_t r = (*bt++) >> 4;
599 while (j + 1 < bt_idx)
601 const uint8_t r = *(bt++);
602 rank += (r >> 4) + (r & 0x0F);
608 const uint8_t r = (*bt);
613 uint8_t off = i % bit_vector_type::block_size;
666 template <
typename archive_t>
670 template <
typename archive_t>
676 return *m_v == *other.m_v;
681 return !(*
this == other);
686template <u
int8_t t_b,
class t_rac, u
int16_t t_k>
689 static_assert(t_b == 1u or t_b == 0u,
"select_support_rrr: bit pattern must be `0` or `1`");
694 typedef typename bit_vector_type::bi_type
bi_type;
710 if (m_v->m_rank[m_v->m_rank.size() - 1] < i)
717 while (end - begin > 1)
719 idx = (begin + end) >> 1;
720 rank = m_v->m_rank[idx];
729 rank = m_v->m_rank[begin];
731 size_type diff_rank = m_v->m_rank[end] - rank;
737 uint8_t bt = 0, s = 0;
740 bt = m_v->m_bt[idx++];
742 btnrp += (s = bi_type::space_for_bt(bt));
745 uint32_t btnr = m_v->m_btnr.
get_int(btnrp - s, s);
752 if ((
size() - m_v->m_rank[m_v->m_rank.size() - 1]) < i)
759 while (end - begin > 1)
761 idx = (begin + end) >> 1;
773 if (m_v->m_rank[end] == m_v->m_rank[begin])
778 uint8_t bt = 0, s = 0;
781 bt = m_v->m_bt[idx++];
783 btnrp += (s = bi_type::space_for_bt(bt));
786 uint32_t btnr = m_v->m_btnr.get_int(btnrp - s, s);
799 return t_b ? select1(i) : select0(i);
838 template <
typename archive_t>
842 template <
typename archive_t>
848 return *m_v == *other.m_v;
853 return !(*
this == other);
bits.hpp contains the sdsl::bits class.
cereal.hpp offers cereal support
#define CEREAL_LOAD_FUNCTION_NAME
#define CEREAL_SAVE_FUNCTION_NAME
static uint32_t bin_to_nr(uint32_t bin)
static uint8_t space_for_bt_pair(uint8_t x)
static uint8_t space_for_bt(uint32_t i)
static uint32_t nr_to_bin(uint8_t k, uint32_t nr)
A generic vector class for integers of width .
value_type get_int(size_type idx, const uint8_t len=64) const
Get the integer value of the binary string of length len starting at position idx in the int_vector.
int_vector_size_type size_type
ptrdiff_t difference_type
int_vector< 64 >::size_type size() const noexcept
void load(std::istream &in)
Load the int_vector for a stream.
size_type size() const noexcept
The number of elements in the int_vector.
int_vector_trait< t_width >::value_type value_type
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
Serializes the int_vector to a stream.
void resize(const size_type size)
Resize the int_vector in terms of elements.
void set_int(size_type idx, value_type x, const uint8_t len=64)
Set the bits from position idx to idx+len-1 to the binary representation of integer x.
Generic iterator for a random access container.
rrr_vector< 15, t_rac, t_k > bit_vector_type
void CEREAL_SAVE_FUNCTION_NAME(archive_t &) const
void CEREAL_LOAD_FUNCTION_NAME(archive_t &)
rank_support_rrr & operator=(rank_support_rrr const &rs)
size_type serialize(std::ostream &, structure_tree_node *v=nullptr, std::string name="") const
Serializes the data structure into a stream.
const size_type rank(size_type i) const
Answers rank queries.
const size_type operator()(size_type i) const
Short hand for rank(i)
rank_support_rrr(bit_vector_type const *v=nullptr)
Standard constructor.
const size_type size() const
Returns the size of the original vector.
bool operator!=(rank_support_rrr const &other) const noexcept
bit_vector_type::size_type size_type
void load(std::istream &, bit_vector_type const *v=nullptr)
Load the data structure from a stream and set the supported vector.
void set_vector(bit_vector_type const *v=nullptr)
Set the supported vector.
bit_vector_type::bi_type bi_type
bool operator==(rank_support_rrr const &other) const noexcept
rrr_vector< t_bs, t_rac, t_k > bit_vector_type
Answers rank queries const size_type rank(size_type i) const
Set the supported vector void set_vector(bit_vector_type const *v=nullptr)
Loads the data structure from the given istream void load(std::istream &in)
random_access_const_iterator< rrr_vector > iterator
rank_support_rrr< 1, t_bs, t_rac, t_k > rank_1_type
Default constructor rrr_vector()
rank_support_rrr< 0, t_bs, t_rac, t_k > rank_0_type
select_support_rrr< 0, t_bs, t_rac, t_k > select_0_type
Returns the size of the original bit vector size_type size() const
Accessing the i th element of the original bit_vector value_type operator[](size_type i) const
bool operator==(rrr_vector const &v) const
rrr_vector & operator=(rrr_vector const &v)
select_support_rrr< 1, t_bs, t_rac, t_k > select_1_type
bool operator!=(rrr_vector const &v) const
Answers select queries Serializes the data structure into the given ostream size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
Get the integer value of the binary string of length len starting at position idx uint64_t get_int(size_type idx, uint8_t len=64) const
size_type select(size_type i) const
Answers select queries.
rrr_vector< 15, t_rac, t_k > bit_vector_type
bool operator==(select_support_rrr const &other) const noexcept
bit_vector_type::size_type size_type
bool operator!=(select_support_rrr const &other) const noexcept
const size_type size() const
void CEREAL_SAVE_FUNCTION_NAME(archive_t &) const
void load(std::istream &, bit_vector_type const *v=nullptr)
size_type serialize(std::ostream &, structure_tree_node *v=nullptr, std::string name="") const
select_support_rrr & operator=(select_support_rrr const &rs)
bit_vector_type::bi_type bi_type
const size_type operator()(size_type i) const
select_support_rrr(bit_vector_type const *v=nullptr)
void CEREAL_LOAD_FUNCTION_NAME(archive_t &)
void set_vector(bit_vector_type const *v=nullptr)
void set_vector(bit_vector_type const *v=nullptr)
rrr_vector< t_bs, t_rac, t_k > bit_vector_type
Answers select queries size_type select(size_type i) const
bit_vector_type::size_type size_type
const size_type size() const
static structure_tree_node * add_child(structure_tree_node *v, std::string const &name, std::string const &type)
static void add_size(structure_tree_node *v, uint64_t value)
int_vector.hpp contains the sdsl::int_vector class.
io.hpp contains some methods for reading/writing sdsl structures.
iterators.hpp contains an generic iterator for random access containers.
Namespace for the succinct data structure library.
size_t block_size(void *ptr)
size_t write_member(T const &t, std::ostream &out, sdsl::structure_tree_node *v=nullptr, std::string name="")
binomial15_impl< T >::impl binomial15_impl< T >::iii
binomial15_impl<> binomial15
void read_member(T &t, std::istream &in)
int_vector< 1 > bit_vector
bit_vector is a specialization of the int_vector.
rrr_vector.hpp contains the sdsl::rrr_vector class, and classes which support rank and select for rrr...
Contains declarations and definitions of data structure concepts.
static constexpr uint32_t sel(uint64_t x, uint32_t i)
Calculate the position of the i-th rightmost 1 bit in the 64bit integer x.
static constexpr uint64_t lo_set[65]
lo_set[i] is a 64-bit word with the i least significant bits set and the high bits not set.
static constexpr uint32_t hi(uint64_t x)
Position of the most significant set bit the 64-bit word x.
static constexpr uint64_t cnt(uint64_t x)
Counts the number of set bits in x.
static size_type adjust_rank(size_type r, SDSL_UNUSED size_type n)
structure_tree.hpp contains a helper class which can represent the memory structure of a class.
util.hpp contains some helper methods for int_vector and other stuff like demangle class names.