]> git.lizzy.rs Git - rust.git/blob - src/libstd/tuple.rs
Find the cratemap at runtime on windows.
[rust.git] / src / libstd / tuple.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Operations on tuples
12
13 #[allow(missing_doc)];
14
15 use clone::Clone;
16
17 pub use self::inner::*;
18
19 /// Method extensions to pairs where both types satisfy the `Clone` bound
20 pub trait CopyableTuple<T, U> {
21     /// Return the first element of self
22     fn first(&self) -> T;
23     /// Return the second element of self
24     fn second(&self) -> U;
25     /// Return the results of swapping the two elements of self
26     fn swap(&self) -> (U, T);
27 }
28
29 impl<T:Clone,U:Clone> CopyableTuple<T, U> for (T, U) {
30     /// Return the first element of self
31     #[inline]
32     fn first(&self) -> T {
33         match *self {
34             (ref t, _) => (*t).clone(),
35         }
36     }
37
38     /// Return the second element of self
39     #[inline]
40     fn second(&self) -> U {
41         match *self {
42             (_, ref u) => (*u).clone(),
43         }
44     }
45
46     /// Return the results of swapping the two elements of self
47     #[inline]
48     fn swap(&self) -> (U, T) {
49         match (*self).clone() {
50             (t, u) => (u, t),
51         }
52     }
53 }
54
55 /// Method extensions for pairs where the types don't necessarily satisfy the
56 /// `Clone` bound
57 pub trait ImmutableTuple<T, U> {
58     /// Return a reference to the first element of self
59     fn first_ref<'a>(&'a self) -> &'a T;
60     /// Return a reference to the second element of self
61     fn second_ref<'a>(&'a self) -> &'a U;
62 }
63
64 impl<T, U> ImmutableTuple<T, U> for (T, U) {
65     #[inline]
66     fn first_ref<'a>(&'a self) -> &'a T {
67         match *self {
68             (ref t, _) => t,
69         }
70     }
71     #[inline]
72     fn second_ref<'a>(&'a self) -> &'a U {
73         match *self {
74             (_, ref u) => u,
75         }
76     }
77 }
78
79 // macro for implementing n-ary tuple functions and operations
80
81 macro_rules! tuple_impls {
82     ($(
83         ($cloneable_trait:ident, $immutable_trait:ident) {
84             $(($get_fn:ident, $get_ref_fn:ident) -> $T:ident {
85                 $get_pattern:pat => $ret:expr
86             })+
87         }
88     )+) => {
89         pub mod inner {
90             use clone::Clone;
91             #[cfg(not(test))] use cmp::*;
92             #[cfg(not(test))] use default::Default;
93             #[cfg(not(test))] use num::Zero;
94
95             $(
96                 pub trait $cloneable_trait<$($T),+> {
97                     $(fn $get_fn(&self) -> $T;)+
98                 }
99
100                 impl<$($T:Clone),+> $cloneable_trait<$($T),+> for ($($T,)+) {
101                     $(
102                         #[inline]
103                         fn $get_fn(&self) -> $T {
104                             self.$get_ref_fn().clone()
105                         }
106                     )+
107                 }
108
109                 pub trait $immutable_trait<$($T),+> {
110                     $(fn $get_ref_fn<'a>(&'a self) -> &'a $T;)+
111                 }
112
113                 impl<$($T),+> $immutable_trait<$($T),+> for ($($T,)+) {
114                     $(
115                         #[inline]
116                         fn $get_ref_fn<'a>(&'a self) -> &'a $T {
117                             match *self { $get_pattern => $ret }
118                         }
119                     )+
120                 }
121
122                 impl<$($T:Clone),+> Clone for ($($T,)+) {
123                     fn clone(&self) -> ($($T,)+) {
124                         ($(self.$get_ref_fn().clone(),)+)
125                     }
126                 }
127
128                 #[cfg(not(test))]
129                 impl<$($T:Eq),+> Eq for ($($T,)+) {
130                     #[inline]
131                     fn eq(&self, other: &($($T,)+)) -> bool {
132                         $(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
133                     }
134                     #[inline]
135                     fn ne(&self, other: &($($T,)+)) -> bool {
136                         $(*self.$get_ref_fn() != *other.$get_ref_fn())||+
137                     }
138                 }
139
140                 #[cfg(not(test))]
141                 impl<$($T:TotalEq),+> TotalEq for ($($T,)+) {
142                     #[inline]
143                     fn equals(&self, other: &($($T,)+)) -> bool {
144                         $(self.$get_ref_fn().equals(other.$get_ref_fn()))&&+
145                     }
146                 }
147
148                 #[cfg(not(test))]
149                 impl<$($T:Ord + Eq),+> Ord for ($($T,)+) {
150                     #[inline]
151                     fn lt(&self, other: &($($T,)+)) -> bool {
152                         lexical_ord!(lt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
153                     }
154                     #[inline]
155                     fn le(&self, other: &($($T,)+)) -> bool {
156                         lexical_ord!(le, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
157                     }
158                     #[inline]
159                     fn ge(&self, other: &($($T,)+)) -> bool {
160                         lexical_ord!(ge, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
161                     }
162                     #[inline]
163                     fn gt(&self, other: &($($T,)+)) -> bool {
164                         lexical_ord!(gt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
165                     }
166                 }
167
168                 #[cfg(not(test))]
169                 impl<$($T:TotalOrd),+> TotalOrd for ($($T,)+) {
170                     #[inline]
171                     fn cmp(&self, other: &($($T,)+)) -> Ordering {
172                         lexical_cmp!($(self.$get_ref_fn(), other.$get_ref_fn()),+)
173                     }
174                 }
175
176                 #[cfg(not(test))]
177                 impl<$($T:Default),+> Default for ($($T,)+) {
178                     #[inline]
179                     fn default() -> ($($T,)+) {
180                         ($({ let x: $T = Default::default(); x},)+)
181                     }
182                 }
183
184                 #[cfg(not(test))]
185                 impl<$($T:Zero),+> Zero for ($($T,)+) {
186                     #[inline]
187                     fn zero() -> ($($T,)+) {
188                         ($({ let x: $T = Zero::zero(); x},)+)
189                     }
190                     #[inline]
191                     fn is_zero(&self) -> bool {
192                         $(self.$get_ref_fn().is_zero())&&+
193                     }
194                 }
195             )+
196         }
197     }
198 }
199
200 // Constructs an expression that performs a lexical ordering using method $rel.
201 // The values are interleaved, so the macro invocation for
202 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
203 // a3, b3)` (and similarly for `lexical_cmp`)
204 macro_rules! lexical_ord {
205     ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
206         if *$a != *$b { lexical_ord!($rel, $a, $b) }
207         else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
208     };
209     ($rel: ident, $a:expr, $b:expr) => { (*$a) . $rel ($b) };
210 }
211
212 macro_rules! lexical_cmp {
213     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
214         match ($a).cmp($b) {
215             Equal => lexical_cmp!($($rest_a, $rest_b),+),
216             ordering   => ordering
217         }
218     };
219     ($a:expr, $b:expr) => { ($a).cmp($b) };
220 }
221
222
223 tuple_impls! {
224     (CloneableTuple1, ImmutableTuple1) {
225         (n0, n0_ref) -> A { (ref a,) => a }
226     }
227
228     (CloneableTuple2, ImmutableTuple2) {
229         (n0, n0_ref) -> A { (ref a,_) => a }
230         (n1, n1_ref) -> B { (_,ref b) => b }
231     }
232
233     (CloneableTuple3, ImmutableTuple3) {
234         (n0, n0_ref) -> A { (ref a,_,_) => a }
235         (n1, n1_ref) -> B { (_,ref b,_) => b }
236         (n2, n2_ref) -> C { (_,_,ref c) => c }
237     }
238
239     (CloneableTuple4, ImmutableTuple4) {
240         (n0, n0_ref) -> A { (ref a,_,_,_) => a }
241         (n1, n1_ref) -> B { (_,ref b,_,_) => b }
242         (n2, n2_ref) -> C { (_,_,ref c,_) => c }
243         (n3, n3_ref) -> D { (_,_,_,ref d) => d }
244     }
245
246     (CloneableTuple5, ImmutableTuple5) {
247         (n0, n0_ref) -> A { (ref a,_,_,_,_) => a }
248         (n1, n1_ref) -> B { (_,ref b,_,_,_) => b }
249         (n2, n2_ref) -> C { (_,_,ref c,_,_) => c }
250         (n3, n3_ref) -> D { (_,_,_,ref d,_) => d }
251         (n4, n4_ref) -> E { (_,_,_,_,ref e) => e }
252     }
253
254     (CloneableTuple6, ImmutableTuple6) {
255         (n0, n0_ref) -> A { (ref a,_,_,_,_,_) => a }
256         (n1, n1_ref) -> B { (_,ref b,_,_,_,_) => b }
257         (n2, n2_ref) -> C { (_,_,ref c,_,_,_) => c }
258         (n3, n3_ref) -> D { (_,_,_,ref d,_,_) => d }
259         (n4, n4_ref) -> E { (_,_,_,_,ref e,_) => e }
260         (n5, n5_ref) -> F { (_,_,_,_,_,ref f) => f }
261     }
262
263     (CloneableTuple7, ImmutableTuple7) {
264         (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_) => a }
265         (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_) => b }
266         (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_) => c }
267         (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_) => d }
268         (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_) => e }
269         (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_) => f }
270         (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g) => g }
271     }
272
273     (CloneableTuple8, ImmutableTuple8) {
274         (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_) => a }
275         (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_) => b }
276         (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_) => c }
277         (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_) => d }
278         (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_) => e }
279         (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_) => f }
280         (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_) => g }
281         (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h) => h }
282     }
283
284     (CloneableTuple9, ImmutableTuple9) {
285         (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_) => a }
286         (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_) => b }
287         (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_) => c }
288         (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_) => d }
289         (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_) => e }
290         (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_) => f }
291         (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_) => g }
292         (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_) => h }
293         (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i) => i }
294     }
295
296     (CloneableTuple10, ImmutableTuple10) {
297         (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_,_) => a }
298         (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_,_) => b }
299         (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_,_) => c }
300         (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_,_) => d }
301         (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_,_) => e }
302         (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_,_) => f }
303         (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_,_) => g }
304         (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_,_) => h }
305         (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i,_) => i }
306         (n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,ref j) => j }
307     }
308
309     (CloneableTuple11, ImmutableTuple11) {
310         (n0,  n0_ref)  -> A { (ref a,_,_,_,_,_,_,_,_,_,_) => a }
311         (n1,  n1_ref)  -> B { (_,ref b,_,_,_,_,_,_,_,_,_) => b }
312         (n2,  n2_ref)  -> C { (_,_,ref c,_,_,_,_,_,_,_,_) => c }
313         (n3,  n3_ref)  -> D { (_,_,_,ref d,_,_,_,_,_,_,_) => d }
314         (n4,  n4_ref)  -> E { (_,_,_,_,ref e,_,_,_,_,_,_) => e }
315         (n5,  n5_ref)  -> F { (_,_,_,_,_,ref f,_,_,_,_,_) => f }
316         (n6,  n6_ref)  -> G { (_,_,_,_,_,_,ref g,_,_,_,_) => g }
317         (n7,  n7_ref)  -> H { (_,_,_,_,_,_,_,ref h,_,_,_) => h }
318         (n8,  n8_ref)  -> I { (_,_,_,_,_,_,_,_,ref i,_,_) => i }
319         (n9,  n9_ref)  -> J { (_,_,_,_,_,_,_,_,_,ref j,_) => j }
320         (n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,ref k) => k }
321     }
322
323     (CloneableTuple12, ImmutableTuple12) {
324         (n0,  n0_ref)  -> A { (ref a,_,_,_,_,_,_,_,_,_,_,_) => a }
325         (n1,  n1_ref)  -> B { (_,ref b,_,_,_,_,_,_,_,_,_,_) => b }
326         (n2,  n2_ref)  -> C { (_,_,ref c,_,_,_,_,_,_,_,_,_) => c }
327         (n3,  n3_ref)  -> D { (_,_,_,ref d,_,_,_,_,_,_,_,_) => d }
328         (n4,  n4_ref)  -> E { (_,_,_,_,ref e,_,_,_,_,_,_,_) => e }
329         (n5,  n5_ref)  -> F { (_,_,_,_,_,ref f,_,_,_,_,_,_) => f }
330         (n6,  n6_ref)  -> G { (_,_,_,_,_,_,ref g,_,_,_,_,_) => g }
331         (n7,  n7_ref)  -> H { (_,_,_,_,_,_,_,ref h,_,_,_,_) => h }
332         (n8,  n8_ref)  -> I { (_,_,_,_,_,_,_,_,ref i,_,_,_) => i }
333         (n9,  n9_ref)  -> J { (_,_,_,_,_,_,_,_,_,ref j,_,_) => j }
334         (n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,ref k,_) => k }
335         (n11, n11_ref) -> L { (_,_,_,_,_,_,_,_,_,_,_,ref l) => l }
336     }
337 }
338
339 #[cfg(test)]
340 mod tests {
341     use super::*;
342     use clone::Clone;
343     use cmp::*;
344
345     #[test]
346     fn test_tuple_ref() {
347         let x = (~"foo", ~"bar");
348         assert_eq!(x.first_ref(), &~"foo");
349         assert_eq!(x.second_ref(), &~"bar");
350     }
351
352     #[test]
353     fn test_tuple() {
354         assert_eq!((948, 4039.48).first(), 948);
355         assert_eq!((34.5, ~"foo").second(), ~"foo");
356         assert_eq!(('a', 2).swap(), (2, 'a'));
357     }
358
359     #[test]
360     fn test_clone() {
361         let a = (1, ~"2");
362         let b = a.clone();
363         assert_eq!(a.first(), b.first());
364         assert_eq!(a.second(), b.second());
365     }
366
367     #[test]
368     fn test_n_tuple() {
369         let t = (0u8, 1u16, 2u32, 3u64, 4u, 5i8, 6i16, 7i32, 8i64, 9i, 10f32, 11f64);
370         assert_eq!(t.n0(), 0u8);
371         assert_eq!(t.n1(), 1u16);
372         assert_eq!(t.n2(), 2u32);
373         assert_eq!(t.n3(), 3u64);
374         assert_eq!(t.n4(), 4u);
375         assert_eq!(t.n5(), 5i8);
376         assert_eq!(t.n6(), 6i16);
377         assert_eq!(t.n7(), 7i32);
378         assert_eq!(t.n8(), 8i64);
379         assert_eq!(t.n9(), 9i);
380         assert_eq!(t.n10(), 10f32);
381         assert_eq!(t.n11(), 11f64);
382
383         assert_eq!(t.n0_ref(), &0u8);
384         assert_eq!(t.n1_ref(), &1u16);
385         assert_eq!(t.n2_ref(), &2u32);
386         assert_eq!(t.n3_ref(), &3u64);
387         assert_eq!(t.n4_ref(), &4u);
388         assert_eq!(t.n5_ref(), &5i8);
389         assert_eq!(t.n6_ref(), &6i16);
390         assert_eq!(t.n7_ref(), &7i32);
391         assert_eq!(t.n8_ref(), &8i64);
392         assert_eq!(t.n9_ref(), &9i);
393         assert_eq!(t.n10_ref(), &10f32);
394         assert_eq!(t.n11_ref(), &11f64);
395     }
396
397     #[test]
398     fn test_tuple_cmp() {
399         let (small, big) = ((1u, 2u, 3u), (3u, 2u, 1u));
400
401         let nan = 0.0/0.0;
402
403         // Eq
404         assert_eq!(small, small);
405         assert_eq!(big, big);
406         assert!(small != big);
407         assert!(big != small);
408
409         // Ord
410         assert!(small < big);
411         assert!(!(small < small));
412         assert!(!(big < small));
413         assert!(!(big < big));
414
415         assert!(small <= small);
416         assert!(big <= big);
417
418         assert!(big > small);
419         assert!(small >= small);
420         assert!(big >= small);
421         assert!(big >= big);
422
423         assert!(!((1.0, 2.0) < (nan, 3.0)));
424         assert!(!((1.0, 2.0) <= (nan, 3.0)));
425         assert!(!((1.0, 2.0) > (nan, 3.0)));
426         assert!(!((1.0, 2.0) >= (nan, 3.0)));
427         assert!(((1.0, 2.0) < (2.0, nan)));
428         assert!(!((2.0, 2.0) < (2.0, nan)));
429
430         // TotalEq
431         assert!(small.equals(&small));
432         assert!(big.equals(&big));
433         assert!(!small.equals(&big));
434         assert!(!big.equals(&small));
435
436         // TotalOrd
437         assert_eq!(small.cmp(&small), Equal);
438         assert_eq!(big.cmp(&big), Equal);
439         assert_eq!(small.cmp(&big), Less);
440         assert_eq!(big.cmp(&small), Greater);
441     }
442 }