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

clean up #101

Merged
merged 2 commits into from
Dec 13, 2022
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed
- Rewrote relocate(). This should be much cleaner now and slightly faster.
[#98](https://github.com/tzaeschke/phtree-cpp/pull/98), [#99](https://github.com/tzaeschke/phtree-cpp/pull/99)

[#98](https://github.com/tzaeschke/phtree-cpp/pull/98),
[#99](https://github.com/tzaeschke/phtree-cpp/pull/99),
[#101](https://github.com/tzaeschke/phtree-cpp/pull/101)
- Cleaned up HandleCollision() and key comparison functions. [#97](https://github.com/tzaeschke/phtree-cpp/pull/97)
- Improved performance by eliminating memory indirection for DIM > 3.
This was enabled by referencing "Node" directly in "Entry" which was enabled by
Expand Down
2 changes: 1 addition & 1 deletion include/phtree/common/b_plus_tree_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class b_plus_tree_map {

template <typename... Args>
auto try_emplace(IterT iter, KeyT key, Args&&... args) {
return emplace_hint(iter, key, std::forward<Args>(args)...);
return emplace_hint(iter, key, std::forward<Args>(args)...).first;
}

void erase(KeyT key) {
Expand Down
8 changes: 4 additions & 4 deletions include/phtree/common/flat_array_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class flat_array_map {
}

template <typename... Args>
std::pair<map_pair*, bool> try_emplace_base(size_t index, Args&&... args) {
std::pair<map_pair*, bool> try_emplace(size_t index, Args&&... args) {
if (!occupied(index)) {
new (reinterpret_cast<void*>(&data_[index])) map_pair(
std::piecewise_construct,
Expand Down Expand Up @@ -268,18 +268,18 @@ class array_map {

template <typename... Args>
auto emplace(Args&&... args) {
return data_->try_emplace_base(std::forward<Args>(args)...);
return data_->try_emplace(std::forward<Args>(args)...);
}

template <typename... Args>
auto try_emplace(size_t index, Args&&... args) {
return data_->try_emplace_base(index, std::forward<Args>(args)...);
return data_->try_emplace(index, std::forward<Args>(args)...);
}

template <typename... Args>
auto try_emplace(const iterator&, size_t index, Args&&... args) {
// We ignore the iterator, this is an array based collection, so access is ~O(1).
return data_->try_emplace_base(index, std::forward<Args>(args)...);
return data_->try_emplace(index, std::forward<Args>(args)...).first;
}

bool erase(size_t index) {
Expand Down
12 changes: 1 addition & 11 deletions include/phtree/common/flat_sparse_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class sparse_map {

template <typename... Args>
auto try_emplace(iterator iter, size_t key, Args&&... args) {
return try_emplace_base(iter, key, std::forward<Args>(args)...);
return try_emplace_base(iter, key, std::forward<Args>(args)...).first;
}

void erase(KeyT key) {
Expand All @@ -128,16 +128,6 @@ class sparse_map {
}

private:
template <typename... Args>
auto emplace_base(KeyT key, Args&&... args) {
auto it = lower_bound(key);
if (it != end() && it->first == key) {
return std::make_pair(it, false);
} else {
return std::make_pair(data_.emplace(it, key, std::forward<Args>(args)...), true);
}
}

template <typename... Args>
auto try_emplace_base(const iterator& it, KeyT key, Args&&... args) {
if (it != end() && it->first == key) {
Expand Down
17 changes: 9 additions & 8 deletions include/phtree/v16/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace improbable::phtree::v16 {
* nodes and dimensionality. Remember that n_max = 2^DIM.
*/
template <dimension_t DIM, typename Entry>
// using EntryMap = std::map<hc_pos_dim_t<DIM>, Entry>;
using EntryMap = typename std::conditional_t<
DIM <= 3,
array_map<Entry, (uint64_t(1) << DIM)>,
Expand Down Expand Up @@ -132,16 +133,16 @@ class Node {
}

template <typename IterT, typename... Args>
EntryT& Emplace(IterT iter, bool& is_inserted, const KeyT& key,
bit_width_t postfix_len, Args&&... args) {
hc_pos_t hc_pos = CalcPosInArray(key, postfix_len); // TODO pass in -> should be known!
auto emplace_result = entries_.try_emplace(iter, hc_pos, key, std::forward<Args>(args)...);
auto& entry = emplace_result.first->second;
// Return if emplace succeed, i.e. there was no entry.
if (emplace_result.second) {
EntryT& Emplace(
IterT iter, bool& is_inserted, const KeyT& key, bit_width_t postfix_len, Args&&... args) {
hc_pos_t hc_pos = CalcPosInArray(key, postfix_len); // TODO pass in -> should be known!
if (iter == entries_.end() || iter->first != hc_pos) {
auto emplace_result =
entries_.try_emplace(iter, hc_pos, key, std::forward<Args>(args)...);
is_inserted = true;
return entry;
return emplace_result->second;
}
auto& entry = iter->second;
return HandleCollision(entry, is_inserted, key, postfix_len, std::forward<Args>(args)...);
}

Expand Down