Skip to content

Commit

Permalink
#1934: Add front and back methods to CircularPhasesBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
thearusable committed Sep 23, 2024
1 parent ff860c8 commit 0fc1778
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions src/vt/utils/container/circular_phases_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,32 +239,58 @@ class CircularPhasesBuffer {
}

/**
* @brief Get iterator to the beginning of the buffer
* \brief Get const iterator to the beginning of the buffer
*
* @return auto the begin iterator
* \return the const begin iterator
*/
auto begin() { return ForwardIterator(head_, findTail(), &buffer_); }
auto begin() const { return ForwardIterator(head_, findTail(), &buffer_); }

/**
* @brief Get const iterator to the beginning of the buffer
* \brief Get const iterator to the space after buffer
*
* @return auto the const begin iterator
* \return the const end iterator
*/
auto begin() const { return ForwardIterator(head_, findTail(), &buffer_); }
auto end() const { return ForwardIterator(); }

/**
* @brief Get iterator to the space after buffer
* \brief Finds the newest phase in the buffer.
*
* @return auto the end iterator
* \return the newest phase in the buffer.
*/
auto end() { return ForwardIterator(); }
PhaseType frontPhase() const {
return inserted_ == 0 ? 0 : head_phase_ - 1;
}

/**
* @brief Get const iterator to the space after buffer
* \brief Get the newest element in buffer.
*
* @return auto the const end iterator
* \return the reference to the newest element.
*/
auto end() const { return ForwardIterator(); }
StoredType& front() {
return buffer_.at(getPrevIndex(head_));
}

/**
* \brief Finds the oldest phase in the buffer.
*
* \return the oldest phase in the buffer.
*/
PhaseType backPhase() const {
if (head_ == inserted_) {
return 0;
}
return head_phase_ - buffer_.size();
}

/**
* \brief Get the oldest element in buffer.
*
* \return the reference to the oldest element.
*/
StoredType& back() {
return buffer_.at(findTail());
}

private:
std::size_t findTail() const {
if (inserted_ == 0) {
Expand Down Expand Up @@ -318,20 +344,19 @@ class CircularPhasesBuffer {
std::vector<StoredType> buffer_;

public:
// TODO: Change iterator to return a pair of phase and reference to data
struct ForwardIterator {
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = StoredType;
using reference = StoredType&;
using reference = const StoredType&;

ForwardIterator(std::size_t head, std::size_t index, const std::vector<value_type>* buffer)
: head_(head), index_(index), buffer_(buffer) {}

ForwardIterator()
: head_(no_pos), index_(no_pos), buffer_(nullptr) {}

reference operator*() { return &buffer_->at(index_); }
reference operator*() { return (*buffer_)[index_]; }

ForwardIterator& operator++() {
advance();
Expand Down

0 comments on commit 0fc1778

Please sign in to comment.