tuple/
m_tuple.rs

1use super::*;
2
3macro_rules! m_tuple {
4    ($($Tuple:ident $Arr:ident { $($T:ident . $t:ident . $idx:tt),* } )*) => ($(
5        impl<$($T),*> $Tuple<$(Option<$T>),*> {
6            pub fn collect(self) -> Option< $Tuple<$($T),*> > {
7                match self {
8                    $Tuple( $( Some($T) ),* ) => Some( $Tuple( $( $T ),* ) ),
9                    _ => None
10                }
11            }
12        }
13        unsafe impl<T> TupleElements for $Tuple<$(A!(T,$T),)*> {
14            type Element = T;
15            const N: usize = $(a!(1, $idx)+)* 0;
16            #[inline(always)]
17            fn get(&self, index: usize) -> Option<&T> {
18                match index {
19                 $( $idx => Some(&self.$idx), )*
20                    _ => None
21                }
22            }
23            #[inline(always)]
24            fn get_mut(&mut self, index: usize) -> Option<&mut T> {
25                match index {
26                 $( $idx => Some(&mut self.$idx), )*
27                    _ => None
28                }
29            }
30            #[inline(always)]
31            fn from_iter<I>(mut iter: I) -> Option<Self> where I: Iterator<Item=Self::Element> {
32             $( let $T = match iter.next() {
33                    Some(v) => v,
34                    None => return None
35                }; )*
36                Some($Tuple($($T),*))
37            }
38        }
39        impl<T> Splat<T> for $Tuple<$(A!(T,$T),)*> where T: Clone {
40            #[inline(always)]
41            fn splat(t: T) -> Self {
42                $Tuple( $( a!(t.clone(), $T) ),* )
43            }
44        }
45        impl<T, U> Map<U> for $Tuple<$(A!(T,$T)),*> {
46            type Output = $Tuple<$(A!(U,$T)),*>;
47            #[inline(always)]
48            fn map<F>(self, f: F) -> Self::Output where F: Fn(T) -> U {
49                $Tuple($(f(self.$idx)),*)
50            }
51            #[inline(always)]
52            fn map_mut<F>(self, mut f: F) -> Self::Output where F: FnMut(T) -> U {
53                $Tuple($(f(self.$idx)),*)
54            }
55
56        }
57
58        unsafe impl<T> TupleElements for ($(A!(T,$T),)*) {
59            type Element = T;
60            const N: usize = $(a!(1, $idx)+)* 0;
61            #[inline(always)]
62            fn get(&self, index: usize) -> Option<&T> {
63                match index {
64                 $( $idx => Some(&self.$idx), )*
65                    _ => None
66                }
67            }
68            #[inline(always)]
69            fn get_mut(&mut self, index: usize) -> Option<&mut T> {
70                match index {
71                 $( $idx => Some(&mut self.$idx), )*
72                    _ => None
73                }
74            }
75            #[inline(always)]
76            fn from_iter<I>(mut iter: I) -> Option<Self> where I: Iterator<Item=Self::Element> {
77             $( let $T = match iter.next() {
78                    Some(v) => v,
79                    None => return None
80                }; )*
81                Some(($($T,)*))
82            }
83        }
84        impl<T> Splat<T> for ($(A!(T,$T),)*) where T: Clone {
85            #[inline(always)]
86            fn splat(t: T) -> Self {
87                ( $( a!(t.clone(), $T), )* )
88            }
89        }
90        impl<T, U> Map<U> for ($(A!(T,$T),)*) {
91            type Output = ($(A!(U,$T),)*);
92            #[inline(always)]
93            fn map<F>(self, f: F) -> Self::Output where F: Fn(T) -> U {
94                ($(f(self.$idx),)*)
95            }
96            #[inline(always)]
97            fn map_mut<F>(self, mut f: F) -> Self::Output where F: FnMut(T) -> U {
98                ($(f(self.$idx),)*)
99            }
100        }
101
102        impl<$($T),*> OpRotateLeft for $Tuple<$($T),*> {
103            type Output = Rot_l!(x_ty_ident, $Tuple; $($T,)*);
104            #[inline(always)]
105            fn rot_l(self) -> Self::Output {
106                rot_l!(x_ty_expr, $Tuple; $(self.$idx,)*)
107            }
108        }
109        impl<$($T),*> OpRotateLeft for ($($T,)*) {
110            type Output = Rot_l!(x_tuple_ident; $($T,)*);
111            #[inline(always)]
112            fn rot_l(self) -> Self::Output {
113                rot_l!(x_tuple_expr; $(self.$idx,)*)
114            }
115        }
116        impl<$($T),*> OpRotateRight for $Tuple<$($T),*> {
117            type Output = Rot_r!(x_ty_ident, $Tuple; $($T,)*);
118            #[inline(always)]
119            fn rot_r(self) -> Self::Output {
120                rot_r!(x_ty_expr, $Tuple; $(self.$idx,)*)
121            }
122        }
123        impl<$($T),*> OpRotateRight for ($($T,)*) {
124            type Output = Rot_r!(x_tuple_ident; $($T,)*);
125            #[inline(always)]
126            fn rot_r(self) -> Self::Output {
127                rot_r!(x_tuple_expr; $(self.$idx,)*)
128            }
129        }
130        impl<$($T),*> OpReverse for $Tuple<$($T),*> {
131            type Output = Rev!(x_ty_ident, $Tuple; $($T,)*);
132            #[inline(always)]
133            fn reverse(self) -> Self::Output {
134                rev!(x_ty_expr, $Tuple; $(self.$idx,)*)
135            }
136        }
137        impl<$($T),*> OpReverse for ($($T,)*) {
138            type Output = Rev!(x_tuple_ident; $($T,)*);
139            #[inline(always)]
140            fn reverse(self) -> Self::Output {
141                rev!(x_tuple_expr; $(self.$idx,)*)
142            }
143        }
144    )*)
145}
146
147macro_rules! impl_join {
148    ( $( $L:ident ( $( $l:ident ),* ) | $R:ident ( $( $r:ident ),* )
149      => $O:ident ( $( $o:ident ),* ) )*
150    ) => ( $(
151        impl<$($l,)* $($r),*> OpJoin<$R<$($r),*>> for $L<$($l),*> {
152            type Output = $O<$($l,)* $($r),*>;
153            #[inline(always)]
154            fn join(self, rhs: $R<$($r),*>) -> Self::Output {
155                let $L($($l),*) = self;
156                let $R($($r),*) = rhs;
157                $O($($l,)* $($r),*)
158            }
159        }
160        impl<$($l,)* $($r),*> OpJoin<($($r,)*)> for ($($l,)*) {
161            type Output = ($($l,)* $($r),*);
162            #[inline(always)]
163            fn join(self, rhs: ($($r,)*)) -> Self::Output {
164                let ($($l,)*) = self;
165                let ($($r,)*) = rhs;
166                ($($l,)* $($r,)*)
167            }
168        }
169        impl<$($l,)* $($r),*> OpSplit<$L<$($l),*>> for $O<$($l,)* $($r),*> {
170            type R = $R<$($r),*>;
171            #[inline(always)]
172            fn split(self) -> ($L<$($l),*>, Self::R) {
173                let $O($($l,)* $($r),*) = self;
174                ( $L($($l),*), $R($($r),*) )
175            }
176        }
177        impl<$($l,)* $($r),*> OpSplit<($($l),*)> for ($($l,)* $($r),*) {
178            type R = ($($r),*);
179            #[inline(always)]
180            fn split(self) -> (($($l),*), Self::R) {
181                let ($($l,)* $($r),*) = self;
182                ( ($($l),*), ($($r),*) )
183            }
184        }
185    )* )
186}
187
188/* python3:
189a = string.ascii_lowercase
190def impl(l, r): return "T{l}({nl}) | T{r}({nr}) => T{o}({no})".format(l=l, r=r, nl=", ".join(a[:l]), nr=", ".join(a[l:l+r]), o=l+r, no=", ".join(a[:l+r]))
191
192for i in range(1,9):
193    for j in range(1,9-i):
194        print(impl(i, j))
195
196*/
197macro_rules! m_join(
198    () => ( impl_join!(
199        T1(a) | T1(b) => T2(a, b)
200        T1(a) | T2(b, c) => T3(a, b, c)
201        T1(a) | T3(b, c, d) => T4(a, b, c, d)
202        T1(a) | T4(b, c, d, e) => T5(a, b, c, d, e)
203        T1(a) | T5(b, c, d, e, f) => T6(a, b, c, d, e, f)
204        T1(a) | T6(b, c, d, e, f, g) => T7(a, b, c, d, e, f, g)
205        T1(a) | T7(b, c, d, e, f, g, h) => T8(a, b, c, d, e, f, g, h)
206        T2(a, b) | T1(c) => T3(a, b, c)
207        T2(a, b) | T2(c, d) => T4(a, b, c, d)
208        T2(a, b) | T3(c, d, e) => T5(a, b, c, d, e)
209        T2(a, b) | T4(c, d, e, f) => T6(a, b, c, d, e, f)
210        T2(a, b) | T5(c, d, e, f, g) => T7(a, b, c, d, e, f, g)
211        T2(a, b) | T6(c, d, e, f, g, h) => T8(a, b, c, d, e, f, g, h)
212        T3(a, b, c) | T1(d) => T4(a, b, c, d)
213        T3(a, b, c) | T2(d, e) => T5(a, b, c, d, e)
214        T3(a, b, c) | T3(d, e, f) => T6(a, b, c, d, e, f)
215        T3(a, b, c) | T4(d, e, f, g) => T7(a, b, c, d, e, f, g)
216        T3(a, b, c) | T5(d, e, f, g, h) => T8(a, b, c, d, e, f, g, h)
217        T4(a, b, c, d) | T1(e) => T5(a, b, c, d, e)
218        T4(a, b, c, d) | T2(e, f) => T6(a, b, c, d, e, f)
219        T4(a, b, c, d) | T3(e, f, g) => T7(a, b, c, d, e, f, g)
220        T4(a, b, c, d) | T4(e, f, g, h) => T8(a, b, c, d, e, f, g, h)
221        T5(a, b, c, d, e) | T1(f) => T6(a, b, c, d, e, f)
222        T5(a, b, c, d, e) | T2(f, g) => T7(a, b, c, d, e, f, g)
223        T5(a, b, c, d, e) | T3(f, g, h) => T8(a, b, c, d, e, f, g, h)
224        T6(a, b, c, d, e, f) | T1(g) => T7(a, b, c, d, e, f, g)
225        T6(a, b, c, d, e, f) | T2(g, h) => T8(a, b, c, d, e, f, g, h)
226        T7(a, b, c, d, e, f, g) | T1(h) => T8(a, b, c, d, e, f, g, h)
227    );
228    )
229);
230
231impl_tuple!(m_tuple);
232m_join!();