diff --git a/.gitignore b/.gitignore index 01b9d96..f084773 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,7 @@ Testing/Temporary _codeql_build_dir _codeql_detected_source_root +# CodeQL build artifacts +_codeql_build_dir +_codeql_detected_source_root + diff --git a/RingBuffer.hpp b/RingBuffer.hpp index 3c92324..bcb4bdd 100644 --- a/RingBuffer.hpp +++ b/RingBuffer.hpp @@ -49,23 +49,27 @@ namespace buffers { ring_buffer_iterator& operator=(ring_buffer_iterator const& ) noexcept = default; template::type* = nullptr> [[nodiscard]] reference operator*() noexcept { - return (*source_)[index_ % N]; + return (*source_)[index_]; } template::type* = nullptr> [[nodiscard]] const_reference operator*() const noexcept { - return (*source_)[index_ % N]; + return (*source_)[index_]; } template::type* = nullptr> [[nodiscard]] pointer operator->() noexcept { - return &((*source_)[index_ % N]); + return &((*source_)[index_]); } template::type* = nullptr> [[nodiscard]] const_pointer operator->() const noexcept { - return &((*source_)[index_ % N]); + return &((*source_)[index_]); } self_type& operator++() noexcept { - ++index_; ++count_; + if (count_ >= source_->size()) { + index_ = N; // Set to sentinel value (out of valid range [0, N-1]) when reaching end + } else { + index_ = (index_ + 1) % N; + } return *this; } self_type operator++(int) noexcept { @@ -214,11 +218,11 @@ using std::bool_constant; // Iterator to oldest element. [[nodiscard]] iterator begin() noexcept { return iterator{this, tail_, 0};} // Iterator to one past newest element. - [[nodiscard]] iterator end() noexcept { return iterator{this, tail_ + size_, size_};} + [[nodiscard]] iterator end() noexcept { return iterator{this, N, size_};} // Const iterator to oldest element. [[nodiscard]] const_iterator cbegin() const noexcept { return const_iterator{this, tail_, 0};} // Const iterator to one past newest element. - [[nodiscard]] const_iterator cend() const noexcept { return const_iterator{this, tail_ + size_, size_};} + [[nodiscard]] const_iterator cend() const noexcept { return const_iterator{this, N, size_};} // Check if buffer has no elements. [[nodiscard]] bool empty() const noexcept { return size_ == 0; } // Check if buffer is at capacity.