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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Handles map code generation.
/// Map code generation.
#[macro_export]
#[doc(hidden)]
macro_rules! map_codegen {
($t:ident,
$(#[$meta:meta])*
$map:ident
$($tail:tt)*
) => {
$(#[$meta])*
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $map<T> {
vec: $crate::alloc::vec::Vec<T>
}
$crate::non_strict! {
impl<T> core::convert::From<$crate::alloc::vec::Vec<T>> for $map<T> {
fn from(vec: $crate::alloc::vec::Vec<T>) -> Self {
Self { vec }
}
}
}
impl<T> $map<T> {
/// Creates an empty map.
#[inline]
pub const fn new() -> Self {
$map { vec: $crate::alloc::vec::Vec::new() }
}
/// Creates an empty map with some capacity.
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
$map { vec: $crate::alloc::vec::Vec::with_capacity(capacity) }
}
/// Reserves some space for the map.
#[inline]
pub fn reserve(&mut self, capa: usize) {
self.vec.reserve(capa)
}
/// Generates an index from a [`usize`] when it is a legal index.
#[inline]
pub fn index_from_usize(&self, n: usize) -> Option<$t> {
if n < self.vec.len() {
Some($t { val: n })
} else {
None
}
}
/// Retrieves an entry in the map.
#[inline]
pub fn get(&self, idx: $t) -> Option<&T> {
self.vec.get(idx.get())
}
/// Retrieves an entry in the map.
#[inline]
pub fn get_mut(&mut self, idx: $t) -> Option<&mut T> {
self.vec.get_mut(idx.get())
}
/// Retrieves the last entry in the map.
#[inline]
pub fn last(&self) -> Option<($t, &T)> {
self.last_index().map(|idx| (idx, &self[idx]))
}
/// Retrieves the last entry in the map.
#[inline]
pub fn last_mut(&mut self) -> Option<($t, &mut T)> {
self.last_index().map(move |idx| (idx, &mut self[idx]))
}
/// Number of elements in the map.
#[inline]
pub fn len(& self) -> usize {
self.vec.len()
}
/// Capacity of the map.
#[inline]
pub fn capacity(& self) -> usize {
self.vec.capacity()
}
$crate::non_strict! {
/// The next free index (wrapped `self.len()`).
#[inline]
pub fn next_index(& self) -> $t {
$t { val: self.len() }
}
}
/// Index of the last element in the map.
#[inline]
pub fn last_index(& self) -> Option<$t> {
let len = self.len();
if len > 0 { Some($t { val: len - 1 }) } else { None }
}
/// Pushes an element, yields its index.
///
/// If element construction requires the element's index, see [`Self::push_idx`].
#[inline]
pub fn push(&mut self, elem: T) -> $t {
let idx = $t { val: self.len() };
self.vec.push(elem);
idx
}
/// Pushes an element generated by a function taking the element's index as input.
///
/// This is useful if you want to store the `T`-element's index inside the element,
/// meaning you need the index to actually create the element.
#[inline]
pub fn push_idx(&mut self, new_elem: impl FnOnce($t) -> T) -> $t {
let idx = $t { val: self.len() };
self.vec.push(new_elem(idx));
idx
}
/// Same as [`push_idx`], but the builder returns a result.
#[inline]
pub fn try_push_idx<E>(&mut self, new_elem: impl FnOnce($t) -> Result<T, E>) -> Result<$t, E> {
let idx = $t { val: self.len() };
self.vec.push(new_elem(idx)?);
Ok(idx)
}
$crate::non_strict! {
/// Pops an element.
///
/// This function is unsafe for the logics of safe indices. This function voids indices
/// previously created (indices for the last element on entry) and should be used with
/// great care.
#[inline]
pub fn pop(&mut self) -> Option<T> {
self.vec.pop()
}
}
$crate::non_strict! {
/// Clears a map.
#[inline]
pub fn clear(&mut self) {
self.vec.clear()
}
}
/// Range of the map.
#[inline]
pub fn range(&self) -> core::ops::RangeInclusive<$t> {
$t { val: 0 } ..= $t { val: self.len() }
}
/// Iterator over all the indices.
#[inline]
pub fn indices(&self) -> impl core::iter::Iterator<Item = $t> {
(0..self.len()).into_iter().map(|i| $t { val: i })
}
/// Ref-iterator over the elements.
#[inline]
pub fn iter(& self) -> core::slice::Iter<T> {
self.vec.iter()
}
/// Ref-iterator over the index/element pairs.
#[inline]
pub fn index_iter<'a>(&'a self) ->
impl core::iter::Iterator<Item = ($t, &'a T)>
+ core::iter::DoubleEndedIterator
+ core::iter::ExactSizeIterator
where T: 'a {
self.vec.iter().enumerate().map(|(idx, elm)| (
$t { val: idx }, elm
))
}
/// Ref-mut-iterator over the index/element pairs.
#[inline]
pub fn index_iter_mut<'a>(&'a mut self) ->
impl core::iter::Iterator<Item = ($t, &'a mut T)>
+ core::iter::DoubleEndedIterator
+ core::iter::ExactSizeIterator
where T: 'a {
self.vec.iter_mut().enumerate().map(|(idx, elm)| (
$t { val: idx }, elm
))
}
/// Own-iterator over the index/element pairs.
#[inline]
pub fn into_index_iter(self) ->
impl core::iter::Iterator<Item = ($t, T)>
+ core::iter::DoubleEndedIterator
+ core::iter::ExactSizeIterator
{
self.vec.into_iter().enumerate().map(|(idx, elm)| (
$t { val: idx }, elm
))
}
/// Ref-mut-iterator over the elements.
#[inline]
pub fn iter_mut(&mut self) -> core::slice::IterMut<T> {
self.vec.iter_mut()
}
/// Shrinks the capacity as much as possible.
#[inline]
pub fn shrink_to_fit(&mut self) {
self.vec.shrink_to_fit()
}
/// Swaps two elements.
#[inline]
pub fn swap(&mut self, a: $t, b: $t) {
self.vec.swap(* a, *b)
}
$crate::non_strict! {
/// Swap remove from `Vec`.
///
/// This function is unsafe for the logics of safe indices. This function voids indices
/// previously created (indices for the last element on entry) and should be used with
/// great care.
#[inline]
pub fn swap_remove(&mut self, idx: $t) -> T {
self.vec.swap_remove(* idx)
}
}
/// Splits the map into the elements before and after some index.
///
/// More precisely, returns a tuple of
///
/// - an iterator over the elements *before* `idx`,
/// - the element at position `idx`, and
/// - an iterator over the elements *after* `idx`.
#[inline]
pub fn split(&self, idx: $t) -> (
impl core::iter::Iterator<Item = ($t, &T)>,
&T,
impl core::iter::Iterator<Item = ($t, &T)>,
) {
let before = self.vec[0..idx.val].iter().enumerate().map(
|(i, elm)| ($t { val: i }, elm)
);
let after = if idx.val < self.vec.len() {
self.vec[idx.val + 1 ..].iter()
} else {
self.vec[0..0].iter()
}.enumerate().map(
move |(i, elm)| ($t { val: 1 + i + idx.val }, elm)
);
(before, &self.vec[idx.val], after)
}
}
impl<T: Clone> $map<T> {
/// Creates an empty vector with some capacity.
#[inline]
pub fn of_elems(elem: T, size: usize) -> Self {
$map { vec: $crate::alloc::vec![ elem ; size ] }
}
}
impl<T> core::iter::IntoIterator for $map<T> {
type Item = T ;
type IntoIter = $crate::alloc::vec::IntoIter<T> ;
fn into_iter(self) -> $crate::alloc::vec::IntoIter<T> {
self.vec.into_iter()
}
}
impl<'a, T> core::iter::IntoIterator for &'a $map<T> {
type Item = &'a T ;
type IntoIter = core::slice::Iter<'a, T> ;
fn into_iter(self) -> core::slice::Iter<'a, T> {
self.iter()
}
}
impl<'a, T> core::iter::IntoIterator for &'a mut $map<T> {
type Item = &'a mut T ;
type IntoIter = core::slice::IterMut<'a, T> ;
fn into_iter(self) -> core::slice::IterMut<'a, T> {
self.iter_mut()
}
}
impl<T> core::iter::FromIterator<T> for $map<T> {
fn from_iter<
I: core::iter::IntoIterator<Item = T>
>(iter: I) -> Self {
$map { vec: iter.into_iter().collect() }
}
}
impl<T> core::ops::Index<$t> for $map<T> {
type Output = T ;
fn index(& self, index: $t) -> & T {
& self.vec[ index.get() ]
}
}
impl<T> core::ops::Index<core::ops::RangeFrom<$t>> for $map<T> {
type Output = [T];
fn index(& self, core::ops::RangeFrom { start }: core::ops::RangeFrom<$t>) -> &[T] {
& self.vec[ start.get() .. ]
}
}
impl<T> core::ops::Index<core::ops::Range<$t>> for $map<T> {
type Output = [T];
fn index(& self, core::ops::Range { start, end }: core::ops::Range<$t>) -> &[T] {
& self.vec[ start.get() .. end.get() ]
}
}
impl<T> core::ops::Index<core::ops::RangeInclusive<$t>> for $map<T> {
type Output = [T];
fn index(& self, range: core::ops::RangeInclusive<$t>) -> &[T] {
& self.vec[ range.start().get() ..= range.end().get() ]
}
}
impl<T> core::ops::Index<core::ops::RangeFull> for $map<T> {
type Output = [T];
fn index(& self, _: core::ops::RangeFull) -> &[T] {
& self.vec[..]
}
}
impl<T> core::ops::Index<core::ops::RangeTo<$t>> for $map<T> {
type Output = [T];
fn index(& self, core::ops::RangeTo { end }: core::ops::RangeTo<$t>) -> &[T] {
& self.vec[..end.get()]
}
}
impl<T> core::ops::Index<core::ops::RangeToInclusive<$t>> for $map<T> {
type Output = [T];
fn index(
& self,
core::ops::RangeToInclusive { end }: core::ops::RangeToInclusive<$t>
) -> &[T] {
& self.vec[..=end.get()]
}
}
impl<T> core::ops::IndexMut<$t> for $map<T> {
fn index_mut(&mut self, index: $t) -> &mut T {
&mut self.vec[ index.get() ]
}
}
$crate::non_strict! {
impl<T> core::ops::Index<
core::ops::Range<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: core::ops::Range<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> core::ops::Index<
core::ops::RangeInclusive<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: core::ops::RangeInclusive<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> core::ops::Index<
core::ops::RangeFrom<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: core::ops::RangeFrom<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> core::ops::Index<
core::ops::RangeTo<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: core::ops::RangeTo<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> core::ops::Index<
core::ops::RangeToInclusive<usize>
> for $map<T> {
type Output = [T] ;
fn index(& self, index: core::ops::RangeToInclusive<usize>) -> & [T] {
self.vec.index(index)
}
}
impl<T> core::ops::Deref for $map<T> {
type Target = $crate::alloc::vec::Vec<T> ;
fn deref(& self) -> & $crate::alloc::vec::Vec<T> {
& self.vec
}
}
}
$crate::handle!{ $t $($tail)* }
};
}