datasketches-cpp
Loading...
Searching...
No Matches
count_min.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 COUNT_MIN_HPP_
21#define COUNT_MIN_HPP_
22
23#include <iterator>
24#include <type_traits>
25#include <vector>
26#include "common_defs.hpp"
27
28namespace datasketches {
29
37template <typename W,
38 typename Allocator = std::allocator<W>>
40 static_assert(std::is_arithmetic<W>::value, "Arithmetic type expected");
41 static_assert(!std::is_same<W, bool>::value, "Boolean weight type is not supported");
42public:
43 using allocator_type = Allocator;
44 using const_iterator = typename std::vector<W, Allocator>::const_iterator;
45
56 count_min_sketch(uint8_t num_hashes, uint32_t num_buckets, uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
57
61 uint8_t get_num_hashes() const;
62
66 uint32_t get_num_buckets() const;
67
71 uint64_t get_seed() const;
72
78 double get_relative_error() const;
79
84 W get_total_weight() const;
85
96 static uint32_t suggest_num_buckets(double relative_error);
97
107 static uint8_t suggest_num_hashes(double confidence);
108
115 W get_estimate(uint64_t item) const;
116
123 W get_estimate(int64_t item) const;
124
131 W get_estimate(const std::string& item) const;
132
141 W get_estimate(const void* item, size_t size) const;
142
150 W get_upper_bound(const void* item, size_t size) const;
151
158 W get_upper_bound(int64_t item) const;
159
166 W get_upper_bound(uint64_t item) const;
167
174 W get_upper_bound(const std::string& item) const;
175
183 W get_lower_bound(const void* item, size_t size) const;
184
191 W get_lower_bound(int64_t item) const;
192
199 W get_lower_bound(uint64_t item) const;
200
207 W get_lower_bound(const std::string& item) const;
208
217 void update(const void* item, size_t size, W weight);
218
224 void update(uint64_t item, W weight = 1);
225
231 void update(int64_t item, W weight = 1);
232
238 void update(const std::string& item, W weight = 1);
239
244 void merge(const count_min_sketch& other_sketch);
245
252 bool is_empty() const;
253
258 string<Allocator> to_string() const;
259
265 const_iterator begin() const;
266
273 const_iterator end() const;
274
275 /*
276 * The serialized sketch binary form has the following structure
277 * Byte 0:
278 * 1 - if and only if the sketch is empty
279 * 0 - otherwise
280 *
281 * Byte 1 (serial version), byte 2 (family id), byte 3 (flags):
282 * 00000001 - default for now.
283 *
284 * Bytes 4 - 7:
285 * uint8_t zero corresponding to ``empty''
286 *
287 * Byte 8:
288 * uint_8 for number of hash functions
289 *
290 * Bytes 9, 13
291 * 4 bytes : uint32 for number of buckets.
292 *
293 * Bytes 14, 15:
294 * seed_hash
295 *
296 * Byte 16:
297 * uint8_t zero corresponding to ``empty''
298 *
299 * All remaining bytes from 17-24 follow the pattern of
300 * Bytes 17-24:
301 * Sketch array entry
302 *
303
304 0 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
305 ||is_empty|ser__ver|familyId| flags |xxxxxxxx|xxxxxxxx|xxxxxxxx|xxxxxxxx|
306
307 1 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
308 ||---------- _num_buckets -----------|num_hash|__seed__ __hash__|xxxxxxxx|
309
310 2 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
311 ||---------------------------- total weight ----------------------------|
312
313 3 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
314 ||---------------------------- sketch entries ---------------------------|
315 ...
316
317 */
318
319
324 size_t get_serialized_size_bytes() const;
325
330 void serialize(std::ostream& os) const;
331
338 template<typename Sink>
339 size_t serialize_to(Sink&& sink) const;
340
341 // This is a convenience alias for users
342 // The type returned by the following serialize method
343 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
344
352 vector_bytes serialize(unsigned header_size_bytes = 0) const;
353
361 static count_min_sketch deserialize(std::istream& is, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
362
371 static count_min_sketch deserialize(const void* bytes, size_t size, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
372
376 allocator_type get_allocator() const;
377
378private:
379 Allocator _allocator;
380 uint8_t _num_hashes;
381 uint32_t _num_buckets;
382 std::vector<W, Allocator> _sketch_array; // the array stored by the sketch
383 uint64_t _seed;
384 W _total_weight;
385 std::vector<uint64_t> hash_seeds;
386
387 enum flags {IS_EMPTY};
388 static const uint8_t PREAMBLE_LONGS_SHORT = 2; // Empty -> need second byte for sketch parameters
389 static const uint8_t PREAMBLE_LONGS_FULL = 3; // Not empty -> need (at least) third byte for total weight.
390 static const uint8_t SERIAL_VERSION_1 = 1;
391 static const uint8_t FAMILY_ID = 18;
392 static const uint8_t NULL_8 = 0;
393 static const uint32_t NULL_32 = 0;
394
401 static void check_header_validity(uint8_t preamble_longs, uint8_t serial_version, uint8_t family_id, uint8_t flags_byte);
402
403 /*
404 * Compute the hash locations for an input item
405 * @param item pointer to the data item to be inserted into or queried from the sketch.
406 * @param size of the data in bytes
407 * @param callback function to invoke for each sketch array location
408 */
409 template<typename F>
410 void foreach_hash_location(const void* item, size_t size, F callback) const;
411
412};
413
414} /* namespace datasketches */
415
416#include "count_min_impl.hpp"
417
418#endif
static count_min_sketch deserialize(std::istream &is, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
This method deserializes a sketch from a given stream.
void serialize(std::ostream &os) const
This method serializes the sketch into a given stream in a binary form.
Definition count_min_impl.hpp:264
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition count_min_impl.hpp:259
double get_relative_error() const
Definition count_min_impl.hpp:78
uint32_t get_num_buckets() const
Definition count_min_impl.hpp:68
size_t serialize_to(Sink &&sink) const
This method serializes the sketch by passing binary fragments to a callback.
Definition count_min_impl.hpp:278
bool is_empty() const
Returns true if this sketch is empty.
Definition count_min_impl.hpp:426
W get_total_weight() const
Definition count_min_impl.hpp:83
uint8_t get_num_hashes() const
Definition count_min_impl.hpp:63
allocator_type get_allocator() const
W get_upper_bound(const void *item, size_t size) const
Query the sketch for the upper bound of a given item.
Definition count_min_impl.hpp:207
W get_lower_bound(const void *item, size_t size) const
Query the sketch for the lower bound of a given item.
Definition count_min_impl.hpp:224
W get_estimate(uint64_t item) const
Suggests the number of buckets required to achieve the given relative error.
Definition count_min_impl.hpp:143
count_min_sketch(uint8_t num_hashes, uint32_t num_buckets, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
Creates an instance of the sketch given parameters _num_hashes, _num_buckets and hash seed,...
Definition count_min_impl.hpp:35
uint64_t get_seed() const
Definition count_min_impl.hpp:73
void merge(const count_min_sketch &other_sketch)
Merges another count_min_sketch into this count_min_sketch.
Definition count_min_impl.hpp:229
void update(const void *item, size_t size, W weight)
Update this sketch with given data of any type.
Definition count_min_impl.hpp:183
string< Allocator > to_string() const
Returns a string describing the sketch.
Definition count_min_impl.hpp:431
size_t get_serialized_size_bytes() const
Computes size needed to serialize the current state of the sketch.
Definition count_min_impl.hpp:354
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition count_min_impl.hpp:254
static count_min_sketch deserialize(const void *bytes, size_t size, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
This method deserializes a sketch from a given array of bytes.
DataSketches namespace.
Definition binomial_bounds.hpp:38