-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp-test.cpp
301 lines (270 loc) · 9.22 KB
/
temp-test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <concepts>
#include <iterator>
#include <compare>
// #include <iterator_concept>
template<typename T>
struct my_iterator
{
using iterator_category = std::random_access_iterator_tag; //bidirectional_iterator_tag;
using difference_type = ssize_t;
using value_type = T;
// using pointer = T*;
// using reference = T&;
///
/// input_iterator
///
T& operator*()const{
return data_begin_[index_];
}
///
/// forward_iterator
///
my_iterator& operator++(){
index_ = ++index_ < capacity_ ?: 0;
return *this;
}
my_iterator operator++(int){
auto copy = *this;
++*this;
return copy;
}
///
/// bidirectional_iterator
///
my_iterator& operator--(){
// index_ = index_ > 0 ? index_ - 1 : capacity_ - 1;
// index_ = index_ == 0 ? capacity_ :;
--index_;
index_ = std::min(index_,capacity_-1);
return *this;
}
my_iterator operator--(int){
auto copy = *this;
--*this;
return copy;
}
///
/// std::random_access_iterator
///
my_iterator& operator+=(difference_type n){
index_ += n;
if(index_ >= capacity_)
index_ -= capacity_;
return *this;
}
my_iterator operator+(difference_type n)const{
auto copy = *this;
return copy += n;
}
friend my_iterator operator+(difference_type n, my_iterator const& i){
return i+n;
}
T& operator[](difference_type n)const{ //const std::iter_difference_t<I> n){
return *(*this + n);
}
my_iterator& operator-=(difference_type n){
index_ -= n;
if(index_ >= capacity_)
index_ += capacity_;
return *this;
}
my_iterator operator-(difference_type n)const{
auto copy = *this;
return copy-=n;
}
difference_type operator-(my_iterator const& i) const{
return this->index_ - i.index_;
}
///
/// equality_comparable
///
auto operator<=>(const my_iterator&) const = default;
// bool operator==(const my_iterator& other)const{
// return pointer == other.pointer;
// }
// bool operator!=(const my_iterator& other)const{
// return pointer != other.pointer;
// }
private:
// T*pointer,*databegin_,*dataend_;
T*data_begin_;
size_t index_, capacity_;
};
// template<class T> constexpr bool always_false = false;
// static_assert(always_false<std::iter_reference_t<my_iterator<int>>>, "asd");
// typedef typename std::iter_reference_t<my_iterator<int>>::something_made_up X;
static_assert(std::weakly_incrementable<my_iterator<int>>);
static_assert(std::equality_comparable<my_iterator<int>>);
static_assert(std::input_iterator<my_iterator<int>>);
static_assert(std::forward_iterator<my_iterator<int>>);
static_assert(std::bidirectional_iterator<my_iterator<int>>);
static_assert(std::random_access_iterator<my_iterator<int>>);
template<typename T, typename A=std::allocator<T>>
struct ring_buffer {
using container = ring_buffer<T,A>;
using value_type = T;
using allocator_type = A;
using size_type = std::size_t;
using reference = T&;
using const_reference = const T&;
using pointer = std::allocator_traits<A>::pointer;
using const_pointer = std::allocator_traits<A>::const_pointer;
using iterator = my_iterator<T>;
using const_iterator = my_iterator<const T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static_assert(std::is_convertible_v<iterator,const_iterator>);
using difference_type = iterator::difference_type;
constexpr ring_buffer() = default;
constexpr ring_buffer(ring_buffer const&) = default;
constexpr ring_buffer(ring_buffer &&) = default;
constexpr ring_buffer& operator=(ring_buffer const&);
constexpr ring_buffer& operator=(ring_buffer &&);
constexpr ~ring_buffer();
/// Alias to operator=
template<typename...Ts>
constexpr void assign(Ts&&...vs){ *this = std::forward<Ts...>(vs...); }
constexpr allocator_type get_allocator() const noexcept{ return allocator_; }
///
/// Element access
///
// constexpr reference at( size_type pos );
// constexpr const_reference at( size_type pos ) const;
// constexpr reference operator[]( size_type pos );
// constexpr const_reference operator[]( size_type pos ) const;
// constexpr reference front();
// constexpr const_reference front() const;
// constexpr reference back();
// constexpr const_reference back() const;
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
constexpr reference at(size_type pos){
if(pos < size())
return *this[pos];
else
throw std::out_of_range();
}
constexpr reference operator[](size_type pos){
return *(begin() + pos);
}
constexpr const_reference at(size_type pos)const{
if(pos < size())
return *this[pos];
else
throw std::out_of_range();
}
constexpr const_reference operator[](size_type pos)const{
return *(begin() + pos);
}
constexpr reference front() { return *this[0]; }
constexpr const_reference front() const{ return *this[0]; }
constexpr reference back() { return *(this->end()-1); }
constexpr const_reference back() const { return *(this->end()-1); }
// constexpr T* data() noexcept;
// constexpr const T* data() const noexcept;
///
/// Iterators
///
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept { const_cast<const ring_buffer<T>&>(*this).begin(); }
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept { const_cast<const ring_buffer<T>&>(*this).end(); }
constexpr reverse_iterator rbegin() noexcept;
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr reverse_iterator rend() noexcept;
constexpr const_reverse_iterator rend() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;
///
/// Capacity
///
[[nodiscard]] constexpr bool empty() const noexcept {
return size_ == 0; //begin() == end();
}
constexpr size_type size() const noexcept{
return size_;
}
constexpr size_type max_size() const noexcept{
return std::numeric_limits<size_type>::max();
}
constexpr void reserve( size_type new_cap ){
if(new_cap > capacity()){
newdata = allocator_.allocate(new_cap);
std::move(begin(),end(),newdata());
///note: see https://stackoverflow.com/questions/65022554/why-is-the-copy-ctor-preferred-over-move-ctor-when-stdvector-relocates-storage
}
}
constexpr size_type capacity() const noexcept;
constexpr void shrink_to_fit();
///
/// Modifiers
///
constexpr void clear() noexcept;
constexpr iterator insert( const_iterator pos, const T& value );
constexpr iterator insert( const_iterator pos, T&& value );
constexpr iterator insert( const_iterator pos, size_type count, const T& value );
template< class InputIt >
constexpr iterator insert( const_iterator pos, InputIt first, InputIt last );
constexpr iterator insert( const_iterator pos, std::initializer_list<T> ilist );
template< class... Args >
constexpr iterator emplace( const_iterator pos, Args&&... args );
constexpr iterator erase( const_iterator pos );
constexpr iterator erase( const_iterator first, const_iterator last );
constexpr void push_back( const T& value ){
if(size()==capacity()){
reserve(capacity*2);
}
}
constexpr void push_back( T&& value );
template< class... Args >
constexpr reference emplace_back( Args&&... args );
constexpr void pop_back();
constexpr void resize( size_type count );
constexpr void resize( size_type count, const value_type& value );
constexpr void swap( container& other ) noexcept/*( see below )*/;
/// EqualityComparable
bool operator==(ring_buffer const& other)const{
// does it check ranges sizes are equal
return std::equal(this->begin(), this->end(), other.begin(), other.end());
}
bool operator!=(ring_buffer const& other)const{
return !(*this == other);
}
// size_type size()const{
// return std::distance(this->begin(), this->end());
// }
// size_type max_size()const{
// return std::numeric_limits<size_type>::max();
// }
private:
T* data_;
A allocator_;
};
// static_assert(std::container<ring_buffer<int>>);
#include<vector>
#include<iostream>
using namespace std;
struct A{
A(const A& a)noexcept{ i=a.i; cerr<<"copied "<<i<<endl; }
A( A&& a)noexcept{ i=a.i; cerr<<"moved "<<i<<endl; }
A(int i)noexcept:i(i){cerr<<"created "<<i<<endl;};
A()noexcept{};
private:
int i=0;
};
static_assert(not std::is_trivial_v<A>);
int main()
{
vector<A> v1;
size_t prevcap=v1.capacity();
for(int i=0; i<10; ++i){
v1.emplace_back(i);
if(prevcap!=v1.capacity()){
cerr<<"capacity increased to "<<v1.capacity()<<endl;
prevcap=v1.capacity();
}
cerr<<"------"<<endl;
}
}