├── LICENSE ├── README.md ├── fixed_ring.h ├── fixed_ring.test.cc ├── heap_span.h ├── p0059r0.pdf ├── p0059r1.pdf ├── ring_span.h ├── ring_view.test.cc └── test ├── compiles_at_all.cc ├── is_default_constructible.cc ├── moveonly_type.cc ├── p0059r1.cc └── run-all-tests.sh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Arthur O'Dwyer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # std::ring_span 2 | 3 | This code came out of the SG14 working meeting at CppCon 2015. 4 | Guy Davidson proposed `std::fixed_ring` and `std::dynamic_ring`: 5 | 6 | - [P0059R0 "A proposal to add a ring adaptor to the standard library"](p0059r0.pdf) 7 | 8 | This code proposes just a single, lower-level primitive: `std::ring_span`, 9 | as described in the current revision of P0059: 10 | 11 | - [P0059R1 "A proposal to add a ring span to the standard library"](p0059r1.pdf) 12 | 13 | A `ring_span` doesn't manage its own memory nor its own objects; it is 14 | merely a non-owning (yet mutable) *view* onto an array of existing objects, 15 | providing the following facilities: 16 | 17 | - `push_back()` assigns a new value to *end() and increments end(). 18 | If the ring-buffer is at capacity, this will cause begin() also to be 19 | incremented; that is, the oldest item in the ring-buffer will be 20 | forgotten. Regardless, push_back() invalidates iterators. 21 | 22 | - `pop_front()` performs a customizable action and increments begin(). 23 | It also invalidates iterators. 24 | 25 | - Iteration of the whole buffer (from front to back) is possible 26 | via the usual `begin()` and `end()` iterators, or via range-based for loop. 27 | 28 | - The `ring_span` itself is a lightweight value type; you can copy it 29 | to get a second (equivalent) view of the same objects. As with `array_view` 30 | and `string_view`, any operation that invalidates a pointer in the range 31 | on which a view was created invalidates pointers and references returned 32 | from the view's functions. 33 | 34 | For the `array_view` proposal, see 35 | [N4177 "Multidimensional bounds, index and array_view"](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4177.html) 36 | -------------------------------------------------------------------------------- /fixed_ring.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ring_view.h" 4 | #include 5 | #include 6 | 7 | // An implementation of fixed_ring as specified in Guy Davidson's 8 | // P0059R0 "A proposal to add a ring adaptor to the standard library". 9 | // 10 | template> 11 | class fixed_ring 12 | { 13 | public: 14 | using container_type = Container; 15 | using value_type = typename Container::value_type; 16 | using size_type = typename Container::size_type; 17 | using reference = typename Container::reference; 18 | using const_reference = typename Container::const_reference; 19 | 20 | // TODO: the proposal doesn't mention these two typedefs 21 | using pointer = typename Container::pointer; 22 | using const_pointer = typename Container::const_pointer; 23 | 24 | // TODO: the proposal mentions these four typedefs, but never uses them, 25 | // since fixed_ring is not iterable. 26 | using iterator = typename Container::iterator; 27 | using const_iterator = typename Container::const_iterator; 28 | using reverse_iterator = typename Container::reverse_iterator; 29 | using const_reverse_iterator = typename Container::const_reverse_iterator; 30 | 31 | fixed_ring() noexcept(std::is_nothrow_default_constructible::value) : 32 | // TODO: proposal says is_nothrow_default_constructible 33 | ctr_(), 34 | rv_(ctr_.begin(), ctr_.end(), ctr_.begin(), 0) 35 | {} 36 | 37 | // TODO: this should probably be disabled if T isn't copy-constructible 38 | fixed_ring(const fixed_ring& rhs) noexcept(std::is_nothrow_copy_constructible::value) : 39 | fixed_ring(&*rhs.rv_.begin() - rhs.ctr_.begin(), rhs.rv_.size(), rhs.ctr_) 40 | {} 41 | 42 | // TODO: this should probably be disabled if T isn't move-constructible 43 | // TODO: this move-constructs a whole new std::array, an expensive default behavior IMHO 44 | fixed_ring(fixed_ring&& rhs) noexcept(std::is_nothrow_move_constructible::value) : 45 | fixed_ring(&*rhs.rv_.begin() - rhs.ctr_.begin(), rhs.rv_.size(), std::move(rhs.ctr_)) 46 | {} 47 | 48 | fixed_ring(const container_type& rhs) noexcept(std::is_nothrow_copy_constructible::value) : 49 | fixed_ring(0, rhs.size(), rhs) 50 | {} 51 | 52 | fixed_ring(container_type&& rhs) noexcept(std::is_nothrow_move_constructible::value) : 53 | fixed_ring(0, rhs.size(), std::move(rhs)) 54 | {} 55 | 56 | fixed_ring& operator=(const fixed_ring& rhs) noexcept(std::is_nothrow_copy_assignable::value) 57 | { 58 | ctr_ = rhs.ctr_; 59 | auto first_idx = (&*rhs.rv_.begin() - rhs.ctr_.begin()); 60 | rv_ = ring_view(ctr_.begin(), ctr_.end(), ctr_.begin() + first_idx, rhs.rv_.size()); 61 | return *this; 62 | } 63 | 64 | fixed_ring& operator=(fixed_ring&& rhs) noexcept(std::is_nothrow_copy_assignable::value) 65 | { 66 | auto first_idx = (&*rhs.rv_.begin() - rhs.ctr_.begin()); 67 | ctr_ = std::move(rhs.ctr_); 68 | rv_ = ring_view(ctr_.begin(), ctr_.end(), ctr_.begin() + first_idx, rhs.rv_.size()); 69 | return *this; 70 | } 71 | 72 | void push(const value_type& v) { rv_.push_back(v); } 73 | void push(value_type&& v) { rv_.push_back(std::move(v)); } 74 | 75 | template bool emplace(Args&&... args) 76 | { 77 | rv_.push(T(std::forward(args)...)); 78 | return true; // TODO: what are the semantics of this return value? 79 | } 80 | 81 | bool try_push(const value_type& v) { return rv_.full() ? false : (rv_.push_back(v), true); } 82 | bool try_push(value_type&& v) { return rv_.full() ? false : (rv_.push_back(std::move(v)), true); } 83 | 84 | template 85 | bool try_emplace(Args&&... args) { return rv_.full() ? false : (rv_.push_back(T(std::forward(args)...)), true); } 86 | 87 | void pop() noexcept { rv_.pop_front(); } 88 | bool empty() const noexcept { return rv_.empty(); } 89 | size_type size() const noexcept { return rv_.size(); } 90 | 91 | // TODO: the proposal doesn't mention this member function 92 | constexpr size_type capacity() const noexcept { return Capacity; } 93 | 94 | reference front() noexcept { return rv_.front(); } 95 | const_reference front() const noexcept { return rv_.front(); } 96 | reference back() noexcept { return rv_.back(); } 97 | const_reference back() const noexcept { return rv_.back(); } 98 | 99 | void swap(fixed_ring& rhs) noexcept 100 | { 101 | using std::swap; 102 | swap(ctr_, rhs.ctr_); 103 | swap(rv_, rhs.rv_); 104 | } 105 | 106 | private: 107 | template 108 | fixed_ring(size_type first_idx, size_type size, Args&&... args) noexcept(Container(std::forward(args)...)) : 109 | ctr_(std::forward(args)...), 110 | rv_(ctr_.begin(), ctr_.end(), ctr_.begin() + first_idx, size) 111 | {} 112 | 113 | Container ctr_; 114 | ring_view rv_; 115 | }; 116 | -------------------------------------------------------------------------------- /fixed_ring.test.cc: -------------------------------------------------------------------------------- 1 | #include "fixed_ring.h" 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | fixed_ring fr; 8 | assert(fr.size() == 0); 9 | fr.push(1); 10 | assert(fr.size() == 1); assert(fr.front() == 1); assert(fr.back() == 1); 11 | fr.push(2); 12 | assert(fr.size() == 2); assert(fr.front() == 1); assert(fr.back() == 2); 13 | fr.push(3); 14 | assert(fr.size() == 3); assert(fr.front() == 1); assert(fr.back() == 3); 15 | fr.push(4); 16 | assert(fr.size() == 4); assert(fr.front() == 1); assert(fr.back() == 4); 17 | fr.push(5); 18 | assert(fr.size() == 4); assert(fr.front() == 2); assert(fr.back() == 5); 19 | fr.push(6); 20 | assert(fr.size() == 4); assert(fr.front() == 3); assert(fr.back() == 6); 21 | fr.pop(); 22 | assert(fr.size() == 3); assert(fr.front() == 4); assert(fr.back() == 6); 23 | } 24 | -------------------------------------------------------------------------------- /heap_span.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace std { namespace experimental { 11 | 12 | template> 13 | class heap_span 14 | { 15 | public: 16 | using type = heap_span; 17 | using value_type = T; 18 | using pointer = T*; 19 | using reference = T&; 20 | using const_reference = const T&; 21 | using size_type = std::size_t; 22 | 23 | heap_span() = default; 24 | 25 | template 26 | heap_span(ContiguousIterator begin, ContiguousIterator end, Comparator cmp = Comparator()) noexcept : 27 | data_(&*begin), 28 | size_(end - begin), 29 | capacity_(end - begin), 30 | less_(std::move(cmp)) 31 | {} 32 | 33 | template 34 | heap_span(ContiguousIterator begin, ContiguousIterator end, size_type size) noexcept : 35 | data_(&*begin), 36 | size_(size), 37 | capacity_(end - begin) 38 | {} 39 | 40 | reference top() noexcept { return data_[0]; } 41 | const_reference top() const noexcept { return data_[0]; } 42 | 43 | bool empty() const noexcept { return size_ == 0; } 44 | bool full() const noexcept { return size_ == capacity_; } 45 | size_type size() const noexcept { return size_; } 46 | size_type capacity() const noexcept { return capacity_; } 47 | 48 | void pop() 49 | { 50 | assert(not empty()); 51 | if (size_ == 1) { 52 | size_ = 0; 53 | } else { 54 | data_[0] = std::move(data_[size_ - 1]); 55 | --size_; 56 | heapify_down_(); 57 | } 58 | } 59 | 60 | template::value>> 61 | void push(const T& value) noexcept(std::is_nothrow_copy_assignable::value) 62 | { 63 | if (full()) { 64 | data_[0] = value; 65 | heapify_down_(); 66 | } else { 67 | data_[size_++] = value; 68 | heapify_up_(); 69 | } 70 | } 71 | 72 | template::value>> 73 | void push(T&& value) noexcept(std::is_nothrow_move_assignable::value) 74 | { 75 | if (full()) { 76 | data_[0] = std::move(value); 77 | heapify_down_(); 78 | } else { 79 | data_[size_++] = std::move(value); 80 | heapify_up_(); 81 | } 82 | } 83 | 84 | template 85 | void emplace(Args&&... args) noexcept(std::is_nothrow_constructible::value && std::is_nothrow_move_assignable::value) 86 | { 87 | if (full()) { 88 | data_[0] = T(std::forward(args)...); 89 | heapify_down_(); 90 | } else { 91 | data_[size_++] = T(std::forward(args)...); 92 | heapify_up_(); 93 | } 94 | } 95 | 96 | void sort() 97 | { 98 | // for the sake of argument 99 | std::sort(data_, data_ + size_, less_); 100 | } 101 | 102 | void make_heap() 103 | { 104 | heapify_(); 105 | } 106 | 107 | void swap(heap_span& rhs) noexcept(std::__is_nothrow_swappable::value) 108 | { 109 | using std::swap; 110 | swap(data_, rhs.data_); 111 | swap(size_, rhs.size_); 112 | swap(capacity_, rhs.capacity_); 113 | swap(less_, rhs.less_); 114 | } 115 | 116 | friend void swap(heap_span& lhs, heap_span& rhs) noexcept(noexcept(lhs.swap(rhs))) 117 | { 118 | lhs.swap(rhs); 119 | } 120 | 121 | private: 122 | // all private members are exposition-only 123 | 124 | static size_type parent_(size_type child) { return (child - 1) / 2; } 125 | static size_type left_child_(size_type parent) { return parent * 2 + 1; } 126 | static size_type right_child_(size_type parent) { return parent * 2 + 2; } 127 | 128 | void heapify_() 129 | { 130 | const size_type final_size = size_; 131 | size_ = 0; 132 | while (size_ != final_size) { 133 | ++size_; 134 | heapify_up_(); 135 | } 136 | } 137 | 138 | void heapify_up_() 139 | { 140 | using std::swap; 141 | assert(size_ >= 1); 142 | size_type idx = size_ - 1; 143 | while (idx != 0) { 144 | size_type parent = parent_(idx); 145 | if (less_(data_[idx], data_[parent])) { 146 | swap(data_[idx], data_[parent]); 147 | idx = parent; 148 | } else { 149 | return; 150 | } 151 | } 152 | } 153 | 154 | void heapify_down_() 155 | { 156 | using std::swap; 157 | size_type idx = 0; 158 | while (right_child_(idx) < size_) { 159 | size_type left = left_child_(idx); 160 | size_type right = right_child_(idx); 161 | if (less_(data_[left], data_[idx])) { 162 | if (less_(data_[right], data_[left])) { 163 | swap(data_[idx], data_[right]); 164 | idx = right; 165 | } else { 166 | swap(data_[idx], data_[left]); 167 | idx = left; 168 | } 169 | } else if (less_(data_[right], data_[idx])) { 170 | swap(data_[idx], data_[right]); 171 | idx = right; 172 | } else { 173 | return; 174 | } 175 | } 176 | if (left_child_(idx) < size_) { 177 | if (less_(data_[left_child_(idx)], data_[idx])) { 178 | swap(data_[idx], data_[left_child_(idx)]); 179 | } 180 | } 181 | } 182 | 183 | T *data_; 184 | size_type size_; 185 | size_type capacity_; 186 | Comparator less_; 187 | }; 188 | 189 | } } // namespace std::experimental 190 | -------------------------------------------------------------------------------- /p0059r0.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quuxplusone/ring_view/6b6ffb9b08367ea1ec141f18c4ad14818693dd33/p0059r0.pdf -------------------------------------------------------------------------------- /p0059r1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quuxplusone/ring_view/6b6ffb9b08367ea1ec141f18c4ad14818693dd33/p0059r1.pdf -------------------------------------------------------------------------------- /ring_span.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Reference implementation of P0059R1 + errata. 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace std { namespace experimental { 12 | 13 | namespace detail { 14 | template class ring_iterator; 15 | } // namespace detail 16 | 17 | template 18 | struct null_popper { 19 | void operator()(T&) { } 20 | }; 21 | 22 | template 23 | struct move_popper { 24 | T operator()(T& t) { return std::move(t); } 25 | }; 26 | 27 | template> 28 | class ring_span 29 | { 30 | public: 31 | using type = ring_span; 32 | using value_type = T; 33 | using pointer = T*; 34 | using reference = T&; 35 | using const_reference = const T&; 36 | using size_type = std::size_t; 37 | using iterator = detail::ring_iterator; // exposition only 38 | using const_iterator = detail::ring_iterator; // exposition only 39 | using reverse_iterator = std::reverse_iterator; 40 | using const_reverse_iterator = std::reverse_iterator; 41 | 42 | ring_span() = default; 43 | 44 | template 45 | ring_span(ContiguousIterator begin, ContiguousIterator end, Popper p = Popper()) noexcept : 46 | data_(&*begin), 47 | size_(end - begin), 48 | capacity_(end - begin), 49 | front_idx_(0), 50 | popper_(std::move(p)) 51 | {} 52 | 53 | template 54 | ring_span(ContiguousIterator begin, ContiguousIterator end, ContiguousIterator first, size_type size, Popper p = Popper()) noexcept : 55 | data_(&*begin), 56 | size_(size), 57 | capacity_(end - begin), 58 | front_idx_(first - begin), 59 | popper_(std::move(p)) 60 | {} 61 | 62 | iterator begin() noexcept { return iterator(0, this); } 63 | iterator end() noexcept { return iterator(size(), this); } 64 | const_iterator begin() const noexcept { return cbegin(); } 65 | const_iterator end() const noexcept { return cend(); } 66 | const_iterator cbegin() const noexcept { return const_iterator(0, this); } 67 | const_iterator cend() const noexcept { return const_iterator(size(), this); } 68 | 69 | reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } 70 | reverse_iterator rend() noexcept { return reverse_iterator(begin()); } 71 | const_reverse_iterator rbegin() const noexcept { return crbegin(); } 72 | const_reverse_iterator rend() const noexcept { return crend(); } 73 | const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); } 74 | const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); } 75 | 76 | reference front() noexcept { return *begin(); } 77 | reference back() noexcept { return *(end() - 1); } 78 | const_reference front() const noexcept { return *begin(); } 79 | const_reference back() const noexcept { return *(end() - 1); } 80 | 81 | bool empty() const noexcept { return size_ == 0; } 82 | bool full() const noexcept { return size_ == capacity_; } 83 | size_type size() const noexcept { return size_; } 84 | size_type capacity() const noexcept { return capacity_; } 85 | 86 | auto pop_front() 87 | { 88 | assert(not empty()); 89 | auto& elt = front_(); 90 | increment_front_(); 91 | return popper_(elt); 92 | } 93 | 94 | auto pop_back() 95 | { 96 | assert(not empty()); 97 | auto& elt = back_(); 98 | decrement_back_(); 99 | return popper_(elt); 100 | } 101 | 102 | template::value>> 103 | void push_back(const T& value) noexcept(std::is_nothrow_copy_assignable::value) 104 | { 105 | if (full()) { 106 | increment_front_and_back_(); 107 | } else { 108 | increment_back_(); 109 | } 110 | back_() = value; 111 | } 112 | 113 | template::value>> 114 | void push_back(T&& value) noexcept(std::is_nothrow_move_assignable::value) 115 | { 116 | if (full()) { 117 | increment_front_and_back_(); 118 | } else { 119 | increment_back_(); 120 | } 121 | back_() = std::move(value); 122 | } 123 | 124 | template 125 | void emplace_back(Args&&... args) noexcept(std::is_nothrow_constructible::value && std::is_nothrow_move_assignable::value) 126 | { 127 | if (full()) { 128 | increment_front_and_back_(); 129 | } else { 130 | increment_back_(); 131 | } 132 | back_() = T(std::forward(args)...); 133 | } 134 | 135 | template::value>> 136 | void push_front(const T& value) noexcept(std::is_nothrow_copy_assignable::value) 137 | { 138 | if (full()) { 139 | decrement_front_and_back_(); 140 | } else { 141 | decrement_front_(); 142 | } 143 | front_() = value; 144 | } 145 | 146 | template::value>> 147 | void push_front(T&& value) noexcept(std::is_nothrow_move_assignable::value) 148 | { 149 | if (full()) { 150 | decrement_front_and_back_(); 151 | } else { 152 | decrement_front_(); 153 | } 154 | front_() = std::move(value); 155 | } 156 | 157 | template 158 | void emplace_front(Args&&... args) noexcept(std::is_nothrow_constructible::value && std::is_nothrow_move_assignable::value) 159 | { 160 | if (full()) { 161 | decrement_front_and_back_(); 162 | } else { 163 | decrement_front_(); 164 | } 165 | front_() = T(std::forward(args)...); 166 | } 167 | 168 | void swap(ring_span& rhs) noexcept(std::__is_nothrow_swappable::value) 169 | { 170 | using std::swap; 171 | swap(data_, rhs.data_); 172 | swap(size_, rhs.size_); 173 | swap(capacity_, rhs.capacity_); 174 | swap(front_idx_, rhs.front_idx_); 175 | swap(popper_, rhs.popper_); 176 | } 177 | 178 | friend void swap(ring_span& lhs, ring_span& rhs) noexcept(noexcept(lhs.swap(rhs))) 179 | { 180 | lhs.swap(rhs); 181 | } 182 | 183 | private: 184 | // all private members are exposition-only 185 | 186 | friend class detail::ring_iterator; 187 | friend class detail::ring_iterator; 188 | 189 | reference at(size_type i) noexcept { return data_[(front_idx_ + i) % capacity_]; } 190 | const_reference at(size_type i) const noexcept { return data_[(front_idx_ + i) % capacity_]; } 191 | 192 | reference front_() noexcept { return *(data_ + front_idx_); } 193 | const_reference front_() const noexcept { return *(data_ + front_idx_); } 194 | reference back_() noexcept { return *(data_ + (front_idx_ + size_ - 1) % capacity_); } 195 | const_reference back_() const noexcept { return *(data_ + (front_idx_ + size_ - 1) % capacity_); } 196 | 197 | void increment_front_() noexcept { 198 | front_idx_ = (front_idx_ + 1) % capacity_; 199 | --size_; 200 | } 201 | void decrement_front_() noexcept { 202 | front_idx_ = (front_idx_ + capacity_ - 1) % capacity_; 203 | ++size_; 204 | } 205 | 206 | void increment_back_() noexcept { 207 | ++size_; 208 | } 209 | void decrement_back_() noexcept { 210 | --size_; 211 | } 212 | 213 | void increment_front_and_back_() noexcept { 214 | front_idx_ = (front_idx_ + 1) % capacity_; 215 | } 216 | void decrement_front_and_back_() noexcept { 217 | front_idx_ = (front_idx_ + capacity_ - 1) % capacity_; 218 | } 219 | 220 | T *data_; 221 | size_type size_; 222 | size_type capacity_; 223 | size_type front_idx_; 224 | Popper popper_; 225 | }; 226 | 227 | namespace detail { 228 | 229 | template 230 | class ring_iterator 231 | { 232 | public: 233 | using type = ring_iterator; 234 | using value_type = typename RV::value_type; 235 | using difference_type = std::ptrdiff_t; 236 | using pointer = typename std::conditional_t*; 237 | using reference = typename std::conditional_t&; 238 | using iterator_category = std::random_access_iterator_tag; 239 | 240 | ring_iterator() = default; 241 | 242 | reference operator*() const noexcept { return rv_->at(idx_); } 243 | ring_iterator& operator++() noexcept { ++idx_; return *this; } 244 | ring_iterator operator++(int) noexcept { auto r(*this); ++*this; return r; } 245 | ring_iterator& operator--() noexcept { ++idx_; return *this; } 246 | ring_iterator operator--(int) noexcept { auto r(*this); ++*this; return r; } 247 | 248 | friend ring_iterator& operator+=(ring_iterator& it, int i) noexcept { it.idx_ += i; return it; } 249 | friend ring_iterator& operator-=(ring_iterator& it, int i) noexcept { it.idx_ -= i; return it; } 250 | friend ring_iterator operator+(ring_iterator it, int i) noexcept { it += i; return it; } 251 | friend ring_iterator operator-(ring_iterator it, int i) noexcept { it -= i; return it; } 252 | 253 | template bool operator==(const ring_iterator& rhs) const noexcept { return idx_ == rhs.idx_; } 254 | template bool operator!=(const ring_iterator& rhs) const noexcept { return idx_ != rhs.idx_; } 255 | template bool operator<(const ring_iterator& rhs) const noexcept { return idx_ < rhs.idx_; } 256 | template bool operator<=(const ring_iterator& rhs) const noexcept { return idx_ <= rhs.idx_; } 257 | template bool operator>(const ring_iterator& rhs) const noexcept { return idx_ > rhs.idx_; } 258 | template bool operator>=(const ring_iterator& rhs) const noexcept { return idx_ >= rhs.idx_; } 259 | 260 | private: 261 | friend RV; 262 | 263 | using size_type = typename RV::size_type; 264 | 265 | ring_iterator(size_type idx, std::conditional_t *rv) noexcept : idx_(idx), rv_(rv) {} 266 | 267 | size_type idx_; 268 | std::conditional_t *rv_; 269 | }; 270 | 271 | } // namespace detail 272 | 273 | } } // namespace std::experimental 274 | -------------------------------------------------------------------------------- /ring_view.test.cc: -------------------------------------------------------------------------------- 1 | #include "ring_span.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std::experimental; 8 | 9 | using T = std::unique_ptr; 10 | using RVT = ring_span; 11 | 12 | int main() 13 | { 14 | std::vector vec(100); 15 | RVT rv(vec.begin(), vec.end()); 16 | assert(rv.capacity() == 100); 17 | assert(rv.size() == 100); 18 | rv.push_back(nullptr); 19 | assert(rv.capacity() == 100); 20 | assert(rv.size() == 100); 21 | rv.pop_front(); 22 | assert(rv.capacity() == 100); 23 | assert(rv.size() == 99); 24 | 25 | std::queue q(rv); 26 | q.push(nullptr); 27 | q.push(nullptr); 28 | q.front(); 29 | q.back(); 30 | q.pop(); 31 | } 32 | -------------------------------------------------------------------------------- /test/compiles_at_all.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "ring_span.h" 3 | 4 | int main() 5 | { 6 | } 7 | -------------------------------------------------------------------------------- /test/is_default_constructible.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "ring_span.h" 5 | 6 | int main() 7 | { 8 | using std::experimental::ring_span; 9 | 10 | ring_span rv1; 11 | ring_span> rv2; 12 | 13 | std::vector> vec; 14 | } 15 | -------------------------------------------------------------------------------- /test/moveonly_type.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "ring_span.h" 12 | 13 | 14 | std::deque expected_destructions; 15 | struct S { 16 | int val; 17 | S(int i): val(i) {} 18 | S(const S&) = delete; 19 | S(S&&) = delete; 20 | S& operator=(const S&) = delete; 21 | S& operator=(S&&) = delete; 22 | ~S() { 23 | assert(val == expected_destructions.front()); 24 | expected_destructions.pop_front(); 25 | } 26 | }; 27 | 28 | using ptr = std::unique_ptr; 29 | 30 | template 31 | std::string rv_to_string(const RingSpan& rv) 32 | { 33 | std::string result; 34 | for (auto&& elt : rv) { 35 | assert(elt); 36 | result += std::to_string(elt->val); 37 | result += " "; 38 | } 39 | if (not rv.empty()) { 40 | result.resize(result.length() - 1); 41 | } 42 | return result; 43 | } 44 | 45 | template 46 | void assert_is(const RingSpan& rv, const std::string& expected) 47 | { 48 | std::string rvs = rv_to_string(rv); 49 | if (rvs != expected) { 50 | fprintf(stderr, "Failed assert_is: %s != %s\n", rvs.c_str(), expected.c_str()); 51 | exit(1); 52 | } 53 | } 54 | 55 | void test_null_popper() 56 | { 57 | expected_destructions = { 2, 1, 4, 3, 7, 8, 10, 5, 9, 6 }; 58 | std::array buffer; 59 | { 60 | std::experimental::ring_span> rv(buffer.begin(), buffer.end(), buffer.begin(), 0); 61 | rv.push_back(std::make_unique(1)); assert_is(rv, "1"); 62 | rv.push_back(std::make_unique(2)); assert_is(rv, "1 2"); 63 | rv.pop_back(); assert_is(rv, "1"); 64 | rv.pop_front(); assert_is(rv, ""); 65 | rv.push_back(std::make_unique(3)); assert_is(rv, "3"); // (destroys 2) 66 | rv.push_front(std::make_unique(4)); assert_is(rv, "4 3"); // (destroys 1) 67 | rv.push_back(std::make_unique(5)); assert_is(rv, "4 3 5"); 68 | rv.push_back(std::make_unique(6)); assert_is(rv, "4 3 5 6"); 69 | rv.push_back(std::make_unique(7)); assert_is(rv, "3 5 6 7"); 70 | rv.push_back(std::make_unique(8)); assert_is(rv, "5 6 7 8"); 71 | rv.pop_back(); assert_is(rv, "5 6 7"); 72 | rv.pop_back(); assert_is(rv, "5 6"); 73 | rv.push_back(std::make_unique(9)); assert_is(rv, "5 6 9"); // (destroys 7) 74 | rv.push_back(std::make_unique(10)); assert_is(rv, "5 6 9 10"); // (destroys 8) 75 | } 76 | 77 | // Ring test is done. 78 | // Destroy the rest of the buffer elements in a nice defined order. 79 | // The underlying buffer at this point is "9 10 5 6". 80 | buffer[1] = nullptr; // 9 x 5 6 81 | buffer[2] = nullptr; // 9 x x 6 82 | buffer[0] = nullptr; // x x x 6 83 | } 84 | 85 | void test_move_popper() 86 | { 87 | expected_destructions = { 2, 1, 4, 3, 8, 7, 10, 5, 9, 6 }; 88 | std::array buffer; 89 | { 90 | std::experimental::ring_span rv(buffer.begin(), buffer.end(), buffer.begin(), 0); 91 | rv.push_back(std::make_unique(1)); assert_is(rv, "1"); 92 | rv.push_back(std::make_unique(2)); assert_is(rv, "1 2"); 93 | rv.pop_back(); assert_is(rv, "1"); 94 | rv.pop_front(); assert_is(rv, ""); 95 | rv.push_back(std::make_unique(3)); assert_is(rv, "3"); 96 | rv.push_front(std::make_unique(4)); assert_is(rv, "4 3"); 97 | rv.push_back(std::make_unique(5)); assert_is(rv, "4 3 5"); 98 | rv.push_back(std::make_unique(6)); assert_is(rv, "4 3 5 6"); 99 | rv.push_back(std::make_unique(7)); assert_is(rv, "3 5 6 7"); 100 | rv.push_back(std::make_unique(8)); assert_is(rv, "5 6 7 8"); 101 | rv.pop_back(); assert_is(rv, "5 6 7"); 102 | rv.pop_back(); assert_is(rv, "5 6"); 103 | rv.push_back(std::make_unique(9)); assert_is(rv, "5 6 9"); 104 | rv.push_back(std::make_unique(10)); assert_is(rv, "5 6 9 10"); 105 | } 106 | 107 | // Ring test is done. 108 | // Destroy the rest of the buffer elements in a nice defined order. 109 | // The underlying buffer at this point is "9 10 5 6". 110 | buffer[1] = nullptr; // 9 x 5 6 111 | buffer[2] = nullptr; // 9 x x 6 112 | buffer[0] = nullptr; // x x x 6 113 | } 114 | 115 | int main() 116 | { 117 | test_null_popper(); 118 | test_move_popper(); 119 | } 120 | -------------------------------------------------------------------------------- /test/p0059r1.cc: -------------------------------------------------------------------------------- 1 | #include "ring_span.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using std::experimental::ring_span; 10 | 11 | void ring_test() 12 | { 13 | std::array buffer; 14 | auto Q = ring_span(buffer.begin(), buffer.end(), buffer.begin(), 0); 15 | 16 | Q.push_back(7); 17 | Q.push_back(3); 18 | assert(Q.size() == 2); 19 | assert(Q.front() == 7); 20 | 21 | Q.pop_front(); 22 | assert(Q.size() == 1); 23 | 24 | Q.push_back(18); 25 | auto Q2 = Q; 26 | assert(Q2.front() == 3); 27 | assert(Q2.back() == 18); 28 | 29 | auto Q3 = std::move(Q2); 30 | assert(Q3.front() == 3); 31 | assert(Q3.back() == 18); 32 | 33 | auto Q4(Q3); 34 | assert(Q4.front() == 3); 35 | assert(Q4.back() == 18); 36 | 37 | auto Q5(std::move(Q3)); 38 | assert(Q5.front() == 3); 39 | assert(Q5.back() == 18); 40 | assert(Q5.size() == 2); 41 | 42 | Q5.pop_front(); 43 | Q5.pop_front(); 44 | assert(Q5.empty()); 45 | 46 | std::array buffer2; 47 | auto Q6 = ring_span(buffer2.begin(), buffer2.end(), buffer2.begin(), 0); 48 | Q6.push_back(6); 49 | Q6.push_back(7); 50 | Q6.push_back(8); 51 | Q6.push_back(9); 52 | Q6.emplace_back(10); // TODO FIXME BUG HACK: P0059R1 has "emplace", not "emplace_back" 53 | Q6.swap(Q5); 54 | assert(Q6.empty()); 55 | assert(Q5.size() == 5); 56 | assert(Q5.front() == 6); 57 | assert(Q5.back() == 10); 58 | } 59 | 60 | template 61 | void thread_communication_test(std::deque input, std::deque expected_output) 62 | { 63 | assert(BufferSize >= input.size()); 64 | 65 | std::array buffer; 66 | std::mutex m; 67 | std::condition_variable cv; 68 | auto r = ring_span(buffer.begin(), buffer.end(), buffer.begin(), 0); 69 | 70 | auto ci = std::async(std::launch::async, [&]() 71 | { 72 | int val = 0; 73 | do 74 | { 75 | val = input.front(); 76 | input.pop_front(); 77 | { 78 | std::lock_guard lg(m); 79 | r.push_back(val); 80 | cv.notify_one(); 81 | } 82 | } while (val != -1); 83 | }); 84 | 85 | auto po = std::async(std::launch::async, [&]() 86 | { 87 | int val = 0; 88 | do 89 | { 90 | std::unique_lock lk(m); 91 | while (r.empty()) cv.wait(lk); // TODO FIXME BUG HACK: P0059R1 omits this loop 92 | val = r.pop_front(); // TODO FIXME BUG HACK: P0059R1 has "pop", not "pop_front" 93 | assert(val == expected_output.front()); 94 | expected_output.pop_front(); 95 | lk.unlock(); // TODO FIXME BUG HACK: this is unnecessary 96 | } while (val != -1); 97 | }); 98 | } 99 | 100 | int main() 101 | { 102 | ring_test(); 103 | thread_communication_test<10>( 104 | {1,2,3,4,5,4,3,2,7,-1}, 105 | {1,2,3,4,5,4,3,2,7,-1} 106 | ); 107 | } 108 | -------------------------------------------------------------------------------- /test/run-all-tests.sh: -------------------------------------------------------------------------------- 1 | for i in ./*.cc; do 2 | echo $i 3 | g++ -std=c++1y -O3 -I .. $i -o ./a.out 4 | ./a.out 5 | done 6 | --------------------------------------------------------------------------------