Remove standalone comparison functions in vec, make the trait impls better.

This commit is contained in:
Huon Wilson
2013-07-03 12:13:00 +10:00
parent 9207802589
commit f19fb2459f
15 changed files with 184 additions and 206 deletions

View File

@@ -601,9 +601,8 @@ fn make_run_args(config: &config, _props: &TestProps, testfile: &Path) ->
ProcArgs {
// If we've got another tool to run under (valgrind),
// then split apart its command
let toolargs = split_maybe_args(&config.runtool);
let mut args = toolargs + [make_exe_name(config, testfile).to_str()];
let mut args = split_maybe_args(&config.runtool);
args.push(make_exe_name(config, testfile).to_str());
let prog = args.shift();
return ProcArgs {prog: prog, args: args};
}

View File

@@ -240,7 +240,6 @@ impl Digest for Sha1 {
#[cfg(test)]
mod tests {
use std::vec;
use digest::{Digest, DigestUtil};
use sha1::Sha1;
@@ -337,7 +336,7 @@ mod tests {
for tests.iter().advance |t| {
(*sh).input_str(t.input);
sh.result(out);
assert!(vec::eq(t.output, out));
assert!(t.output.as_slice() == out);
let out_str = (*sh).result_str();
assert_eq!(out_str.len(), 40);
@@ -357,7 +356,7 @@ mod tests {
left = left - take;
}
sh.result(out);
assert!(vec::eq(t.output, out));
assert!(t.output.as_slice() == out);
let out_str = (*sh).result_str();
assert_eq!(out_str.len(), 40);

View File

@@ -55,7 +55,6 @@ use std::io;
use std::comm::GenericChan;
use std::comm::GenericPort;
use std::sys::size_of;
use std::vec;
/**
A FlatPort, consisting of a `BytePort` that receives byte vectors,
@@ -274,7 +273,7 @@ impl<T,U:Unflattener<T>,P:BytePort> GenericPort<T> for FlatPort<T, U, P> {
}
};
if vec::eq(command, CONTINUE) {
if CONTINUE.as_slice() == command {
let msg_len = match self.byte_port.try_recv(size_of::<u64>()) {
Some(bytes) => {
io::u64_from_be_bytes(bytes, 0, size_of::<u64>())
@@ -931,7 +930,7 @@ mod test {
fn test_try_recv_none3<P:BytePort>(loader: PortLoader<P>) {
static CONTINUE: [u8, ..4] = [0xAA, 0xBB, 0xCC, 0xDD];
// The control word is followed by garbage
let bytes = CONTINUE.to_owned() + [0];
let bytes = CONTINUE.to_owned() + &[0u8];
let port = loader(bytes);
let res: Option<int> = port.try_recv();
assert!(res.is_none());
@@ -955,7 +954,7 @@ mod test {
1, sys::size_of::<u64>()) |len_bytes| {
len_bytes.to_owned()
};
let bytes = CONTINUE.to_owned() + len_bytes + [0, 0, 0, 0];
let bytes = CONTINUE.to_owned() + len_bytes + &[0u8, 0, 0, 0];
let port = loader(bytes);

View File

@@ -207,7 +207,7 @@ impl Add<BigUint, BigUint> for BigUint {
let new_len = uint::max(self.data.len(), other.data.len());
let mut carry = 0;
let sum = do vec::from_fn(new_len) |i| {
let mut sum = do vec::from_fn(new_len) |i| {
let ai = if i < self.data.len() { self.data[i] } else { 0 };
let bi = if i < other.data.len() { other.data[i] } else { 0 };
let (hi, lo) = BigDigit::from_uint(
@@ -216,8 +216,8 @@ impl Add<BigUint, BigUint> for BigUint {
carry = hi;
lo
};
if carry == 0 { return BigUint::new(sum) };
return BigUint::new(sum + [carry]);
if carry != 0 { sum.push(carry); }
return BigUint::new(sum);
}
}
@@ -284,15 +284,15 @@ impl Mul<BigUint, BigUint> for BigUint {
if n == 1 { return copy *a; }
let mut carry = 0;
let prod = do a.data.iter().transform |ai| {
let mut prod = do a.data.iter().transform |ai| {
let (hi, lo) = BigDigit::from_uint(
(*ai as uint) * (n as uint) + (carry as uint)
);
carry = hi;
lo
}.collect::<~[BigDigit]>();
if carry == 0 { return BigUint::new(prod) };
return BigUint::new(prod + [carry]);
if carry != 0 { prod.push(carry); }
return BigUint::new(prod);
}
@@ -621,15 +621,15 @@ impl BigUint {
if n_bits == 0 || self.is_zero() { return copy *self; }
let mut carry = 0;
let shifted = do self.data.iter().transform |elem| {
let mut shifted = do self.data.iter().transform |elem| {
let (hi, lo) = BigDigit::from_uint(
(*elem as uint) << n_bits | (carry as uint)
);
carry = hi;
lo
}.collect::<~[BigDigit]>();
if carry == 0 { return BigUint::new(shifted); }
return BigUint::new(shifted + [carry]);
if carry != 0 { shifted.push(carry); }
return BigUint::new(shifted);
}

View File

@@ -131,13 +131,13 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
}
ty::ty_struct(def_id, ref substs) => {
let fields = ty::lookup_struct_fields(cx.tcx, def_id);
let ftys = do fields.map |field| {
let mut ftys = do fields.map |field| {
ty::lookup_field_type(cx.tcx, def_id, field.id, substs)
};
let packed = ty::lookup_packed(cx.tcx, def_id);
let dtor = ty::ty_dtor(cx.tcx, def_id).has_drop_flag();
let ftys =
if dtor { ftys + [ty::mk_bool()] } else { ftys };
if dtor { ftys.push(ty::mk_bool()); }
return Univariant(mk_struct(cx, ftys, packed), dtor)
}
ty::ty_enum(def_id, ref substs) => {
@@ -263,7 +263,7 @@ fn generic_fields_of(cx: &mut CrateContext, r: &Repr, sizing: bool) -> ~[Type] {
let padding = largest_size - most_aligned.size;
struct_llfields(cx, most_aligned, sizing)
+ [Type::array(&Type::i8(), padding)]
+ &[Type::array(&Type::i8(), padding)]
}
}
}
@@ -512,7 +512,7 @@ pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: int,
let discr_ty = C_int(ccx, discr);
let contents = build_const_struct(ccx, case,
~[discr_ty] + vals);
C_struct(contents + [padding(max_sz - case.size)])
C_struct(contents + &[padding(max_sz - case.size)])
}
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
if discr == nndiscr {

View File

@@ -113,7 +113,7 @@ fn shim_types(ccx: @mut CrateContext, id: ast::node_id) -> ShimTypes {
_ => ccx.sess.bug("c_arg_and_ret_lltys called on non-function type")
};
let llsig = foreign_signature(ccx, &fn_sig);
let bundle_ty = Type::struct_(llsig.llarg_tys + [llsig.llret_ty.ptr_to()], false);
let bundle_ty = Type::struct_(llsig.llarg_tys + &[llsig.llret_ty.ptr_to()], false);
let ret_def = !ty::type_is_bot(fn_sig.output) &&
!ty::type_is_nil(fn_sig.output);
let fn_ty = abi_info(ccx).compute_info(llsig.llarg_tys, llsig.llret_ty, ret_def);

View File

@@ -278,7 +278,7 @@ impl Reflector {
let opaqueptrty = ty::mk_ptr(ccx.tcx, ty::mt { ty: opaquety, mutbl: ast::m_imm });
let make_get_disr = || {
let sub_path = bcx.fcx.path + [path_name(special_idents::anon)];
let sub_path = bcx.fcx.path + &[path_name(special_idents::anon)];
let sym = mangle_internal_name_by_path_and_seq(ccx,
sub_path,
"get_disr");

View File

@@ -245,7 +245,7 @@ impl Type {
}
pub fn box(ctx: &CrateContext, ty: &Type) -> Type {
Type::struct_(Type::box_header_fields(ctx) + [*ty], false)
Type::struct_(Type::box_header_fields(ctx) + &[*ty], false)
}
pub fn opaque_box(ctx: &CrateContext) -> Type {

View File

@@ -141,7 +141,7 @@ fn first_sentence_(s: &str) -> ~str {
pub fn paragraphs(s: &str) -> ~[~str] {
let mut whitespace_lines = 0;
let mut accum = ~"";
let paras = do s.any_line_iter().fold(~[]) |paras, line| {
let mut paras = do s.any_line_iter().fold(~[]) |paras, line| {
let mut res = paras;
if line.is_whitespace() {
@@ -166,11 +166,8 @@ pub fn paragraphs(s: &str) -> ~[~str] {
res
};
if !accum.is_empty() {
paras + [accum]
} else {
if !accum.is_empty() { paras.push(accum); }
paras
}
}
#[cfg(test)]

View File

@@ -172,7 +172,7 @@ pub fn header_kind(doc: doc::ItemTag) -> ~str {
}
pub fn header_name(doc: doc::ItemTag) -> ~str {
let fullpath = (doc.path() + [doc.name()]).connect("::");
let fullpath = (doc.path() + &[doc.name()]).connect("::");
match &doc {
&doc::ModTag(_) if doc.id() != syntax::ast::crate_node_id => {
fullpath

View File

@@ -163,7 +163,7 @@ pub fn make_filename(
}
}
doc::ItemPage(doc) => {
(doc.path() + [doc.name()]).connect("_")
(doc.path() + &[doc.name()]).connect("_")
}
}
};

View File

@@ -176,13 +176,14 @@ pub fn to_managed<T:Copy>(v: &[T]) -> @[T] {
#[cfg(not(test))]
pub mod traits {
use at_vec::append;
use vec::Vector;
use kinds::Copy;
use ops::Add;
impl<'self,T:Copy> Add<&'self [T],@[T]> for @[T] {
impl<'self,T:Copy, V: Vector<T>> Add<V,@[T]> for @[T] {
#[inline]
fn add(&self, rhs: & &'self [T]) -> @[T] {
append(*self, (*rhs))
fn add(&self, rhs: &V) -> @[T] {
append(*self, rhs.as_slice())
}
}
}
@@ -312,7 +313,7 @@ mod test {
#[test]
fn append_test() {
assert_eq!(@[1,2,3] + [4,5,6], @[1,2,3,4,5,6]);
assert_eq!(@[1,2,3] + &[4,5,6], @[1,2,3,4,5,6]);
}
#[test]

View File

@@ -72,7 +72,7 @@ pub use tuple::{CloneableTuple10, CloneableTuple11, CloneableTuple12};
pub use tuple::{ImmutableTuple2, ImmutableTuple3, ImmutableTuple4, ImmutableTuple5};
pub use tuple::{ImmutableTuple6, ImmutableTuple7, ImmutableTuple8, ImmutableTuple9};
pub use tuple::{ImmutableTuple10, ImmutableTuple11, ImmutableTuple12};
pub use vec::{VectorVector, CopyableVector, ImmutableVector};
pub use vec::{Vector, VectorVector, CopyableVector, ImmutableVector};
pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector};
pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector};
pub use io::{Reader, ReaderUtil, Writer, WriterUtil};

View File

@@ -16,14 +16,13 @@ use cast::transmute;
use cast;
use container::{Container, Mutable};
use cmp;
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use cmp::{Eq, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use clone::Clone;
use iterator::{FromIterator, Iterator, IteratorUtil};
use kinds::Copy;
use libc;
use libc::c_void;
use num::Zero;
use ops::Add;
use option::{None, Option, Some};
use ptr::to_unsafe_ptr;
use ptr;
@@ -40,8 +39,6 @@ use unstable::intrinsics::{get_tydesc, contains_managed};
use vec;
use util;
#[cfg(not(test))] use cmp::Equiv;
#[doc(hidden)]
pub mod rustrt {
use libc;
@@ -549,179 +546,165 @@ pub fn as_mut_buf<T,U>(s: &mut [T], f: &fn(*mut T, uint) -> U) -> U {
// Equality
/// Tests whether two slices are equal to one another. This is only true if both
/// slices are of the same length, and each of the corresponding elements return
/// true when queried via the `eq` function.
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;
}
true
}
/// Similar to the `vec::eq` function, but this is defined for types which
/// implement `TotalEq` as opposed to types which implement `Eq`. Equality
/// comparisons are done via the `equals` function instead of `eq`.
fn equals<T: TotalEq>(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].equals(&b[i]) { return false; }
i += 1;
}
true
}
#[cfg(not(test))]
impl<'self,T:Eq> Eq for &'self [T] {
#[inline]
fn eq(&self, other: & &'self [T]) -> bool { eq(*self, *other) }
pub mod traits {
use super::Vector;
use kinds::Copy;
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Equal, Equiv};
use ops::Add;
impl<'self,T:Eq> Eq for &'self [T] {
fn eq(&self, other: & &'self [T]) -> bool {
self.len() == other.len() &&
self.iter().zip(other.iter()).all(|(s,o)| *s == *o)
}
#[inline]
fn ne(&self, other: & &'self [T]) -> bool { !self.eq(other) }
}
}
#[cfg(not(test))]
impl<T:Eq> Eq for ~[T] {
impl<T:Eq> Eq for ~[T] {
#[inline]
fn eq(&self, other: &~[T]) -> bool { eq(*self, *other) }
fn eq(&self, other: &~[T]) -> bool { self.as_slice() == *other }
#[inline]
fn ne(&self, other: &~[T]) -> bool { !self.eq(other) }
}
}
#[cfg(not(test))]
impl<T:Eq> Eq for @[T] {
impl<T:Eq> Eq for @[T] {
#[inline]
fn eq(&self, other: &@[T]) -> bool { eq(*self, *other) }
fn eq(&self, other: &@[T]) -> bool { self.as_slice() == *other }
#[inline]
fn ne(&self, other: &@[T]) -> bool { !self.eq(other) }
}
}
#[cfg(not(test))]
impl<'self,T:TotalEq> TotalEq for &'self [T] {
#[inline]
fn equals(&self, other: & &'self [T]) -> bool { equals(*self, *other) }
}
#[cfg(not(test))]
impl<T:TotalEq> TotalEq for ~[T] {
#[inline]
fn equals(&self, other: &~[T]) -> bool { equals(*self, *other) }
}
#[cfg(not(test))]
impl<T:TotalEq> TotalEq for @[T] {
#[inline]
fn equals(&self, other: &@[T]) -> bool { equals(*self, *other) }
}
#[cfg(not(test))]
impl<'self,T:Eq> Equiv<~[T]> for &'self [T] {
#[inline]
fn equiv(&self, other: &~[T]) -> bool { eq(*self, *other) }
}
// Lexicographical comparison
fn cmp<T: TotalOrd>(a: &[T], b: &[T]) -> Ordering {
let low = uint::min(a.len(), b.len());
for uint::range(0, low) |idx| {
match a[idx].cmp(&b[idx]) {
Greater => return Greater,
Less => return Less,
Equal => ()
impl<'self,T:TotalEq> TotalEq for &'self [T] {
fn equals(&self, other: & &'self [T]) -> bool {
self.len() == other.len() &&
self.iter().zip(other.iter()).all(|(s,o)| s.equals(o))
}
}
a.len().cmp(&b.len())
}
#[cfg(not(test))]
impl<'self,T:TotalOrd> TotalOrd for &'self [T] {
impl<T:TotalEq> TotalEq for ~[T] {
#[inline]
fn cmp(&self, other: & &'self [T]) -> Ordering { cmp(*self, *other) }
}
#[cfg(not(test))]
impl<T: TotalOrd> TotalOrd for ~[T] {
#[inline]
fn cmp(&self, other: &~[T]) -> Ordering { cmp(*self, *other) }
}
#[cfg(not(test))]
impl<T: TotalOrd> TotalOrd for @[T] {
#[inline]
fn cmp(&self, other: &@[T]) -> Ordering { cmp(*self, *other) }
}
fn lt<T:Ord>(a: &[T], b: &[T]) -> bool {
let (a_len, b_len) = (a.len(), b.len());
let 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;
fn equals(&self, other: &~[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
}
a_len < b_len
}
fn le<T:Ord>(a: &[T], b: &[T]) -> bool { !lt(b, a) }
fn ge<T:Ord>(a: &[T], b: &[T]) -> bool { !lt(a, b) }
fn gt<T:Ord>(a: &[T], b: &[T]) -> bool { lt(b, a) }
#[cfg(not(test))]
impl<'self,T:Ord> Ord for &'self [T] {
impl<T:TotalEq> TotalEq for @[T] {
#[inline]
fn lt(&self, other: & &'self [T]) -> bool { lt((*self), (*other)) }
#[inline]
fn le(&self, other: & &'self [T]) -> bool { le((*self), (*other)) }
#[inline]
fn ge(&self, other: & &'self [T]) -> bool { ge((*self), (*other)) }
#[inline]
fn gt(&self, other: & &'self [T]) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<T:Ord> Ord for ~[T] {
#[inline]
fn lt(&self, other: &~[T]) -> bool { lt((*self), (*other)) }
#[inline]
fn le(&self, other: &~[T]) -> bool { le((*self), (*other)) }
#[inline]
fn ge(&self, other: &~[T]) -> bool { ge((*self), (*other)) }
#[inline]
fn gt(&self, other: &~[T]) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<T:Ord> Ord for @[T] {
#[inline]
fn lt(&self, other: &@[T]) -> bool { lt((*self), (*other)) }
#[inline]
fn le(&self, other: &@[T]) -> bool { le((*self), (*other)) }
#[inline]
fn ge(&self, other: &@[T]) -> bool { ge((*self), (*other)) }
#[inline]
fn gt(&self, other: &@[T]) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<'self,T:Copy> Add<&'self [T], ~[T]> for ~[T] {
#[inline]
fn add(&self, rhs: & &'self [T]) -> ~[T] {
append(copy *self, (*rhs))
fn equals(&self, other: &@[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
}
impl<'self,T:Eq, V: Vector<T>> Equiv<V> for &'self [T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'self,T:Eq, V: Vector<T>> Equiv<V> for ~[T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'self,T:Eq, V: Vector<T>> Equiv<V> for @[T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'self,T:TotalOrd> TotalOrd for &'self [T] {
fn cmp(&self, other: & &'self [T]) -> Ordering {
for self.iter().zip(other.iter()).advance |(s,o)| {
match s.cmp(o) {
Equal => {},
non_eq => { return non_eq; }
}
}
self.len().cmp(&other.len())
}
}
impl<T: TotalOrd> TotalOrd for ~[T] {
#[inline]
fn cmp(&self, other: &~[T]) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
}
impl<T: TotalOrd> TotalOrd for @[T] {
#[inline]
fn cmp(&self, other: &@[T]) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
}
impl<'self,T:Ord> Ord for &'self [T] {
fn lt(&self, other: & &'self [T]) -> bool {
for self.iter().zip(other.iter()).advance |(s,o)| {
if *s < *o { return true; }
if *s > *o { return false; }
}
self.len() < other.len()
}
#[inline]
fn le(&self, other: & &'self [T]) -> bool { !(*other < *self) }
#[inline]
fn ge(&self, other: & &'self [T]) -> bool { !(*self < *other) }
#[inline]
fn gt(&self, other: & &'self [T]) -> bool { *other < *self }
}
impl<T:Ord> Ord for ~[T] {
#[inline]
fn lt(&self, other: &~[T]) -> bool { self.as_slice() < other.as_slice() }
#[inline]
fn le(&self, other: &~[T]) -> bool { self.as_slice() <= other.as_slice() }
#[inline]
fn ge(&self, other: &~[T]) -> bool { self.as_slice() >= other.as_slice() }
#[inline]
fn gt(&self, other: &~[T]) -> bool { self.as_slice() > other.as_slice() }
}
impl<T:Ord> Ord for @[T] {
#[inline]
fn lt(&self, other: &@[T]) -> bool { self.as_slice() < other.as_slice() }
#[inline]
fn le(&self, other: &@[T]) -> bool { self.as_slice() <= other.as_slice() }
#[inline]
fn ge(&self, other: &@[T]) -> bool { self.as_slice() >= other.as_slice() }
#[inline]
fn gt(&self, other: &@[T]) -> bool { self.as_slice() > other.as_slice() }
}
impl<'self,T:Copy, V: Vector<T>> Add<V, ~[T]> for &'self [T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
let mut res = self.to_owned();
res.push_all(rhs.as_slice());
res
}
}
impl<T:Copy, V: Vector<T>> Add<V, ~[T]> for ~[T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
let mut res = self.to_owned();
res.push_all(rhs.as_slice());
res
}
}
}
#[cfg(test)]
pub mod traits {}
/// Any vector that can be represented as a slice.
pub trait Vector<T> {
/// Work with `self` as a slice.
fn as_slice<'a>(&'a self) -> &'a [T];
}
impl<'self,T> Vector<T> for &'self [T] {
#[inline(always)]
fn as_slice<'a>(&'a self) -> &'a [T] { *self }
}
impl<T> Vector<T> for ~[T] {
#[inline(always)]
fn as_slice<'a>(&'a self) -> &'a [T] { let v: &'a [T] = *self; v }
}
impl<T> Vector<T> for @[T] {
#[inline(always)]
fn as_slice<'a>(&'a self) -> &'a [T] { let v: &'a [T] = *self; v }
}
impl<'self, T> Container for &'self [T] {

View File

@@ -96,7 +96,7 @@ fn recurse_or_fail(depth: int, st: Option<State>) {
fn_box: || @Cons((), fn_box()),
tuple: (@Cons((), st.tuple.first()),
~Cons((), @*st.tuple.second())),
vec: st.vec + [@Cons((), *st.vec.last())],
vec: st.vec + &[@Cons((), *st.vec.last())],
res: r(@Cons((), st.res._l))
}
}