2012-12-04 00:48:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2012-07-04 21:53:12 +00:00
|
|
|
//! Operations on tuples
|
2012-03-16 01:58:14 +00:00
|
|
|
|
2013-01-09 03:37:25 +00:00
|
|
|
use kinds::Copy;
|
2012-12-23 22:41:37 +00:00
|
|
|
use vec;
|
2012-08-27 23:26:35 +00:00
|
|
|
|
2013-02-28 16:57:33 +00:00
|
|
|
#[cfg(notest)] use cmp::{Eq, Ord};
|
|
|
|
|
2012-10-07 23:01:01 +00:00
|
|
|
pub trait CopyableTuple<T, U> {
|
2013-03-05 03:36:15 +00:00
|
|
|
pure fn first(&self) -> T;
|
|
|
|
pure fn second(&self) -> U;
|
|
|
|
pure fn swap(&self) -> (U, T);
|
2012-07-17 23:49:54 +00:00
|
|
|
}
|
2012-01-17 18:14:05 +00:00
|
|
|
|
2013-02-21 01:07:17 +00:00
|
|
|
impl<T:Copy,U:Copy> CopyableTuple<T, U> for (T, U) {
|
2012-07-16 21:32:59 +00:00
|
|
|
|
|
|
|
/// Return the first element of self
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2013-03-05 03:36:15 +00:00
|
|
|
pure fn first(&self) -> T {
|
|
|
|
let (t, _) = *self;
|
2012-08-02 00:30:05 +00:00
|
|
|
return t;
|
2012-07-16 21:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the second element of self
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2013-03-05 03:36:15 +00:00
|
|
|
pure fn second(&self) -> U {
|
|
|
|
let (_, u) = *self;
|
2012-08-02 00:30:05 +00:00
|
|
|
return u;
|
2012-07-16 21:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the results of swapping the two elements of self
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2013-03-05 03:36:15 +00:00
|
|
|
pure fn swap(&self) -> (U, T) {
|
|
|
|
let (t, u) = *self;
|
2012-08-02 00:30:05 +00:00
|
|
|
return (u, t);
|
2012-07-16 21:32:59 +00:00
|
|
|
}
|
2012-01-17 18:14:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-07 23:01:01 +00:00
|
|
|
pub trait ImmutableTuple<T, U> {
|
|
|
|
pure fn first_ref(&self) -> &self/T;
|
|
|
|
pure fn second_ref(&self) -> &self/U;
|
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl<T, U> ImmutableTuple<T, U> for (T, U) {
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-10-07 23:01:01 +00:00
|
|
|
pure fn first_ref(&self) -> &self/T {
|
|
|
|
match *self {
|
|
|
|
(ref t, _) => t,
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-10-07 23:01:01 +00:00
|
|
|
pure fn second_ref(&self) -> &self/U {
|
|
|
|
match *self {
|
|
|
|
(_, ref u) => u,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 22:42:13 +00:00
|
|
|
pub trait ExtendedTupleOps<A,B> {
|
2012-09-28 05:20:47 +00:00
|
|
|
fn zip(&self) -> ~[(A, B)];
|
|
|
|
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C];
|
2012-07-17 23:49:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 19:34:00 +00:00
|
|
|
impl<A:Copy,B:Copy> ExtendedTupleOps<A,B> for (&self/[A], &self/[B]) {
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-09-28 05:20:47 +00:00
|
|
|
fn zip(&self) -> ~[(A, B)] {
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b) => {
|
|
|
|
vec::zip_slice(*a, *b)
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 00:27:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-09-28 05:20:47 +00:00
|
|
|
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b) => {
|
|
|
|
vec::map2(*a, *b, f)
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 00:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:07:17 +00:00
|
|
|
impl<A:Copy,B:Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
|
2012-07-17 23:49:54 +00:00
|
|
|
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-09-28 05:20:47 +00:00
|
|
|
fn zip(&self) -> ~[(A, B)] {
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b) => {
|
|
|
|
vec::zip_slice(*a, *b)
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 00:27:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-09-28 05:20:47 +00:00
|
|
|
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b) => {
|
|
|
|
vec::map2(*a, *b, f)
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 00:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-17 18:14:05 +00:00
|
|
|
|
2013-02-19 01:45:56 +00:00
|
|
|
// FIXME #4898: impl for one-tuples
|
|
|
|
|
2012-11-30 08:47:45 +00:00
|
|
|
#[cfg(notest)]
|
2013-02-21 01:07:17 +00:00
|
|
|
impl<A:Eq,B:Eq> Eq for (A, B) {
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn eq(&self, other: &(A, B)) -> bool {
|
|
|
|
match (*self) {
|
|
|
|
(ref self_a, ref self_b) => match other {
|
|
|
|
&(ref other_a, ref other_b) => {
|
|
|
|
(*self_a).eq(other_a) && (*self_b).eq(other_b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn ne(&self, other: &(A, B)) -> bool { !(*self).eq(other) }
|
2012-09-20 01:00:26 +00:00
|
|
|
}
|
2012-08-27 23:26:35 +00:00
|
|
|
|
2012-11-30 08:47:45 +00:00
|
|
|
#[cfg(notest)]
|
2013-02-21 01:07:17 +00:00
|
|
|
impl<A:Ord,B:Ord> Ord for (A, B) {
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn lt(&self, other: &(A, B)) -> bool {
|
|
|
|
match (*self) {
|
|
|
|
(ref self_a, ref self_b) => {
|
|
|
|
match (*other) {
|
|
|
|
(ref other_a, ref other_b) => {
|
|
|
|
if (*self_a).lt(other_a) { return true; }
|
|
|
|
if (*other_a).lt(self_a) { return false; }
|
|
|
|
if (*self_b).lt(other_b) { return true; }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn le(&self, other: &(A, B)) -> bool { !(*other).lt(&(*self)) }
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn ge(&self, other: &(A, B)) -> bool { !(*self).lt(other) }
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn gt(&self, other: &(A, B)) -> bool { (*other).lt(&(*self)) }
|
2012-09-20 01:00:26 +00:00
|
|
|
}
|
2012-08-27 23:26:35 +00:00
|
|
|
|
2012-11-30 08:47:45 +00:00
|
|
|
#[cfg(notest)]
|
2013-02-21 01:07:17 +00:00
|
|
|
impl<A:Eq,B:Eq,C:Eq> Eq for (A, B, C) {
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn eq(&self, other: &(A, B, C)) -> bool {
|
|
|
|
match (*self) {
|
|
|
|
(ref self_a, ref self_b, ref self_c) => match other {
|
|
|
|
&(ref other_a, ref other_b, ref other_c) => {
|
|
|
|
(*self_a).eq(other_a) && (*self_b).eq(other_b)
|
|
|
|
&& (*self_c).eq(other_c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn ne(&self, other: &(A, B, C)) -> bool { !(*self).eq(other) }
|
2012-09-20 01:00:26 +00:00
|
|
|
}
|
2012-08-27 23:26:35 +00:00
|
|
|
|
2012-11-30 08:47:45 +00:00
|
|
|
#[cfg(notest)]
|
2013-02-21 01:07:17 +00:00
|
|
|
impl<A:Ord,B:Ord,C:Ord> Ord for (A, B, C) {
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn lt(&self, other: &(A, B, C)) -> bool {
|
|
|
|
match (*self) {
|
|
|
|
(ref self_a, ref self_b, ref self_c) => {
|
|
|
|
match (*other) {
|
|
|
|
(ref other_a, ref other_b, ref other_c) => {
|
|
|
|
if (*self_a).lt(other_a) { return true; }
|
|
|
|
if (*other_a).lt(self_a) { return false; }
|
|
|
|
if (*self_b).lt(other_b) { return true; }
|
|
|
|
if (*other_b).lt(self_b) { return false; }
|
|
|
|
if (*self_c).lt(other_c) { return true; }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn le(&self, other: &(A, B, C)) -> bool { !(*other).lt(&(*self)) }
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn ge(&self, other: &(A, B, C)) -> bool { !(*self).lt(other) }
|
2013-01-13 14:37:30 +00:00
|
|
|
#[inline(always)]
|
2012-11-15 02:59:30 +00:00
|
|
|
pure fn gt(&self, other: &(A, B, C)) -> bool { (*other).lt(&(*self)) }
|
2012-09-20 01:00:26 +00:00
|
|
|
}
|
2012-08-27 23:26:35 +00:00
|
|
|
|
2012-10-07 23:01:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tuple_ref() {
|
2012-10-08 03:30:17 +00:00
|
|
|
let x = (~"foo", ~"bar");
|
2013-03-06 21:58:02 +00:00
|
|
|
fail_unless!(x.first_ref() == &~"foo");
|
|
|
|
fail_unless!(x.second_ref() == &~"bar");
|
2012-10-07 23:01:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 18:14:05 +00:00
|
|
|
#[test]
|
2012-09-02 23:34:20 +00:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-01-17 18:14:05 +00:00
|
|
|
fn test_tuple() {
|
2013-03-06 21:58:02 +00:00
|
|
|
fail_unless!((948, 4039.48).first() == 948);
|
|
|
|
fail_unless!((34.5, ~"foo").second() == ~"foo");
|
|
|
|
fail_unless!(('a', 2).swap() == (2, 'a'));
|
2012-01-17 18:14:05 +00:00
|
|
|
}
|
|
|
|
|