7#include <initializer_list>
14template<
class T,
size_t N>
class Ring {
18 Ring(std::initializer_list<T> list) { std::copy(list.begin(), list.end(), array_); }
30 T&
front() {
return array_[first_]; }
31 const T&
front()
const {
return array_[first_]; }
34 return array_[(first_ + i) % N];
38 return array_[(first_ + i) % N];
49 auto res = array_[first_];
50 array_[first_] = item;
51 first_ = (first_ + 1) % N;
58 swap(r1.array_, r2.array_);
59 swap(r1.first_, r2.first_);
63 std::array<T, N> array_;
68template<
class T>
class Ring<T, 1> {
72 Ring(std::initializer_list<T> list)
73 : item_(*list.begin()) {}
86 const T&
front()
const {
return item_; }
109 swap(r1.item_, r2.item_);
117template<
class T>
class Ring<T, 2> {
121 Ring(std::initializer_list<T> list) { std::copy(list.begin(), list.end(), array_); }
134 const T&
front()
const {
return array_[0]; }
149 auto res = array_[0];
150 array_[0] = array_[1];
158 swap(r1.array_, r2.array_);
162 std::array<T, 2> array_;
Ring & operator=(Ring other) noexcept
Ring(const Ring &)=default
const T & operator[](size_t i) const
friend void swap(Ring &r1, Ring &r2) noexcept
Ring(std::initializer_list< T > list)
const T & operator[](size_t i) const
Ring & operator=(Ring other) noexcept
friend void swap(Ring &r1, Ring &r2) noexcept
Ring(std::initializer_list< T > list)
A ring buffer with N elements.
Ring & operator=(Ring other) noexcept
const T & operator[](size_t i) const
T put(T item)
Puts item into buffer.
friend void swap(Ring &r1, Ring &r2) noexcept
Ring(std::initializer_list< T > list)