#ifndef COUNT_MIN_HPP_
#define COUNT_MIN_HPP_
#include <iterator>
#include "common_defs.hpp"
namespace datasketches {
template <typename W,
typename Allocator = std::allocator<W>>
class count_min_sketch{
static_assert(std::is_arithmetic<W>::value, "Arithmetic type expected");
public:
using allocator_type = Allocator;
using const_iterator = typename std::vector<W, Allocator>::const_iterator;
count_min_sketch(uint8_t num_hashes, uint32_t num_buckets, uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
uint8_t get_num_hashes() const;
uint32_t get_num_buckets() const;
uint64_t get_seed() const;
double get_relative_error() const;
W get_total_weight() const;
static uint32_t suggest_num_buckets(double relative_error);
static uint8_t suggest_num_hashes(double confidence);
W get_estimate(uint64_t item) const;
W get_estimate(int64_t item) const;
W get_estimate(const std::string& item) const;
W get_estimate(const void* item, size_t size) const;
W get_upper_bound(const void* item, size_t size) const;
W get_upper_bound(int64_t item) const;
W get_upper_bound(uint64_t item) const;
W get_upper_bound(const std::string& item) const;
W get_lower_bound(const void* item, size_t size) const;
W get_lower_bound(int64_t item) const;
W get_lower_bound(uint64_t item) const;
W get_lower_bound(const std::string& item) const;
void update(const void* item, size_t size, W weight);
void update(uint64_t item, W weight = 1);
void update(int64_t item, W weight = 1);
void update(const std::string& item, W weight = 1);
void merge(const count_min_sketch& other_sketch);
bool is_empty() const;
string<Allocator> to_string() const;
const_iterator begin() const;
const_iterator end() const;
size_t get_serialized_size_bytes() const;
void serialize(std::ostream& os) const;
using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
vector_bytes serialize(unsigned header_size_bytes = 0) const;
static count_min_sketch deserialize(std::istream& is, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
static count_min_sketch deserialize(const void* bytes, size_t size, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
allocator_type get_allocator() const;
private:
Allocator _allocator;
uint8_t _num_hashes;
uint32_t _num_buckets;
std::vector<W, Allocator> _sketch_array; uint64_t _seed;
W _total_weight;
std::vector<uint64_t> hash_seeds;
enum flags {IS_EMPTY};
static const uint8_t PREAMBLE_LONGS_SHORT = 2; static const uint8_t PREAMBLE_LONGS_FULL = 3; static const uint8_t SERIAL_VERSION_1 = 1;
static const uint8_t FAMILY_ID = 18;
static const uint8_t NULL_8 = 0;
static const uint32_t NULL_32 = 0;
static void check_header_validity(uint8_t preamble_longs, uint8_t serial_version, uint8_t family_id, uint8_t flags_byte);
std::vector<uint64_t> get_hashes(const void* item, size_t size) const;
};
}
#include "count_min_impl.hpp"
#endif