31 #ifndef ETL_CIRCULAR_BUFFER_INCLUDED
32 #define ETL_CIRCULAR_BUFFER_INCLUDED
42 #include "static_assert.h"
57 :
exception(reason_, file_name_, line_number_)
100 return (
in + 1U) % BUFFER_SIZE ==
out;
106 return max_size() - size();
112 return BUFFER_SIZE - 1U;
118 return BUFFER_SIZE - 1U;
124 circular_buffer_base(
size_type BUFFER_SIZE_)
125 : BUFFER_SIZE(BUFFER_SIZE_)
140 template <
typename T>
145 typedef T value_type;
146 typedef T& reference;
147 typedef const T& const_reference;
148 #if ETL_CPP11_SUPPORTED
149 typedef T&& rvalue_reference;
152 typedef const T* const_pointer;
154 typedef typename etl::iterator_traits<pointer>::difference_type difference_type;
179 , current(other.current)
189 current = other.current;
199 return picb->pbuffer[current];
207 return picb->pbuffer[current];
215 return picb->pbuffer[current];
223 return picb->pbuffer[current];
234 if (current == picb->BUFFER_SIZE)
262 current = picb->BUFFER_SIZE - 1;
289 n = picb->BUFFER_SIZE + n;
292 current %= picb->BUFFER_SIZE;
302 return (this->
operator+=(-n));
334 return (lhs.current == rhs.current);
342 return !(lhs == rhs);
348 const difference_type lhs_index = lhs.get_index();
349 const difference_type rhs_index = rhs.get_index();
350 const difference_type reference_index = lhs.container().
begin().get_index();
351 const size_t buffer_size = lhs.container().max_size() + 1;
353 const difference_type lhs_distance = (lhs_index < reference_index) ? buffer_size + lhs_index - reference_index : lhs_index - reference_index;
354 const difference_type rhs_distance = (rhs_index < reference_index) ? buffer_size + rhs_index - reference_index : rhs_index - reference_index;
356 return lhs_distance < rhs_distance;
378 difference_type get_index()
const
384 const icircular_buffer& container()
const
390 pointer get_buffer()
const
398 difference_type distance(difference_type firstIndex, difference_type index)
const
400 if (index < firstIndex)
402 return picb->BUFFER_SIZE + current - firstIndex;
406 return index - firstIndex;
448 , current(other.current)
457 , current(other.current)
467 current = other.current;
478 current = other.current;
488 return picb->pbuffer[current];
496 return picb->pbuffer[current];
507 if (current == picb->BUFFER_SIZE)
535 current = picb->BUFFER_SIZE - 1;
562 n = picb->BUFFER_SIZE + n;
565 current %= picb->BUFFER_SIZE;
575 return (this->
operator+=(-n));
607 return (lhs.current == rhs.current);
615 return !(lhs == rhs);
621 const difference_type lhs_index = lhs.get_index();
622 const difference_type rhs_index = rhs.get_index();
623 const difference_type reference_index = lhs.container().
begin().get_index();
624 const size_t buffer_size = lhs.container().max_size() + 1;
626 const difference_type lhs_distance = (lhs_index < reference_index) ? buffer_size + lhs_index - reference_index : lhs_index - reference_index;
627 const difference_type rhs_distance = (rhs_index < reference_index) ? buffer_size + rhs_index - reference_index : rhs_index - reference_index;
629 return lhs_distance < rhs_distance;
651 difference_type get_index()
const
657 const icircular_buffer& container()
const
663 pointer get_buffer()
const
686 friend class const_iterator;
817 return pbuffer[
in == 0U ? BUFFER_SIZE - 1 :
in - 1U];
828 return pbuffer[
in == 0U ? BUFFER_SIZE - 1 :
in - 1U];
836 return pbuffer[(
out + index) % BUFFER_SIZE];
845 return pbuffer[(
out + index) % BUFFER_SIZE];
853 void push(const_reference item)
855 ::new (&pbuffer[
in]) T(item);
856 in = (
in + 1U) % BUFFER_SIZE;
863 out = (
out + 1U) % BUFFER_SIZE;
867 ETL_INCREMENT_DEBUG_COUNT;
871 #if ETL_CPP11_SUPPORTED
877 void push(rvalue_reference item)
879 ::new (&pbuffer[
in]) T(etl::move(item));
880 in = (
in + 1U) % BUFFER_SIZE;
887 out = (
out + 1U) % BUFFER_SIZE;
891 ETL_INCREMENT_DEBUG_COUNT;
899 template <
typename TIterator>
900 void push(TIterator first,
const TIterator& last)
902 while (first != last)
915 out = (
out + 1U) % BUFFER_SIZE;
916 ETL_DECREMENT_DEBUG_COUNT
939 ETL_RESET_DEBUG_COUNT
971 return distance(lhs.base(), rhs.base());
979 return distance(lhs.base(), rhs.base());
996 template <
typename TIterator1,
typename TIterator2>
997 static difference_type
distance(
const TIterator1& range_begin,
const TIterator2& range_end)
999 difference_type distance1 =
distance(range_begin);
1000 difference_type distance2 =
distance(range_end);
1002 return distance2 - distance1;
1008 template <
typename TIterator>
1009 static difference_type
distance(
const TIterator& other)
1011 const difference_type index = other.get_index();
1012 const difference_type reference_index = other.container().out;
1013 const size_t buffer_size = other.container().BUFFER_SIZE;
1015 if (index < reference_index)
1017 return buffer_size + index - reference_index;
1021 return index - reference_index;
1032 #if defined(ETL_POLYMORPHIC_CIRCULAR_BUFFER) || defined(ETL_POLYMORPHIC_CONTAINERS)
1049 template <
typename T,
size_t MAX_SIZE_>
1054 ETL_STATIC_ASSERT((MAX_SIZE_ > 0U),
"Zero capacity etl::circular_buffer is not valid");
1070 template <
typename TIterator>
1074 while (first != last)
1076 this->
push(*first++);
1080 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
1087 this->
push(init.begin(), init.end());
1116 #if ETL_CPP11_SUPPORTED
1126 while (itr != other.end())
1128 this->
push(etl::move(*itr));
1143 this->
push(etl::move(*itr));
1170 template <
typename T>
1187 template <
typename TIterator>
1191 while (first != last)
1193 this->
push(*first++);
1197 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
1204 this->
push(init.begin(), init.end());
1233 #if ETL_CPP11_SUPPORTED
1243 while (itr != other.end())
1245 this->
push(etl::move(*itr));
1260 this->
push(etl::move(*itr));
1277 swap(this->pbuffer, other.pbuffer);
1292 #if ETL_CPP17_SUPPORTED
1293 template <
typename T,
typename... Ts>
1294 circular_buffer(T, Ts...)
1295 ->circular_buffer<etl::enable_if_t<(etl::is_same_v<T, Ts> && ...), T>, 1U +
sizeof...(Ts)>;
1301 template <
typename T>
1310 template <
typename T>
1313 return (lhs.size() == rhs.size()) && etl::equal(lhs.
begin(), lhs.
end(), rhs.
begin());
1319 template <
typename T>
1322 return !(lhs == rhs);
Definition: circular_buffer.h:79
size_t size_type
The type used for determining the size of queue.
Definition: circular_buffer.h:83
size_type out
Index to the next read.
Definition: circular_buffer.h:133
ETL_DECLARE_DEBUG_COUNT
Internal debugging.
Definition: circular_buffer.h:134
size_type in
Index to the next write.
Definition: circular_buffer.h:132
Empty exception for the circular_buffer.
Definition: circular_buffer.h:66
Exception for the circular_buffer.
Definition: circular_buffer.h:53
Definition: circular_buffer.h:1172
circular_buffer_ext(TIterator first, const TIterator &last, void *buffer, size_t max_size)
Definition: circular_buffer.h:1188
circular_buffer_ext & operator=(const circular_buffer_ext &other)
Assignment operator.
Definition: circular_buffer.h:1223
circular_buffer_ext(void *buffer, size_t max_size)
Constructor.
Definition: circular_buffer.h:1178
circular_buffer_ext(const circular_buffer_ext &other, void *buffer, size_t max_size)
Copy Constructor.
Definition: circular_buffer.h:1211
void swap(circular_buffer_ext &other)
Swap with another circular buffer.
Definition: circular_buffer.h:1271
~circular_buffer_ext()
Destructor.
Definition: circular_buffer.h:1283
Definition: circular_buffer.h:1051
circular_buffer(const circular_buffer &other)
Copy Constructor.
Definition: circular_buffer.h:1094
circular_buffer & operator=(const circular_buffer &other)
Assignment operator.
Definition: circular_buffer.h:1106
~circular_buffer()
Destructor.
Definition: circular_buffer.h:1155
circular_buffer(TIterator first, const TIterator &last)
Definition: circular_buffer.h:1071
circular_buffer()
Constructor.
Definition: circular_buffer.h:1061
Iterator iterating through the circular buffer.
Definition: circular_buffer.h:429
const_iterator()
Constructor.
Definition: circular_buffer.h:437
const_iterator(const icircular_buffer< T > *picb_, size_type current_)
Protected constructor. Only icircular_buffer can create one.
Definition: circular_buffer.h:673
const_iterator(const typename icircular_buffer::iterator &other)
Copy Constructor from iterator.
Definition: circular_buffer.h:446
friend bool operator!=(const const_iterator &lhs, const const_iterator &rhs)
Inequality operator.
Definition: circular_buffer.h:613
const_iterator & operator--()
Pre-decrement.
Definition: circular_buffer.h:530
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs)
Equality operator.
Definition: circular_buffer.h:605
const_iterator & operator=(const typename icircular_buffer::iterator &other)
Assignment operator.
Definition: circular_buffer.h:464
const_iterator(const const_iterator &other)
Copy Constructor from const iterator.
Definition: circular_buffer.h:455
const_iterator & operator+=(int n)
Add offset.
Definition: circular_buffer.h:560
const_reference operator*() const
Definition: circular_buffer.h:486
const_iterator & operator-=(int n)
Subtract offset.
Definition: circular_buffer.h:573
friend const_iterator operator-(const const_iterator &lhs, int n)
Subtract offset.
Definition: circular_buffer.h:593
const_iterator & operator++()
Pre-increment.
Definition: circular_buffer.h:502
const_pointer operator->() const
-> operator
Definition: circular_buffer.h:494
friend const_iterator operator+(const const_iterator &lhs, int n)
Add offset.
Definition: circular_buffer.h:581
Iterator iterating through the circular buffer.
Definition: circular_buffer.h:160
iterator & operator++()
Pre-increment.
Definition: circular_buffer.h:229
iterator(const icircular_buffer< T > *picb_, size_type current_)
Protected constructor. Only icircular_buffer can create one.
Definition: circular_buffer.h:413
friend bool operator==(const iterator &lhs, const iterator &rhs)
Equality operator.
Definition: circular_buffer.h:332
friend iterator operator-(const iterator &lhs, int n)
Subtract offset.
Definition: circular_buffer.h:320
friend iterator operator+(const iterator &lhs, int n)
Add offset.
Definition: circular_buffer.h:308
iterator & operator+=(int n)
Add offset.
Definition: circular_buffer.h:287
iterator()
Constructor.
Definition: circular_buffer.h:168
friend bool operator!=(const iterator &lhs, const iterator &rhs)
Inequality operator.
Definition: circular_buffer.h:340
iterator & operator=(const iterator &other)
Assignment operator.
Definition: circular_buffer.h:186
reference operator*()
Definition: circular_buffer.h:197
iterator & operator--()
Pre-decrement.
Definition: circular_buffer.h:257
pointer operator->()
-> operator
Definition: circular_buffer.h:213
iterator & operator-=(int n)
Subtract offset.
Definition: circular_buffer.h:300
iterator(const iterator &other)
Copy Constructor.
Definition: circular_buffer.h:177
Definition: circular_buffer.h:142
static difference_type distance(const TIterator1 &range_begin, const TIterator2 &range_end)
Measures the distance between two iterators.
Definition: circular_buffer.h:997
const_reverse_iterator rend() const
Gets a const reverse iterator to the end of the buffer.
Definition: circular_buffer.h:774
void pop()
pop
Definition: circular_buffer.h:911
reverse_iterator rend()
Gets a reverse iterator to the end of the buffer.
Definition: circular_buffer.h:766
reference operator[](int index)
Get a reference to the item.
Definition: circular_buffer.h:834
const_reverse_iterator rbegin() const
Gets a const reverse iterator to the start of the buffer.
Definition: circular_buffer.h:750
void push(TIterator first, const TIterator &last)
Push a buffer from an iterator range.
Definition: circular_buffer.h:900
friend difference_type operator-(const iterator &lhs, const iterator &rhs)
Definition: circular_buffer.h:953
const_iterator end() const
Gets a const iterator to the end of the buffer.
Definition: circular_buffer.h:726
const_reference back() const
Definition: circular_buffer.h:824
iterator end()
Gets an iterator to the end of the buffer.
Definition: circular_buffer.h:718
iterator begin()
Gets an iterator to the start of the buffer.
Definition: circular_buffer.h:694
const_iterator begin() const
Gets a const iterator to the start of the buffer.
Definition: circular_buffer.h:702
const_iterator cbegin() const
Gets a const iterator to the start of the buffer.
Definition: circular_buffer.h:710
const_reverse_iterator crend() const
Gets a const reverse iterator to the end of the buffer.
Definition: circular_buffer.h:782
reference front()
Definition: circular_buffer.h:791
void clear()
Clears the buffer.
Definition: circular_buffer.h:933
reference back()
Definition: circular_buffer.h:813
reverse_iterator rbegin()
Gets a reverse iterator to the start of the buffer.
Definition: circular_buffer.h:742
void pop(size_type n)
pop(n)
Definition: circular_buffer.h:922
void push(const_reference item)
Definition: circular_buffer.h:853
const_iterator cend() const
Gets a const iterator to the end of the buffer.
Definition: circular_buffer.h:734
const_reverse_iterator crbegin() const
Gets a const reverse iterator to the start of the buffer.
Definition: circular_buffer.h:758
static difference_type distance(const TIterator &other)
Measures the distance from the _begin iterator to the specified iterator.
Definition: circular_buffer.h:1009
~icircular_buffer()
Destructor.
Definition: circular_buffer.h:1039
const_reference front() const
Definition: circular_buffer.h:802
Definition: iterator.h:251
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
is_trivially_destructible
Definition: type_traits_generator.h:1171
Definition: absolute.h:37
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633
void swap(etl::circular_buffer_ext< T > &lhs, etl::circular_buffer_ext< T > &rhs)
Template deduction guides.
Definition: circular_buffer.h:1302
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:594
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition: array.h:570
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:582
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:606
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:621
iterator
Definition: iterator.h:422