datasketches-cpp
Loading...
Searching...
No Matches
density_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 DENSITY_SKETCH_HPP_
21#define DENSITY_SKETCH_HPP_
22
23#include <type_traits>
24#include <vector>
25#include <functional>
26#include <numeric>
27#include <cmath>
28
29#include "common_defs.hpp"
30
31namespace datasketches {
32
33template<typename T>
34struct gaussian_kernel {
35 // any two vectors of T, so the sketch's points can use its allocator
36 template<typename V1, typename V2>
37 T operator()(const V1& v1, const V2& v2) const {
38 return exp(-std::inner_product(v1.begin(), v1.end(), v2.begin(), 0.0, std::plus<T>(), [](T a, T b){return (a-b)*(a-b);}));
39 }
40};
41
54template<
55 typename T,
56 typename Kernel = gaussian_kernel<T>,
57 typename Allocator = std::allocator<T>
58>
60 static_assert(std::is_floating_point<T>::value, "Floating point type expected");
61
62public:
63 using Vector = std::vector<T, Allocator>;
64 using Level = std::vector<Vector, typename std::allocator_traits<Allocator>::template rebind_alloc<Vector>>;
65 using Levels = std::vector<Level, typename std::allocator_traits<Allocator>::template rebind_alloc<Level>>;
66
74 density_sketch(uint16_t k, uint32_t dim, const Kernel& kernel = Kernel(), const Allocator& allocator = Allocator());
75
80 uint16_t get_k() const;
81
86 uint32_t get_dim() const;
87
92 bool is_empty() const;
93
98 uint64_t get_n() const;
99
104 uint32_t get_num_retained() const;
105
110 bool is_estimation_mode() const;
111
116 template<typename FwdVector>
117 void update(FwdVector&& point);
118
123 template<typename FwdSketch>
124 void merge(FwdSketch&& other);
125
130 T get_estimate(const std::vector<T>& point) const;
131
136 Allocator get_allocator() const;
137
142 void serialize(std::ostream& os) const;
143
144 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
145
153 vector_bytes serialize(unsigned header_size_bytes = 0) const;
154
162 static density_sketch deserialize(std::istream& is,
163 const Kernel& kernel=Kernel(), const Allocator& allocator = Allocator());
164
173 static density_sketch deserialize(const void* bytes, size_t size,
174 const Kernel& kernel=Kernel(), const Allocator& allocator = Allocator());
175
181 string<Allocator> to_string(bool print_levels = false, bool print_items = false) const;
182
183 class const_iterator;
184
190 const_iterator begin() const;
191
198 const_iterator end() const;
199
200private:
201 enum flags { RESERVED0, RESERVED1, IS_EMPTY };
202 static const uint8_t PREAMBLE_INTS_SHORT = 3;
203 static const uint8_t PREAMBLE_INTS_LONG = 6;
204 static const uint8_t FAMILY_ID = 19;
205 static const uint8_t SERIAL_VERSION = 1;
206 static const size_t LEVELS_ARRAY_START = 5;
207
208 Kernel kernel_;
209 uint16_t k_;
210 uint32_t dim_;
211 uint32_t num_retained_;
212 uint64_t n_;
213 Levels levels_;
214
215 void compact();
216 void compact_level(unsigned height);
217
218 static void check_k(uint16_t k);
219 static void check_serial_version(uint8_t serial_version);
220 static void check_family_id(uint8_t family_id);
221 static void check_header_validity(uint8_t preamble_ints, uint8_t flags_byte, uint8_t serial_version);
222
223 density_sketch(uint16_t k, uint32_t dim, uint32_t num_retained, uint64_t n, Levels&& levels,
224 const Kernel& kernel = Kernel());
225};
226
227template<typename T, typename K, typename A>
228class density_sketch<T, K, A>::const_iterator {
229public:
230 using Vector = density_sketch<T, K, A>::Vector;
231 using iterator_category = std::input_iterator_tag;
232 using value_type = std::pair<const Vector&, const uint64_t>;
233 using difference_type = void;
234 using pointer = return_value_holder<value_type>;
235 using reference = const value_type;
236 const_iterator& operator++();
237 const_iterator& operator++(int);
238 bool operator==(const const_iterator& other) const;
239 bool operator!=(const const_iterator& other) const;
240 const value_type operator*() const;
241 const return_value_holder<value_type> operator->() const;
242private:
243 using LevelsIterator = typename density_sketch<T, K, A>::Levels::const_iterator;
244 using LevelIterator = typename density_sketch<T, K, A>::Level::const_iterator;
245 LevelsIterator levels_it_;
246 LevelsIterator levels_end_;
247 LevelIterator level_it_;
248 unsigned height_;
249 friend class density_sketch<T, K, A>;
250 const_iterator(LevelsIterator begin, LevelsIterator end);
251};
252
253} /* namespace datasketches */
254
255#include "density_sketch_impl.hpp"
256
257#endif
density_sketch(uint16_t k, uint32_t dim, const Kernel &kernel=Kernel(), const Allocator &allocator=Allocator())
Constructor.
T get_estimate(const std::vector< T > &point) const
Density estimate at a given point.
Definition density_sketch_impl.hpp:115
void serialize(std::ostream &os) const
This method serializes the sketch into a given stream in a binary form.
Definition density_sketch_impl.hpp:184
void merge(FwdSketch &&other)
Merges another sketch into this one.
Definition density_sketch_impl.hpp:98
static density_sketch deserialize(const void *bytes, size_t size, const Kernel &kernel=Kernel(), const Allocator &allocator=Allocator())
This method deserializes a sketch from a given array of bytes.
uint32_t get_num_retained() const
Returns the number of retained points in the sketch.
Definition density_sketch_impl.hpp:77
uint32_t get_dim() const
Returns configured dimensions.
Definition density_sketch_impl.hpp:62
string< Allocator > to_string(bool print_levels=false, bool print_items=false) const
Prints a summary of the sketch.
Definition density_sketch_impl.hpp:420
uint16_t get_k() const
Returns configured parameter K.
Definition density_sketch_impl.hpp:57
bool is_empty() const
Returns true if this sketch is empty.
Definition density_sketch_impl.hpp:67
void update(FwdVector &&point)
Updates this sketch with a given point.
Definition density_sketch_impl.hpp:88
Allocator get_allocator() const
Returns an instance of the allocator for this sketch.
Definition density_sketch_impl.hpp:127
vector_bytes serialize(unsigned header_size_bytes=0) const
This method serializes the sketch as a vector of bytes.
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition density_sketch_impl.hpp:468
static density_sketch deserialize(std::istream &is, const Kernel &kernel=Kernel(), const Allocator &allocator=Allocator())
This method deserializes a sketch from a given stream.
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition density_sketch_impl.hpp:473
bool is_estimation_mode() const
Returns true if this sketch is in estimation mode.
Definition density_sketch_impl.hpp:82
uint64_t get_n() const
Returns the length of the input stream (number of points observed by this sketch).
Definition density_sketch_impl.hpp:72
DataSketches namespace.
Definition binomial_bounds.hpp:38