8#ifndef INCLUDED_SDSL_K2_TREE
9#define INCLUDED_SDSL_K2_TREE
36template <u
int8_t k,
typename t_bv = bit_vector,
typename t_rank =
typename t_bv::rank_1_type>
59 int simulated_size = std::pow(k, k_height);
60 std::vector<std::deque<bit_vector>> acc(k_height + 1);
62 k2_tree_ns::_build_from_matrix<bit_vector>(matrix, k, simulated_size, k_height, 1, 0, 0, acc);
66 for (
int i = 1; i < k_height; i++)
67 for (
auto it = acc[i].begin(); it != acc[i].end(); it++) t_size += (*it).size();
69 for (
auto it = acc[k_height].begin(); it != acc[k_height].end(); it++) l_size += (*it).size();
75 for (
int j = 1; j < k_height; j++)
76 for (
auto it = acc[j].begin(); it != acc[j].end(); it++)
77 for (
unsigned i = 0; i < (*it).size(); i++)
80 k_t_.
set_int(n, (*it).get_int(i, 1), 1);
84 for (
auto it = acc[k_height].begin(); it != acc[k_height].end(); it++)
85 for (
unsigned i = 0; i < (*it).size(); i++)
88 k_l_.
set_int(n * 1, (*it).get_int(i, 1), 1);
92 k2_tree_ns::build_template_vector<t_bv>(k_t_, k_l_, k_t, k_l);
108 if (level >= k_t.size())
110 if (k_l[level - k_t.size()] == 1) acc.push_back(col);
116 idx_type y = k_t_rank(level + 1) * std::pow(k_k, 2) + k_k * std::floor(row /
static_cast<double>(n));
117 for (
unsigned j = 0; j < k_k; j++)
_neigh(n / k_k, row % n, col + n * j, y + j, acc);
134 if (level >= k_t.size())
136 if (k_l[level - k_t.size()] == 1) { acc.push_back(row); }
142 idx_type y = k_t_rank(level + 1) * std::pow(k_k, 2) + std::floor(col /
static_cast<double>(n));
143 for (
unsigned j = 0; j < k_k; j++)
_reverse_neigh(n / k_k, row + n * j, col % n, y + j * k_k, acc);
159 typedef std::tuple<idx_type, idx_type, size_type, idx_type, idx_type> t_part_tuple;
162 k_height = std::ceil(std::log(
size) / std::log(k_k));
163 k_height = k_height > 1 ? k_height : 1;
168 std::queue<t_part_tuple> q;
171 size_type l = std::pow(k_k, k_height - 1);
172 std::vector<idx_type> pos_by_chunk(k_2 + 1, 0);
174 q.push(t_part_tuple(0, edges.size(), l, 0, 0));
178 std::vector<idx_type> amount_by_chunk(k_2, 0);
179 std::tie(i, j, l, r_0, c_0) = q.front();
182 for (it = i; it < j; it++)
184 std::get<1>(edges[it]),
200 for (it = 0; it < k_2; it++, t++)
201 if (amount_by_chunk[it] != 0) k_l_[t] = 1;
208 for (it = 1; it < k_2; it++) pos_by_chunk[it] = pos_by_chunk[it - 1] + amount_by_chunk[it - 1];
210 pos_by_chunk[k_2] = j;
212 for (it = 0; it < k_2; it++, t++)
214 if (amount_by_chunk[it] != 0)
219 q.push(t_part_tuple(pos_by_chunk[it], pos_by_chunk[it + 1], l / k_k, r_0 + r * l, c_0 + c * l));
224 for (
unsigned ch = 0; ch < k_2; ch++)
226 idx_type be = ch == 0 ? i : pos_by_chunk[ch - 1];
227 for (it = pos_by_chunk[ch]; it < be + amount_by_chunk[ch];)
231 if (pos_by_chunk[chunk] != it)
232 std::iter_swap(edges.begin() + it, edges.begin() + pos_by_chunk[chunk]);
235 pos_by_chunk[chunk]++;
241 k2_tree_ns::build_template_vector<t_bv>(k_t_, k_l_, k_t, k_l);
243 k_t_rank = t_rank(&k_t);
256 k2_tree(std::vector<std::vector<int>> & matrix)
258 if (matrix.size() < 1) {
throw std::logic_error(
"Matrix has no elements"); }
259 std::vector<bit_vector> t;
261 if (matrix.size() < k_k)
264 k_height = std::ceil(std::log(matrix.size()) / std::log(k_k));
268 k_t_rank = t_rank(&k_t);
283 assert(edges.size() > 0);
306 assert(buf_x.
size() == buf_y.
size());
307 assert(buf_x.
size() > 0);
309 std::vector<std::tuple<idx_type, idx_type>> edges;
310 edges.reserve(buf_x.
size());
315 for (
auto v : buf_x) max = std::max(
static_cast<size_type>(v), max);
316 for (
auto v : buf_y) max = std::max(
static_cast<size_type>(v), max);
320 for (uint64_t i = 0; i < buf_x.
size(); i++)
321 edges.
push_back(std::tuple<idx_type, idx_type>{ buf_x[i], buf_y[i] });
330 , k_height(tr.k_height)
331 , k_t_rank(tr.k_t_rank)
333 k_t_rank.set_vector(&k_t);
343 k_t = std::move(tr.k_t);
344 k_l = std::move(tr.k_l);
345 k_k = std::move(tr.k_k);
346 k_height = std::move(tr.k_height);
347 k_t_rank = std::move(tr.k_t_rank);
348 k_t_rank.set_vector(&k_t);
359 *
this = std::move(tmp);
368 if (k_k != tr.k_k || k_height != tr.k_height)
return false;
369 if (k_t.size() != tr.k_t.size() || k_l.size() != tr.k_l.size())
return false;
370 for (
unsigned i = 0; i < k_t.size(); i++)
371 if (k_t[i] != tr.k_t[i])
return false;
372 for (
unsigned i = 0; i < k_l.size(); i++)
373 if (k_l[i] != tr.k_l[i])
return false;
390 if (k_t.size() == 0 && k_l.size() == 0)
return false;
391 size_type n = std::pow(k_k, k_height - 1);
398 row = std::floor(i /
static_cast<double>(n));
399 col = std::floor(j /
static_cast<double>(n));
405 while (level < k_t.size())
407 if (k_t[level] == 0)
return false;
408 row = std::floor(i /
static_cast<double>(n));
409 col = std::floor(j /
static_cast<double>(n));
412 level = k_t_rank(level + 1) * k_2 + k_k * row + col;
416 return k_l[level - k_t.size()] == 1;
426 std::vector<idx_type> acc{};
427 if (k_l.size() == 0 && k_t.size() == 0)
return acc;
429 idx_type y = k_k * std::floor(i /
static_cast<double>(n));
430 for (
unsigned j = 0; j < k_k; j++)
_neigh(n / k_k, i % n, n * j, y + j, acc);
441 std::vector<idx_type> acc{};
442 if (k_l.size() == 0 && k_t.size() == 0)
return acc;
445 idx_type y = std::floor(i /
static_cast<double>(n));
446 for (
unsigned j = 0; j < k_k; j++)
_reverse_neigh(n / k_k, n * j, i % n, y + j * k_k, acc);
463 written_bytes += k_t.serialize(out, child,
"t");
464 written_bytes += k_l.serialize(out, child,
"l");
465 written_bytes += k_t_rank.serialize(out, child,
"t_rank");
467 written_bytes +=
write_member(k_height, out, child,
"height");
469 return written_bytes;
481 k_t_rank.set_vector(&k_t);
487 template <
typename archive_t>
498 template <
typename archive_t>
499 typename std::enable_if<cereal::traits::is_output_serializable<k2_tree, archive_t>::value,
void>::type
507 k_t_rank.set_vector(&k_t);
bit_vectors.hpp contains classes for uncompressed and compressed bit vector representations.
uint64_t size() const
Returns the number of elements currently stored.
void shrink_to_fit()
Free unused allocated memory.
size_type size() const noexcept
The number of elements in the int_vector.
void push_back(value_type value)
Insert element at the end.
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.
bool adj(idx_type i, idx_type j) const
Indicates wheter node j is adjacent to node i or not.
void load(std::istream &in)
Load from istream.
void _neigh(size_type n, idx_type row, idx_type col, size_type level, std::vector< idx_type > &acc) const
Recursive function to retrieve list of neighbors.
void build_from_edges(std::vector< std::tuple< idx_type, idx_type > > &edges, const size_type size)
Build a tree from an edges collection.
void CEREAL_SAVE_FUNCTION_NAME(archive_t &ar) const
Serialise (save) via cereal.
k2_tree & operator=(k2_tree &&tr)
Move assignment operator.
k2_tree_ns::idx_type idx_type
void _reverse_neigh(size_type n, idx_type row, idx_type col, size_type level, std::vector< idx_type > &acc) const
Recursive function to retrieve list of reverse neighbors.
k2_tree(std::vector< std::tuple< idx_type, idx_type > > &edges, const size_type size)
Constructor.
bool operator==(const k2_tree &tr) const
Equal operator.
std::vector< idx_type > neigh(idx_type i) const
Returns a list of neighbors of node i.
void build_from_matrix(const std::vector< std::vector< int > > &matrix)
std::vector< idx_type > reverse_neigh(idx_type i) const
Returns a list of reverse neighbors of node i.
std::enable_if< cereal::traits::is_output_serializable< k2_tree, archive_t >::value, void >::type CEREAL_LOAD_FUNCTION_NAME(archive_t &ar)
Load via cereal.
k2_tree(const k2_tree &tr)
k2_tree & operator=(k2_tree &tr)
Assignment operator.
k2_tree_ns::size_type size_type
k2_tree(std::vector< std::vector< int > > &matrix)
Constructor.
k2_tree(std::string filename, size_type size=0)
Constructor.
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
Serialize to a stream.
static structure_tree_node * add_child(structure_tree_node *v, const std::string &name, const std::string &type)
static void add_size(structure_tree_node *v, uint64_t value)
int_vector_buffer.hpp contains the sdsl::int_vector_buffer class.
io.hpp contains some methods for reading/writing sdsl structures.
k2_tree_helper.hpp contains helper functions and definitions for a k^2-tree implementation.
uint16_t get_chunk_idx(idx_type v, idx_type u, idx_type c_0, idx_type r_0, size_type l, uint8_t k)
Get the chunk index ([0, k^2[) of a submatrix point.
int_vector ::size_type size_type
int_vector ::size_type idx_type
Namespace for the succinct data structure library.
size_t write_member(const T &t, std::ostream &out, sdsl::structure_tree_node *v=nullptr, std::string name="")
void read_member(T &t, std::istream &in)
int_vector< 1 > bit_vector
bit_vector is a specialization of the int_vector.
int_vector ::size_type size(const range_type &r)
Size of a range.