Skip to content

Commit 315e50b

Browse files
MaheshGPaiMahesh Pai
authored andcommitted
Addressing review comments
1 parent 866f6d0 commit 315e50b

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

tdigest/include/tdigest.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ class tdigest {
106106
*/
107107
explicit tdigest(uint16_t k = DEFAULT_K, const Allocator& allocator = Allocator());
108108

109+
/**
110+
* Copy constructor
111+
* @param other sketch to be copied
112+
*/
113+
tdigest(const tdigest& other);
114+
109115
/**
110116
* Update this t-Digest with the given value
111117
* @param value to update the t-Digest with
@@ -275,13 +281,11 @@ class tdigest {
275281
private:
276282
bool reverse_merge_;
277283
uint16_t k_;
278-
uint16_t internal_k_;
279284
T min_;
280285
T max_;
281286
size_t centroids_capacity_;
282287
vector_centroid centroids_;
283288
uint64_t centroids_weight_;
284-
size_t buffer_capacity_;
285289
vector_t buffer_;
286290

287291
static const size_t BUFFER_MULTIPLIER = 4;

tdigest/include/tdigest_impl.hpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,24 @@ bool tdigest<T, A>::is_single_value() const {
597597
return get_total_weight() == 1;
598598
}
599599

600+
template<typename T, typename A>
601+
tdigest<T, A>::tdigest(const tdigest<T, A>& other):
602+
reverse_merge_(other.reverse_merge_),
603+
k_(other.k_),
604+
min_(other.min_),
605+
max_(other.max_),
606+
centroids_capacity_(other.centroids_capacity_),
607+
centroids_(other.centroids_, other.get_allocator()),
608+
centroids_weight_(other.centroids_weight_),
609+
buffer_(other.buffer_, other.get_allocator())
610+
{
611+
if (other.k_ < 10) throw std::invalid_argument("k must be at least 10");
612+
const size_t fudge = other.k_ < 30 ? 30 : 10;
613+
centroids_capacity_ = 2 * k_ + fudge;
614+
centroids_.reserve(centroids_capacity_);
615+
buffer_.reserve(centroids_capacity_ * BUFFER_MULTIPLIER);
616+
}
617+
600618
template<typename T, typename A>
601619
tdigest<T, A>::tdigest(bool reverse_merge, uint16_t k, T min, T max, vector_centroid&& centroids, uint64_t weight, vector_t&& buffer):
602620
reverse_merge_(reverse_merge),
@@ -638,11 +656,11 @@ template <typename T, typename A>
638656
}
639657

640658
template<typename T, typename A>
641-
tdigest<T, A>::const_iterator::const_iterator(const tdigest& tdigest_, const bool is_end):
642-
centroids_()
659+
tdigest<T, A>::const_iterator::const_iterator(const tdigest<T, A>& tdigest_, const bool is_end):
660+
centroids_(tdigest_.get_allocator())
643661
{
644662
// Create a copy of the tdigest to generate the centroids after processing the buffered values
645-
tdigest tmp(tdigest_);
663+
tdigest<T, A> tmp(tdigest_);
646664
tmp.compress();
647665
centroids_.insert(centroids_.end(), tmp.centroids_.begin(), tmp.centroids_.end());
648666

tdigest/test/tdigest_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,14 @@ TEST_CASE("iterate centroids", "[tdigest]") {
460460
}
461461

462462
auto centroid_count = 0;
463+
uint64_t total_weight = 0;
463464
for (const auto &centroid: td) {
464465
centroid_count++;
466+
total_weight += centroid.second;
465467
}
466468
// Ensure that centroids are retrieved for a case where there is buffered values
467469
REQUIRE(centroid_count == 10);
470+
REQUIRE(td.get_total_weight() == total_weight);
468471
}
469472

470473
} /* namespace datasketches */

0 commit comments

Comments
 (0)