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+
120#ifndef COUNT_MIN_HPP_
221#define COUNT_MIN_HPP_
322
@@ -14,36 +33,37 @@ namespace datasketches {
1433 * @author Charlie Dickens
1534 */
1635
17- template <typename W>
36+ template <typename W,
37+ typename Allocator = std::allocator<W>>
1838class count_min_sketch {
1939 static_assert (std::is_arithmetic<W>::value, " Arithmetic type expected" );
2040public:
41+ using allocator_type = Allocator;
2142
22- using vector_bytes = std::vector<uint8_t >;
2343 /* *
24- * Creates an instance of the sketch given parameters _num_hashes, _num_buckets and hash seed, `seed`.
25- * @param num_hashes : number of hash functions in the sketch. Equivalently the number of rows in the array
26- * @param num_buckets : number of buckets that hash functions map into. Equivalently the number of columns in the array
27- * @param seed for hash function
28- *
29- * The items inserted into the sketch can be arbitrary type, so long as they are hashable via murmurhash.
30- * Only update and estimate methods are added for uint64_t and string types.
31- */
32- count_min_sketch (uint8_t num_hashes, uint32_t num_buckets, uint64_t seed = DEFAULT_SEED ) ;
44+ * Creates an instance of the sketch given parameters _num_hashes, _num_buckets and hash seed, `seed`.
45+ * @param num_hashes : number of hash functions in the sketch. Equivalently the number of rows in the array
46+ * @param num_buckets : number of buckets that hash functions map into. Equivalently the number of columns in the array
47+ * @param seed for hash function
48+ *
49+ * The items inserted into the sketch can be arbitrary type, so long as they are hashable via murmurhash.
50+ * Only update and estimate methods are added for uint64_t and string types.
51+ */
52+ count_min_sketch (uint8_t num_hashes, uint32_t num_buckets, uint64_t seed = DEFAULT_SEED , const Allocator& allocator = Allocator() ) ;
3353
3454 /* *
35- * @return configured _num_hashes of this sketch
36- */
55+ * @return configured _num_hashes of this sketch
56+ */
3757 uint8_t get_num_hashes () const ;
3858
3959 /* *
40- * @return configured _num_buckets of this sketch
41- */
60+ * @return configured _num_buckets of this sketch
61+ */
4262 uint32_t get_num_buckets () const ;
4363
4464 /* *
45- * @return configured seed of this sketch
46- */
65+ * @return configured seed of this sketch
66+ */
4767 uint64_t get_seed () const ;
4868
4969 /* *
@@ -54,20 +74,20 @@ class count_min_sketch{
5474 double get_relative_error () const ;
5575
5676 /* *
57- * @return _total_weight : typename W
58- * The total weight currently inserted into the stream.
59- */
77+ * @return _total_weight : typename W
78+ * The total weight currently inserted into the stream.
79+ */
6080 W get_total_weight () const ;
6181
6282 /*
63- * @param relative_error : double -- the desired accuracy within which estimates should lie.
64- * For example, when relative_error = 0.05, then the returned frequency estimates satisfy the
65- * `relative_error` guarantee that never overestimates the weights but may underestimate the weights
66- * by 5% of the total weight in the sketch.
67- * @return number_of_buckets : the number of hash buckets at every level of the
68- * sketch required in order to obtain the specified relative error.
69- * [1] - Section 3 ``Data Structure'', page 6.
70- */
83+ * @param relative_error : double -- the desired accuracy within which estimates should lie.
84+ * For example, when relative_error = 0.05, then the returned frequency estimates satisfy the
85+ * `relative_error` guarantee that never overestimates the weights but may underestimate the weights
86+ * by 5% of the total weight in the sketch.
87+ * @return number_of_buckets : the number of hash buckets at every level of the
88+ * sketch required in order to obtain the specified relative error.
89+ * [1] - Section 3 ``Data Structure'', page 6.
90+ */
7191 static uint32_t suggest_num_buckets (double relative_error) ;
7292
7393 /*
@@ -77,7 +97,7 @@ class count_min_sketch{
7797 * order to achieve the specified confidence of the sketch.
7898 * confidence = 1 - delta, with delta denoting the sketch failure probability in the literature.
7999 * [1] - Section 3 ``Data Structure'', page 6.
80- */
100+ */
81101 static uint8_t suggest_num_hashes (double confidence) ;
82102
83103 /* *
@@ -88,7 +108,15 @@ class count_min_sketch{
88108 */
89109 W get_estimate (uint64_t item) const ;
90110
91- /* *
111+ /* *
112+ * Specific get_estimate function for int64_t type
113+ * see generic get_estimate function
114+ * @param item : uint64_t type.
115+ * @return an estimate of the item's frequency.
116+ */
117+ W get_estimate (int64_t item) const ;
118+
119+ /* *
92120 * Specific get_estimate function for std::string type
93121 * see generic get_estimate function
94122 * @param item : std::string type
@@ -97,51 +125,53 @@ class count_min_sketch{
97125 W get_estimate (const std::string& item) const ;
98126
99127 /* *
100- * This is the generic estimate query function for any of the given datatypes.
101- * Query the sketch for the estimate of a given item.
102- * @param item : pointer to the data item to be query from the sketch.
103- * @param size : size_t
104- * @return the estimated frequency of the item denoted f_est satisfying
105- * f_true - relative_error*_total_weight <= f_est <= f_true
106- */
107- W get_estimate (const void * item, size_t size) const ;
128+ * This is the generic estimate query function for any of the given datatypes.
129+ * Query the sketch for the estimate of a given item.
130+ * @param item : pointer to the data item to be query from the sketch.
131+ * @param size : size_t
132+ * @return the estimated frequency of the item denoted f_est satisfying
133+ * f_true - relative_error*_total_weight <= f_est <= f_true
134+ */
135+ W get_estimate (const void * item, size_t size) const ;
108136
109137 /* *
110- * Query the sketch for the upper bound of a given item.
111- * @param item : uint64_t or std::string to query
112- * @return the upper bound on the true frequency of the item
113- * f_true <= f_est + relative_error*_total_weight
114- */
115- W get_upper_bound (const void * item, size_t size) const ;
116- W get_upper_bound (uint64_t ) const ;
117- W get_upper_bound (const std::string& item) const ;
138+ * Query the sketch for the upper bound of a given item.
139+ * @param item : uint64_t or std::string to query
140+ * @return the upper bound on the true frequency of the item
141+ * f_true <= f_est + relative_error*_total_weight
142+ */
143+ W get_upper_bound (const void * item, size_t size) const ;
144+ W get_upper_bound (int64_t ) const ;
145+ W get_upper_bound (uint64_t ) const ;
146+ W get_upper_bound (const std::string& item) const ;
118147
119148 /* *
120- * Query the sketch for the lower bound of a given item.
121- * @param item : uint64_t or std::string to query
122- * @return the lower bound for the query result, f_est, on the true frequency, f_est of the item
123- * f_true - relative_error*_total_weight <= f_est
124- */
149+ * Query the sketch for the lower bound of a given item.
150+ * @param item : uint64_t or std::string to query
151+ * @return the lower bound for the query result, f_est, on the true frequency, f_est of the item
152+ * f_true - relative_error*_total_weight <= f_est
153+ */
125154 W get_lower_bound (const void * item, size_t size) const ;
155+ W get_lower_bound (int64_t ) const ;
126156 W get_lower_bound (uint64_t ) const ;
127157 W get_lower_bound (const std::string& item) const ;
128158
129159 /*
130- * Update this sketch with given data of any type.
131- * This is a "universal" update that covers all cases above,
132- * but may produce different hashes.
133- * @param item pointer to the data item to be inserted into the sketch.
134- * @param size of the data in bytes
135- * @return vector of uint64_t which each represent the index to which `value' must update in the sketch
136- */
160+ * Update this sketch with given data of any type.
161+ * This is a "universal" update that covers all cases above,
162+ * but may produce different hashes.
163+ * @param item pointer to the data item to be inserted into the sketch.
164+ * @param size of the data in bytes
165+ * @return vector of uint64_t which each represent the index to which `value' must update in the sketch
166+ */
137167 void update (const void * item, size_t size, W weight) ;
138168
139169 /* *
140- * Update this sketch with a given uint64_t item.
141- * @param item : uint64_t to update the sketch with
142- * @param weight : arithmetic type
143- * void function which inserts an item of type uint64_t into the sketch
144- */
170+ * Update this sketch with a given uint64_t item.
171+ * @param item : uint64_t to update the sketch with
172+ * @param weight : arithmetic type
173+ * void function which inserts an item of type uint64_t into the sketch
174+ */
145175 void update (uint64_t item, W weight) ;
146176 void update (uint64_t item) ;
147177 void update (int64_t item, W weight) ;
@@ -157,9 +187,9 @@ class count_min_sketch{
157187 void update (const std::string& item) ;
158188
159189 /*
160- * merges a separate count_min_sketch into this count_min_sketch.
161- */
162- void merge (const count_min_sketch<W> &other_sketch) ;
190+ * merges a separate count_min_sketch into this count_min_sketch.
191+ */
192+ void merge (const count_min_sketch &other_sketch) ;
163193
164194 /* *
165195 * Returns true if this sketch is empty.
@@ -169,8 +199,14 @@ class count_min_sketch{
169199 */
170200 bool is_empty () const ;
171201
202+ /* *
203+ * @brief Returns a string describing the sketch
204+ * @return A string with a human-readable description of the sketch
205+ */
206+ string<Allocator> to_string () const ;
207+
172208 // Iterators
173- using const_iterator = typename std::vector<W>::const_iterator ;
209+ using const_iterator = typename std::vector<W, Allocator >::const_iterator ;
174210 const_iterator begin () const ;
175211 const_iterator end () const ;
176212
@@ -222,8 +258,22 @@ class count_min_sketch{
222258 *
223259 */
224260
261+
262+ /* *
263+ * Computes size needed to serialize the current state of the sketch.
264+ * @return size in bytes needed to serialize this sketch
265+ */
266+ size_t get_serialized_size_bytes () const ;
267+
268+ /* *
269+ * This method serializes a binary image of the sketch to an output stream.
270+ */
225271 void serialize (std::ostream& os) const ;
226272
273+ // This is a convenience alias for users
274+ // The type returned by the following serialize method
275+ using vector_bytes = std::vector<uint8_t , typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t >>;
276+
227277 /* *
228278 * This method serializes the sketch as a vector of bytes.
229279 * An optional header can be reserved in front of the sketch.
@@ -234,27 +284,33 @@ class count_min_sketch{
234284 vector_bytes serialize (unsigned header_size_bytes = 0 ) const ;
235285
236286 /* *
237- * This method deserializes a sketch from a given stream.
238- * @param is input stream
239- * @param seed the seed for the hash function that was used to create the sketch
240- * @return an instance of a sketch
241- */
242- // static count_min_sketch deserialize(std::istream& is, uint64_t seed=DEFAULT_SEED) const;
243- static count_min_sketch deserialize (std::istream& is, uint64_t seed) ;
287+ * This method deserializes a sketch from a given stream.
288+ * @param is input stream
289+ * @param seed the seed for the hash function that was used to create the sketch
290+ * @return an instance of a sketch
291+ */
292+ static count_min_sketch deserialize (std::istream& is, uint64_t seed=DEFAULT_SEED , const Allocator& allocator = Allocator());
244293
245294 /* *
246- * This method deserializes a sketch from a given array of bytes.
247- * @param bytes pointer to the array of bytes
248- * @param size the size of the array
249- * @param seed the seed for the hash function that was used to create the sketch
250- * @return an instance of the sketch
251- */
252- static count_min_sketch deserialize (const void * bytes, size_t size, uint64_t seed=DEFAULT_SEED );
295+ * This method deserializes a sketch from a given array of bytes.
296+ * @param bytes pointer to the array of bytes
297+ * @param size the size of the array
298+ * @param seed the seed for the hash function that was used to create the sketch
299+ * @return an instance of the sketch
300+ */
301+ static count_min_sketch deserialize (const void * bytes, size_t size, uint64_t seed=DEFAULT_SEED , const Allocator& allocator = Allocator());
302+
303+ /* *
304+ * Returns the allocator for this sketch.
305+ * @return allocator
306+ */
307+ allocator_type get_allocator () const ;
253308
254309private:
310+ Allocator _allocator;
255311 uint8_t _num_hashes ;
256312 uint32_t _num_buckets ;
257- std::vector<W> _sketch_array ; // the array stored by the sketch
313+ std::vector<W, Allocator > _sketch_array ; // the array stored by the sketch
258314 uint64_t _seed ;
259315 W _total_weight ;
260316 std::vector<uint64_t > hash_seeds ;
0 commit comments