Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions kll/include/kll_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ class kll_helper {
// assumes that destination has initialized objects
// does not destroy the originals after the move
template <typename T, typename C>
static void merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c);
static void merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c,
const C& comparator);

// this version is to merge from two different buffers into a third buffer
// initializes objects is the destination buffer
// moves objects from buf_a and destroys the originals
// copies objects from buf_b
// Merges the sorted items buf_a[start_a, start_a + len_a) and the sorted items
// buf_c[start_c + len_a, start_c + len_a + len_b) into buf_c[start_c, start_c + len_a + len_b).
// The first len_a slots of the destination must hold no objects.
// Moves the items and destroys the originals; copies nothing.
template <typename T, typename C>
static void merge_sorted_arrays(const T* buf_a, uint32_t start_a, uint32_t len_a, const T* buf_b, uint32_t start_b, uint32_t len_b, T* buf_c, uint32_t start_c);
static void merge_sorted_arrays_into_gap(T* buf_a, uint32_t start_a, uint32_t len_a, T* buf_c, uint32_t start_c, uint32_t len_b,
const C& comparator);

struct compress_result {
uint8_t final_num_levels;
Expand Down Expand Up @@ -101,7 +103,7 @@ class kll_helper {

template <typename T, typename C>
static compress_result general_compress(uint16_t k, uint8_t m, uint8_t num_levels_in, T* items,
uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted);
uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted, const C& comparator);

template<typename T>
static void copy_construct(const T* src, size_t src_first, size_t src_last, T* dst, size_t dst_first);
Expand Down
45 changes: 19 additions & 26 deletions kll/include/kll_helper_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ void kll_helper::randomly_halve_up(T* buf, uint32_t start, uint32_t length) {
// assumes that destination has initialized objects
// does not destroy the originals after the move
template <typename T, typename C>
void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c) {
void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c,
const C& comparator) {
const uint32_t len_c = len_a + len_b;
const uint32_t lim_a = start_a + len_a;
const uint32_t lim_b = start_b + len_b;
Expand All @@ -145,7 +146,7 @@ void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, u
} else if (b == lim_b) {
if (a != c) buf[c] = std::move(buf[a]);
a++;
} else if (C()(buf[a], buf[b])) {
} else if (comparator(buf[a], buf[b])) {
if (a != c) buf[c] = std::move(buf[a]);
a++;
} else {
Expand All @@ -156,34 +157,26 @@ void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, u
if (a != lim_a || b != lim_b) throw std::logic_error("inconsistent state");
}

// this version is to merge from two different buffers into a third buffer
// initializes objects is the destination buffer
// moves objects from buf_a and destroys the originals
// copies objects from buf_b
// Merges into the gap in front of the items of b, which are already in the destination.
// Every destination slot before the next item of b holds no object: it was either empty,
// or its item was moved out and destroyed. Once a runs out, the rest of b is in place.
template <typename T, typename C>
void kll_helper::merge_sorted_arrays(const T* buf_a, uint32_t start_a, uint32_t len_a, const T* buf_b, uint32_t start_b, uint32_t len_b, T* buf_c, uint32_t start_c) {
const uint32_t len_c = len_a + len_b;
void kll_helper::merge_sorted_arrays_into_gap(T* buf_a, uint32_t start_a, uint32_t len_a, T* buf_c, uint32_t start_c, uint32_t len_b,
const C& comparator) {
const uint32_t lim_a = start_a + len_a;
const uint32_t lim_b = start_b + len_b;
const uint32_t lim_c = start_c + len_c;

const uint32_t lim_b = start_c + len_a + len_b;
uint32_t a = start_a;
uint32_t b = start_b;

for (uint32_t c = start_c; c < lim_c; c++) {
if (a == lim_a) {
new (&buf_c[c]) T(buf_b[b++]);
} else if (b == lim_b) {
new (&buf_c[c]) T(std::move(buf_a[a]));
buf_a[a++].~T();
} else if (C()(buf_a[a], buf_b[b])) {
new (&buf_c[c]) T(std::move(buf_a[a]));
uint32_t b = start_c + len_a;
uint32_t c = start_c;
while (a < lim_a) {
if (b == lim_b || comparator(buf_a[a], buf_c[b])) {
new (&buf_c[c++]) T(std::move(buf_a[a]));
buf_a[a++].~T();
} else {
new (&buf_c[c]) T(buf_b[b++]);
new (&buf_c[c++]) T(std::move(buf_c[b]));
buf_c[b++].~T();
}
}
if (a != lim_a || b != lim_b) throw std::logic_error("inconsistent state");
}

// Mirrors the level loop of general_compress() on the level sizes only.
Expand Down Expand Up @@ -240,7 +233,7 @@ kll_helper::compress_result kll_helper::predict_compress(uint16_t k, uint8_t m,
*/
template <typename T, typename C>
kll_helper::compress_result kll_helper::general_compress(uint16_t k, uint8_t m, uint8_t num_levels_in, T* items,
uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted)
uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted, const C& comparator)
{
if (num_levels_in == 0) { throw std::invalid_argument("num_levels_in == 0"); } // things are too weird if zero levels are allowed
const uint32_t starting_item_count = in_levels[num_levels_in] - in_levels[0];
Expand Down Expand Up @@ -289,14 +282,14 @@ kll_helper::compress_result kll_helper::general_compress(uint16_t k, uint8_t m,

// level zero might not be sorted, so we must sort it if we wish to compact it
if ((current_level == 0) && !is_level_zero_sorted) {
std::sort(items + adj_beg, items + adj_beg + adj_pop, C());
std::sort(items + adj_beg, items + adj_beg + adj_pop, comparator);
}

if (pop_above == 0) { // Level above is empty, so halve up
randomly_halve_up(items, adj_beg, adj_pop);
} else { // Level above is nonempty, so halve down, then merge up
randomly_halve_down(items, adj_beg, adj_pop);
merge_sorted_arrays<T, C>(items, adj_beg, half_adj_pop, raw_lim, pop_above, adj_beg + half_adj_pop);
merge_sorted_arrays<T, C>(items, adj_beg, half_adj_pop, raw_lim, pop_above, adj_beg + half_adj_pop, comparator);
}

// track the fact that we just eliminated some data
Expand Down
19 changes: 17 additions & 2 deletions kll/include/kll_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace kll_constants {
const uint16_t MAX_K = (1 << 16) - 1;
/// maximum weight of a weighted update, the same as in datasketches-java
const uint64_t MAX_WEIGHT = (1ULL << 61) - 1;
/// maximum stream length n, the largest signed 64-bit integer, as in datasketches-java
const uint64_t MAX_N = (1ULL << 63) - 1;
}

/**
Expand Down Expand Up @@ -246,20 +248,24 @@ class kll_sketch {
* the input string uses a compatible encoding (valid UTF-8).
* The first update allocates storage for items, unless reserve() or a merge already did.
* @param item from a stream of items
* @throw std::overflow_error if n would exceed kll_constants::MAX_N, in which case the sketch is unchanged
*/
template<typename FwdT>
void update(FwdT&& item);

/**
* Updates this sketch with the given data item repeated the given number of times.
* The result is equivalent to calling update(item) weight times, at a cost that grows
* The result is equivalent to calling update(item) weight times.
* If the weight is less than the free space in level zero, the item is copied into level zero
* weight times. Otherwise a sketch of the weighted item is merged in, at a cost that grows
* with the logarithm of the weight rather than with the weight itself.
* If cross-language portability is required, callers should ensure that
* the input string uses a compatible encoding (valid UTF-8).
* The first update allocates storage for items, unless reserve() or a merge already did.
* @param item from a stream of items
* @param weight number of times the item is repeated, from 1 to kll_constants::MAX_WEIGHT (2^61 - 1)
* @throw std::invalid_argument if the weight is 0 or greater than kll_constants::MAX_WEIGHT
* @throw std::overflow_error if n would exceed kll_constants::MAX_N, in which case the sketch is unchanged
*/
template<typename FwdT>
void update(FwdT&& item, uint64_t weight);
Expand All @@ -271,7 +277,10 @@ class kll_sketch {
* If other is this sketch, this throws std::invalid_argument.
* The first merge of a non-empty sketch allocates storage for items,
* unless reserve() or an update already did.
* If copying an item throws, this sketch stays valid and usable, but may hold part of other.
* The comparator must not throw: if it does, the state of this sketch is undefined.
* @param other sketch to merge into this one
* @throw std::overflow_error if n would exceed kll_constants::MAX_N, in which case the sketch is unchanged
*/
template<typename FwdSk>
void merge(FwdSk&& other);
Expand Down Expand Up @@ -633,6 +642,12 @@ class kll_sketch {
// &items_[index], or nullptr without item storage, which makes every item range empty
inline T* item_ptr(uint32_t index) const;
inline uint32_t internal_update();
// inserts an item into level zero; if constructing it throws, the sketch is unchanged
template<typename FwdT> void insert_level_zero(FwdT&& item);
// throws std::overflow_error if n_ + increase would exceed kll_constants::MAX_N
inline void check_n_increase(uint64_t increase) const;
// destroys the items and frees items_, for a constructor that throws
void destroy_items();

// The following code is only valid in the special case of exactly reaching capacity while updating.
// It cannot be used while merging, while reducing k, or anything else.
Expand All @@ -646,7 +661,7 @@ class kll_sketch {
template<typename O> void merge_higher_levels(O&& other, uint64_t final_n);

template<typename FwdSk>
void populate_work_arrays(FwdSk&& other, T* workbuf, uint32_t* worklevels, uint8_t provisional_num_levels);
void populate_work_arrays(FwdSk&& other, T* workbuf, const uint32_t* worklevels, uint8_t provisional_num_levels);

void assert_correct_total_weight() const;
uint32_t safe_level_size(uint8_t level) const;
Expand Down
Loading
Loading