-
Notifications
You must be signed in to change notification settings - Fork 12
/
ring.hpp
357 lines (286 loc) · 7.85 KB
/
ring.hpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// Copyright (c) 2018 Arthur O'Dwyer
// Copyright 2020-2020 by Martin Moene
//
// https://github.com/martinmoene/ring-span-lite
//
// See https://quuxplusone.github.io/blog/2018/05/30/reference-types-with-metadata-cause-problems/
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef NONSTD_RING_LITE_HPP
#define NONSTD_RING_LITE_HPP
#include <nonstd/ring_span.hpp>
#if nsrs_CPP11_OR_GREATER
# include <array>
#endif
namespace nonstd { namespace ring_span_lite {
namespace std11 {
template< class C >
typename C::iterator begin( C & c ) { return c.begin(); }
template< class C >
typename C::iterator end( C & c ) { return c.end(); }
template< typename T, std::size_t N >
T * begin( T (&array)[N] ) { return &array[0]; }
template< typename T, std::size_t N >
T * end( T (&array)[N] ) { return &array[N]; }
} // namespace std11
template< class Q >
struct is_array : std11::false_type {};
template< class T >
struct is_array<T[]> : std11::true_type {};
template< class T, std::size_t N >
struct is_array<T[N]> : std11::true_type {};
template< class Q >
struct is_std_array_oracle : std11::false_type{};
//#if nsrs_HAVE( ARRAY )
#if nsrs_CPP11_OR_GREATER
template< class T, std::size_t Extent >
struct is_std_array_oracle< std::array<T, Extent> > : std11::true_type{};
#endif
template< class Q >
struct is_std_array : is_std_array_oracle< Q >{};
template< typename Container >
struct vt
{
typedef typename Container::value_type value_type;
};
template< typename T, std::size_t N >
struct vt< T[N] >
{
typedef T value_type;
};
template
<
typename Container /*= std::vector<T>*/
#if nsrs_RING_SPAN_LITE_EXTENSION
, bool CapacityIsPowerOf2 = false
#endif
>
class ring
{
public:
#if nsrs_RING_SPAN_LITE_EXTENSION
typedef ring_span<
typename vt<Container>::value_type
, default_popper<typename vt<Container>::value_type>
, CapacityIsPowerOf2
> RingSpan;
#else
typedef ring_span< typename vt<Container>::value_type > RingSpan;
#endif
typedef typename RingSpan::value_type value_type;
typedef typename RingSpan::size_type size_type;
typedef typename RingSpan::reference reference;
typedef typename RingSpan::const_reference const_reference;
typedef typename RingSpan::iterator iterator;
typedef typename RingSpan::const_iterator const_iterator;
#if nsrs_RING_SPAN_LITE_EXTENSION
typedef typename RingSpan::reverse_iterator reverse_iterator;
typedef typename RingSpan::const_reverse_iterator const_reverse_iterator;
#endif
// construct from C-Array, std::array:
nsrs_REQUIRES_0((
is_array<Container>::value
|| is_std_array<Container>::value
))
explicit ring()
: cont()
, rs( std11::begin(cont), std11::end(cont) )
{}
// construct from container not being C-Array or std::array:
nsrs_REQUIRES_0((
!is_array<Container>::value
&& !is_std_array<Container>::value
))
explicit ring( size_type capacity )
: cont( capacity )
, rs( cont.begin(), cont.end() )
{}
// observers:
bool empty() const nsrs_noexcept
{
return rs.empty();
}
bool full() const nsrs_noexcept
{
return rs.full();
}
size_type size() const nsrs_noexcept
{
return rs.size();
}
size_type capacity() const nsrs_noexcept
{
return rs.capacity();
}
// element access:
reference front() nsrs_noexcept
{
return rs.front();
}
const_reference front() const nsrs_noexcept
{
return rs.front();
}
reference back() nsrs_noexcept
{
return rs.back();
}
const_reference back() const nsrs_noexcept
{
return rs.back();
}
// iteration:
iterator begin() nsrs_noexcept
{
return rs.begin();
}
const_iterator begin() const nsrs_noexcept
{
return rs.begin();
}
const_iterator cbegin() const nsrs_noexcept
{
return rs.cbegin();
}
iterator end() nsrs_noexcept
{
return rs.end();
}
const_iterator end() const nsrs_noexcept
{
return rs.end();
}
const_iterator cend() const nsrs_noexcept
{
return rs.cend();
}
#if nsrs_RING_SPAN_LITE_EXTENSION
reverse_iterator rbegin() nsrs_noexcept
{
return rs.rbegin();
}
reverse_iterator rend() nsrs_noexcept
{
return rs.rend();
}
const_reverse_iterator rbegin() const nsrs_noexcept
{
return rs.rbegin();
}
const_reverse_iterator rend() const nsrs_noexcept
{
return rs.rend();
}
const_reverse_iterator crbegin() const nsrs_noexcept
{
return rs.crbegin();
}
const_reverse_iterator crend() const nsrs_noexcept
{
return rs.crend();
}
#endif
#if nsrs_RING_SPAN_LITE_EXTENSION
reference operator[]( size_type idx ) nsrs_noexcept
{
return rs[ idx ];
}
const_reference operator[]( size_type idx ) const nsrs_noexcept
{
return rs[ idx ];
}
#endif
// element insertion, extraction:
value_type pop_front()
{
return rs.pop_front();
}
#if nsrs_RING_SPAN_LITE_EXTENSION
value_type pop_back()
{
return rs.pop_back();
}
#endif
#if nsrs_CPP11_OR_GREATER
nsrs_REQUIRES_0( std::is_copy_assignable<value_type>::value )
void push_back( value_type const & value) noexcept( std::is_nothrow_copy_assignable<value_type>::value )
#else
void push_back( value_type const & value )
#endif
{
rs.push_back( value );
}
#if nsrs_CPP11_OR_GREATER
nsrs_REQUIRES_0( std::is_move_assignable<value_type>::value )
void push_back( value_type && value ) noexcept( std::is_nothrow_move_assignable<value_type>::value )
{
rs.push_back( std::move( value ) );
}
template< typename... Args
nsrs_REQUIRES_T(
std::is_constructible<value_type, Args&&...>::value
&& std::is_move_assignable<value_type>::value
)
>
void emplace_back( Args &&... args ) noexcept
(
std::is_nothrow_constructible<value_type, Args...>::value
&& std::is_nothrow_move_assignable<value_type>::value
)
{
rs.emplace_back( std::forward<Args>(args)... );
}
#endif
#if nsrs_RING_SPAN_LITE_EXTENSION
#if nsrs_CPP11_OR_GREATER
nsrs_REQUIRES_0( std::is_copy_assignable<value_type>::value )
void push_front( value_type const & value ) noexcept(( std::is_nothrow_copy_assignable<value_type>::value ))
#else
void push_front( value_type const & value )
#endif
{
rs.push_front( value );
}
#if nsrs_CPP11_OR_GREATER
nsrs_REQUIRES_0( std::is_move_assignable<value_type>::value )
void push_front( value_type && value ) noexcept(( std::is_nothrow_move_assignable<value_type>::value ))
{
rs.push_front( std::move( value ) );
}
template< typename... Args
nsrs_REQUIRES_T(
std::is_constructible<value_type, Args&&...>::value
&& std::is_move_assignable<value_type>::value
)
>
void emplace_front( Args&&... args ) noexcept
(
std::is_nothrow_constructible<value_type, Args...>::value
&& std::is_nothrow_move_assignable<value_type>::value
)
{
rs.emplace_front( std::forward<Args>(args)... );
}
#endif
#endif // nsrs_RING_SPAN_LITE_EXTENSION
// swap:
void swap( ring & rhs )
#if nsrs_CPP11_OR_GREATER
noexcept(
nonstd::ring_span_lite::std17::is_nothrow_swappable<typename RingSpan::Popper>::value
)
#endif
{
rhs.swap( *this );
}
private:
Container cont;
RingSpan rs;
};
} // namespace ring_span_lite
// Make types available in namespace nonstd:
using ring_span_lite::ring;
} // namespace nonstd
#endif // NONSTD_RING_LITE_HPP