Skip to content

Commit

Permalink
Merge pull request #65 from adamant-pwn/master
Browse files Browse the repository at this point in the history
Use universal visitors for const-ref saving
  • Loading branch information
jermp authored Jun 21, 2024
2 parents 2ee455f + 0625f0c commit c977ec4
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 48 deletions.
2 changes: 1 addition & 1 deletion external/essentials
24 changes: 17 additions & 7 deletions include/builders/internal_memory_builder_single_phf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,14 @@ struct internal_memory_builder_single_phf {
m_free_slots.swap(other.m_free_slots);
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_seed);
visitor.visit(m_num_keys);
visitor.visit(m_num_buckets);
visitor.visit(m_table_size);
visitor.visit(m_bucketer);
visitor.visit(m_pilots);
visitor.visit(m_free_slots);
visit_impl(visitor, *this);
}

static uint64_t estimate_num_bytes_for_construction(uint64_t num_keys,
Expand All @@ -208,6 +207,17 @@ struct internal_memory_builder_single_phf {
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_seed);
visitor.visit(t.m_num_keys);
visitor.visit(t.m_num_buckets);
visitor.visit(t.m_table_size);
visitor.visit(t.m_bucketer);
visitor.visit(t.m_pilots);
visitor.visit(t.m_free_slots);
}

uint64_t m_seed;
uint64_t m_num_keys;
uint64_t m_num_buckets;
Expand Down
14 changes: 12 additions & 2 deletions include/encoders/bit_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,23 @@ struct bit_vector {
uint64_t m_buf;
};

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_size);
visitor.visit(m_bits);
visit_impl(visitor, *this);
}

protected:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_size);
visitor.visit(t.m_bits);
}

size_t m_size;
std::vector<uint64_t> m_bits;
};
Expand Down
17 changes: 13 additions & 4 deletions include/encoders/compact_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,24 @@ struct compact_vector {
m_bits.swap(other.m_bits);
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_size);
visitor.visit(m_width);
visitor.visit(m_mask);
visitor.visit(m_bits);
visit_impl(visitor, *this);
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_size);
visitor.visit(t.m_width);
visitor.visit(t.m_mask);
visitor.visit(t.m_bits);
}
uint64_t m_size;
uint64_t m_width;
uint64_t m_mask;
Expand Down
18 changes: 14 additions & 4 deletions include/encoders/darray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,25 @@ struct darray {
essentials::vec_bytes(m_overflow_positions);
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_positions);
visitor.visit(m_block_inventory);
visitor.visit(m_subblock_inventory);
visitor.visit(m_overflow_positions);
visit_impl(visitor, *this);
}

protected:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_positions);
visitor.visit(t.m_block_inventory);
visitor.visit(t.m_subblock_inventory);
visitor.visit(t.m_overflow_positions);
}

static void flush_cur_block(std::vector<uint64_t>& cur_block_positions,
std::vector<int64_t>& block_inventory,
std::vector<uint16_t>& subblock_inventory,
Expand Down
15 changes: 12 additions & 3 deletions include/encoders/ef_sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,23 @@ struct ef_sequence {
return 8 * (m_high_bits.bytes() + m_high_bits_d1.bytes() + m_low_bits.bytes());
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_high_bits);
visitor.visit(m_high_bits_d1);
visitor.visit(m_low_bits);
visit_impl(visitor, *this);
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_high_bits);
visitor.visit(t.m_high_bits_d1);
visitor.visit(t.m_low_bits);
}
bit_vector m_high_bits;
darray1 m_high_bits_d1;
compact_vector m_low_bits;
Expand Down
52 changes: 45 additions & 7 deletions include/encoders/encoders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ struct compact {
return m_values.access(i);
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visitor.visit(m_values);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_values);
Expand Down Expand Up @@ -97,14 +102,23 @@ struct partitioned_compact {
return m_values.get_bits(position, num_bits);
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_size);
visitor.visit(m_bits_per_value);
visitor.visit(m_values);
visit_impl(visitor, *this);
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_size);
visitor.visit(t.m_bits_per_value);
visitor.visit(t.m_values);
}
uint64_t m_size;
std::vector<uint32_t> m_bits_per_value;
bit_vector m_values;
Expand Down Expand Up @@ -169,13 +183,22 @@ struct dictionary {
return m_dict.access(rank);
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_ranks);
visitor.visit(m_dict);
visit_impl(visitor, *this);
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_ranks);
visitor.visit(t.m_dict);
}
compact_vector m_ranks;
compact_vector m_dict;
};
Expand Down Expand Up @@ -203,6 +226,11 @@ struct elias_fano {
return m_values.diff(i);
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visitor.visit(m_values);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_values);
Expand Down Expand Up @@ -270,13 +298,23 @@ struct dual {
return m_back.access(i - m_front.size());
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_front);
visitor.visit(m_back);
visit_impl(visitor, *this);
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_front);
visitor.visit(t.m_back);
}

Front m_front;
Back m_back;
};
Expand Down
35 changes: 28 additions & 7 deletions include/partitioned_phf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ template <typename Hasher, typename Encoder, bool Minimal>
struct partitioned_phf {
private:
struct partition {
template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(offset);
visitor.visit(f);
visit_impl(visitor, *this);
}

uint64_t offset;
single_phf<Hasher, Encoder, Minimal> f;

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.offset);
visitor.visit(t.f);
}
};

public:
Expand Down Expand Up @@ -140,16 +151,26 @@ struct partitioned_phf {
return m_seed;
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_seed);
visitor.visit(m_num_keys);
visitor.visit(m_table_size);
visitor.visit(m_bucketer);
visitor.visit(m_partitions);
visit_impl(visitor, *this);
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_seed);
visitor.visit(t.m_num_keys);
visitor.visit(t.m_table_size);
visitor.visit(t.m_bucketer);
visitor.visit(t.m_partitions);
}

uint64_t m_seed;
uint64_t m_num_keys;
uint64_t m_table_size;
Expand Down
23 changes: 16 additions & 7 deletions include/single_phf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,27 @@ struct single_phf {
return m_seed;
}

template <typename Visitor>
void visit(Visitor& visitor) const {
visit_impl(visitor, *this);
}

template <typename Visitor>
void visit(Visitor& visitor) {
visitor.visit(m_seed);
visitor.visit(m_num_keys);
visitor.visit(m_table_size);
visitor.visit(m_M);
visitor.visit(m_bucketer);
visitor.visit(m_pilots);
visitor.visit(m_free_slots);
visit_impl(visitor, *this);
}

private:
template <typename Visitor, typename T>
static void visit_impl(Visitor& visitor, T&& t) {
visitor.visit(t.m_seed);
visitor.visit(t.m_num_keys);
visitor.visit(t.m_table_size);
visitor.visit(t.m_M);
visitor.visit(t.m_bucketer);
visitor.visit(t.m_pilots);
visitor.visit(t.m_free_slots);
}
uint64_t m_seed;
uint64_t m_num_keys;
uint64_t m_table_size;
Expand Down
Loading

0 comments on commit c977ec4

Please sign in to comment.