提交 0e5a0e1d 编写于 作者: P Patrick Walton

libcore: Implement equality and ordering on vectors

上级 88e0476b
//! Vectors
import cmp::{Eq, Ord};
import option::{Some, None};
import ptr::addr_of;
import libc::size_t;
......@@ -1374,6 +1375,80 @@ fn iter2<U, T>(v1: &[U], v2: &[T], f: fn(U, T)) {
}
}
// Equality
pure fn eq<T: Eq>(a: &[T], b: &[T]) -> bool {
let (a_len, b_len) = (a.len(), b.len());
if a_len != b_len { return false; }
let mut i = 0;
while i < a_len {
if a[i] != b[i] { return false; }
i += 1;
}
return true;
}
impl<T: Eq> &[T]: Eq {
#[inline(always)]
pure fn eq(&&other: &[T]) -> bool {
eq(self, other)
}
}
impl<T: Eq> ~[T]: Eq {
#[inline(always)]
pure fn eq(&&other: ~[T]) -> bool {
eq(self, other)
}
}
impl<T: Eq> @[T]: Eq {
#[inline(always)]
pure fn eq(&&other: @[T]) -> bool {
eq(self, other)
}
}
// Lexicographical comparison
pure fn lt<T: Ord>(a: &[T], b: &[T]) -> bool {
let (a_len, b_len) = (a.len(), b.len());
let mut end = uint::min(a_len, b_len);
let mut i = 0;
while i < end {
let (c_a, c_b) = (&a[i], &b[i]);
if *c_a < *c_b { return true; }
if *c_a > *c_b { return false; }
i += 1;
}
return a_len < b_len;
}
impl<T: Ord> &[T]: Ord {
#[inline(always)]
pure fn lt(&&other: &[T]) -> bool {
lt(self, other)
}
}
impl<T: Ord> ~[T]: Ord {
#[inline(always)]
pure fn lt(&&other: ~[T]) -> bool {
lt(self, other)
}
}
impl<T: Ord> @[T]: Ord {
#[inline(always)]
pure fn lt(&&other: @[T]) -> bool {
lt(self, other)
}
}
#[cfg(notest)]
impl<T: copy> ~[T]: add<&[const T],~[T]> {
#[inline(always)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册