Skip to content

Commit bd7f23a

Browse files
Copilotbugparty
andcommitted
Remove redundant modulo operations and fix end() iterator
- Removed % N from operator* and operator-> since index_ is always in [0, N-1] for valid iterators - Changed end() and cend() to use N as sentinel value, consistent with operator++ implementation - With the current operator++ that keeps index_ bounded and sets it to N at end, the modulo in dereference operators is redundant Co-authored-by: bugparty <1510776+bugparty@users.noreply.github.com>
1 parent d06bf03 commit bd7f23a

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

RingBuffer.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ namespace buffers {
4949
ring_buffer_iterator& operator=(ring_buffer_iterator const& ) noexcept = default;
5050
template<bool Z = C, typename std::enable_if<(!Z), int>::type* = nullptr>
5151
[[nodiscard]] reference operator*() noexcept {
52-
return (*source_)[index_ % N];
52+
return (*source_)[index_];
5353
}
5454
template<bool Z = C, typename std::enable_if<(Z), int>::type* = nullptr>
5555
[[nodiscard]] const_reference operator*() const noexcept {
56-
return (*source_)[index_ % N];
56+
return (*source_)[index_];
5757
}
5858
template<bool Z = C, typename std::enable_if<(!Z), int>::type* = nullptr>
5959
[[nodiscard]] pointer operator->() noexcept {
60-
return &((*source_)[index_ % N]);
60+
return &((*source_)[index_]);
6161
}
6262
template<bool Z = C, typename std::enable_if<(Z), int>::type* = nullptr>
6363
[[nodiscard]] const_pointer operator->() const noexcept {
64-
return &((*source_)[index_ % N]);
64+
return &((*source_)[index_]);
6565
}
6666
self_type& operator++() noexcept {
6767
++count_;
@@ -218,11 +218,11 @@ using std::bool_constant;
218218
// Iterator to oldest element.
219219
[[nodiscard]] iterator begin() noexcept { return iterator{this, tail_, 0};}
220220
// Iterator to one past newest element.
221-
[[nodiscard]] iterator end() noexcept { return iterator{this, tail_ + size_, size_};}
221+
[[nodiscard]] iterator end() noexcept { return iterator{this, N, size_};}
222222
// Const iterator to oldest element.
223223
[[nodiscard]] const_iterator cbegin() const noexcept { return const_iterator{this, tail_, 0};}
224224
// Const iterator to one past newest element.
225-
[[nodiscard]] const_iterator cend() const noexcept { return const_iterator{this, tail_ + size_, size_};}
225+
[[nodiscard]] const_iterator cend() const noexcept { return const_iterator{this, N, size_};}
226226
// Check if buffer has no elements.
227227
[[nodiscard]] bool empty() const noexcept { return size_ == 0; }
228228
// Check if buffer is at capacity.

0 commit comments

Comments
 (0)