Skip to content

Commit

Permalink
added .at methods and range_check
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Jun 14, 2024
1 parent ebfba30 commit 79bc6de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
21 changes: 20 additions & 1 deletion include/loki/details/pddl/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class PDDLFactory

size_t m_count = 0;

void range_check(size_t pos) const
{
if (pos >= size())
{
throw std::out_of_range("SegmentedVector::range_check: pos (which is " + std::to_string(pos) + ") >= this->size() (which is "
+ std::to_string(size()) + ")");
}
}

public:
explicit PDDLFactory(size_t elements_per_segment) : m_persistent_vector(SegmentedVector<HolderType>(elements_per_segment)) {}
PDDLFactory(const PDDLFactory& other) = delete;
Expand Down Expand Up @@ -101,7 +110,17 @@ class PDDLFactory
*/

/// @brief Returns a pointer to an existing object with the given identifier.
[[nodiscard]] HolderType const* get(size_t identifier) const { return &(m_persistent_vector.at(identifier)); }
[[nodiscard]] HolderType const* operator[](size_t identifier) const
{
assert(identifier < size());
return &(m_persistent_vector.at(identifier));
}

[[nodiscard]] HolderType const* at(size_t identifier) const
{
range_check(identifier);
return &(m_persistent_vector.at(identifier));
}

/**
* Iterators
Expand Down
13 changes: 11 additions & 2 deletions include/loki/details/utils/segmented_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ class SegmentedVector

size_t element_index(size_t pos) const { return pos % m_elements_per_segment; }

void range_check(size_t pos) const
{
if (pos >= size())
{
throw std::out_of_range("SegmentedVector::range_check: pos (which is " + std::to_string(pos) + ") >= this->size() (which is "
+ std::to_string(size()) + ")");
}
}

public:
explicit SegmentedVector(size_t elements_per_segment) : m_elements_per_segment(elements_per_segment), m_size(0), m_capacity(0) {}

Expand Down Expand Up @@ -105,13 +114,13 @@ class SegmentedVector

T& at(size_t pos)
{
assert(pos < size());
range_check(pos);
return m_data[segment_index(pos)].at(element_index(pos));
}

const T& at(size_t pos) const
{
assert(pos < size());
range_check(pos);
return m_data[segment_index(pos)].at(element_index(pos));
}

Expand Down

0 comments on commit 79bc6de

Please sign in to comment.