rust/src/libcore/tuple.rs

372 lines
12 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations on tuples
2012-03-15 20:58:14 -05:00
use kinds::Copy;
use vec;
2012-08-27 18:26:35 -05:00
pub use self::inner::*;
pub trait CopyableTuple<T, U> {
fn first(&self) -> T;
fn second(&self) -> U;
fn swap(&self) -> (U, T);
}
2012-01-17 12:14:05 -06:00
impl<T:Copy,U:Copy> CopyableTuple<T, U> for (T, U) {
/// Return the first element of self
#[inline(always)]
fn first(&self) -> T {
match *self {
(t, _) => t,
}
}
/// Return the second element of self
#[inline(always)]
fn second(&self) -> U {
match *self {
(_, u) => u,
}
}
/// Return the results of swapping the two elements of self
#[inline(always)]
fn swap(&self) -> (U, T) {
match *self {
(t, u) => (u, t),
}
}
2013-04-03 01:40:27 -05:00
}
2013-04-10 15:11:35 -05:00
pub trait ImmutableTuple<T, U> {
fn first_ref<'a>(&'a self) -> &'a T;
fn second_ref<'a>(&'a self) -> &'a U;
}
impl<T, U> ImmutableTuple<T, U> for (T, U) {
#[inline(always)]
fn first_ref<'a>(&'a self) -> &'a T {
match *self {
(ref t, _) => t,
}
}
#[inline(always)]
fn second_ref<'a>(&'a self) -> &'a U {
match *self {
(_, ref u) => u,
}
}
}
pub trait ExtendedTupleOps<A,B> {
2012-09-28 00:20:47 -05:00
fn zip(&self) -> ~[(A, B)];
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C];
}
impl<'self,A:Copy,B:Copy> ExtendedTupleOps<A,B> for (&'self [A], &'self [B]) {
#[inline(always)]
2012-09-28 00:20:47 -05:00
fn zip(&self) -> ~[(A, B)] {
match *self {
(ref a, ref b) => {
vec::zip_slice(*a, *b)
}
}
}
#[inline(always)]
2012-09-28 00:20:47 -05:00
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
match *self {
(ref a, ref b) => {
2013-04-25 00:38:44 -05:00
vec::map_zip(*a, *b, f)
2012-09-28 00:20:47 -05:00
}
}
}
}
impl<A:Copy,B:Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
#[inline(always)]
2012-09-28 00:20:47 -05:00
fn zip(&self) -> ~[(A, B)] {
match *self {
(ref a, ref b) => {
vec::zip_slice(*a, *b)
}
}
}
#[inline(always)]
2012-09-28 00:20:47 -05:00
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
match *self {
(ref a, ref b) => {
2013-04-25 00:38:44 -05:00
vec::map_zip(*a, *b, f)
2012-09-28 00:20:47 -05:00
}
}
}
}
2012-01-17 12:14:05 -06:00
// macro for implementing n-ary tuple functions and operations
macro_rules! tuple_impls(
($(
($cloneable_trait:ident, $immutable_trait:ident) {
$(($get_fn:ident, $get_ref_fn:ident) -> $T:ident {
$get_pattern:pat => $ret:expr
})+
}
)+) => (
pub mod inner {
use clone::Clone;
#[cfg(not(test))] use cmp::{Eq, Ord};
$(
pub trait $cloneable_trait<$($T),+> {
$(fn $get_fn(&self) -> $T;)+
}
impl<$($T:Clone),+> $cloneable_trait<$($T),+> for ($($T),+) {
$(
#[inline(always)]
fn $get_fn(&self) -> $T {
self.$get_ref_fn().clone()
}
)+
}
pub trait $immutable_trait<$($T),+> {
$(fn $get_ref_fn<'a>(&'a self) -> &'a $T;)+
}
impl<$($T),+> $immutable_trait<$($T),+> for ($($T),+) {
$(
#[inline(always)]
fn $get_ref_fn<'a>(&'a self) -> &'a $T {
match *self { $get_pattern => $ret }
}
)+
}
2012-08-27 18:26:35 -05:00
impl<$($T:Clone),+> Clone for ($($T),+) {
fn clone(&self) -> ($($T),+) {
($(self.$get_ref_fn().clone()),+)
}
}
2012-08-27 18:26:35 -05:00
#[cfg(not(test))]
impl<$($T:Eq),+> Eq for ($($T),+) {
#[inline(always)]
fn eq(&self, other: &($($T),+)) -> bool {
$(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
}
#[inline(always)]
fn ne(&self, other: &($($T),+)) -> bool {
!(*self == *other)
}
}
2012-08-27 18:26:35 -05:00
#[cfg(not(test))]
impl<$($T:Ord),+> Ord for ($($T),+) {
#[inline(always)]
fn lt(&self, other: &($($T),+)) -> bool {
lexical_lt!($(*self.$get_ref_fn(), *other.$get_ref_fn()),+)
}
#[inline(always)]
fn le(&self, other: &($($T),+)) -> bool { !(*other).lt(&(*self)) }
#[inline(always)]
fn ge(&self, other: &($($T),+)) -> bool { !(*self).lt(other) }
#[inline(always)]
fn gt(&self, other: &($($T),+)) -> bool { (*other).lt(&(*self)) }
}
)+
2013-05-18 03:59:16 -05:00
}
)
)
// Constructs an expression that performs a lexical less-than ordering.
// The values are interleaved, so the macro invocation for
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_lt!(a1, b1, a2, b2, a3, b3)`
macro_rules! lexical_lt(
($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => (
if $a < $b { true } else { lexical_lt!($($rest_a, $rest_b),+) }
);
($a:expr, $b:expr) => ($a < $b);
)
tuple_impls!(
(CloneableTuple2, ImmutableTuple2) {
(n0, n0_ref) -> A { (ref a,_) => a }
(n1, n1_ref) -> B { (_,ref b) => b }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple3, ImmutableTuple3) {
(n0, n0_ref) -> A { (ref a,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_) => b }
(n2, n2_ref) -> C { (_,_,ref c) => c }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple4, ImmutableTuple4) {
(n0, n0_ref) -> A { (ref a,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d) => d }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple5, ImmutableTuple5) {
(n0, n0_ref) -> A { (ref a,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e) => e }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple6, ImmutableTuple6) {
(n0, n0_ref) -> A { (ref a,_,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e,_) => e }
(n5, n5_ref) -> F { (_,_,_,_,_,ref f) => f }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple7, ImmutableTuple7) {
(n0, n0_ref) -> A { (ref a,_,_,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_,_,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e,_,_) => e }
(n5, n5_ref) -> F { (_,_,_,_,_,ref f,_) => f }
(n6, n6_ref) -> G { (_,_,_,_,_,_,ref g) => g }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple8, ImmutableTuple8) {
(n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_) => e }
(n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_) => f }
(n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_) => g }
(n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h) => h }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple9, ImmutableTuple9) {
(n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_) => e }
(n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_) => f }
(n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_) => g }
(n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_) => h }
(n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i) => i }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple10, ImmutableTuple10) {
(n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_,_) => e }
(n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_,_) => f }
(n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_,_) => g }
(n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_,_) => h }
(n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i,_) => i }
(n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,ref j) => j }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple11, ImmutableTuple11) {
(n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_,_,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_,_,_) => e }
(n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_,_,_) => f }
(n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_,_,_) => g }
(n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_,_,_) => h }
(n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i,_,_) => i }
(n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,ref j,_) => j }
(n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,ref k) => k }
}
2013-05-18 03:59:16 -05:00
(CloneableTuple12, ImmutableTuple12) {
(n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_,_,_,_) => a }
(n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_,_,_,_) => b }
(n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_,_,_,_) => c }
(n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_,_,_,_) => d }
(n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_,_,_,_) => e }
(n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_,_,_,_) => f }
(n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_,_,_,_) => g }
(n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_,_,_,_) => h }
(n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i,_,_,_) => i }
(n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,ref j,_,_) => j }
(n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,ref k,_) => k }
(n11, n11_ref) -> L { (_,_,_,_,_,_,_,_,_,_,_,ref l) => l }
}
2013-05-18 03:59:16 -05:00
)
2013-05-18 11:23:52 -05:00
#[cfg(test)]
mod tests {
use super::*;
use clone::Clone;
#[test]
fn test_tuple_ref() {
let x = (~"foo", ~"bar");
assert_eq!(x.first_ref(), &~"foo");
assert_eq!(x.second_ref(), &~"bar");
}
2013-05-18 11:23:52 -05:00
#[test]
#[allow(non_implicitly_copyable_typarams)]
fn test_tuple() {
assert_eq!((948, 4039.48).first(), 948);
assert_eq!((34.5, ~"foo").second(), ~"foo");
assert_eq!(('a', 2).swap(), (2, 'a'));
}
2012-01-17 12:14:05 -06:00
2013-05-18 11:23:52 -05:00
#[test]
fn test_clone() {
let a = (1, ~"2");
let b = a.clone();
assert_eq!(a.first(), b.first());
assert_eq!(a.second(), b.second());
}
2013-05-18 03:59:16 -05:00
2013-05-18 11:23:52 -05:00
#[test]
fn test_n_tuple() {
let t = (0u8, 1u16, 2u32, 3u64, 4u, 5i8, 6i16, 7i32, 8i64, 9i, 10f32, 11f64);
assert_eq!(t.n0(), 0u8);
assert_eq!(t.n1(), 1u16);
assert_eq!(t.n2(), 2u32);
assert_eq!(t.n3(), 3u64);
assert_eq!(t.n4(), 4u);
assert_eq!(t.n5(), 5i8);
assert_eq!(t.n6(), 6i16);
assert_eq!(t.n7(), 7i32);
assert_eq!(t.n8(), 8i64);
assert_eq!(t.n9(), 9i);
assert_eq!(t.n10(), 10f32);
assert_eq!(t.n11(), 11f64);
assert_eq!(t.n0_ref(), &0u8);
assert_eq!(t.n1_ref(), &1u16);
assert_eq!(t.n2_ref(), &2u32);
assert_eq!(t.n3_ref(), &3u64);
assert_eq!(t.n4_ref(), &4u);
assert_eq!(t.n5_ref(), &5i8);
assert_eq!(t.n6_ref(), &6i16);
assert_eq!(t.n7_ref(), &7i32);
assert_eq!(t.n8_ref(), &8i64);
assert_eq!(t.n9_ref(), &9i);
assert_eq!(t.n10_ref(), &10f32);
assert_eq!(t.n11_ref(), &11f64);
}
2013-05-18 03:59:16 -05:00
}