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
pub trait Array<T>
{
fn len(&self) -> usize;
fn is_empty(&self) -> bool;
fn first(&self) -> Option<&T>;
fn first_mut(&mut self) -> Option<&mut T>;
fn last(&self) -> Option<&T>;
fn last_mut(&mut self) -> Option<&mut T>;
fn get(&self, index: usize) -> Option<&T>;
fn get_mut(&mut self, index: usize) -> Option<&mut T>;
fn as_ptr(&self) -> *const T;
fn as_mut_ptr(&mut self) -> *mut T;
fn as_slice(&self) -> &[T];
fn as_mut_slice(&mut self) -> &mut [T];
fn map_<F>(self, f: F) -> Self where T: Copy, F: FnMut(T) -> T, Self: Sized;
fn foldl<A, F>(self, acc: A, f: F) -> A where T: Copy, F: FnMut(A, T) -> A, Self: Sized;
fn foldr<A, F>(self, acc: A, f: F) -> A where T: Copy, F: FnMut(A, T) -> A, Self: Sized;
fn from_fn<F>(f: F) -> Self where F: FnMut(usize) -> T, Self: Sized;
fn from_iter<I: Iterator<Item=T>>(iter: I) -> Option<Self> where Self: Sized;
}
macro_rules! impl_array
{
(@replace $a:expr, $sub:expr) => ($sub);
(@do_impl $count:tt $($idx:tt)+) => {
impl<T> $crate::Array<T> for [T; $count]
{
fn len(&self) -> usize { $count }
fn is_empty(&self) -> bool { false }
fn first(&self) -> Option<&T> { Some(&self[0]) }
fn first_mut(&mut self) -> Option<&mut T> { Some(&mut self[0]) }
fn last(&self) -> Option<&T> { Some(&self[$count - 1]) }
fn last_mut(&mut self) -> Option<&mut T> { Some(&mut self[$count - 1]) }
fn get(&self, index: usize) -> Option<&T> { if index < $count { Some(&self[index]) } else { None } }
fn get_mut(&mut self, index: usize) -> Option<&mut T> { if index < $count { Some(&mut self[index]) } else { None } }
fn as_ptr(&self) -> *const T { &self[0] }
fn as_mut_ptr(&mut self) -> *mut T { &mut self[0] }
fn as_slice(&self) -> &[T] { self }
fn as_mut_slice(&mut self) -> &mut [T] { self }
fn map_<F>(self, mut f: F) -> Self where T: Copy, F: FnMut(T) -> T { [$( f(self[$count - $idx - 1]) ),+] }
fn foldl<A, F>(self, mut acc: A, mut f: F) -> A where T: Copy, F: FnMut(A, T) -> A { $( acc = f(acc, self[$count - $idx - 1]); )+ acc }
fn foldr<A, F>(self, mut acc: A, mut f: F) -> A where T: Copy, F: FnMut(A, T) -> A { $( acc = f(acc, self[$idx]); )+ acc }
fn from_fn<F>(mut f: F) -> Self where F: FnMut(usize) -> T { [$( f($count - $idx - 1) ),+] }
fn from_iter<I: Iterator<Item=T>>(mut iter: I) -> Option<Self>
{
Some([$( impl_array!(@replace $idx, match iter.next() { Some(v) => v, None => return None }) ),+])
}
}
};
($count:tt $idx:tt) => { impl_array!(@do_impl $count $idx); };
($head:tt $($tail:tt)+) => { impl_array!(@do_impl $head $($tail)+); impl_array!($($tail)+); };
}
impl_array!(32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0);
impl<T> Array<T> for [T; 0]
{
fn len(&self) -> usize { 0 }
fn is_empty(&self) -> bool { true }
fn first(&self) -> Option<&T> { None }
fn first_mut(&mut self) -> Option<&mut T> { None }
fn last(&self) -> Option<&T> { None }
fn last_mut(&mut self) -> Option<&mut T> { None }
fn get(&self, _index: usize) -> Option<&T> { None }
fn get_mut(&mut self, _index: usize) -> Option<&mut T> { None }
fn as_ptr(&self) -> *const T { self as _ }
fn as_mut_ptr(&mut self) -> *mut T { get_mut_ptr(self) }
fn as_slice(&self) -> &[T] { self }
fn as_mut_slice(&mut self) -> &mut [T] { self }
fn map_<F>(self, _f: F) -> Self where T: Copy, F: FnMut(T) -> T { self }
fn foldl<A, F>(self, acc: A, _f: F) -> A where T: Copy, F: FnMut(A, T) -> A { acc }
fn foldr<A, F>(self, acc: A, _f: F) -> A where T: Copy, F: FnMut(A, T) -> A { acc }
fn from_fn<F>(_f: F) -> Self where F: FnMut(usize) -> T { [] }
fn from_iter<I: Iterator<Item=T>>(_iter: I) -> Option<Self> { Some([]) }
}
fn get_mut_ptr<T>(a: &mut [T; 0]) -> *mut T { a.as_mut_ptr() }
pub mod sized;
pub use sized::*;
#[cfg(test)]
mod tests;