Skip to content

Commit

Permalink
Merge pull request #371 from apache/theta_iterator_fix
Browse files Browse the repository at this point in the history
remove std::iterator from wrapped_compact_theta_sketch_alloc
  • Loading branch information
jmalkin authored May 5, 2023
2 parents 5bc6aeb + 0c85ebc commit 2b497c3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions theta/include/theta_intersection_base_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void theta_intersection_base<EN, EK, P, S, CS, A>::update(SS&& sketch) {
is_valid_ = true;
const uint8_t lg_size = lg_size_from_count(sketch.get_num_retained(), theta_update_sketch_base<EN, EK, A>::REBUILD_THRESHOLD);
table_ = hash_table(lg_size, lg_size, resize_factor::X1, 1, table_.theta_, table_.seed_, table_.allocator_, table_.is_empty_);
for (auto& entry: sketch) {
for (auto&& entry: sketch) {
auto result = table_.find(EK()(entry));
if (result.second) {
throw std::invalid_argument("duplicate key, possibly corrupted input sketch");
Expand All @@ -64,7 +64,7 @@ void theta_intersection_base<EN, EK, P, S, CS, A>::update(SS&& sketch) {
matched_entries.reserve(max_matches);
uint32_t match_count = 0;
uint32_t count = 0;
for (auto& entry: sketch) {
for (auto&& entry: sketch) {
if (EK()(entry) < table_.theta_) {
auto result = table_.find(EK()(entry));
if (result.second) {
Expand Down
2 changes: 1 addition & 1 deletion theta/include/theta_set_difference_base_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ CS theta_set_difference_base<EN, EK, CS, A>::compute(FwdSketch&& a, const Sketch
}

// scan A lookup B
for (auto& entry: a) {
for (auto&& entry: a) {
const uint64_t hash = EK()(entry);
if (hash < theta) {
auto result = table.find(hash);
Expand Down
13 changes: 10 additions & 3 deletions theta/include/theta_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,22 @@ class wrapped_compact_theta_sketch_alloc : public base_theta_sketch_alloc<Alloca
};

template<typename Allocator>
class wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator: public std::iterator<std::input_iterator_tag, uint64_t> {
class wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = const uint64_t;
using difference_type = void;
using pointer = value_type*;
using reference = uint64_t;

const_iterator(const void* ptr, uint8_t entry_bits, uint32_t num_entries, uint32_t index);
const_iterator& operator++();
const_iterator operator++(int);
bool operator==(const const_iterator& other) const;
bool operator!=(const const_iterator& other) const;
const uint64_t& operator*() const;
const uint64_t* operator->() const;
reference operator*() const;
pointer operator->() const;

private:
const void* ptr_;
uint8_t entry_bits_;
Expand Down
4 changes: 2 additions & 2 deletions theta/include/theta_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,13 +869,13 @@ bool wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator==(c
}

template<typename Allocator>
const uint64_t& wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator*() const {
auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator*() const -> reference {
if (entry_bits_ == 64) return *reinterpret_cast<const uint64_t*>(ptr_);
return buffer_[buf_i_];
}

template<typename Allocator>
const uint64_t* wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator->() const {
auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator->() const -> pointer {
if (entry_bits_ == 64) return reinterpret_cast<const uint64_t*>(ptr_);
return buffer_ + buf_i_;
}
Expand Down
2 changes: 1 addition & 1 deletion theta/include/theta_union_base_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void theta_union_base<EN, EK, P, S, CS, A>::update(SS&& sketch) {
if (sketch.get_seed_hash() != compute_seed_hash(table_.seed_)) throw std::invalid_argument("seed hash mismatch");
table_.is_empty_ = false;
union_theta_ = std::min(union_theta_, sketch.get_theta64());
for (auto& entry: sketch) {
for (auto&& entry: sketch) {
const uint64_t hash = EK()(entry);
if (hash < union_theta_ && hash < table_.theta_) {
auto result = table_.find(hash);
Expand Down
4 changes: 3 additions & 1 deletion theta/include/theta_update_sketch_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ class theta_iterator {
theta_iterator operator++(int);
bool operator==(const theta_iterator& other) const;
bool operator!=(const theta_iterator& other) const;
Entry& operator*() const;
reference operator*() const;
pointer operator->() const;

private:
Entry* entries_;
Expand All @@ -221,6 +222,7 @@ class theta_const_iterator {
bool operator==(const theta_const_iterator& other) const;
bool operator!=(const theta_const_iterator& other) const;
reference operator*() const;
pointer operator->() const;

private:
const Entry* entries_;
Expand Down
12 changes: 11 additions & 1 deletion theta/include/theta_update_sketch_base_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ auto theta_iterator<Entry, ExtractKey>::operator*() const -> reference {
return entries_[index_];
}

template<typename Entry, typename ExtractKey>
auto theta_iterator<Entry, ExtractKey>::operator->() const -> pointer {
return entries_ + index_;
}

// const iterator

template<typename Entry, typename ExtractKey>
Expand Down Expand Up @@ -419,10 +424,15 @@ bool theta_const_iterator<Entry, ExtractKey>::operator==(const theta_const_itera
}

template<typename Entry, typename ExtractKey>
auto theta_const_iterator<Entry, ExtractKey>::operator*() const -> const Entry& {
auto theta_const_iterator<Entry, ExtractKey>::operator*() const -> reference {
return entries_[index_];
}

template<typename Entry, typename ExtractKey>
auto theta_const_iterator<Entry, ExtractKey>::operator->() const -> pointer {
return entries_ + index_;
}

} /* namespace datasketches */

#endif

0 comments on commit 2b497c3

Please sign in to comment.