Skip to content

Commit 20a8529

Browse files
committed
Merge #39: Improve C++ header documentation
db0cade Make Minisketch copying work for invalid objects (Pieter Wuille) 2baf23f Improve C++ header documentation (Pieter Wuille) Pull request description: ACKs for top commit: gmaxwell: utACK db0cade Tree-SHA512: 6c1a8d6628c8b2c610e34dfa516299294cf44da0fb62905ccf0395c35eef9206fe6392d37334d450f3bd09b61f33195099ef19fc6a315c29b54828cb9ef1f75b
2 parents ba7905f + db0cade commit 20a8529

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

include/minisketch.h

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,83 +186,109 @@ 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. */
205212
Minisketch(const Minisketch& sketch) noexcept
206213
{
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+
}
208217
}
209218

210219
/** Make this Minisketch a clone of the specified one. */
211220
Minisketch& operator=(const Minisketch& sketch) noexcept
212221
{
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+
}
214225
return *this;
215226
}
216227

228+
/** Check whether this Minisketch object is valid. */
217229
explicit operator bool() const noexcept { return bool{m_minisketch}; }
218230

231+
/** Construct an (invalid) Minisketch object. */
219232
Minisketch() noexcept = default;
233+
234+
/** Move constructor. */
220235
Minisketch(Minisketch&&) noexcept = default;
236+
237+
/** Move assignment. */
221238
Minisketch& operator=(Minisketch&&) noexcept = default;
222239

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. */
224246
Minisketch(uint32_t bits, uint32_t implementation, size_t capacity) noexcept
225247
{
226248
m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_create(bits, implementation, capacity));
227249
}
228250

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. */
230253
static Minisketch CreateFP(uint32_t bits, uint32_t implementation, size_t max_elements, uint32_t fpbits) noexcept
231254
{
232255
return Minisketch(bits, implementation, ComputeCapacity(bits, max_elements, fpbits));
233256
}
234257

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

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

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

244-
/** See minisketch_set_seed(). */
267+
/** Set the seed for a (valid) Minisketch object. See minisketch_set_seed(). */
245268
Minisketch& SetSeed(uint64_t seed) noexcept
246269
{
247270
minisketch_set_seed(m_minisketch.get(), seed);
248271
return *this;
249272
}
250273

251-
/** See minisketch_add_uint64(). */
274+
/** Add (or remove, if already present) an element to a (valid) Minisketch object.
275+
* See minisketch_add_uint64(). */
252276
Minisketch& Add(uint64_t element) noexcept
253277
{
254278
minisketch_add_uint64(m_minisketch.get(), element);
255279
return *this;
256280
}
257281

258-
/** See minisketch_merge(). */
282+
/** Merge sketch into *this; both have to be valid Minisketch objects.
283+
* See minisketch_merge for details. */
259284
Minisketch& Merge(const Minisketch& sketch) noexcept
260285
{
261286
minisketch_merge(m_minisketch.get(), sketch.m_minisketch.get());
262287
return *this;
263288
}
264289

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. */
266292
bool Decode(std::vector<uint64_t>& result) const
267293
{
268294
ssize_t ret = minisketch_decode(m_minisketch.get(), result.size(), result.data());
@@ -271,18 +297,19 @@ class Minisketch
271297
return true;
272298
}
273299

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

277-
/** Serialize the sketch as a byte vector. */
303+
/** Serialize this (valid) Minisketch object as a byte vector. */
278304
std::vector<unsigned char> Serialize() const
279305
{
280306
std::vector<unsigned char> result(GetSerializedSize());
281307
minisketch_serialize(m_minisketch.get(), result.data());
282308
return result;
283309
}
284310

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). */
286313
template<typename T>
287314
Minisketch& Deserialize(
288315
const T& obj,
@@ -298,7 +325,7 @@ class Minisketch
298325
}
299326

300327
#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. */
302329
std::optional<std::vector<uint64_t>> Decode(size_t max_elements) const
303330
{
304331
std::vector<uint64_t> result(max_elements);

0 commit comments

Comments
 (0)