Skip to content

Commit 2baf23f

Browse files
committed
Improve C++ header documentation
1 parent ba7905f commit 2baf23f

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

include/minisketch.h

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,26 @@ class Minisketch
186186
std::unique_ptr<minisketch, Deleter> m_minisketch;
187187

188188
public:
189-
/** See minisketch_bits_supported(). */
189+
/** Check whether the library supports fields of the given size. */
190190
static bool BitsSupported(uint32_t bits) noexcept { return minisketch_bits_supported(bits); }
191191

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. */
196193
static uint32_t MaxImplementation() noexcept { return minisketch_implementation_max(); }
197194

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. */
199206
static size_t ComputeCapacity(uint32_t bits, size_t max_elements, uint32_t fpbits) noexcept { return minisketch_compute_capacity(bits, max_elements, fpbits); }
200207

201-
/** See minisketch_compute_max_elements(). */
208+
/** Reverse operation of ComputeCapacity. See minisketch_compute_max_elements. */
202209
static size_t ComputeMaxElements(uint32_t bits, size_t capacity, uint32_t fpbits) noexcept { return minisketch_compute_max_elements(bits, capacity, fpbits); }
203210

204211
/** Construct a clone of the specified sketch. */
@@ -214,55 +221,70 @@ class Minisketch
214221
return *this;
215222
}
216223

224+
/** Check whether this Minisketch object is valid. */
217225
explicit operator bool() const noexcept { return bool{m_minisketch}; }
218226

227+
/** Construct an (invalid) Minisketch object. */
219228
Minisketch() noexcept = default;
229+
230+
/** Move constructor. */
220231
Minisketch(Minisketch&&) noexcept = default;
232+
233+
/** Move assignment. */
221234
Minisketch& operator=(Minisketch&&) noexcept = default;
222235

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. */
224242
Minisketch(uint32_t bits, uint32_t implementation, size_t capacity) noexcept
225243
{
226244
m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_create(bits, implementation, capacity));
227245
}
228246

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. */
230249
static Minisketch CreateFP(uint32_t bits, uint32_t implementation, size_t max_elements, uint32_t fpbits) noexcept
231250
{
232251
return Minisketch(bits, implementation, ComputeCapacity(bits, max_elements, fpbits));
233252
}
234253

235-
/** See minisketch_get_bits(). */
254+
/** Return the field size for a (valid) Minisketch object. */
236255
uint32_t GetBits() const noexcept { return minisketch_bits(m_minisketch.get()); }
237256

238-
/** See minisketch_get_capacity(). */
257+
/** Return the capacity for a (valid) Minisketch object. */
239258
size_t GetCapacity() const noexcept { return minisketch_capacity(m_minisketch.get()); }
240259

241-
/** See minisketch_get_implementation(). */
260+
/** Return the implementation number for a (valid) Minisketch object. */
242261
uint32_t GetImplementation() const noexcept { return minisketch_implementation(m_minisketch.get()); }
243262

244-
/** See minisketch_set_seed(). */
263+
/** Set the seed for a (valid) Minisketch object. See minisketch_set_seed(). */
245264
Minisketch& SetSeed(uint64_t seed) noexcept
246265
{
247266
minisketch_set_seed(m_minisketch.get(), seed);
248267
return *this;
249268
}
250269

251-
/** See minisketch_add_uint64(). */
270+
/** Add (or remove, if already present) an element to a (valid) Minisketch object.
271+
* See minisketch_add_uint64(). */
252272
Minisketch& Add(uint64_t element) noexcept
253273
{
254274
minisketch_add_uint64(m_minisketch.get(), element);
255275
return *this;
256276
}
257277

258-
/** See minisketch_merge(). */
278+
/** Merge sketch into *this; both have to be valid Minisketch objects.
279+
* See minisketch_merge for details. */
259280
Minisketch& Merge(const Minisketch& sketch) noexcept
260281
{
261282
minisketch_merge(m_minisketch.get(), sketch.m_minisketch.get());
262283
return *this;
263284
}
264285

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. */
266288
bool Decode(std::vector<uint64_t>& result) const
267289
{
268290
ssize_t ret = minisketch_decode(m_minisketch.get(), result.size(), result.data());
@@ -271,18 +293,19 @@ class Minisketch
271293
return true;
272294
}
273295

274-
/** See minisketch_serialized_size(). */
296+
/** Get the serialized size in bytes for this (valid) Minisketch object.. */
275297
size_t GetSerializedSize() const noexcept { return minisketch_serialized_size(m_minisketch.get()); }
276298

277-
/** Serialize the sketch as a byte vector. */
299+
/** Serialize this (valid) Minisketch object as a byte vector. */
278300
std::vector<unsigned char> Serialize() const
279301
{
280302
std::vector<unsigned char> result(GetSerializedSize());
281303
minisketch_serialize(m_minisketch.get(), result.data());
282304
return result;
283305
}
284306

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). */
286309
template<typename T>
287310
Minisketch& Deserialize(
288311
const T& obj,
@@ -298,7 +321,7 @@ class Minisketch
298321
}
299322

300323
#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. */
302325
std::optional<std::vector<uint64_t>> Decode(size_t max_elements) const
303326
{
304327
std::vector<uint64_t> result(max_elements);

0 commit comments

Comments
 (0)