Skip to content
Draft
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
26 changes: 26 additions & 0 deletions fi/include/frequent_items_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ class frequent_items_sketch {
*/
explicit frequent_items_sketch(uint8_t lg_max_map_size, uint8_t lg_start_map_size = LG_MIN_MAP_SIZE,
const E& equal = E(), const A& allocator = A());
/**
* Copy constructor
* @param other sketch to be copied
*/
frequent_items_sketch(const frequent_items_sketch& other) = default;

/**
* Move constructor
* @param other sketch to be moved
*/
frequent_items_sketch(frequent_items_sketch&& other) = default;

/**
* Copy assignment
* @param other sketch to be copied
* @return reference to this sketch
*/
frequent_items_sketch& operator=(const frequent_items_sketch& other);

/**
* Move assignment
* @param other sketch to be moved
* @return reference to this sketch
*/
frequent_items_sketch& operator=(frequent_items_sketch&& other) = default;

/**
* Update this sketch with an item and a positive weight (frequency count).
Expand All @@ -91,6 +116,7 @@ class frequent_items_sketch {
* Update this sketch with an item and a positive weight (frequency count).
* If cross-language portability is required, callers should ensure that
* the input string uses a compatible encoding (valid UTF-8).
* The item may be moved from, even if the call throws.
* @param item for which the weight should be increased (rvalue)
* @param weight the amount by which the weight of the item should be increased
* A count of zero is a no-op, and a negative count will throw an exception.
Expand Down
13 changes: 11 additions & 2 deletions fi/include/frequent_items_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,29 @@ map(
if (lg_start_map_size > lg_max_map_size) { throw std::invalid_argument("starting size must not be greater than maximum size"); }
}

template<typename T, typename W, typename H, typename E, typename A>
frequent_items_sketch<T, W, H, E, A>& frequent_items_sketch<T, W, H, E, A>::operator=(const frequent_items_sketch& other) {
// map first: its copy can throw, and the weights must not change then
map = other.map;
total_weight = other.total_weight;
offset = other.offset;
return *this;
}

template<typename T, typename W, typename H, typename E, typename A>
void frequent_items_sketch<T, W, H, E, A>::update(const T& item, W weight) {
check_weight(weight);
if (weight == 0) { return; }
total_weight += weight;
offset += map.adjust_or_insert(item, weight);
total_weight += weight;
}

template<typename T, typename W, typename H, typename E, typename A>
void frequent_items_sketch<T, W, H, E, A>::update(T&& item, W weight) {
check_weight(weight);
if (weight == 0) { return; }
total_weight += weight;
offset += map.adjust_or_insert(std::move(item), weight);
total_weight += weight;
}

template<typename T, typename W, typename H, typename E, typename A>
Expand Down
2 changes: 1 addition & 1 deletion fi/include/reverse_purge_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class reverse_purge_hash_map {
void subtract_and_keep_positive_only(V amount);
void hash_delete(uint32_t probe);
uint32_t internal_adjust_or_insert(const K& key, V value);
V resize_or_purge_if_needed();
V resize_or_purge_if_needed(uint32_t new_index);
void resize(uint8_t lg_new_size);
V purge();
};
Expand Down
100 changes: 51 additions & 49 deletions fi/include/reverse_purge_hash_map_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,40 @@ allocator_(allocator),
lg_cur_size_(lg_cur_size),
lg_max_size_(lg_max_size),
num_active_(0),
keys_(allocator_.allocate(1ULL << lg_cur_size)),
keys_(nullptr),
values_(nullptr),
states_(nullptr)
{
const size_t size = 1ULL << lg_cur_size;
AllocV av(allocator_);
values_ = av.allocate(1ULL << lg_cur_size);
AllocU16 au16(allocator_);
states_ = au16.allocate(1ULL << lg_cur_size);
std::fill(states_, states_ + (1ULL << lg_cur_size), static_cast<uint16_t>(0));
// the destructor does not run if a constructor throws
try {
keys_ = allocator_.allocate(size);
values_ = av.allocate(size);
states_ = au16.allocate(size);
} catch (...) {
if (values_ != nullptr) { av.deallocate(values_, size); }
if (keys_ != nullptr) { allocator_.deallocate(keys_, size); }
throw;
}
std::fill(states_, states_ + size, static_cast<uint16_t>(0));
}

template<typename K, typename V, typename H, typename E, typename A>
reverse_purge_hash_map<K, V, H, E, A>::reverse_purge_hash_map(const reverse_purge_hash_map<K, V, H, E, A>& other):
equal_(other.equal_),
allocator_(other.allocator_),
lg_cur_size_(other.lg_cur_size_),
lg_max_size_(other.lg_max_size_),
num_active_(other.num_active_),
keys_(allocator_.allocate(1ULL << lg_cur_size_)),
values_(nullptr),
states_(nullptr)
reverse_purge_hash_map(other.lg_cur_size_, other.lg_max_size_, other.equal_, other.allocator_)
{
AllocV av(allocator_);
values_ = av.allocate(1ULL << lg_cur_size_);
AllocU16 au16(allocator_);
states_ = au16.allocate(1ULL << lg_cur_size_);
// the destructor stops after num_active_ keys, so count a key only once it is built
const uint32_t size = 1 << lg_cur_size_;
if (num_active_ > 0) {
auto num = num_active_;
for (uint32_t i = 0; i < size; i++) {
if (other.states_[i] > 0) {
new (&keys_[i]) K(other.keys_[i]);
values_[i] = other.values_[i];
if (--num == 0) { break; }
}
for (uint32_t i = 0; i < size && num_active_ < other.num_active_; i++) {
if (other.states_[i] > 0) {
new (&keys_[i]) K(other.keys_[i]);
values_[i] = other.values_[i];
states_[i] = other.states_[i];
num_active_++;
}
}
std::copy(other.states_, other.states_ + size, states_);
}

template<typename K, typename V, typename H, typename E, typename A>
Expand Down Expand Up @@ -155,8 +151,15 @@ V reverse_purge_hash_map<K, V, H, E, A>::adjust_or_insert(FwdK&& key, V value) {
const uint32_t num_active_before = num_active_;
const uint32_t index = internal_adjust_or_insert(key, value);
if (num_active_ > num_active_before) {
new (&keys_[index]) K(std::forward<FwdK>(key));
return resize_or_purge_if_needed();
try {
new (&keys_[index]) K(std::forward<FwdK>(key));
} catch (...) {
// the slot was empty, so emptying it again leaves the map unchanged
states_[index] = 0;
num_active_--;
throw;
}
return resize_or_purge_if_needed(index);
}
return 0;
}
Expand Down Expand Up @@ -307,10 +310,18 @@ uint32_t reverse_purge_hash_map<K, V, H, E, A>::internal_adjust_or_insert(const
}

template<typename K, typename V, typename H, typename E, typename A>
V reverse_purge_hash_map<K, V, H, E, A>::resize_or_purge_if_needed() {
V reverse_purge_hash_map<K, V, H, E, A>::resize_or_purge_if_needed(uint32_t new_index) {
if (num_active_ > get_capacity()) {
if (lg_cur_size_ < lg_max_size_) { // can grow
resize(lg_cur_size_ + 1);
try {
resize(lg_cur_size_ + 1);
} catch (...) {
// resize() builds the larger map separately, so take the new key out of this one
keys_[new_index].~K();
states_[new_index] = 0;
num_active_--;
throw;
}
} else { // at target size, must purge
const V offset = purge();
if (num_active_ > get_capacity()) {
Expand All @@ -324,28 +335,19 @@ V reverse_purge_hash_map<K, V, H, E, A>::resize_or_purge_if_needed() {

template<typename K, typename V, typename H, typename E, typename A>
void reverse_purge_hash_map<K, V, H, E, A>::resize(uint8_t lg_new_size) {
const uint32_t old_size = 1 << lg_cur_size_;
K* old_keys = keys_;
V* old_values = values_;
uint16_t* old_states = states_;
const uint32_t new_size = 1 << lg_new_size;
keys_ = allocator_.allocate(new_size);
AllocV av(allocator_);
values_ = av.allocate(new_size);
AllocU16 au16(allocator_);
states_ = au16.allocate(new_size);
std::fill(states_, states_ + new_size, static_cast<uint16_t>(0));
num_active_ = 0;
lg_cur_size_ = lg_new_size;
for (uint32_t i = 0; i < old_size; i++) {
if (old_states[i] > 0) {
adjust_or_insert(std::move(old_keys[i]), old_values[i]);
old_keys[i].~K();
// an allocation failure leaves this map unchanged; key moves, hashes and comparisons must not throw
reverse_purge_hash_map resized(lg_new_size, lg_max_size_, equal_, allocator_);
const uint32_t size = 1 << lg_cur_size_;
for (uint32_t i = 0; i < size; i++) {
if (states_[i] > 0) {
resized.adjust_or_insert(std::move(keys_[i]), values_[i]);
}
}
allocator_.deallocate(old_keys, old_size);
av.deallocate(old_values, old_size);
au16.deallocate(old_states, old_size);
// equal_ and allocator_ may not be assignable, so swap only the buffers
std::swap(lg_cur_size_, resized.lg_cur_size_);
std::swap(keys_, resized.keys_);
std::swap(values_, resized.values_);
std::swap(states_, resized.states_);
}

template<typename K, typename V, typename H, typename E, typename A>
Expand Down
1 change: 1 addition & 0 deletions fi/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ target_sources(fi_test
reverse_purge_hash_map_test.cpp
frequent_items_sketch_test.cpp
frequent_items_sketch_custom_type_test.cpp
frequent_items_sketch_allocation_failure_test.cpp
)

if (SERDE_COMPAT)
Expand Down
Loading