datasketches-cpp
tuple_union.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 TUPLE_UNION_HPP_
21 #define TUPLE_UNION_HPP_
22 
23 #include "tuple_sketch.hpp"
24 #include "theta_union_base.hpp"
25 
26 namespace datasketches {
27 
28 // for types with defined + operation
29 template<typename Summary>
30 struct default_tuple_union_policy {
31  void operator()(Summary& summary, const Summary& other) const {
32  summary += other;
33  }
34 };
35 
40 template<
41  typename Summary,
42  typename Policy = default_tuple_union_policy<Summary>,
43  typename Allocator = std::allocator<Summary>
44 >
45 class tuple_union {
46 public:
47  using Entry = std::pair<uint64_t, Summary>;
48  using ExtractKey = pair_extract_key<uint64_t, Summary>;
51  using AllocEntry = typename std::allocator_traits<Allocator>::template rebind_alloc<Entry>;
52  using resize_factor = theta_constants::resize_factor;
53 
54  // reformulate the external policy that operates on Summary
55  // in terms of operations on Entry
56  struct internal_policy {
57  internal_policy(const Policy& external_policy): external_policy_(external_policy) {}
58  void operator()(Entry& internal_entry, const Entry& incoming_entry) const {
59  external_policy_(internal_entry.second, incoming_entry.second);
60  }
61  void operator()(Entry& internal_entry, Entry&& incoming_entry) const {
62  external_policy_(internal_entry.second, std::move(incoming_entry.second));
63  }
64  const Policy& get_external_policy() const { return external_policy_; }
65  Policy external_policy_;
66  };
67 
68  using State = theta_union_base<Entry, ExtractKey, internal_policy, Sketch, CompactSketch, AllocEntry>;
69 
70  // No constructor here. Use builder instead.
71  class builder;
72 
77  template<typename FwdSketch>
78  void update(FwdSketch&& sketch);
79 
85  CompactSketch get_result(bool ordered = true) const;
86 
90  void reset();
91 
92 protected:
93  State state_;
94 
95  // for builder
96  tuple_union(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p, uint64_t theta, uint64_t seed, const Policy& policy, const Allocator& allocator);
97 };
98 
100 template<typename S, typename P, typename A>
101 class tuple_union<S, P, A>::builder: public tuple_base_builder<builder, P, A> {
102 public:
109  builder(const P& policy = P(), const A& allocator = A());
110 
115  tuple_union build() const;
116 };
117 
118 } /* namespace datasketches */
119 
120 #include "tuple_union_impl.hpp"
121 
122 #endif
Compact Tuple sketch.
Definition: tuple_sketch.hpp:416
Tuple base builder.
Definition: tuple_sketch.hpp:614
Base class for Tuple sketch.
Definition: tuple_sketch.hpp:54
Tuple union builder.
Definition: tuple_union.hpp:101
Tuple Union.
Definition: tuple_union.hpp:45
CompactSketch get_result(bool ordered=true) const
Produces a copy of the current state of the union as a compact sketch.
Definition: tuple_union_impl.hpp:34
void update(FwdSketch &&sketch)
Update the union with a given sketch.
void reset()
Reset the union to the initial empty state.
Definition: tuple_union_impl.hpp:39
datasketches::resize_factor resize_factor
hash table resize factor
Definition: theta_constants.hpp:31
DataSketches namespace.
Definition: binomial_bounds.hpp:38