Simple RAII C++11 wrapper around the minisketch API. */
| 194 | #if __cplusplus >= 201103L |
| 195 | /** Simple RAII C++11 wrapper around the minisketch API. */ |
| 196 | class Minisketch |
| 197 | { |
| 198 | struct Deleter |
| 199 | { |
| 200 | void operator()(minisketch* ptr) const |
| 201 | { |
| 202 | minisketch_destroy(ptr); |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | std::unique_ptr<minisketch, Deleter> m_minisketch; |
| 207 | |
| 208 | public: |
| 209 | /** Check whether the library supports fields of the given size. */ |
| 210 | static bool BitsSupported(uint32_t bits) noexcept { return minisketch_bits_supported(bits); } |
| 211 | |
| 212 | /** Get the highest supported implementation number. */ |
| 213 | static uint32_t MaxImplementation() noexcept { return minisketch_implementation_max(); } |
| 214 | |
| 215 | /** Check whether the library supports fields with a given size and implementation number. |
| 216 | * If a particular field size `bits` is supported, implementation 0 is always supported for it. |
| 217 | * Higher implementation numbers may or may not be available as well, up to MaxImplementation(). |
| 218 | */ |
| 219 | static bool ImplementationSupported(uint32_t bits, uint32_t implementation) noexcept { return minisketch_implementation_supported(bits, implementation); } |
| 220 | |
| 221 | /** Given field size and a maximum number of decodable elements n, compute what capacity c to |
| 222 | * use so that sketches with more elements than n have a chance no higher than 2^-fpbits of |
| 223 | * being decoded incorrectly (and will instead fail when decoding for up to n elements). |
| 224 | * |
| 225 | * See minisketch_compute_capacity for more details. */ |
| 226 | static size_t ComputeCapacity(uint32_t bits, size_t max_elements, uint32_t fpbits) noexcept { return minisketch_compute_capacity(bits, max_elements, fpbits); } |
| 227 | |
| 228 | /** Reverse operation of ComputeCapacity. See minisketch_compute_max_elements. */ |
| 229 | static size_t ComputeMaxElements(uint32_t bits, size_t capacity, uint32_t fpbits) noexcept { return minisketch_compute_max_elements(bits, capacity, fpbits); } |
| 230 | |
| 231 | /** Construct a clone of the specified sketch. */ |
| 232 | Minisketch(const Minisketch& sketch) noexcept |
| 233 | { |
| 234 | if (sketch.m_minisketch) { |
| 235 | m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_clone(sketch.m_minisketch.get())); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** Make this Minisketch a clone of the specified one. */ |
| 240 | Minisketch& operator=(const Minisketch& sketch) noexcept |
| 241 | { |
| 242 | if (sketch.m_minisketch) { |
| 243 | m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_clone(sketch.m_minisketch.get())); |
| 244 | } |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | /** Check whether this Minisketch object is valid. */ |
| 249 | explicit operator bool() const noexcept { return bool{m_minisketch}; } |
| 250 | |
| 251 | /** Construct an (invalid) Minisketch object. */ |
| 252 | Minisketch() noexcept = default; |
| 253 |