2#define _MINISKETCH_H_ 1
9 typedef SSIZE_T ssize_t;
16# ifdef MINISKETCH_BUILD
17# define MINISKETCH_API __declspec(dllexport)
19# define MINISKETCH_API
21# elif defined(__GNUC__) && (__GNUC__ >= 4) && defined(MINISKETCH_BUILD)
22# define MINISKETCH_API __attribute__ ((visibility ("default")))
24# define MINISKETCH_API
29# if __cplusplus >= 201103L
33# if __cplusplus >= 201703L
194#if __cplusplus >= 201103L
206 std::unique_ptr<minisketch, Deleter> m_minisketch;
226 static size_t ComputeCapacity(uint32_t bits,
size_t max_elements, uint32_t fpbits)
noexcept {
return minisketch_compute_capacity(bits, max_elements, fpbits); }
229 static size_t ComputeMaxElements(uint32_t bits,
size_t capacity, uint32_t fpbits)
noexcept {
return minisketch_compute_max_elements(bits, capacity, fpbits); }
232 Minisketch(
const Minisketch& sketch)
noexcept
234 if (sketch.m_minisketch) {
235 m_minisketch = std::unique_ptr<minisketch, Deleter>(
minisketch_clone(sketch.m_minisketch.get()));
240 Minisketch& operator=(
const Minisketch& sketch)
noexcept
242 if (
this != &sketch && sketch.m_minisketch) {
243 m_minisketch = std::unique_ptr<minisketch, Deleter>(
minisketch_clone(sketch.m_minisketch.get()));
249 explicit operator bool() const noexcept {
return bool{m_minisketch}; }
252 Minisketch() noexcept = default;
255 Minisketch(Minisketch&&) noexcept = default;
258 Minisketch& operator=(Minisketch&&) noexcept = default;
266 Minisketch(uint32_t bits, uint32_t implementation,
size_t capacity) noexcept
268 m_minisketch = std::unique_ptr<minisketch, Deleter>(
minisketch_create(bits, implementation, capacity));
273 static Minisketch CreateFP(uint32_t bits, uint32_t implementation,
size_t max_elements, uint32_t fpbits)
noexcept
275 return Minisketch(bits, implementation, ComputeCapacity(bits, max_elements, fpbits));
279 uint32_t GetBits() const noexcept {
return minisketch_bits(m_minisketch.get()); }
288 Minisketch& SetSeed(uint64_t seed)
noexcept
296 Minisketch& Add(uint64_t element)
noexcept
304 Minisketch& Merge(
const Minisketch& sketch)
noexcept
312 bool Decode(std::vector<uint64_t>& result)
const
315 if (
ret == -1)
return false;
324 std::vector<unsigned char>
Serialize()
const
326 std::vector<unsigned char> result(GetSerializedSize());
334 Minisketch& Deserialize(
336 typename std::enable_if<
337 std::is_convertible<
typename std::remove_pointer<
decltype(obj.data())>::type (*)[],
const unsigned char (*)[]>::value &&
338 std::is_convertible<
decltype(obj.size()), std::size_t>::value,
340 >::type =
nullptr) noexcept
342 assert(GetSerializedSize() == obj.size());
347#if __cplusplus >= 201703L
349 std::optional<std::vector<uint64_t>>
Decode(
size_t max_elements)
const
351 std::vector<uint64_t> result(max_elements);
353 if (
ret == -1)
return {};
359 std::optional<std::vector<uint64_t>> DecodeFP(uint32_t fpbits)
const
361 return Decode(ComputeMaxElements(GetBits(), GetCapacity(), fpbits));
MINISKETCH_API void minisketch_serialize(const minisketch *sketch, unsigned char *output)
Serialize a sketch to bytes.
MINISKETCH_API void minisketch_set_seed(minisketch *sketch, uint64_t seed)
Set the seed for randomizing algorithm choices to a fixed value.
MINISKETCH_API uint32_t minisketch_implementation(const minisketch *sketch)
Get the implementation of a sketch.
MINISKETCH_API size_t minisketch_capacity(const minisketch *sketch)
Get the capacity of a sketch.
MINISKETCH_API size_t minisketch_compute_capacity(uint32_t bits, size_t max_elements, uint32_t fpbits)
Compute the capacity needed to achieve a certain rate of false positives.
MINISKETCH_API ssize_t minisketch_decode(const minisketch *sketch, size_t max_elements, uint64_t *output)
Decode a sketch.
MINISKETCH_API int minisketch_bits_supported(uint32_t bits)
Determine whether support for elements of bits bits was compiled in.
MINISKETCH_API size_t minisketch_merge(minisketch *sketch, const minisketch *other_sketch)
Merge the elements of another sketch into this sketch.
MINISKETCH_API size_t minisketch_serialized_size(const minisketch *sketch)
Compute the size in bytes for serializing a given sketch.
MINISKETCH_API minisketch * minisketch_create(uint32_t bits, uint32_t implementation, size_t capacity)
Construct a sketch for a given element size, implementation and capacity.
MINISKETCH_API uint32_t minisketch_implementation_max(void)
Determine the maximum number of implementations available.
MINISKETCH_API void minisketch_add_uint64(minisketch *sketch, uint64_t element)
Add an element to a sketch.
MINISKETCH_API minisketch * minisketch_clone(const minisketch *sketch)
Clone a sketch.
MINISKETCH_API uint32_t minisketch_bits(const minisketch *sketch)
Get the element size of a sketch in bits.
MINISKETCH_API size_t minisketch_compute_max_elements(uint32_t bits, size_t capacity, uint32_t fpbits)
Compute what max_elements can be decoded for a certain rate of false positives.
MINISKETCH_API void minisketch_deserialize(minisketch *sketch, const unsigned char *input)
Deserialize a sketch from bytes.
MINISKETCH_API void minisketch_destroy(minisketch *sketch)
Destroy a sketch.
struct minisketch minisketch
Opaque type for decoded sketches.
MINISKETCH_API int minisketch_implementation_supported(uint32_t bits, uint32_t implementation)
Determine if the a combination of bits and implementation number is available.
DecodeResult Decode(const std::string &str, CharLimit limit)
Decode a Bech32 or Bech32m string.
void Serialize(Stream &, V)=delete