datasketches-cpp
Loading...
Searching...
No Matches
frequent_items_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 FREQUENT_ITEMS_SKETCH_HPP_
21#define FREQUENT_ITEMS_SKETCH_HPP_
22
23#include <memory>
24#include <vector>
25#include <iostream>
26#include <functional>
27#include <type_traits>
28
29#include "reverse_purge_hash_map.hpp"
30#include "common_defs.hpp"
31#include "serde.hpp"
32
33namespace datasketches {
34
40
53template<
54 typename T,
55 typename W = uint64_t,
56 typename H = std::hash<T>,
57 typename E = std::equal_to<T>,
58 typename A = std::allocator<T>
59>
61 static_assert(std::is_arithmetic<W>::value, "Arithmetic type expected");
62public:
63
64 static const uint8_t LG_MIN_MAP_SIZE = 3;
65
77 explicit frequent_items_sketch(uint8_t lg_max_map_size, uint8_t lg_start_map_size = LG_MIN_MAP_SIZE,
78 const E& equal = E(), const A& allocator = A());
79
88 void update(const T& item, W weight = 1);
89
98 void update(T&& item, W weight = 1);
99
107 void merge(const frequent_items_sketch& other);
108
116 void merge(frequent_items_sketch&& other);
117
123 void reset();
124
130 bool is_empty() const;
131
135 uint32_t get_num_active_items() const;
136
142 W get_total_weight() const;
143
152 W get_estimate(const T& item) const;
153
161 W get_lower_bound(const T& item) const;
162
170 W get_upper_bound(const T& item) const;
171
177 W get_maximum_error() const;
178
184 double get_epsilon() const;
185
192 static double get_epsilon(uint8_t lg_max_map_size);
193
201 static double get_apriori_error(uint8_t lg_max_map_size, W estimated_total_weight);
202
203 class row;
204 using vector_row = typename std::vector<row, typename std::allocator_traits<A>::template rebind_alloc<row>>;
205
227 vector_row get_frequent_items(frequent_items_error_type err_type) const;
228
251 vector_row get_frequent_items(frequent_items_error_type err_type, W threshold) const;
252
259 template<typename SerDe = serde<T>>
260 size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
261
267 template<typename SerDe = serde<T>>
268 void serialize(std::ostream& os, const SerDe& sd = SerDe()) const;
269
270 // This is a convenience alias for users
271 // The type returned by the following serialize method
272 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<A>::template rebind_alloc<uint8_t>>;
273
283 template<typename SerDe = serde<T>>
284 vector_bytes serialize(unsigned header_size_bytes = 0, const SerDe& sd = SerDe()) const;
285
294 template<typename SerDe = serde<T>>
295 static frequent_items_sketch deserialize(std::istream& is, const SerDe& sd = SerDe(),
296 const E& equal = E(), const A& allocator = A());
297
307 template<typename SerDe = serde<T>>
308 static frequent_items_sketch deserialize(const void* bytes, size_t size, const SerDe& sd = SerDe(),
309 const E& equal = E(), const A& allocator = A());
310
315 string<A> to_string(bool print_items = false) const;
316
317private:
318 static const uint8_t SERIAL_VERSION = 1;
319 static const uint8_t FAMILY_ID = 10;
320 static const uint8_t PREAMBLE_LONGS_EMPTY = 1;
321 static const uint8_t PREAMBLE_LONGS_NONEMPTY = 4;
322 static constexpr double EPSILON_FACTOR = 3.5;
323 // Emptiness of a serialized image is determined by preamble longs (1 for empty, 4 otherwise).
324 // The flags byte is only cross-checked against it. Due to a mistake different bits were used
325 // in C++ and Java to indicate an empty sketch, therefore both are set for compatibility with
326 // the historical binary format, and either one is accepted on read. No other flag bits are defined.
327 enum flags { IS_EMPTY_1 = 0, IS_EMPTY_2 = 2 };
328 W total_weight;
329 W offset;
330 reverse_purge_hash_map<T, W, H, E, A> map;
331 static void check_preamble_longs(uint8_t preamble_longs, uint8_t flags_byte);
332 static void check_total_weight(W total_weight);
333 static void check_serial_version(uint8_t serial_version);
334 static void check_family_id(uint8_t family_id);
335 static void check_size(uint8_t lg_cur_size, uint8_t lg_max_size);
336
337 // version for integral signed type
338 template<typename WW = W, typename std::enable_if<std::is_integral<WW>::value && std::is_signed<WW>::value, int>::type = 0>
339 static inline void check_weight(WW weight);
340
341 // version for integral unsigned type
342 template<typename WW = W, typename std::enable_if<std::is_integral<WW>::value && std::is_unsigned<WW>::value, int>::type = 0>
343 static inline void check_weight(WW weight);
344
345 // version for floating point type
346 template<typename WW = W, typename std::enable_if<std::is_floating_point<WW>::value, int>::type = 0>
347 static inline void check_weight(WW weight);
348
349 // for deserialize
350 class items_deleter;
351};
352
354template<typename T, typename W, typename H, typename E, typename A>
355class frequent_items_sketch<T, W, H, E, A>::row {
356public:
357 row(const T* item, W weight, W offset):
358 item(item), weight(weight), offset(offset) {}
360 const T& get_item() const { return *item; }
362 W get_estimate() const { return weight + offset; }
364 W get_lower_bound() const { return weight; }
366 W get_upper_bound() const { return weight + offset; }
367private:
368 const T* item;
369 W weight;
370 W offset;
371};
372
373}
374
375#include "frequent_items_sketch_impl.hpp"
376
377# endif
Row in the output from get_frequent_items.
Definition frequent_items_sketch.hpp:355
W get_upper_bound() const
Definition frequent_items_sketch.hpp:366
W get_estimate() const
Definition frequent_items_sketch.hpp:362
const T & get_item() const
Definition frequent_items_sketch.hpp:360
W get_lower_bound() const
Definition frequent_items_sketch.hpp:364
frequent_items_sketch(uint8_t lg_max_map_size, uint8_t lg_start_map_size=LG_MIN_MAP_SIZE, const E &equal=E(), const A &allocator=A())
Construct this sketch with parameters lg_max_map_size and lg_start_map_size.
Definition frequent_items_sketch_impl.hpp:37
W get_upper_bound(const T &item) const
Returns the guaranteed upper bound weight (frequency) of the given item.
Definition frequent_items_sketch_impl.hpp:127
void merge(const frequent_items_sketch &other)
This function merges the other sketch into this one.
Definition frequent_items_sketch_impl.hpp:68
void serialize(std::ostream &os, const SerDe &sd=SerDe()) const
This method serializes the sketch into a given stream in a binary form.
Definition frequent_items_sketch_impl.hpp:174
vector_row get_frequent_items(frequent_items_error_type err_type) const
Returns an array of rows that include frequent items, estimates, upper and lower bounds given an erro...
Definition frequent_items_sketch_impl.hpp:153
bool is_empty() const
A sketch is empty if it has not been updated with any positive weight.
Definition frequent_items_sketch_impl.hpp:97
string< A > to_string(bool print_items=false) const
Returns a human readable summary of this sketch.
Definition frequent_items_sketch_impl.hpp:449
void update(const T &item, W weight=1)
Update this sketch with an item and a positive weight (frequency count).
Definition frequent_items_sketch_impl.hpp:52
static double get_apriori_error(uint8_t lg_max_map_size, W estimated_total_weight)
Returns the estimated a priori error given the max_map_size for the sketch and the estimated_total_st...
Definition frequent_items_sketch_impl.hpp:147
W get_total_weight() const
Returns the sum of the weights (frequencies) in the stream seen so far by the sketch.
Definition frequent_items_sketch_impl.hpp:109
static frequent_items_sketch deserialize(const void *bytes, size_t size, const SerDe &sd=SerDe(), const E &equal=E(), const A &allocator=A())
This method deserializes a sketch from a given array of bytes.
double get_epsilon() const
Returns epsilon value of this sketch.
Definition frequent_items_sketch_impl.hpp:137
size_t get_serialized_size_bytes(const SerDe &sd=SerDe()) const
Computes size needed to serialize the current state of the sketch.
Definition frequent_items_sketch_impl.hpp:221
W get_maximum_error() const
Definition frequent_items_sketch_impl.hpp:132
W get_estimate(const T &item) const
Returns the estimate of the weight (frequency) of the given item.
Definition frequent_items_sketch_impl.hpp:114
static frequent_items_sketch deserialize(std::istream &is, const SerDe &sd=SerDe(), const E &equal=E(), const A &allocator=A())
This method deserializes a sketch from a given stream.
void reset()
Resets this sketch to the empty state, as if newly constructed.
Definition frequent_items_sketch_impl.hpp:90
W get_lower_bound(const T &item) const
Returns the guaranteed lower bound weight (frequency) of the given item.
Definition frequent_items_sketch_impl.hpp:122
uint32_t get_num_active_items() const
Definition frequent_items_sketch_impl.hpp:104
DataSketches namespace.
Definition binomial_bounds.hpp:38
frequent_items_error_type
Frequent items error type.
Definition frequent_items_sketch.hpp:36
@ NO_FALSE_NEGATIVES
include an item in the result list if get_upper_bound(item) > threshold
Definition frequent_items_sketch.hpp:38
@ NO_FALSE_POSITIVES
include an item in the result list if get_lower_bound(item) > threshold
Definition frequent_items_sketch.hpp:37