datasketches-cpp
Loading...
Searching...
No Matches
theta_set_difference_base_impl.hpp
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef THETA_A_SET_DIFFERENCE_BASE_IMPL_HPP_
21#define THETA_A_SET_DIFFERENCE_BASE_IMPL_HPP_
22
23#include <algorithm>
24#include <stdexcept>
25
26#include "conditional_back_inserter.hpp"
27#include "conditional_forward.hpp"
28
29namespace datasketches {
30
31template<typename EN, typename EK, typename CS, typename A>
32theta_set_difference_base<EN, EK, CS, A>::theta_set_difference_base(uint64_t seed, const A& allocator):
33allocator_(allocator),
34seed_hash_(compute_seed_hash(seed))
35{}
36
37template<typename EN, typename EK, typename CS, typename A>
38template<typename FwdSketch, typename Sketch>
39CS theta_set_difference_base<EN, EK, CS, A>::compute(FwdSketch&& a, const Sketch& b, bool ordered) const {
40 if (a.is_empty() || (a.get_num_retained() > 0 && b.is_empty())) return CS(a, ordered);
41 if (a.get_seed_hash() != seed_hash_) throw std::invalid_argument("A seed hash mismatch");
42 // an empty sketch has no hashes, so its seed hash is meaningless and must be ignored,
43 // consistent with deserialization, theta_union::update() and theta_intersection::update()
44 if (!b.is_empty() && b.get_seed_hash() != seed_hash_) throw std::invalid_argument("B seed hash mismatch");
45
46 const uint64_t theta = std::min(a.get_theta64(), b.get_theta64());
47 std::vector<EN, A> entries(allocator_);
48 bool is_empty = a.is_empty();
49
50 if (b.get_num_retained() == 0) {
51 std::copy_if(forward_begin(std::forward<FwdSketch>(a)), forward_end(std::forward<FwdSketch>(a)), std::back_inserter(entries),
52 key_less_than<uint64_t, EN, EK>(theta));
53 } else {
54 if (a.is_ordered() && b.is_ordered()) { // sort-based
55 std::set_difference(forward_begin(std::forward<FwdSketch>(a)), forward_end(std::forward<FwdSketch>(a)), b.begin(), b.end(),
56 conditional_back_inserter(entries, key_less_than<uint64_t, EN, EK>(theta)), comparator());
57 } else { // hash-based
58 const uint8_t lg_size = lg_size_from_count(b.get_num_retained(), hash_table::REBUILD_THRESHOLD);
59 hash_table table(lg_size, lg_size, hash_table::resize_factor::X1, 1, 0, 0, allocator_); // theta and seed are not used here
60 for (const auto& entry: b) {
61 const uint64_t hash = EK()(entry);
62 if (hash < theta) {
63 table.insert(table.find(hash).first, hash);
64 } else if (b.is_ordered()) {
65 break; // early stop
66 }
67 }
68
69 // scan A lookup B
70 for (auto&& entry: a) {
71 const uint64_t hash = EK()(entry);
72 if (hash < theta) {
73 auto result = table.find(hash);
74 if (!result.second) entries.emplace_back(conditional_forward<FwdSketch>(entry));
75 } else if (a.is_ordered()) {
76 break; // early stop
77 }
78 }
79 }
80 }
81 if (entries.empty() && theta == theta_constants::MAX_THETA) is_empty = true;
82 if (ordered && !a.is_ordered()) std::sort(entries.begin(), entries.end(), comparator());
83 return CS(is_empty, a.is_ordered() || ordered, seed_hash_, theta, std::move(entries));
84}
85
86} /* namespace datasketches */
87
88#endif
DataSketches namespace.
Definition binomial_bounds.hpp:38