Skip to content

Commit

Permalink
values only in buffer, optional serialization with buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSaydakov committed Mar 15, 2024
1 parent 4e2b8b6 commit c785fc6
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 98 deletions.
19 changes: 11 additions & 8 deletions tdigest/include/tdigest.hpp
Expand Up @@ -23,6 +23,7 @@
#include <cstddef>
#include <limits>
#include <type_traits>
#include <vector>

#include "common_defs.hpp"

Expand Down Expand Up @@ -85,6 +86,7 @@ class tdigest {
T mean_;
W weight_;
};
using vector_t = std::vector<T, Allocator>;
using vector_centroid = std::vector<centroid, typename std::allocator_traits<Allocator>::template rebind_alloc<centroid>>;
using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;

Expand Down Expand Up @@ -168,24 +170,27 @@ class tdigest {

/**
* Computes size needed to serialize the current state.
* @param with_buffer optionally serialize buffered values avoiding compression
* @return size in bytes needed to serialize this tdigest
*/
size_t get_serialized_size_bytes() const;
size_t get_serialized_size_bytes(bool with_buffer = false) const;

/**
* This method serializes t-Digest into a given stream in a binary form
* @param os output stream
* @param with_buffer optionally serialize buffered values avoiding compression
*/
void serialize(std::ostream& os) const;
void serialize(std::ostream& os, bool with_buffer = false) const;

/**
* This method serializes t-Digest as a vector of bytes.
* An optional header can be reserved in front of the sketch.
* It is an uninitialized space of a given size.
* @param header_size_bytes space to reserve in front of the sketch
* @param with_buffer optionally serialize buffered values avoiding compression
* @return serialized sketch as a vector of bytes
*/
vector_bytes serialize(unsigned header_size_bytes = 0) const;
vector_bytes serialize(unsigned header_size_bytes = 0, bool with_buffer = false) const;

/**
* This method deserializes t-Digest from a given stream.
Expand All @@ -205,7 +210,6 @@ class tdigest {
static tdigest deserialize(const void* bytes, size_t size, const Allocator& allocator = Allocator());

private:
Allocator allocator_;
bool reverse_merge_;
uint16_t k_;
uint16_t internal_k_;
Expand All @@ -215,8 +219,7 @@ class tdigest {
vector_centroid centroids_;
uint64_t centroids_weight_;
size_t buffer_capacity_;
vector_centroid buffer_;
uint64_t buffered_weight_;
vector_t buffer_;

static const uint8_t PREAMBLE_LONGS_EMPTY_OR_SINGLE = 1;
static const uint8_t PREAMBLE_LONGS_MULTIPLE = 2;
Expand All @@ -230,10 +233,10 @@ class tdigest {

bool is_single_value() const;
uint8_t get_preamble_longs() const;
void merge_buffered();
void merge(vector_centroid& buffer, W weight);

// for deserialize
tdigest(bool reverse_merge, uint16_t k, T min, T max, vector_centroid&& centroids, uint64_t total_weight_, const Allocator& allocator);
tdigest(bool reverse_merge, uint16_t k, T min, T max, vector_centroid&& centroids, uint64_t total_weight_, vector_t&& buffer);

static double weighted_average(double x1, double w1, double x2, double w2);

Expand Down

0 comments on commit c785fc6

Please sign in to comment.