From b80945fd5af649d90bf070f4f99395b3c48ef9c1 Mon Sep 17 00:00:00 2001 From: Dominik Drexler Date: Sat, 29 Jun 2024 17:39:24 +0200 Subject: [PATCH] made segmented vector more stl like, added emplace_back and back functionalities --- include/loki/details/pddl/factory.hpp | 3 +- .../loki/details/utils/segmented_vector.hpp | 33 +++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/include/loki/details/pddl/factory.hpp b/include/loki/details/pddl/factory.hpp index 920617f2..472fcd79 100644 --- a/include/loki/details/pddl/factory.hpp +++ b/include/loki/details/pddl/factory.hpp @@ -76,8 +76,7 @@ class PDDLFactory assert(identifier == m_persistent_vector.size()); // Explicitly call the constructor of T to give exclusive access to the factory. - auto element = HolderType(std::move(SubType(identifier, std::forward(args)...))); - const auto* element_ptr = &(m_persistent_vector.push_back(std::move(element))); + const auto* element_ptr = &m_persistent_vector.emplace_back(SubType(identifier, std::forward(args)...)); // The pointer to the location in persistent memory. assert(element_ptr); diff --git a/include/loki/details/utils/segmented_vector.hpp b/include/loki/details/utils/segmented_vector.hpp index f08de2be..6c3cc29e 100644 --- a/include/loki/details/utils/segmented_vector.hpp +++ b/include/loki/details/utils/segmented_vector.hpp @@ -71,7 +71,7 @@ class SegmentedVector /** * Modifiers */ - const T& push_back(T value) + void push_back(T value) { // Increase capacity if necessary if (m_size >= m_capacity) @@ -83,9 +83,24 @@ class SegmentedVector // Take ownership of memory segment.push_back(std::move(value)); ++m_size; + } + + template + T& emplace_back(Args&&... args) + { + // Increase capacity if necessary + if (m_size >= m_capacity) + { + increase_capacity(); + } + + auto& segment = m_data[segment_index(size())]; + + // Emplace the new element directly in the segment + auto& element = segment.emplace_back(std::forward(args)...); + ++m_size; - // Fetch return value - return segment.back(); + return element; } void pop_back() @@ -124,6 +139,18 @@ class SegmentedVector return m_data[segment_index(pos)].at(element_index(pos)); } + T& back() + { + range_check(size() - 1); + return m_data[segment_index(size() - 1)].at(element_index(size() - 1)); + } + + const T& back() const + { + range_check(size() - 1); + return m_data[segment_index(size() - 1)].at(element_index(size() - 1)); + } + /** * Iterators */