20#include "pw_assert/assert.h"
21#include "pw_containers/dynamic_deque.h"
22#include "pw_multibuf/internal/chunk_iterator.h"
23#include "pw_multibuf/internal/entry.h"
25namespace pw::multibuf {
43template <
typename SizeType,
bool kIsConst>
47 using Deque =
typename ChunkIteratorType::Deque;
50 using size_type = SizeType;
51 using difference_type = ptrdiff_t;
52 using value_type =
typename ChunkIteratorType::ByteType;
53 using pointer = value_type*;
54 using reference = value_type&;
55 using iterator_category = std::random_access_iterator_tag;
60 constexpr operator ByteIterator<SizeType,
true>()
const {
61 return {chunk_, offset_};
64 constexpr reference operator*()
const {
return chunk_->data()[offset_]; }
66 constexpr reference operator[](difference_type n)
const {
70 constexpr ByteIterator& operator++() {
return operator+=(1); }
80 return operator-=(-n);
82 size_t delta =
static_cast<size_t>(n);
84 while (delta != 0 && chunk_->size() <= delta) {
85 delta -= chunk_->size();
100 constexpr ByteIterator& operator--() {
return operator-=(1); }
110 return operator+=(-n);
112 size_t delta =
static_cast<size_t>(n);
113 while (delta > offset_) {
116 offset_ = chunk_->size();
126 constexpr friend difference_type operator-(
const ByteIterator& lhs,
131 difference_type delta = 0;
132 auto chunk = rhs.chunk_;
133 size_t offset = rhs.offset_;
134 while (chunk != lhs.chunk_) {
136 delta += bytes.size() - offset;
140 return delta +
static_cast<difference_type
>(lhs.offset_);
143 constexpr friend bool operator==(
const ByteIterator& lhs,
145 return lhs.Compare(rhs) == 0;
148 constexpr friend bool operator!=(
const ByteIterator& lhs,
150 return lhs.Compare(rhs) != 0;
153 constexpr friend bool operator<(
const ByteIterator& lhs,
155 return lhs.Compare(rhs) < 0;
158 constexpr friend bool operator>(
const ByteIterator& lhs,
160 return lhs.Compare(rhs) > 0;
163 constexpr friend bool operator<=(
const ByteIterator& lhs,
165 return lhs.Compare(rhs) <= 0;
168 constexpr friend bool operator>=(
const ByteIterator& lhs,
170 return lhs.Compare(rhs) >= 0;
175 template <
typename,
bool>
179 friend class ::pw::multibuf::internal::GenericMultiBuf;
182 friend class ::pw::multibuf::test::IteratorTest;
185 : chunk_(chunk), offset_(offset) {}
187 constexpr size_type chunk_index()
const {
return chunk_.index_; }
190 constexpr int Compare(
const ByteIterator& other)
const {
191 if (chunk_ != other.chunk_) {
192 return chunk_.index_ < other.chunk_.index_ ? -1 : 1;
194 if (offset_ != other.offset_) {
195 return offset_ < other.offset_ ? -1 : 1;
Definition: byte_iterator.h:44
Definition: chunk_iterator.h:43