1use super::{Map, Set};
2
3macro_rules! natural_set {
4 ($($ty:ident),*) => {
5 $(
6 impl Set for $ty {
7 type Item = $ty;
8
9 type Map<V> = Vec<V>;
15
16 type Iter<'a> = std::ops::Range<$ty>;
18
19 fn len(&self) -> usize {
21 *self as usize
22 }
23
24 fn iter(&self) -> Self::Iter<'_> {
26 0..*self
27 }
28
29 fn map<V: Clone, F>(&self, f: F) -> Self::Map<V>
30 where
31 F: Fn(&Self::Item) -> V,
32 {
33 let mut map = Vec::with_capacity(*self as usize);
34 for i in 0..*self {
35 map.push(f(&i))
36 }
37 map
38 }
39 }
40
41 impl<T> Map<$ty, T> for Vec<T> {
42 fn len(&self) -> usize {
43 self.len()
44 }
45
46 fn get(&self, key: &$ty) -> Option<&T> {
47 self.as_slice().get(*key as usize)
48 }
49
50 fn set(&mut self, key: &$ty, value: T) {
51 self[*key as usize] = value
52 }
53
54 fn map<F>(&mut self, f: F)
55 where
56 F: Fn(&$ty, T) -> T,
57 {
58 for (i, v) in self.iter_mut().enumerate() {
59 unsafe {
60 let t = std::ptr::read(v);
61 std::ptr::write(v, f(&(i as $ty), t));
62 }
63 }
64 }
65 }
66 )*
67 };
68}
69
70natural_set!(u32, u64, usize);