From 88206359979222ee4aa5d2ec54c8547aabb05150 Mon Sep 17 00:00:00 2001 From: tzaeschke Date: Tue, 13 Dec 2022 17:49:18 +0100 Subject: [PATCH 1/2] clean up --- CHANGELOG.md | 5 +++-- include/phtree/common/b_plus_tree_map.h | 2 +- include/phtree/common/flat_array_map.h | 8 ++++---- include/phtree/common/flat_sparse_map.h | 12 +----------- include/phtree/v16/node.h | 17 +++++++++-------- 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 218b4149..89de648b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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), + [#100](https://github.com/tzaeschke/phtree-cpp/pull/100) - 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 diff --git a/include/phtree/common/b_plus_tree_map.h b/include/phtree/common/b_plus_tree_map.h index 0cd22608..8d8edbdb 100644 --- a/include/phtree/common/b_plus_tree_map.h +++ b/include/phtree/common/b_plus_tree_map.h @@ -199,7 +199,7 @@ class b_plus_tree_map { template auto try_emplace(IterT iter, KeyT key, Args&&... args) { - return emplace_hint(iter, key, std::forward(args)...); + return emplace_hint(iter, key, std::forward(args)...).first; } void erase(KeyT key) { diff --git a/include/phtree/common/flat_array_map.h b/include/phtree/common/flat_array_map.h index d1ff9b53..8db59a89 100644 --- a/include/phtree/common/flat_array_map.h +++ b/include/phtree/common/flat_array_map.h @@ -145,7 +145,7 @@ class flat_array_map { } template - std::pair try_emplace_base(size_t index, Args&&... args) { + std::pair try_emplace(size_t index, Args&&... args) { if (!occupied(index)) { new (reinterpret_cast(&data_[index])) map_pair( std::piecewise_construct, @@ -268,18 +268,18 @@ class array_map { template auto emplace(Args&&... args) { - return data_->try_emplace_base(std::forward(args)...); + return data_->try_emplace(std::forward(args)...); } template auto try_emplace(size_t index, Args&&... args) { - return data_->try_emplace_base(index, std::forward(args)...); + return data_->try_emplace(index, std::forward(args)...); } template 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)...); + return data_->try_emplace(index, std::forward(args)...).first; } bool erase(size_t index) { diff --git a/include/phtree/common/flat_sparse_map.h b/include/phtree/common/flat_sparse_map.h index fad7a47f..8385f0f2 100644 --- a/include/phtree/common/flat_sparse_map.h +++ b/include/phtree/common/flat_sparse_map.h @@ -109,7 +109,7 @@ class sparse_map { template auto try_emplace(iterator iter, size_t key, Args&&... args) { - return try_emplace_base(iter, key, std::forward(args)...); + return try_emplace_base(iter, key, std::forward(args)...).first; } void erase(KeyT key) { @@ -128,16 +128,6 @@ class sparse_map { } private: - template - 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)...), true); - } - } - template auto try_emplace_base(const iterator& it, KeyT key, Args&&... args) { if (it != end() && it->first == key) { diff --git a/include/phtree/v16/node.h b/include/phtree/v16/node.h index 57824226..da67b98b 100644 --- a/include/phtree/v16/node.h +++ b/include/phtree/v16/node.h @@ -40,6 +40,7 @@ namespace improbable::phtree::v16 { * nodes and dimensionality. Remember that n_max = 2^DIM. */ template +// using EntryMap = std::map, Entry>; using EntryMap = typename std::conditional_t< DIM <= 3, array_map, @@ -132,16 +133,16 @@ class Node { } template - 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)...); - 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)...); is_inserted = true; - return entry; + return emplace_result->second; } + auto& entry = iter->second; return HandleCollision(entry, is_inserted, key, postfix_len, std::forward(args)...); } From f53dba14c643dd8fcb6ea3565d580edecb001f33 Mon Sep 17 00:00:00 2001 From: tzaeschke Date: Tue, 13 Dec 2022 17:51:39 +0100 Subject: [PATCH 2/2] clean up --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89de648b..15334446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - 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), - [#100](https://github.com/tzaeschke/phtree-cpp/pull/100) + [#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