@@ -186,19 +186,26 @@ class Minisketch
186
186
std::unique_ptr<minisketch, Deleter> m_minisketch;
187
187
188
188
public:
189
- /* * See minisketch_bits_supported() . */
189
+ /* * Check whether the library supports fields of the given size . */
190
190
static bool BitsSupported (uint32_t bits) noexcept { return minisketch_bits_supported (bits); }
191
191
192
- /* * minisketch_implementation_supported(). */
193
- static bool ImplementationSupported (uint32_t bits, uint32_t implementation) noexcept { return minisketch_implementation_supported (bits, implementation); }
194
-
195
- /* * See minisketch_implementation_max(). */
192
+ /* * Get the highest supported implementation number. */
196
193
static uint32_t MaxImplementation () noexcept { return minisketch_implementation_max (); }
197
194
198
- /* * See minisketch_compute_capacity(). */
195
+ /* * Check whether the library supports fields with a given size and implementation number.
196
+ * If a particular field size `bits` is supported, implementation 0 is always supported for it.
197
+ * Higher implementation numbers may or may not be available as well, up to MaxImplementation().
198
+ */
199
+ static bool ImplementationSupported (uint32_t bits, uint32_t implementation) noexcept { return minisketch_implementation_supported (bits, implementation); }
200
+
201
+ /* * Given field size and a maximum number of decodable elements n, compute what capacity c to
202
+ * use so that sketches with more elements than n have a chance no higher than 2^-fpbits of
203
+ * being decoded incorrectly (and will instead fail when decoding for up to n elements).
204
+ *
205
+ * See minisketch_compute_capacity for more details. */
199
206
static size_t ComputeCapacity (uint32_t bits, size_t max_elements, uint32_t fpbits) noexcept { return minisketch_compute_capacity (bits, max_elements, fpbits); }
200
207
201
- /* * See minisketch_compute_max_elements() . */
208
+ /* * Reverse operation of ComputeCapacity. See minisketch_compute_max_elements. */
202
209
static size_t ComputeMaxElements (uint32_t bits, size_t capacity, uint32_t fpbits) noexcept { return minisketch_compute_max_elements (bits, capacity, fpbits); }
203
210
204
211
/* * Construct a clone of the specified sketch. */
@@ -214,55 +221,70 @@ class Minisketch
214
221
return *this ;
215
222
}
216
223
224
+ /* * Check whether this Minisketch object is valid. */
217
225
explicit operator bool () const noexcept { return bool {m_minisketch}; }
218
226
227
+ /* * Construct an (invalid) Minisketch object. */
219
228
Minisketch () noexcept = default ;
229
+
230
+ /* * Move constructor. */
220
231
Minisketch (Minisketch&&) noexcept = default ;
232
+
233
+ /* * Move assignment. */
221
234
Minisketch& operator =(Minisketch&&) noexcept = default ;
222
235
223
- /* * Construct a Minisketch object with the specified parameters. */
236
+ /* * Construct a Minisketch object with the specified parameters.
237
+ *
238
+ * If bits is not BitsSupported(), or the combination of bits and capacity is not
239
+ * ImplementationSupported(), or OOM occurs internally, an invalid Minisketch
240
+ * object will be constructed. Use operator bool() to check that this isn't the
241
+ * case before performing any other operations. */
224
242
Minisketch (uint32_t bits, uint32_t implementation, size_t capacity) noexcept
225
243
{
226
244
m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_create (bits, implementation, capacity));
227
245
}
228
246
229
- /* * Create a Minisketch object sufficiently large for the specified number of elements at given fpbits. */
247
+ /* * Create a Minisketch object sufficiently large for the specified number of elements at given fpbits.
248
+ * It may construct an invalid object, which you may need to check for. */
230
249
static Minisketch CreateFP (uint32_t bits, uint32_t implementation, size_t max_elements, uint32_t fpbits) noexcept
231
250
{
232
251
return Minisketch (bits, implementation, ComputeCapacity (bits, max_elements, fpbits));
233
252
}
234
253
235
- /* * See minisketch_get_bits() . */
254
+ /* * Return the field size for a (valid) Minisketch object . */
236
255
uint32_t GetBits () const noexcept { return minisketch_bits (m_minisketch.get ()); }
237
256
238
- /* * See minisketch_get_capacity() . */
257
+ /* * Return the capacity for a (valid) Minisketch object . */
239
258
size_t GetCapacity () const noexcept { return minisketch_capacity (m_minisketch.get ()); }
240
259
241
- /* * See minisketch_get_implementation() . */
260
+ /* * Return the implementation number for a (valid) Minisketch object . */
242
261
uint32_t GetImplementation () const noexcept { return minisketch_implementation (m_minisketch.get ()); }
243
262
244
- /* * See minisketch_set_seed(). */
263
+ /* * Set the seed for a (valid) Minisketch object. See minisketch_set_seed(). */
245
264
Minisketch& SetSeed (uint64_t seed) noexcept
246
265
{
247
266
minisketch_set_seed (m_minisketch.get (), seed);
248
267
return *this ;
249
268
}
250
269
251
- /* * See minisketch_add_uint64(). */
270
+ /* * Add (or remove, if already present) an element to a (valid) Minisketch object.
271
+ * See minisketch_add_uint64(). */
252
272
Minisketch& Add (uint64_t element) noexcept
253
273
{
254
274
minisketch_add_uint64 (m_minisketch.get (), element);
255
275
return *this ;
256
276
}
257
277
258
- /* * See minisketch_merge(). */
278
+ /* * Merge sketch into *this; both have to be valid Minisketch objects.
279
+ * See minisketch_merge for details. */
259
280
Minisketch& Merge (const Minisketch& sketch) noexcept
260
281
{
261
282
minisketch_merge (m_minisketch.get (), sketch.m_minisketch .get ());
262
283
return *this ;
263
284
}
264
285
265
- /* * Decode this sketch into the result vector, up to as many elements as the vector's size permits. */
286
+ /* * Decode this (valid) Minisketch object into the result vector, up to as many elements as the
287
+ * vector's size permits. */
266
288
bool Decode (std::vector<uint64_t >& result) const
267
289
{
268
290
ssize_t ret = minisketch_decode (m_minisketch.get (), result.size (), result.data ());
@@ -271,18 +293,19 @@ class Minisketch
271
293
return true ;
272
294
}
273
295
274
- /* * See minisketch_serialized_size() . */
296
+ /* * Get the serialized size in bytes for this (valid) Minisketch object. . */
275
297
size_t GetSerializedSize () const noexcept { return minisketch_serialized_size (m_minisketch.get ()); }
276
298
277
- /* * Serialize the sketch as a byte vector. */
299
+ /* * Serialize this (valid) Minisketch object as a byte vector. */
278
300
std::vector<unsigned char > Serialize () const
279
301
{
280
302
std::vector<unsigned char > result (GetSerializedSize ());
281
303
minisketch_serialize (m_minisketch.get (), result.data ());
282
304
return result;
283
305
}
284
306
285
- /* * Deserialize into this sketch from an object containing its bytes (which has data() and size() members). */
307
+ /* * Deserialize into this (valid) Minisketch from an object containing its bytes (which has data()
308
+ * and size() members). */
286
309
template <typename T>
287
310
Minisketch& Deserialize (
288
311
const T& obj,
@@ -298,7 +321,7 @@ class Minisketch
298
321
}
299
322
300
323
#if __cplusplus >= 201703L
301
- /* * C++17 only: decode up to a specified number of elements into an optional vector. */
324
+ /* * C++17 only: like Decode(), but up to a specified number of elements into an optional vector. */
302
325
std::optional<std::vector<uint64_t >> Decode (size_t max_elements) const
303
326
{
304
327
std::vector<uint64_t > result (max_elements);
0 commit comments