Skip to content

Commit

Permalink
made segmented vector more stl like, added emplace_back and back func…
Browse files Browse the repository at this point in the history
…tionalities
  • Loading branch information
drexlerd committed Jun 29, 2024
1 parent 79bc6de commit b80945f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
3 changes: 1 addition & 2 deletions include/loki/details/pddl/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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>(args)...));
// The pointer to the location in persistent memory.
assert(element_ptr);

Expand Down
33 changes: 30 additions & 3 deletions include/loki/details/utils/segmented_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -83,9 +83,24 @@ class SegmentedVector
// Take ownership of memory
segment.push_back(std::move(value));
++m_size;
}

template<typename... Args>
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>(args)...);
++m_size;

// Fetch return value
return segment.back();
return element;
}

void pop_back()
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit b80945f

Please sign in to comment.