Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of key bounds in FlatMap #27

Closed
wants to merge 1 commit into from
Closed
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
73 changes: 62 additions & 11 deletions src/flat_map/include/sourcemeta/noa/flat_map.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef SOURCEMETA_NOA_FLAT_MAP_H_
#define SOURCEMETA_NOA_FLAT_MAP_H_

#include <algorithm> // std::swap
#include <cassert> // assert
#include <algorithm> // std::swap
#include <algorithm> // std::min, std::max, std::min_element, std::max_element
#include <cassert> // assert
#include <initializer_list> // std::initializer_list
#include <iterator> // std::advance
#include <limits> // std::numeric_limits
#include <utility> // std::pair, std::move
#include <vector> // std::vector

Expand Down Expand Up @@ -56,6 +58,9 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
}

auto assign(key_type &&key, mapped_type &&value) -> hash_type {
const auto key_size{key.size()};
this->key_lower_size = std::min(this->key_lower_size, key_size);
this->key_upper_size = std::max(this->key_upper_size, key_size);
const auto key_hash{this->hash(key)};

if (this->hasher.is_perfect(key_hash)) {
Expand All @@ -79,6 +84,9 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
}

auto assign(const key_type &key, const mapped_type &value) -> hash_type {
const auto key_size{key.size()};
this->key_lower_size = std::min(this->key_lower_size, key_size);
this->key_upper_size = std::max(this->key_upper_size, key_size);
const auto key_hash{this->hash(key)};

if (this->hasher.is_perfect(key_hash)) {
Expand All @@ -105,6 +113,10 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
inline auto find(const key_type &key, const hash_type key_hash) const
-> const_iterator {
assert(this->hash(key) == key_hash);
const auto key_size{key.size()};
if (key_size < this->key_lower_size || key_size > this->key_upper_size) {
return this->cend();
}

// Move the perfect hash condition out of the loop for extra performance
if (this->hasher.is_perfect(key_hash)) {
Expand All @@ -129,9 +141,14 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
return this->cend();
}

inline auto try_at(const key_type &key, const hash_type key_hash) const
inline auto try_at(const key_type &key,
const hash_type key_hash) const noexcept
-> const mapped_type * {
assert(this->hash(key) == key_hash);
const auto key_size{key.size()};
if (key_size < this->key_lower_size || key_size > this->key_upper_size) {
return nullptr;
}

// Move the perfect hash condition out of the loop for extra performance
if (this->hasher.is_perfect(key_hash)) {
Expand All @@ -155,6 +172,10 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
// As a performance optimisation if the hash is known
auto contains(const key_type &key, const hash_type key_hash) const -> bool {
assert(this->hash(key) == key_hash);
const auto key_size{key.size()};
if (key_size < this->key_lower_size || key_size > this->key_upper_size) {
return false;
}

// Move the perfect hash condition out of the loop for extra performance
if (this->hasher.is_perfect(key_hash)) {
Expand All @@ -176,7 +197,7 @@ template <typename Key, typename Value, typename Hash> class FlatMap {

// As a performance optimisation if the hash is known

inline auto at(const key_type &key, const hash_type key_hash) const
inline auto at(const key_type &key, const hash_type key_hash) const noexcept
-> const mapped_type & {
assert(this->hash(key) == key_hash);

Expand All @@ -203,7 +224,7 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
#endif
}

inline auto at(const key_type &key, const hash_type key_hash)
inline auto at(const key_type &key, const hash_type key_hash) noexcept
-> mapped_type & {
assert(this->hash(key) == key_hash);

Expand Down Expand Up @@ -235,26 +256,43 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
}

auto erase(const key_type &key, const hash_type key_hash) -> size_type {
const auto current_size{this->size()};
auto current_size{this->size()};

if (this->hasher.is_perfect(key_hash)) {
for (auto &entry : this->data) {
if (entry.hash == key_hash) {
std::swap(entry, this->data.back());
this->data.pop_back();
return current_size - 1;
current_size -= 1;
break;
}
}
} else {
for (auto &entry : this->data) {
if (entry.hash == key_hash && entry.first == key) {
std::swap(entry, this->data.back());
this->data.pop_back();
return current_size - 1;
current_size -= 1;
break;
}
}
}

if (!this->data.empty()) {
this->key_lower_size =
std::min_element(this->data.cbegin(), this->data.cend(),
[](const auto &left, const auto &right) {
return left.first.size() < right.first.size();
})
->first.size();
this->key_upper_size =
std::max_element(this->data.cbegin(), this->data.cend(),
[](const auto &left, const auto &right) {
return left.first.size() < right.first.size();
})
->first.size();
}

return current_size;
}

Expand All @@ -264,14 +302,25 @@ template <typename Key, typename Value, typename Hash> class FlatMap {

inline auto size() const noexcept -> size_type { return this->data.size(); }

inline auto empty() const noexcept -> bool { return this->data.empty(); }
inline auto empty() const noexcept -> bool {
assert(!this->data.empty() ||
this->key_lower_size == std::numeric_limits<std::size_t>::max());
assert(!this->data.empty() || this->key_upper_size == 0);
return this->data.empty();
}

inline auto clear() noexcept -> void { this->data.clear(); }
inline auto clear() noexcept -> void {
this->key_lower_size = std::numeric_limits<std::size_t>::max();
this->key_upper_size = 0;
this->data.clear();
}

auto operator!=(const FlatMap &other) const -> bool = default;

auto operator==(const FlatMap &other) const -> bool {
if (this->size() != other.size()) {
if (this->size() != other.size() ||
this->key_lower_size != other.key_lower_size ||
this->key_upper_size != other.key_upper_size) {
return false;
}

Expand All @@ -290,6 +339,8 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
private:
underlying_type data;
Hash hasher;
std::size_t key_lower_size{std::numeric_limits<std::size_t>::max()};
std::size_t key_upper_size{0};
};

} // namespace sourcemeta::noa
Expand Down
Loading