Skip to content

Commit 26bf012

Browse files
authored
Merge pull request #282 from apache/quantiles_patch
fixes for type converting constructor, align documentation with kll
2 parents 6b2e542 + 65a3002 commit 26bf012

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

quantiles/include/quantiles_sketch.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ class quantiles_sketch {
162162
quantiles_sketch& operator=(const quantiles_sketch& other);
163163
quantiles_sketch& operator=(quantiles_sketch&& other) noexcept;
164164

165+
/**
166+
* @brief Type converting constructor
167+
* @param other quantiles sketch of a different type
168+
* @param allocator instance of an Allocator
169+
*/
165170
template<typename From, typename FC, typename FA>
166171
explicit quantiles_sketch(const quantiles_sketch<From, FC, FA>& other, const Allocator& allocator = Allocator());
167172

quantiles/include/quantiles_sketch_impl.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,8 @@ min_value_(nullptr),
151151
max_value_(nullptr),
152152
is_sorted_(false)
153153
{
154-
static_assert(std::is_convertible<From, T>::value
155-
|| std::is_constructible<From, T>::value,
156-
"Copy constructor across types requires std::is_convertible or std::is_constructible");
154+
static_assert(std::is_constructible<T, From>::value,
155+
"Type converting constructor requires new type to be constructible from existing type");
157156

158157
base_buffer_.reserve(2 * std::min(quantiles_constants::MIN_K, k_));
159158

@@ -174,7 +173,7 @@ is_sorted_(false)
174173
for (auto pair : other) {
175174
const uint64_t wt = pair.second;
176175
if (wt == 1) {
177-
base_buffer_.push_back(pair.first);
176+
base_buffer_.push_back(T(pair.first));
178177
// resize where needed as if adding points via update()
179178
if (base_buffer_.size() + 1 > base_buffer_.capacity()) {
180179
const size_t new_size = std::max(std::min(static_cast<size_t>(2 * k_), 2 * base_buffer_.size()), static_cast<size_t>(1));
@@ -183,7 +182,7 @@ is_sorted_(false)
183182
}
184183
else {
185184
const uint8_t idx = count_trailing_zeros_in_u64(pair.second) - 1;
186-
levels_[idx].push_back(pair.first);
185+
levels_[idx].push_back(T(pair.first));
187186
}
188187
}
189188

quantiles/test/quantiles_sketch_test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,38 @@ TEST_CASE("quantiles sketch", "[quantiles_sketch]") {
934934
}
935935
}
936936

937+
class A {
938+
int val;
939+
public:
940+
A(int val): val(val) {}
941+
int get_val() const { return val; }
942+
};
943+
944+
struct less_A {
945+
bool operator()(const A& a1, const A& a2) const { return a1.get_val() < a2.get_val(); }
946+
};
947+
948+
class B {
949+
int val;
950+
public:
951+
explicit B(const A& a): val(a.get_val()) {}
952+
int get_val() const { return val; }
953+
};
954+
955+
struct less_B {
956+
bool operator()(const B& b1, const B& b2) const { return b1.get_val() < b2.get_val(); }
957+
};
958+
959+
SECTION("type conversion: custom types") {
960+
quantiles_sketch<A, less_A> sa;
961+
sa.update(1);
962+
sa.update(2);
963+
sa.update(3);
964+
965+
quantiles_sketch<B, less_B> sb(sa);
966+
REQUIRE(sb.get_n() == 3);
967+
}
968+
937969
// cleanup
938970
if (test_allocator_total_bytes != 0) {
939971
REQUIRE(test_allocator_total_bytes == 0);

0 commit comments

Comments
 (0)