21#include "pw_assert/assert.h"
22#include "pw_bytes/span.h"
23#include "pw_containers/dynamic_deque.h"
24#include "pw_multibuf/v2/internal/entry.h"
26namespace pw::multibuf::v2::internal {
29template <Mutability kMutability>
44template <Mutability kMutability>
47 using SpanType = std::
48 conditional_t<kMutability == Mutability::kConst, ConstByteSpan, ByteSpan>;
49 using ByteType =
typename SpanType::element_type;
52 using size_type = Entry::size_type;
53 using difference_type = Entry::difference_type;
54 using value_type = SpanType;
55 using pointer = value_type*;
56 using const_pointer =
const value_type*;
57 using reference = value_type&;
58 using const_reference =
const value_type&;
59 using iterator_category = std::bidirectional_iterator_tag;
70 return {deque_, chunk_, entries_per_chunk_};
73 constexpr reference operator*() {
74 PW_ASSERT(is_valid());
78 constexpr const_reference operator*()
const {
79 PW_ASSERT(is_valid());
83 constexpr pointer operator->() {
84 PW_ASSERT(is_valid());
88 constexpr const_pointer operator->()
const {
89 PW_ASSERT(is_valid());
111 return lhs.deque_ == rhs.deque_ &&
112 lhs.entries_per_chunk_ == rhs.entries_per_chunk_ &&
113 lhs.chunk_ == rhs.chunk_;
118 return !(lhs == rhs);
125 template <Mutability>
129 template <Mutability>
137 size_type entries_per_chunk)
138 : deque_(deque), chunk_(chunk), entries_per_chunk_(entries_per_chunk) {
142 [[nodiscard]]
constexpr bool is_valid()
const {
143 return deque_ !=
nullptr && chunk_ < num_chunks();
146 constexpr size_type num_chunks()
const {
150 constexpr void ResetCurrent();
152 const Deque* deque_ =
nullptr;
153 size_type chunk_ = 0;
160template <Mutability kMutability>
163 deque_ = other.deque_;
164 chunk_ = other.chunk_;
165 entries_per_chunk_ = other.entries_per_chunk_;
170template <Mutability kMutability>
171constexpr ChunkIterator<kMutability>& ChunkIterator<kMutability>::operator++() {
172 PW_ASSERT(is_valid());
173 size_t left = current_.size();
178 while (chunk_ < num_chunks() &&
186template <Mutability kMutability>
187constexpr ChunkIterator<kMutability>& ChunkIterator<kMutability>::operator--() {
188 PW_ASSERT(deque_ !=
nullptr);
189 PW_ASSERT(chunk_ != 0);
190 current_ = SpanType();
191 while (chunk_ != 0) {
192 SpanType prev =
Entry::GetView(*deque_, chunk_ - 1, entries_per_chunk_);
193 if (!current_.empty() && prev.data() + prev.size() != current_.data()) {
196 current_ = SpanType(prev.data(), prev.size() + current_.size());
202template <Mutability kMutability>
203constexpr void ChunkIterator<kMutability>::ResetCurrent() {
205 current_ = SpanType();
206 chunk_ = num_chunks();
211 for (size_type i = chunk_ + 1; i < num_chunks(); ++i) {
216 if (current_.empty()) {
221 if (current_.data() + current_.size() != next.data()) {
224 current_ = SpanType(current_.data(), current_.size() + next.size());
226 if (current_.empty()) {
227 chunk_ = num_chunks();
Definition: dynamic_deque.h:60
Definition: chunk_iterator.h:45
Definition: multibuf.h:1057
static constexpr size_type num_chunks(const Deque &deque, size_type entries_per_chunk)
Returns the number of chunks in a deque.
Definition: entry.h:68
static constexpr ByteSpan GetView(const Deque &deque, size_type chunk, size_type entries_per_chunk, size_type layer)
Returns a view of the visible data length of the chunk at the given layer.
Definition: entry.h:235
static constexpr size_type GetLength(const Deque &deque, size_type chunk, size_type entries_per_chunk, size_type layer)
Returns the length of the given layer of the chunk.
Definition: entry.h:216
static constexpr size_type kMinEntriesPerChunk
Minimum number of entries per chunk.
Definition: entry.h:65