26#ifndef GUL17_SLIDINGBUFFER_H_
27#define GUL17_SLIDINGBUFFER_H_
181 std::array<ElementT, fixed_capacity>,
182 std::vector<ElementT>>
275 decrease_idx(idx_end_);
292 increase_idx(idx_begin_);
311 storage_[idx_end_] =
in;
313 increase_idx(idx_end_);
315 increase_idx(idx_begin_);
316 else if (idx_end_ == idx_begin_)
325 storage_[idx_end_] = std::move(
in);
327 increase_idx(idx_end_);
329 increase_idx(idx_begin_);
330 else if (idx_end_ == idx_begin_)
349 decrease_idx(idx_begin_);
352 decrease_idx(idx_end_);
353 else if (idx_end_ == idx_begin_)
356 storage_[idx_begin_] =
in;
364 decrease_idx(idx_begin_);
367 decrease_idx(idx_end_);
368 else if (idx_end_ == idx_begin_)
371 storage_[idx_begin_] = std::move(
in);
388 return (
idx >= capacity()) ? storage_[
idx - capacity()] : storage_[
idx];
398 return (
idx >= capacity()) ? storage_[
idx - capacity()] : storage_[
idx];
414 auto const s = size();
417 ": idx (which is ",
idx,
") >= this->size() (which is ",
s,
")"));
419 return operator[](
idx);
427 auto const s = size();
430 ": idx (which is ",
idx,
") >= this->size() (which is ",
s,
")"));
432 return operator[](
idx);
443 return storage_[idx_begin_];
451 return storage_[idx_begin_];
463 return storage_[capacity() - 1];
465 return storage_[idx_end_ - 1];
474 return storage_[capacity() - 1];
476 return storage_[idx_end_ - 1];
491 if (idx_end_ >= idx_begin_)
492 return idx_end_ - idx_begin_;
494 return idx_end_ + capacity() - idx_begin_;
536 std::fill(storage_.begin(), storage_.end(),
value_type{});
561 "resize() only possible if the underlying container is resizable");
572 "reserve() only possible if the underlying container is resizable");
583 return (
not full_)
and (idx_begin_ == idx_end_);
593 auto friend operator<< (std::ostream&
s,
596 auto const size =
buffer.size();
643 template <
typename BufferPo
inter>
651 BufferPointer buffer_;
689 auto previous = *
this;
727 auto previous = *
this;
756 auto operator*() const noexcept -> typename std::conditional_t<
757 std::is_const<std::remove_pointer_t<BufferPointer>>::value,
760 return (*buffer_)[position_];
764 auto operator->() const noexcept -> typename std::conditional_t<
765 std::is_const<std::remove_pointer_t<BufferPointer>>::value,
768 return &(*buffer_)[position_];
773 std::is_const<std::remove_pointer_t<BufferPointer>>::value,
776 return *(*
this + offs);
788 return lhs.position_ == rhs.position_;
800 return not(lhs == rhs);
810 return lhs.position_ > rhs.position_;
818 return lhs.position_ < rhs.position_;
829 return lhs.position_ >= rhs.position_;
840 return lhs.position_ <= rhs.position_;
867 return std::make_reverse_iterator(end());
896 return std::make_reverse_iterator(begin());
916 return std::make_reverse_iterator(cend());
940 return std::make_reverse_iterator(cbegin());
944 void decrease_idx(
size_t &
idx)
noexcept
947 idx = capacity() - 1;
952 void increase_idx(
size_t &
idx)
noexcept
956 if (
idx >= capacity())
1000 std::rotate(storage_.begin(), storage_.begin() +
static_cast<difference_type>(idx_begin_), storage_.end());
1012 std::rotate(storage_.begin(), storage_.begin() +
static_cast<difference_type>(idx_begin_), storage_.end());
1077 std::array<ElementT, fixed_capacity>,
1078 std::vector<ElementT>>
1126 if (
not full_
and (idx_end_ == 0
or idx_end_ >= idx_begin_))
1127 return storage_.begin() +
static_cast<difference_type
>(idx_begin_);
1129 return storage_.begin();
1146 if (
not full_
and (idx_end_ == 0
or idx_end_ >= idx_begin_))
1147 return storage_.cbegin() + idx_begin_;
1149 return storage_.cbegin();
1171 if (full_
or idx_begin_ != 0)
1172 return storage_.end();
1174 return storage_.begin() +
static_cast<difference_type
>(idx_end_);
1191 if (full_
or idx_begin_ != 0)
1192 return storage_.cend();
1194 return storage_.cbegin() + idx_end_;
1207 return std::make_reverse_iterator(end());
1220 return std::make_reverse_iterator(cend());
1230 return std::make_reverse_iterator(begin());
1241 return std::make_reverse_iterator(cbegin());
1266 "resize() only possible if the underlying container is resizable");
1285 and (idx_begin_ != 0);
1294 std::move_backward(storage_.begin() +
static_cast<difference_type
>(idx_begin_),
1295 storage_.begin() +
static_cast<difference_type
>(
old_capacity), storage_.end());
1304 std::rotate(storage_.begin(), storage_.begin() +
static_cast<difference_type
>(
required_shift), storage_.end());
1316 "reserve() only possible if the underlying container is resizable");
Declaration of the overload set for cat() and of the associated class ConvertingStringView.
A variant of SlidingBuffer that exposes the underlying container through its iterator interface.
Definition SlidingBuffer.h:1080
auto begin() noexcept -> iterator
Return an iterator to the first occupied element of the underlying container.
Definition SlidingBuffer.h:1124
typename Container::iterator iterator
Iterator to an element.
Definition SlidingBuffer.h:1083
auto end() const noexcept -> const_iterator
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:1178
typename Container::const_reverse_iterator const_reverse_iterator
Iterator to a const element in reversed container.
Definition SlidingBuffer.h:1089
auto crend() const noexcept -> const_reverse_iterator
Return a constant iterator to the last element of the reversed underlying container.
Definition SlidingBuffer.h:1239
auto cbegin() const noexcept -> const_iterator
Return a constant iterator to the first occupied element of the underlying container.
Definition SlidingBuffer.h:1144
auto cend() const noexcept -> const_iterator
Return a constant iterator to the element following the last element in the used space of the underly...
Definition SlidingBuffer.h:1189
auto crbegin() const noexcept -> const_reverse_iterator
Return a constant reverse iterator to the first used element of the reversed underlying container.
Definition SlidingBuffer.h:1218
auto rbegin() noexcept -> reverse_iterator
Return a reverse iterator to the first used element of the reversed underlying container.
Definition SlidingBuffer.h:1205
auto end() noexcept -> iterator
Return an iterator to the element following the last element in the used space of the underlying cont...
Definition SlidingBuffer.h:1169
auto rend() noexcept -> reverse_iterator
Return an iterator to the last element of the reversed underlying container.
Definition SlidingBuffer.h:1228
auto reserve(size_type size, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
Resize the container (identical to resize()).
Definition SlidingBuffer.h:1313
typename Container::const_iterator const_iterator
Iterator to a const element.
Definition SlidingBuffer.h:1085
typename Container::reverse_iterator reverse_iterator
Iterator to an element in reversed container.
Definition SlidingBuffer.h:1087
auto begin() const noexcept -> const_iterator
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:1133
auto resize(size_type new_capacity, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
Resize the container.
Definition SlidingBuffer.h:1263
Iterator of the SlidingBuffer container.
Definition SlidingBuffer.h:645
auto operator+=(difference_type d) noexcept -> SlidingBufferIterator &
Increase iterator by a given number of positions.
Definition SlidingBuffer.h:695
std::random_access_iterator_tag iterator_category
Defines the category of the iterator.
Definition SlidingBuffer.h:657
auto operator-=(difference_type d) noexcept -> SlidingBufferIterator &
Decrease iterator by a given number of positions.
Definition SlidingBuffer.h:733
SlidingBufferIterator(BufferPointer buff, size_type num=0) noexcept
Create an iterator pointing into a SlidingBuffer.
Definition SlidingBuffer.h:673
auto operator--(int) noexcept -> SlidingBufferIterator
Post-decrement iterator by one position.
Definition SlidingBuffer.h:725
friend auto operator+(difference_type d, const SlidingBufferIterator &it) noexcept -> SlidingBufferIterator
Add an integer and an iterator.
Definition SlidingBuffer.h:711
auto operator++(int) noexcept -> SlidingBufferIterator
Post-increment iterator by one position.
Definition SlidingBuffer.h:687
friend auto operator+(const SlidingBufferIterator &it, difference_type d) noexcept -> SlidingBufferIterator
Add an integer to an iterator.
Definition SlidingBuffer.h:703
value_type * pointer
This type represents a pointer-to-value_type.
Definition SlidingBuffer.h:663
friend auto operator>=(const SlidingBufferIterator &lhs, const SlidingBufferIterator &rhs) noexcept -> bool
Determine if the left iterator refers to position that is greater than or equal to than the right one...
Definition SlidingBuffer.h:826
friend auto operator>(const SlidingBufferIterator &lhs, const SlidingBufferIterator &rhs) noexcept -> bool
Determine if the left iterator refers to a greater position than the right one.
Definition SlidingBuffer.h:807
auto operator->() const noexcept -> typename std::conditional_t< std::is_const< std::remove_pointer_t< BufferPointer > >::value, const_pointer, pointer >
Access member of element pointed to by the iterator.
Definition SlidingBuffer.h:764
auto operator--() noexcept -> SlidingBufferIterator &
Pre-decrement iterator by one position.
Definition SlidingBuffer.h:718
ElementT value_type
The type "pointed to" by the iterator.
Definition SlidingBuffer.h:659
auto operator++() noexcept -> SlidingBufferIterator &
Pre-increment iterator by one position.
Definition SlidingBuffer.h:680
auto operator*() const noexcept -> typename std::conditional_t< std::is_const< std::remove_pointer_t< BufferPointer > >::value, const_reference, reference >
Access element pointed to by the iterator.
Definition SlidingBuffer.h:756
friend auto operator-(const SlidingBufferIterator &it, difference_type d) noexcept -> SlidingBufferIterator
Subtract an integer from an iterator.
Definition SlidingBuffer.h:741
friend auto operator-(const SlidingBufferIterator &lhs, const SlidingBufferIterator &rhs) noexcept -> difference_type
Subtract two iterators.
Definition SlidingBuffer.h:749
friend auto operator==(const SlidingBufferIterator &lhs, const SlidingBufferIterator &rhs) noexcept -> bool
Compare two iterators for equality.
Definition SlidingBuffer.h:785
auto operator[](difference_type offs) noexcept -> typename std::conditional_t< std::is_const< std::remove_pointer_t< BufferPointer > >::value, const_reference, reference >
Dereference the iterator at a certain index offset.
Definition SlidingBuffer.h:772
friend auto operator!=(const SlidingBufferIterator &lhs, const SlidingBufferIterator &rhs) noexcept -> bool
Compare two iterators for inequality.
Definition SlidingBuffer.h:797
value_type & reference
This type represents a reference-to-value_type.
Definition SlidingBuffer.h:665
std::ptrdiff_t difference_type
Distance between iterators is represented as this type.
Definition SlidingBuffer.h:661
A circular data buffer of (semi-)fixed capacity to which elements can be added at the front or at the...
Definition SlidingBuffer.h:184
auto back() const noexcept -> const_reference
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:471
auto end() const noexcept -> const_iterator
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:882
auto crend() const noexcept -> const_reverse_iterator
Return a read-only iterator to the element following the last element of the reversed container.
Definition SlidingBuffer.h:938
Container container_type
Type of the underlying container (e.g. std::array<value_type, ..>)
Definition SlidingBuffer.h:189
auto push_back(const value_type &in) -> void
Insert one element at the end of the buffer; if it is full, an element at the front is dropped to mak...
Definition SlidingBuffer.h:309
SlidingBuffer(SlidingBuffer &&) noexcept(std::is_nothrow_move_constructible< container_type >::value)=default
Default move constructor.
SlidingBuffer()=default
Construct an empty sliding buffer.
auto operator[](size_type idx) noexcept -> reference
Access an element in the buffer by index without bounds checking.
Definition SlidingBuffer.h:384
typename Container::const_reference const_reference
Reference to a constant element.
Definition SlidingBuffer.h:199
auto rend() noexcept -> reverse_iterator
Return an iterator to the element following the last element of the reversed container.
Definition SlidingBuffer.h:894
typename Container::const_pointer const_pointer
Pointer to a constant element.
Definition SlidingBuffer.h:203
auto rbegin() noexcept -> reverse_iterator
Return an iterator to the first element of the reversed container.
Definition SlidingBuffer.h:865
auto clear() -> void
Empty the buffer.
Definition SlidingBuffer.h:529
auto resize(size_type new_capacity, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
Resize the container.
Definition SlidingBuffer.h:558
auto begin() noexcept -> iterator
Return an iterator to the first element of the container.
Definition SlidingBuffer.h:849
auto push_front(const value_type &in) -> void
Insert one element at the front of the buffer; if it is full, an element at the back is dropped to ma...
Definition SlidingBuffer.h:347
auto cbegin() const noexcept -> const_iterator
Return a read-only iterator to the first element of the container.
Definition SlidingBuffer.h:904
std::reverse_iterator< iterator > reverse_iterator
Iterator to an element in reversed container.
Definition SlidingBuffer.h:209
typename Container::reference reference
Reference to an element.
Definition SlidingBuffer.h:197
auto back() noexcept -> reference
Return the backmost element (the one with the highest valid index).
Definition SlidingBuffer.h:460
auto pop_back() -> void
Remove the last element from the buffer.
Definition SlidingBuffer.h:273
auto size() const noexcept -> size_type
Return the number of elements in the container, i.e.
Definition SlidingBuffer.h:486
auto crbegin() const noexcept -> const_reverse_iterator
Return a read-only iterator to the first element of the reversed container.
Definition SlidingBuffer.h:914
typename Container::size_type size_type
Unsigned integer type (usually std::size_t)
Definition SlidingBuffer.h:193
auto cend() const noexcept -> const_iterator
Return a read-only iterator to the element following the last element of the container.
Definition SlidingBuffer.h:926
auto front() noexcept -> reference
Return the foremost element (the one with index 0).
Definition SlidingBuffer.h:441
auto empty() const noexcept -> bool
Check if the buffer contains no elements, i.e.
Definition SlidingBuffer.h:581
auto push_front(value_type &&in) -> void
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:362
auto at(const size_type idx) const noexcept(false) -> const_reference
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:425
auto constexpr capacity() const noexcept -> size_type
Return the maximum possible number of elements in the container.
Definition SlidingBuffer.h:504
ElementT value_type
Type of the elements in the underlying container.
Definition SlidingBuffer.h:191
auto end() noexcept -> iterator
Return an iterator to the element following the last element of the container.
Definition SlidingBuffer.h:876
auto reserve(size_type size, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
Resize the container (identical to resize()).
Definition SlidingBuffer.h:569
auto change_capacity(size_type new_capacity, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
Change the underlying container's capacity.
Definition SlidingBuffer.h:975
typename Container::difference_type difference_type
Signed integer type (usually std::ptrdiff_t)
Definition SlidingBuffer.h:195
auto operator[](size_type idx) const noexcept -> const_reference
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:394
SlidingBuffer(SlidingBuffer const &)=default
Default copy constructor.
auto filled() const noexcept -> bool
Return true if the buffer is completely filled with elements.
Definition SlidingBuffer.h:517
auto at(const size_type idx) noexcept(false) -> reference
Access an element in the buffer by index with bounds checking.
Definition SlidingBuffer.h:412
typename Container::pointer pointer
Pointer to an element.
Definition SlidingBuffer.h:201
auto pop_front() -> void
Remove the first element from the buffer.
Definition SlidingBuffer.h:290
std::reverse_iterator< const_iterator > const_reverse_iterator
Iterator to a const element in reversed container.
Definition SlidingBuffer.h:211
auto begin() const noexcept -> const_iterator
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:855
auto front() const noexcept -> const_reference
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:449
auto push_back(value_type &&in) -> void
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SlidingBuffer.h:323
ShrinkBehavior
Determine how a SlidingBuffer handles decreases of its size.
Definition SlidingBuffer.h:69
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
std::string cat()
Efficiently concatenate an arbitrary number of strings and numbers.
Definition cat.h:100
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29