datasketches-cpp
Loading...
Searching...
No Matches
array_tuple_sketch.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 ARRAY_TUPLE_SKETCH_HPP_
21#define ARRAY_TUPLE_SKETCH_HPP_
22
23#include <vector>
24#include <memory>
25#include <type_traits>
26#include <algorithm>
27
28#include "serde.hpp"
29#include "tuple_sketch.hpp"
30
31namespace datasketches {
32
33// This simple array is faster than std::vector and should be sufficient for this application
34template<typename T, typename Allocator = std::allocator<T>>
35class array {
36public:
37 using value_type = T;
38 using allocator_type = Allocator;
39 using alloc_traits = std::allocator_traits<Allocator>;
40
41 explicit array(uint8_t size, const T& value, const Allocator& allocator = Allocator()):
42 allocator_(allocator), size_(size), array_(allocator_.allocate(size_)) {
43 init_values(value, std::is_trivially_copyable<T>());
44 }
45 array(const array& other):
46 allocator_(other.allocator_),
47 size_(other.size_),
48 array_(allocator_.allocate(size_))
49 {
50 copy_from(other, std::is_trivially_copyable<T>());
51 }
52 array(array&& other) noexcept:
53 allocator_(std::move(other.allocator_)),
54 size_(other.size_),
55 array_(other.array_)
56 {
57 other.array_ = nullptr;
58 other.size_ = 0;
59 }
60 ~array() {
61 if (array_ != nullptr) {
62 destroy_values(std::is_trivially_destructible<T>());
63 allocator_.deallocate(array_, size_);
64 }
65 }
66 array& operator=(const array& other) {
67 array copy(other);
68 std::swap(allocator_, copy.allocator_);
69 std::swap(size_, copy.size_);
70 std::swap(array_, copy.array_);
71 return *this;
72 }
73 array& operator=(array&& other) {
74 std::swap(allocator_, other.allocator_);
75 std::swap(size_, other.size_);
76 std::swap(array_, other.array_);
77 return *this;
78 }
79 T& operator[](size_t index) { return array_[index]; }
80 T operator[](size_t index) const { return array_[index]; }
81 uint8_t size() const { return size_; }
82 T* data() { return array_; }
83 const T* data() const { return array_; }
84 bool operator==(const array& other) const {
85 if (size_ != other.size_) return false;
86 for (uint8_t i = 0; i < size_; ++i) if (array_[i] != other.array_[i]) return false;
87 return true;
88 }
89private:
90 void init_values(const T& value, std::true_type) {
91 std::fill(array_, array_ + size_, value);
92 }
93 void init_values(const T& value, std::false_type) {
94 for (uint8_t i = 0; i < size_; ++i) {
95 alloc_traits::construct(allocator_, array_ + i, value);
96 }
97 }
98 void copy_from(const array& other, std::true_type) {
99 std::copy(other.array_, other.array_ + size_, array_);
100 }
101 void copy_from(const array& other, std::false_type) {
102 for (uint8_t i = 0; i < size_; ++i) {
103 alloc_traits::construct(allocator_, array_ + i, other.array_[i]);
104 }
105 }
106 void destroy_values(std::true_type) {}
107 void destroy_values(std::false_type) {
108 for (uint8_t i = 0; i < size_; ++i) {
109 alloc_traits::destroy(allocator_, array_ + i);
110 }
111 }
112
113 Allocator allocator_;
114 uint8_t size_;
115 T* array_;
116};
117
119template<typename Array, typename Allocator = typename Array::allocator_type>
121public:
122 default_array_tuple_update_policy(uint8_t num_values = 1, const Allocator& allocator = Allocator()):
123 allocator_(allocator), num_values_(num_values) {}
124 Array create() const {
125 return Array(num_values_, 0, allocator_);
126 }
127 template<typename InputArray> // to allow any type with indexed access (such as double* or std::vector)
128 void update(Array& array, const InputArray& update) const {
129 for (uint8_t i = 0; i < num_values_; ++i) array[i] += update[i];
130 }
131 uint8_t get_num_values() const {
132 return num_values_;
133 }
134
135private:
136 Allocator allocator_;
137 uint8_t num_values_;
138};
139
140// forward declaration
141template<typename Array, typename Allocator> class compact_array_tuple_sketch;
142
151template<
152 typename Array,
154 typename Allocator = typename Array::allocator_type
155>
156class update_array_tuple_sketch: public update_tuple_sketch<Array, Array, Policy, Allocator> {
157public:
159 using resize_factor = typename Base::resize_factor;
160
161 class builder;
162
163 compact_array_tuple_sketch<Array, Allocator> compact(bool ordered = true) const;
164
166 uint8_t get_num_values() const;
167
168private:
169 // for builder
170 update_array_tuple_sketch(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p, uint64_t theta,
171 uint64_t seed, const Policy& policy, const Allocator& allocator);
172};
173
175template<typename Array, typename Policy, typename Allocator>
176class update_array_tuple_sketch<Array, Policy, Allocator>::builder: public tuple_base_builder<builder, Policy, Allocator> {
177public:
183 builder(const Policy& policy = Policy(), const Allocator& allocator = Allocator());
184
186 update_array_tuple_sketch build() const;
187};
188
190template<
191 typename Array,
192 typename Allocator = typename Array::allocator_type
193>
194class compact_array_tuple_sketch: public compact_tuple_sketch<Array, Allocator> {
195public:
197 using Entry = typename Base::Entry;
198 using AllocEntry = typename Base::AllocEntry;
199 using AllocU64 = typename Base::AllocU64;
200 using vector_bytes = typename Base::vector_bytes;
201
202 static const uint8_t SERIAL_VERSION = 1;
203 static const uint8_t SKETCH_FAMILY = 9;
204 static const uint8_t SKETCH_TYPE = 3;
205 enum flags { UNUSED1, UNUSED2, IS_EMPTY, HAS_ENTRIES, IS_ORDERED };
206
213 template<typename Sketch>
214 compact_array_tuple_sketch(const Sketch& other, bool ordered = true);
215
217 uint8_t get_num_values() const;
218
223 void serialize(std::ostream& os) const;
224
232 vector_bytes serialize(unsigned header_size_bytes = 0) const;
233
241 static compact_array_tuple_sketch deserialize(std::istream& is, uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
242
251 static compact_array_tuple_sketch deserialize(const void* bytes, size_t size, uint64_t seed = DEFAULT_SEED,
252 const Allocator& allocator = Allocator());
253
254private:
255 uint8_t num_values_;
256
257 template<typename Ar, typename P, typename Al> friend class array_tuple_union;
258 template<typename Ar, typename P, typename Al> friend class array_tuple_intersection;
259 template<typename Ar, typename Al> friend class array_tuple_a_not_b;
260 compact_array_tuple_sketch(bool is_empty, bool is_ordered, uint16_t seed_hash, uint64_t theta, std::vector<Entry, AllocEntry>&& entries, uint8_t num_values);
261 compact_array_tuple_sketch(uint8_t num_values, Base&& base);
262};
263
264} /* namespace datasketches */
265
266#include "array_tuple_sketch_impl.hpp"
267
268#endif
array tuple A-not-B
Definition array_tuple_a_not_b.hpp:33
array tuple intersection
Definition array_tuple_intersection.hpp:37
array tuple union
Definition array_tuple_union.hpp:54
Compact array tuple sketch.
Definition array_tuple_sketch.hpp:194
uint8_t get_num_values() const
Definition array_tuple_sketch_impl.hpp:65
void serialize(std::ostream &os) const
This method serializes the sketch into a given stream in a binary form.
Definition array_tuple_sketch_impl.hpp:70
compact_array_tuple_sketch(const Sketch &other, bool ordered=true)
Copy constructor.
static compact_array_tuple_sketch deserialize(std::istream &is, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
This method deserializes a sketch from a given stream.
Definition array_tuple_sketch_impl.hpp:144
Compact Tuple sketch.
Definition tuple_sketch.hpp:457
virtual bool is_empty() const
Definition tuple_sketch_impl.hpp:329
virtual bool is_ordered() const
Definition tuple_sketch_impl.hpp:334
default array tuple update policy
Definition array_tuple_sketch.hpp:120
Tuple base builder.
Definition tuple_sketch.hpp:655
Update array tuple sketch builder.
Definition array_tuple_sketch.hpp:176
Update array tuple sketch.
Definition array_tuple_sketch.hpp:156
uint8_t get_num_values() const
Definition array_tuple_sketch_impl.hpp:28
Update Tuple sketch.
Definition tuple_sketch.hpp:222
DataSketches namespace.
Definition binomial_bounds.hpp:38