-
Notifications
You must be signed in to change notification settings - Fork 26
/
sequence_access.rst
355 lines (216 loc) · 8.42 KB
/
sequence_access.rst
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
Sequence Access
***************
.. namespace:: flux
``first``
---------
.. function::
template <sequence Seq> \
auto first(Seq& seq) -> cursor_t<Seq>;
Returns a :concept:`cursor` to the first element of :var:`seq`.
:complexity:
Constant if :expr:`multipass_sequence<Seq>`, otherwise linear
``is_last``
-----------
.. function::
template <sequence Seq> \
auto is_last(Seq& seq, cursor_t<Seq> const& cur) -> bool;
Returns :texpr:`true` if :var:`cur` is at the past-the-end position of :var:`seq`.
:complexity:
Constant
``last``
--------
.. function::
template <bounded_sequence Seq> \
auto last(Seq& seq) -> cursor_t<Seq>;
Returns a :concept:`cursor` which is in the past-the-end position of :var:`seq`.
:expr:`is_last(seq, last(seq))` is always :texpr:`true`.
:complexity:
Constant
``inc``
-------
.. function::
template <sequence Seq> \
auto inc(Seq& seq, cursor_t<Seq>& cur) -> cursor_t<Seq>&;
Advances the cursor by one position.
:returns:
A reference to :var:`cur`
:complexity:
Constant if :expr:`random_access_sequence<Seq>`, otherwise linear.
.. function::
template <random_access_sequence Seq> \
auto inc(Seq& seq, cursor_t<Seq>& cur, distance_t offset) -> cursor_t<Seq>&
Advances the cursor by :var:`offset` positions, or decrements it if :var:`offset` is negative.
:returns:
A reference to :var:`cur`
:complexity:
Constant
``dec``
-------
.. function::
template <bidirectional_sequence Seq> \
auto dec(Seq& seq, cursor_t<Seq>& cur) -> cursor_t<Seq>&;
Decrements the cursor by one position
:returns:
A reference to :var:`cur`
:complexity:
Constant if :expr:`random_access_sequence<Seq>`, otherwise linear
``read_at``
-----------
.. function::
template <sequence Seq> \
auto read_at(Seq& seq, cursor_t<Seq> const& cur) -> element_t<Seq>;
Accesses the element at the given cursor position.
:complexity:
Constant
``read_at_unchecked``
---------------------
.. function::
template <sequence Seq> \
auto read_at_unchecked(Seq& seq, cursor_t<Seq> const& cur) -> element_t<Seq>;
Accesses the element at the given cursor position without performing safety checks.
.. danger:: Using this function can result in undefined behaviour that would otherwise be caught by Flux's safety checks.
:complexity:
Constant
``move_at``
-----------
.. function::
template <sequence Seq> \
auto move_at(Seq& seq, cursor_t<Seq> const& cur) -> rvalue_element_t<Seq>;
Accesses the element at the given cursor position as an rvalue reference or prvalue temporary.
:complexity:
Constant
``move_at_unchecked``
---------------------
.. function::
template <sequence Seq> \
auto move_at_unchecked(Seq& seq, cursor_t<Seq> const& cur) -> rvalue_element_t<Seq>;
Accesses the element at the given cursor position as an rvalue reference or prvalue temporary without performing safety checks.
.. danger:: Using this function can result in undefined behaviour that would otherwise be caught by Flux's safety checks.
:complexity:
Constant
``distance``
------------
.. function::
template <multipass_sequence Seq> \
auto distance(Seq& seq, cursor_t<Seq> const& from, cursor_t<seq> const& to) -> distance_t;
Returns number of times :var:`from` needs to be incremented until it is equal to :var:`to`.
For random-access sequences, :var:`to` may point to an earlier position than :var:`from`, in which case the result will be negative.
:complexity:
Constant if :expr:`random_access_sequence<Seq>`, otherwise linear
``data``
--------
.. function::
template <contiguous_sequence Seq> \
auto data(Seq& seq) -> std::add_pointer_t<std::remove_reference_t<element_t<Seq>>>;
Provides a pointer to the start of the underlying raw storage of a :concept:`contiguous_sequence`.
:complexity:
Constant
``size``
--------
.. function::
auto size(sized_sequence auto& seq) -> distance_t;
Returns the number of elements in the sequence. Performs no iteration.
:complexity:
Constant
``usize``
---------
.. function::
auto usize(sized_sequence auto& seq) -> std::size_t;
Returns the number of elements in the sequence as an unsigned :type:`std::size_t`.
:complexity:
Constant
``next``
--------
.. function::
template <sequence Seq> \
auto next(Seq& seq, cursor_t<Seq> cur) -> cursor_t<Seq>;
Returns a new cursor which points to the next position after :var:`cur`.
:complexity:
Constant if :expr:`random_access_sequence<Seq>`, otherwise linear
.. function::
template <sequence Seq> \
auto next(Seq& seq, cursor_t<Seq> cur, distance_t offset) -> cursor_t<Seq>;
If :var:`offset` is non-negative, returns a new cursor which points to a position :var:`offset` places past :var:`cur`.
If :var:`offset` is negative:
* If :var:`Seq` is bidirectional, returns a new cursor which points to a position :expr:`-offset` places before :var:`cur`
* Otherwise, returns :var:`cur`
:complexity:
Constant if :expr:`random_access_sequence<Seq>`, otherwise linear
``prev``
--------
.. function::
template <bidirectional_sequence Seq> \
auto prev(Seq& seq, cursor_t<Seq> cur) -> cursor_t<Seq>;
Returns a new cursor which points to the position before :var:`cur`
:complexity:
Constant if :expr:`random_access_sequence<Seq>`, otherwise linear
``is_empty``
------------
.. function::
template <sequence Seq> \
requires sized_sequence<Seq> || multipass_sequence<Seq> \
auto is_empty(Seq& seq) -> bool;
Returns :texpr:`true` if :var:`seq` contains no elements.
Equivalent to::
if constexpr (sized_sequence<Seq>) {
return size(seq) == 0;
} else {
return is_last(seq, first(seq));
}
:complexity:
Constant
``swap_with``
-------------
.. function::
template <sequence Seq1, sequence Seq2> \
requires element_swappable_with<Seq1, Seq2> \
auto swap_with(Seq1& seq1, cursor_t<Seq1> const& cur1, \
Seq2& seq2, cursor_t<Seq2> const& cur2) -> void;
Swaps the elements at position :var:`cur1` of :var:`seq1` and position :var:`cur2` of :var:`seq2`.
:complexity:
Constant
``swap_at``
-----------
.. function::
template <multipass_sequence Seq> \
requires element_swappable_with<Seq, Seq> \
auto swap_at(Seq& seq, cursor_t<Seq> const& cur1, cursor_t<Seq> const& cur2) -> void;
Swaps the elements at position :var:`cur1` and :var:`cur2`.
Equivalent to :expr:`swap_with(seq, cur1, seq, cur2)`
:complexity:
Constant
``front``
---------
.. function::
template <multipass_sequence Seq> \
auto front(Seq& seq) -> optional<element_t<Seq>>;
If :var:`seq` is empty, returns a disengaged :type:`optional`. Otherwise, returns an engaged :type:`optional` containing the first element of :var:`seq` (which may be a reference).
:complexity:
Constant
``back``
--------
.. function::
template <bidirectional_sequence Seq> \
requires bounded_sequence<Seq> \
auto back(Seq& seq) -> optional<element_t<Seq>>;
If :var:`seq` is empty, returns a disengaged :type:`optional`. Otherwise, returns an engaged :type:`optional` containing the rear-most element of :var:`seq` (which may be a reference).
:complexity:
Constant
``begin``
---------
.. function::
auto begin(sequence auto& seq) -> std::input_iterator auto;
Returns a C++20 iterator of implementation-defined type pointing to the first element of :var:`seq`. The resulting iterator forms a valid range with the sentinel returned by :expr:`end(seq)`.
The category of the returned iterator depends on the properties of :var:`seq`.
Multipass, bidirectional, random-access and contiguous sequences will return
forward, bidirectional, random-access and contiguous iterators respectively.
:complexity:
Constant
``end``
-------
.. function::
auto end(sequence auto& seq)
If :var:`seq` is a :concept:`bounded_sequence` whose cursor type satisfies :concept:`std::copy_constructible`, returns an iterator of the same type as :expr:`begin(seq)` which points to the past-the-end position of :var:`seq`.
Otherwise, returns :expr:`std::default_sentinel`.
:complexity:
Constant