@@ -186,83 +186,109 @@ 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. */
205
212
Minisketch (const Minisketch& sketch) noexcept
206
213
{
207
- m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_clone (sketch.m_minisketch .get ()));
214
+ if (sketch.m_minisketch ) {
215
+ m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_clone (sketch.m_minisketch .get ()));
216
+ }
208
217
}
209
218
210
219
/* * Make this Minisketch a clone of the specified one. */
211
220
Minisketch& operator =(const Minisketch& sketch) noexcept
212
221
{
213
- m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_clone (sketch.m_minisketch .get ()));
222
+ if (sketch.m_minisketch ) {
223
+ m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_clone (sketch.m_minisketch .get ()));
224
+ }
214
225
return *this ;
215
226
}
216
227
228
+ /* * Check whether this Minisketch object is valid. */
217
229
explicit operator bool () const noexcept { return bool {m_minisketch}; }
218
230
231
+ /* * Construct an (invalid) Minisketch object. */
219
232
Minisketch () noexcept = default ;
233
+
234
+ /* * Move constructor. */
220
235
Minisketch (Minisketch&&) noexcept = default ;
236
+
237
+ /* * Move assignment. */
221
238
Minisketch& operator =(Minisketch&&) noexcept = default ;
222
239
223
- /* * Construct a Minisketch object with the specified parameters. */
240
+ /* * Construct a Minisketch object with the specified parameters.
241
+ *
242
+ * If bits is not BitsSupported(), or the combination of bits and capacity is not
243
+ * ImplementationSupported(), or OOM occurs internally, an invalid Minisketch
244
+ * object will be constructed. Use operator bool() to check that this isn't the
245
+ * case before performing any other operations. */
224
246
Minisketch (uint32_t bits, uint32_t implementation, size_t capacity) noexcept
225
247
{
226
248
m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_create (bits, implementation, capacity));
227
249
}
228
250
229
- /* * Create a Minisketch object sufficiently large for the specified number of elements at given fpbits. */
251
+ /* * Create a Minisketch object sufficiently large for the specified number of elements at given fpbits.
252
+ * It may construct an invalid object, which you may need to check for. */
230
253
static Minisketch CreateFP (uint32_t bits, uint32_t implementation, size_t max_elements, uint32_t fpbits) noexcept
231
254
{
232
255
return Minisketch (bits, implementation, ComputeCapacity (bits, max_elements, fpbits));
233
256
}
234
257
235
- /* * See minisketch_get_bits() . */
258
+ /* * Return the field size for a (valid) Minisketch object . */
236
259
uint32_t GetBits () const noexcept { return minisketch_bits (m_minisketch.get ()); }
237
260
238
- /* * See minisketch_get_capacity() . */
261
+ /* * Return the capacity for a (valid) Minisketch object . */
239
262
size_t GetCapacity () const noexcept { return minisketch_capacity (m_minisketch.get ()); }
240
263
241
- /* * See minisketch_get_implementation() . */
264
+ /* * Return the implementation number for a (valid) Minisketch object . */
242
265
uint32_t GetImplementation () const noexcept { return minisketch_implementation (m_minisketch.get ()); }
243
266
244
- /* * See minisketch_set_seed(). */
267
+ /* * Set the seed for a (valid) Minisketch object. See minisketch_set_seed(). */
245
268
Minisketch& SetSeed (uint64_t seed) noexcept
246
269
{
247
270
minisketch_set_seed (m_minisketch.get (), seed);
248
271
return *this ;
249
272
}
250
273
251
- /* * See minisketch_add_uint64(). */
274
+ /* * Add (or remove, if already present) an element to a (valid) Minisketch object.
275
+ * See minisketch_add_uint64(). */
252
276
Minisketch& Add (uint64_t element) noexcept
253
277
{
254
278
minisketch_add_uint64 (m_minisketch.get (), element);
255
279
return *this ;
256
280
}
257
281
258
- /* * See minisketch_merge(). */
282
+ /* * Merge sketch into *this; both have to be valid Minisketch objects.
283
+ * See minisketch_merge for details. */
259
284
Minisketch& Merge (const Minisketch& sketch) noexcept
260
285
{
261
286
minisketch_merge (m_minisketch.get (), sketch.m_minisketch .get ());
262
287
return *this ;
263
288
}
264
289
265
- /* * Decode this sketch into the result vector, up to as many elements as the vector's size permits. */
290
+ /* * Decode this (valid) Minisketch object into the result vector, up to as many elements as the
291
+ * vector's size permits. */
266
292
bool Decode (std::vector<uint64_t >& result) const
267
293
{
268
294
ssize_t ret = minisketch_decode (m_minisketch.get (), result.size (), result.data ());
@@ -271,18 +297,19 @@ class Minisketch
271
297
return true ;
272
298
}
273
299
274
- /* * See minisketch_serialized_size() . */
300
+ /* * Get the serialized size in bytes for this (valid) Minisketch object. . */
275
301
size_t GetSerializedSize () const noexcept { return minisketch_serialized_size (m_minisketch.get ()); }
276
302
277
- /* * Serialize the sketch as a byte vector. */
303
+ /* * Serialize this (valid) Minisketch object as a byte vector. */
278
304
std::vector<unsigned char > Serialize () const
279
305
{
280
306
std::vector<unsigned char > result (GetSerializedSize ());
281
307
minisketch_serialize (m_minisketch.get (), result.data ());
282
308
return result;
283
309
}
284
310
285
- /* * Deserialize into this sketch from an object containing its bytes (which has data() and size() members). */
311
+ /* * Deserialize into this (valid) Minisketch from an object containing its bytes (which has data()
312
+ * and size() members). */
286
313
template <typename T>
287
314
Minisketch& Deserialize (
288
315
const T& obj,
@@ -298,7 +325,7 @@ class Minisketch
298
325
}
299
326
300
327
#if __cplusplus >= 201703L
301
- /* * C++17 only: decode up to a specified number of elements into an optional vector. */
328
+ /* * C++17 only: like Decode(), but up to a specified number of elements into an optional vector. */
302
329
std::optional<std::vector<uint64_t >> Decode (size_t max_elements) const
303
330
{
304
331
std::vector<uint64_t > result (max_elements);
0 commit comments