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
49template <typename W,
50 typename Allocator = std::allocator<W>>
52 static_assert(std::is_arithmetic<W>::value, "Arithmetic type expected");
53 static_assert(!std::is_same<W, bool>::value, "Boolean weight type is not supported");
54public:
55 using allocator_type = Allocator;
56 using const_iterator = typename std::vector<W, Allocator>::const_iterator;
57
72 count_min_sketch(uint8_t num_hashes, uint32_t num_buckets, uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
73
77 uint8_t get_num_hashes() const;
78
82 uint8_t get_serial_version() const;
83
87 uint32_t get_num_buckets() const;
88
92 uint64_t get_seed() const;
93
99 double get_relative_error() const;
100
105 W get_total_weight() const;
106
117 static uint32_t suggest_num_buckets(double relative_error);
118
128 static uint8_t suggest_num_hashes(double confidence);
129
136 W get_estimate(uint64_t item) const;
137
144 W get_estimate(int64_t item) const;
145
152 W get_estimate(const std::string& item) const;
153
162 W get_estimate(const void* item, size_t size) const;
163
171 W get_upper_bound(const void* item, size_t size) const;
172
179 W get_upper_bound(int64_t item) const;
180
187 W get_upper_bound(uint64_t item) const;
188
195 W get_upper_bound(const std::string& item) const;
196
204 W get_lower_bound(const void* item, size_t size) const;
205
212 W get_lower_bound(int64_t item) const;
213
220 W get_lower_bound(uint64_t item) const;
221
228 W get_lower_bound(const std::string& item) const;
229
238 void update(const void* item, size_t size, W weight);
239
245 void update(uint64_t item, W weight = 1);
246
252 void update(int64_t item, W weight = 1);
253
259 void update(const std::string& item, W weight = 1);
260
265 void merge(const count_min_sketch& other_sketch);
266
273 bool is_empty() const;
274
279 string<Allocator> to_string() const;
280
286 const_iterator begin() const;
287
294 const_iterator end() const;
295
296 /*
297 * The serialized sketch binary form has the following structure
298 * Byte 0:
299 * 1 - if and only if the sketch is empty
300 * 0 - otherwise
301 *
302 * Byte 1 (serial version), byte 2 (family id), byte 3 (flags):
303 * 00000001 - default for now.
304 *
305 * Bytes 4 - 7:
306 * uint8_t zero corresponding to ``empty''
307 *
308 * Byte 8:
309 * uint_8 for number of hash functions
310 *
311 * Bytes 9, 13
312 * 4 bytes : uint32 for number of buckets.
313 *
314 * Bytes 14, 15:
315 * seed_hash
316 *
317 * Byte 16:
318 * uint8_t zero corresponding to ``empty''
319 *
320 * All remaining bytes from 17-24 follow the pattern of
321 * Bytes 17-24:
322 * Sketch array entry
323 *
324
325 0 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
326 ||is_empty|ser__ver|familyId| flags |xxxxxxxx|xxxxxxxx|xxxxxxxx|xxxxxxxx|
327
328 1 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
329 ||---------- _num_buckets -----------|num_hash|__seed__ __hash__|xxxxxxxx|
330
331 2 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
332 ||---------------------------- total weight ----------------------------|
333
334 3 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
335 ||---------------------------- sketch entries ---------------------------|
336 ...
337
338 */
339
340
345 size_t get_serialized_size_bytes() const;
346
351 void serialize(std::ostream& os) const;
352
359 template<typename Sink>
360 size_t serialize_to(Sink&& sink) const;
361
362 // This is a convenience alias for users
363 // The type returned by the following serialize method
364 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
365
373 vector_bytes serialize(unsigned header_size_bytes = 0) const;
374
382 static count_min_sketch deserialize(std::istream& is, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
383
392 static count_min_sketch deserialize(const void* bytes, size_t size, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
393
397 allocator_type get_allocator() const;
398
399private:
400 Allocator _allocator;
401 uint8_t _serial_version; // determines the row seeds and the hashing
402 uint8_t _num_hashes;
403 uint32_t _num_buckets;
404 std::vector<W, Allocator> _sketch_array; // the array stored by the sketch
405 uint64_t _seed;
406 W _total_weight;
407 std::vector<uint64_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint64_t>> hash_seeds;
408
409 enum flags {IS_EMPTY};
410 static const uint8_t PREAMBLE_LONGS_SHORT = 2; // Empty -> need second byte for sketch parameters
411 static const uint8_t PREAMBLE_LONGS_FULL = 3; // Not empty -> need (at least) third byte for total weight.
412 static const uint8_t SERIAL_VERSION_1 = 1;
413 static const uint8_t SERIAL_VERSION_2 = 2;
414 static const uint8_t MAX_NUM_HASHES = 127; // stored as a signed byte in Java and Go
415 static const uint8_t FAMILY_ID = 18;
416 static const uint8_t NULL_8 = 0;
417 static const uint32_t NULL_32 = 0;
418
419 // used by deserialize to keep the hashing of a version 1 image
420 count_min_sketch(uint8_t num_hashes, uint32_t num_buckets, uint64_t seed, uint8_t serial_version, const Allocator& allocator);
421
428 static void check_header_validity(uint8_t preamble_longs, uint8_t serial_version, uint8_t family_id);
429
430 /*
431 * Compute the hash locations for an input item
432 * @param item pointer to the data item to be inserted into or queried from the sketch.
433 * @param size of the data in bytes
434 * @param callback function to invoke for each sketch array location
435 */
436 template<typename F>
437 void foreach_hash_location(const void* item, size_t size, F callback) const;
438
439};
440
441} /* namespace datasketches */
442
443#include "count_min_impl.hpp"
444
445#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:305
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition count_min_impl.hpp:300
double get_relative_error() const
Definition count_min_impl.hpp:105
uint32_t get_num_buckets() const
Definition count_min_impl.hpp:95
size_t serialize_to(Sink &&sink) const
This method serializes the sketch by passing binary fragments to a callback.
Definition count_min_impl.hpp:319
bool is_empty() const
Returns true if this sketch is empty.
Definition count_min_impl.hpp:467
W get_total_weight() const
Definition count_min_impl.hpp:110
uint8_t get_num_hashes() const
Definition count_min_impl.hpp:90
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:240
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:258
uint8_t get_serial_version() const
Definition count_min_impl.hpp:85
W get_estimate(uint64_t item) const
Suggests the number of buckets required to achieve the given relative error.
Definition count_min_impl.hpp:174
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,...
uint64_t get_seed() const
Definition count_min_impl.hpp:100
void merge(const count_min_sketch &other_sketch)
Merges another count_min_sketch into this count_min_sketch.
Definition count_min_impl.hpp:264
vector_bytes serialize(unsigned header_size_bytes=0) const
This method serializes the sketch as a vector of bytes.
void update(const void *item, size_t size, W weight)
Update this sketch with given data of any type.
Definition count_min_impl.hpp:215
string< Allocator > to_string() const
Returns a string describing the sketch.
Definition count_min_impl.hpp:472
size_t get_serialized_size_bytes() const
Computes size needed to serialize the current state of the sketch.
Definition count_min_impl.hpp:395
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition count_min_impl.hpp:295
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