Skip to content

Commit 5d01bbd

Browse files
simplified type converting constructor
1 parent 5f6eb9b commit 5d01bbd

2 files changed

Lines changed: 14 additions & 54 deletions

File tree

kll/include/kll_sketch.hpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,6 @@ class kll_sketch {
216216
*/
217217
uint16_t get_k() const;
218218

219-
/**
220-
* Returns min_k, which is an internal parameter for error estimation
221-
* after merging sketches with different k.
222-
* @return parameter min_k
223-
*/
224-
uint16_t get_min_k() const;
225-
226219
/**
227220
* Returns the length of the input stream.
228221
* @return stream length
@@ -235,13 +228,6 @@ class kll_sketch {
235228
*/
236229
uint32_t get_num_retained() const;
237230

238-
/**
239-
* Returns the current capacity in items allocated by the sketch.
240-
* For internal use.
241-
* @return the capacity
242-
*/
243-
uint32_t get_capacity() const;
244-
245231
/**
246232
* Returns true if this sketch is in estimation mode.
247233
* @return estimation mode flag
@@ -653,6 +639,9 @@ class kll_sketch {
653639
return true;
654640
}
655641

642+
// for type converting constructor
643+
template<typename TT, typename CC, typename SS, typename AA>
644+
friend class kll_sketch;
656645
};
657646

658647
template<typename T, typename C, typename S, typename A>

kll/include/kll_sketch_impl.hpp

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -152,46 +152,27 @@ template<typename T, typename C, typename S, typename A>
152152
template<typename TT, typename CC, typename SS, typename AA>
153153
kll_sketch<T, C, S, A>::kll_sketch(const kll_sketch<TT, CC, SS, AA>& other, const A& allocator):
154154
allocator_(allocator),
155-
k_(other.get_k()),
156-
m_(DEFAULT_M),
157-
min_k_(other.get_min_k()),
158-
n_(other.get_n()),
159-
num_levels_(1),
160-
levels_(2, 0, allocator),
155+
k_(other.k_),
156+
m_(other.m_),
157+
min_k_(other.min_k_),
158+
n_(other.n_),
159+
num_levels_(other.num_levels_),
160+
levels_(other.levels_, allocator_),
161161
items_(nullptr),
162-
items_size_(other.get_capacity()),
162+
items_size_(other.items_size_),
163163
min_value_(nullptr),
164164
max_value_(nullptr),
165-
is_level_zero_sorted_(false)
165+
is_level_zero_sorted_(other.is_level_zero_sorted_)
166166
{
167167
static_assert(
168168
std::is_constructible<T, TT>::value,
169169
"Type converting constructor requires new type to be constructible from existing type"
170170
);
171171
items_ = allocator_.allocate(items_size_);
172-
levels_[0] = items_size_ - other.get_num_retained();
173-
levels_[1] = items_size_;
174-
175-
if (!other.is_empty()) {
176-
min_value_ = new (allocator_.allocate(1)) T(other.get_min_value());
177-
max_value_ = new (allocator_.allocate(1)) T(other.get_max_value());
178-
size_t index = levels_[0];
179-
for (auto pair: other) {
180-
new (&items_[index]) T(pair.first);
181-
const uint8_t level = count_trailing_zeros_in_u64(pair.second);
182-
if (level == num_levels_) {
183-
++num_levels_;
184-
levels_.resize(num_levels_ + 1);
185-
levels_[level] = index;
186-
levels_[num_levels_] = items_size_;
187-
} else if (level < num_levels_ - 1 || level > num_levels_) {
188-
throw std::invalid_argument("corrupt source sketch");
189-
}
190-
++index;
191-
}
192-
}
172+
for (auto i = levels_[0]; i < levels_[num_levels_]; ++i) new (&items_[i]) T(other.items_[i]);
173+
if (other.min_value_ != nullptr) min_value_ = new (allocator_.allocate(1)) T(*other.min_value_);
174+
if (other.max_value_ != nullptr) max_value_ = new (allocator_.allocate(1)) T(*other.max_value_);
193175
check_sorting();
194-
assert_correct_total_weight();
195176
}
196177

197178
template<typename T, typename C, typename S, typename A>
@@ -257,11 +238,6 @@ uint16_t kll_sketch<T, C, S, A>::get_k() const {
257238
return k_;
258239
}
259240

260-
template<typename T, typename C, typename S, typename A>
261-
uint16_t kll_sketch<T, C, S, A>::get_min_k() const {
262-
return min_k_;
263-
}
264-
265241
template<typename T, typename C, typename S, typename A>
266242
uint64_t kll_sketch<T, C, S, A>::get_n() const {
267243
return n_;
@@ -272,11 +248,6 @@ uint32_t kll_sketch<T, C, S, A>::get_num_retained() const {
272248
return levels_[num_levels_] - levels_[0];
273249
}
274250

275-
template<typename T, typename C, typename S, typename A>
276-
uint32_t kll_sketch<T, C, S, A>::get_capacity() const {
277-
return items_size_;
278-
}
279-
280251
template<typename T, typename C, typename S, typename A>
281252
bool kll_sketch<T, C, S, A>::is_estimation_mode() const {
282253
return num_levels_ > 1;

0 commit comments

Comments
 (0)