std,extra: remove use of & support for @[].

This commit is contained in:
Huon Wilson 2014-02-01 15:57:17 +11:00
parent e0c1707560
commit 2ed980fe25
14 changed files with 2 additions and 566 deletions

View File

@ -80,7 +80,7 @@ pub mod BigDigit {
/**
A big unsigned integer type.
A `BigUint`-typed value `BigUint { data: @[a, b, c] }` represents a number
A `BigUint`-typed value `BigUint { data: ~[a, b, c] }` represents a number
`(a + b * BigDigit::base + c * BigDigit::base^2)`.
*/
#[deriving(Clone)]

View File

@ -18,7 +18,6 @@ Core encoding and decoding interfaces.
#[forbid(non_camel_case_types)];
use std::at_vec;
use std::hashmap::{HashMap, HashSet};
use std::rc::Rc;
use std::trie::{TrieMap, TrieSet};
@ -444,26 +443,6 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~[T] {
}
}
impl<S:Encoder,T:Encodable<S>> Encodable<S> for @[T] {
fn encode(&self, s: &mut S) {
s.emit_seq(self.len(), |s| {
for (i, e) in self.iter().enumerate() {
s.emit_seq_elt(i, |s| e.encode(s))
}
})
}
}
impl<D:Decoder,T:Decodable<D>> Decodable<D> for @[T] {
fn decode(d: &mut D) -> @[T] {
d.read_seq(|d, len| {
at_vec::from_fn(len, |i| {
d.read_seq_elt(i, |d| Decodable::decode(d))
})
})
}
}
impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> {
fn encode(&self, s: &mut S) {
s.emit_option(|s| {

View File

@ -1,423 +0,0 @@
// 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 managed vectors (`@[T]` type)
use clone::Clone;
use container::Container;
use iter::{Iterator, FromIterator};
use option::{Option, Some, None};
use mem;
use unstable::raw::Repr;
use vec::{ImmutableVector, OwnedVector};
/// Code for dealing with @-vectors. This is pretty incomplete, and
/// contains a bunch of duplication from the code for ~-vectors.
/// Returns the number of elements the vector can hold without reallocating
#[inline]
pub fn capacity<T>(v: @[T]) -> uint {
unsafe {
let managed_box = v.repr();
(*managed_box).data.alloc / mem::size_of::<T>()
}
}
/**
* Builds a vector by calling a provided function with an argument
* function that pushes an element to the back of a vector.
* The initial size for the vector may optionally be specified
*
* # Arguments
*
* * size - An option, maybe containing initial size of the vector to reserve
* * builder - A function that will construct the vector. It receives
* as an argument a function that will push an element
* onto the vector being constructed.
*/
#[inline]
pub fn build<A>(size: Option<uint>, builder: |push: |v: A||) -> @[A] {
let mut vec = @[];
unsafe { raw::reserve(&mut vec, size.unwrap_or(4)); }
builder(|x| unsafe { raw::push(&mut vec, x) });
vec
}
// Appending
/// Iterates over the `rhs` vector, copying each element and appending it to the
/// `lhs`. Afterwards, the `lhs` is then returned for use again.
#[inline]
pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] {
build(Some(lhs.len() + rhs.len()), |push| {
for x in lhs.iter() {
push((*x).clone());
}
for elt in rhs.iter() {
push(elt.clone());
}
})
}
/// Apply a function to each element of a vector and return the results
#[inline]
pub fn map<T, U>(v: &[T], f: |x: &T| -> U) -> @[U] {
build(Some(v.len()), |push| {
for elem in v.iter() {
push(f(elem));
}
})
}
/**
* Creates and initializes an immutable vector.
*
* Creates an immutable vector of size `n_elts` and initializes the elements
* to the value returned by the function `op`.
*/
#[inline]
pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> @[T] {
build(Some(n_elts), |push| {
let mut i: uint = 0u;
while i < n_elts { push(op(i)); i += 1u; }
})
}
/**
* Creates and initializes an immutable vector.
*
* Creates an immutable vector of size `n_elts` and initializes the elements
* to the value `t`.
*/
#[inline]
pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] {
build(Some(n_elts), |push| {
let mut i: uint = 0u;
while i < n_elts {
push(t.clone());
i += 1u;
}
})
}
/**
* Creates and initializes an immutable managed vector by moving all the
* elements from an owned vector.
*/
#[inline]
pub fn to_managed_move<T>(v: ~[T]) -> @[T] {
let mut av = @[];
unsafe {
raw::reserve(&mut av, v.len());
for x in v.move_iter() {
raw::push(&mut av, x);
}
av
}
}
/**
* Creates and initializes an immutable managed vector by copying all the
* elements of a slice.
*/
#[inline]
pub fn to_managed<T:Clone>(v: &[T]) -> @[T] {
from_fn(v.len(), |i| v[i].clone())
}
impl<T> Clone for @[T] {
fn clone(&self) -> @[T] {
*self
}
}
impl<A> FromIterator<A> for @[A] {
#[inline]
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> @[A] {
let (lower, _) = iterator.size_hint();
build(Some(lower), |push| {
for x in *iterator {
push(x);
}
})
}
}
#[cfg(not(test))]
#[allow(missing_doc)]
pub mod traits {
use at_vec::append;
use clone::Clone;
use ops::Add;
use vec::Vector;
impl<'a,T:Clone, V: Vector<T>> Add<V,@[T]> for @[T] {
#[inline]
fn add(&self, rhs: &V) -> @[T] {
append(*self, rhs.as_slice())
}
}
}
#[cfg(test)]
pub mod traits {}
#[allow(missing_doc)]
pub mod raw {
use at_vec::capacity;
use cast;
use cast::{transmute, transmute_copy};
use container::Container;
use option::None;
use mem;
use num::next_power_of_two;
use ptr;
use unstable::intrinsics::{move_val_init, TyDesc};
use unstable::intrinsics;
use unstable::raw::{Box, Vec};
/**
* Sets the length of a vector
*
* This will explicitly set the size of the vector, without actually
* modifying its buffers, so it is up to the caller to ensure that
* the vector is actually the specified size.
*/
#[inline]
pub unsafe fn set_len<T>(v: &mut @[T], new_len: uint) {
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
(*repr).data.fill = new_len * mem::size_of::<T>();
}
/**
* Pushes a new value onto this vector.
*/
#[inline]
pub unsafe fn push<T>(v: &mut @[T], initval: T) {
let full = {
let repr: *Box<Vec<T>> = cast::transmute_copy(v);
(*repr).data.alloc > (*repr).data.fill
};
if full {
push_fast(v, initval);
} else {
push_slow(v, initval);
}
}
#[inline] // really pretty please
unsafe fn push_fast<T>(v: &mut @[T], initval: T) {
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
let amt = v.len();
(*repr).data.fill += mem::size_of::<T>();
let p = ptr::offset(&(*repr).data.data as *T, amt as int) as *mut T;
move_val_init(&mut(*p), initval);
}
#[inline]
unsafe fn push_slow<T>(v: &mut @[T], initval: T) {
reserve_at_least(v, v.len() + 1u);
push_fast(v, initval);
}
/**
* Reserves capacity for exactly `n` elements in the given vector.
*
* If the capacity for `v` is already equal to or greater than the
* requested capacity, then no action is taken.
*
* # Arguments
*
* * v - A vector
* * n - The number of elements to reserve space for
*/
#[inline]
pub unsafe fn reserve<T>(v: &mut @[T], n: uint) {
// Only make the (slow) call into the runtime if we have to
if capacity(*v) < n {
let ptr: *mut *mut Box<Vec<()>> = transmute(v);
let ty = intrinsics::get_tydesc::<T>();
return reserve_raw(ty, ptr, n);
}
}
// Implementation detail. Shouldn't be public
#[allow(missing_doc)]
#[inline]
pub fn reserve_raw(ty: *TyDesc, ptr: *mut *mut Box<Vec<()>>, n: uint) {
// check for `uint` overflow
unsafe {
if n > (**ptr).data.alloc / (*ty).size {
let alloc = n * (*ty).size;
let total_size = alloc + mem::size_of::<Vec<()>>();
if alloc / (*ty).size != n || total_size < alloc {
fail!("vector size is too large: {}", n);
}
(*ptr) = local_realloc(*ptr as *(), total_size) as *mut Box<Vec<()>>;
(**ptr).data.alloc = alloc;
}
}
#[inline]
fn local_realloc(ptr: *(), size: uint) -> *() {
use rt::local::Local;
use rt::task::Task;
let mut task = Local::borrow(None::<Task>);
task.get().heap.realloc(ptr as *mut Box<()>, size) as *()
}
}
/**
* Reserves capacity for at least `n` elements in the given vector.
*
* This function will over-allocate in order to amortize the
* allocation costs in scenarios where the caller may need to
* repeatedly reserve additional space.
*
* If the capacity for `v` is already equal to or greater than the
* requested capacity, then no action is taken.
*
* # Arguments
*
* * v - A vector
* * n - The number of elements to reserve space for
*/
#[inline]
pub unsafe fn reserve_at_least<T>(v: &mut @[T], n: uint) {
reserve(v, next_power_of_two(n));
}
}
#[cfg(test)]
mod test {
use super::*;
use prelude::*;
use bh = extra::test::BenchHarness;
#[test]
fn test() {
// Some code that could use that, then:
fn seq_range(lo: uint, hi: uint) -> @[uint] {
build(None, |push| {
for i in range(lo, hi) {
push(i);
}
})
}
assert_eq!(seq_range(10, 15), @[10, 11, 12, 13, 14]);
assert_eq!(from_fn(5, |x| x+1), @[1, 2, 3, 4, 5]);
assert_eq!(from_elem(5, 3.14), @[3.14, 3.14, 3.14, 3.14, 3.14]);
}
#[test]
fn append_test() {
assert_eq!(@[1,2,3] + &[4,5,6], @[1,2,3,4,5,6]);
}
#[test]
fn test_to_managed_move() {
assert_eq!(to_managed_move::<int>(~[]), @[]);
assert_eq!(to_managed_move(~[true]), @[true]);
assert_eq!(to_managed_move(~[1, 2, 3, 4, 5]), @[1, 2, 3, 4, 5]);
assert_eq!(to_managed_move(~[~"abc", ~"123"]), @[~"abc", ~"123"]);
assert_eq!(to_managed_move(~[~[42]]), @[~[42]]);
}
#[test]
fn test_to_managed() {
assert_eq!(to_managed::<int>([]), @[]);
assert_eq!(to_managed([true]), @[true]);
assert_eq!(to_managed([1, 2, 3, 4, 5]), @[1, 2, 3, 4, 5]);
assert_eq!(to_managed([@[42]]), @[@[42]]);
}
#[bench]
fn bench_capacity(b: &mut bh) {
let x = @[1, 2, 3];
b.iter(|| {
let _ = capacity(x);
});
}
#[bench]
fn bench_build_sized(b: &mut bh) {
let len = 64;
b.iter(|| {
build(Some(len), |push| for i in range(0, 1024) { push(i) });
});
}
#[bench]
fn bench_build(b: &mut bh) {
b.iter(|| {
for i in range(0, 95) {
build(None, |push| push(i));
}
});
}
#[bench]
fn bench_append(b: &mut bh) {
let lhs = @[7, ..128];
let rhs = range(0, 256).to_owned_vec();
b.iter(|| {
let _ = append(lhs, rhs);
})
}
#[bench]
fn bench_map(b: &mut bh) {
let elts = range(0, 256).to_owned_vec();
b.iter(|| {
let _ = map(elts, |x| x*2);
})
}
#[bench]
fn bench_from_fn(b: &mut bh) {
b.iter(|| {
let _ = from_fn(1024, |x| x);
});
}
#[bench]
fn bench_from_elem(b: &mut bh) {
b.iter(|| {
let _ = from_elem(1024, 0u64);
});
}
#[bench]
fn bench_to_managed_move(b: &mut bh) {
b.iter(|| {
let elts = range(0, 1024).to_owned_vec(); // yikes! can't move out of capture, though
to_managed_move(elts);
})
}
#[bench]
fn bench_to_managed(b: &mut bh) {
let elts = range(0, 1024).to_owned_vec();
b.iter(|| {
let _ = to_managed(elts);
});
}
#[bench]
fn bench_clone(b: &mut bh) {
let elts = to_managed(range(0, 1024).to_owned_vec());
b.iter(|| {
let _ = elts.clone();
});
}
}

View File

@ -114,7 +114,6 @@ pub mod tuple;
pub mod vec;
pub mod vec_ng;
pub mod at_vec;
pub mod str;
pub mod ascii;

View File

@ -622,13 +622,6 @@ impl BytesContainer for ~[u8] {
}
}
impl BytesContainer for @[u8] {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
self.as_slice()
}
}
impl BytesContainer for CString {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {

View File

@ -807,8 +807,6 @@ mod tests {
#[test]
fn test_push_many() {
use to_man = at_vec::to_managed_move;
macro_rules! t(
(s: $path:expr, $push:expr, $exp:expr) => (
{
@ -833,8 +831,6 @@ mod tests {
t!(v: b!("a/b/c"), [b!("d"), b!("e")], b!("a/b/c/d/e"));
t!(v: b!("a/b/c"), [b!("d"), b!("/e"), b!("f")], b!("/e/f"));
t!(v: b!("a/b/c"), [b!("d").to_owned(), b!("e").to_owned()], b!("a/b/c/d/e"));
t!(v: b!("a/b/c"), [to_man(b!("d").to_owned()), to_man(b!("e").to_owned())],
b!("a/b/c/d/e"));
}
#[test]
@ -916,8 +912,6 @@ mod tests {
#[test]
fn test_join_many() {
use to_man = at_vec::to_managed_move;
macro_rules! t(
(s: $path:expr, $join:expr, $exp:expr) => (
{
@ -941,8 +935,6 @@ mod tests {
t!(s: "a/b/c", [~"d", ~"e"], "a/b/c/d/e");
t!(v: b!("a/b/c"), [b!("d"), b!("e")], b!("a/b/c/d/e"));
t!(v: b!("a/b/c"), [b!("d").to_owned(), b!("e").to_owned()], b!("a/b/c/d/e"));
t!(v: b!("a/b/c"), [to_man(b!("d").to_owned()), to_man(b!("e").to_owned())],
b!("a/b/c/d/e"));
}
#[test]

View File

@ -1587,8 +1587,6 @@ mod tests {
#[test]
fn test_push_many() {
use to_man = at_vec::to_managed_move;
macro_rules! t(
(s: $path:expr, $push:expr, $exp:expr) => (
{
@ -1613,8 +1611,6 @@ mod tests {
t!(v: b!("a\\b\\c"), [b!("d"), b!("e")], b!("a\\b\\c\\d\\e"));
t!(v: b!("a\\b\\c"), [b!("d"), b!("\\e"), b!("f")], b!("\\e\\f"));
t!(v: b!("a\\b\\c"), [b!("d").to_owned(), b!("e").to_owned()], b!("a\\b\\c\\d\\e"));
t!(v: b!("a\\b\\c"), [to_man(b!("d").to_owned()), to_man(b!("e").to_owned())],
b!("a\\b\\c\\d\\e"));
}
#[test]
@ -1731,8 +1727,6 @@ mod tests {
#[test]
fn test_join_many() {
use to_man = at_vec::to_managed_move;
macro_rules! t(
(s: $path:expr, $join:expr, $exp:expr) => (
{
@ -1756,8 +1750,6 @@ mod tests {
t!(s: "a\\b\\c", [~"d", ~"e"], "a\\b\\c\\d\\e");
t!(v: b!("a\\b\\c"), [b!("d"), b!("e")], b!("a\\b\\c\\d\\e"));
t!(v: b!("a\\b\\c"), [b!("d").to_owned(), b!("e").to_owned()], b!("a\\b\\c\\d\\e"));
t!(v: b!("a\\b\\c"), [to_man(b!("d").to_owned()), to_man(b!("e").to_owned())],
b!("a\\b\\c\\d\\e"));
}
#[test]

View File

@ -251,9 +251,6 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
}
fn visit_evec_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
self.align_to::<@[u8]>();
if ! self.inner.visit_evec_box(mtbl, inner) { return false; }
self.bump_past::<@[u8]>();
true
}

View File

@ -637,12 +637,6 @@ fn test_repr() {
exact_test(&(0 as *mut ()), "(0x0 as *mut ())");
exact_test(&(1,), "(1,)");
exact_test(&(@[1,2,3,4,5,6,7,8]),
"@[1, 2, 3, 4, 5, 6, 7, 8]");
exact_test(&(@[1u8,2u8,3u8,4u8]),
"@[1u8, 2u8, 3u8, 4u8]");
exact_test(&(@["hi", "there"]),
"@[\"hi\", \"there\"]");
exact_test(&(~["hi", "there"]),
"~[\"hi\", \"there\"]");
exact_test(&(&["hi", "there"]),

View File

@ -332,6 +332,6 @@ mod bench {
#[bench]
fn alloc_managed_big(bh: &mut BenchHarness) {
bh.iter(|| { @[10, ..1000]; });
bh.iter(|| { @([10, ..1000]); });
}
}

View File

@ -266,13 +266,6 @@ impl<A:IterBytes> IterBytes for ~[A] {
}
}
impl<A:IterBytes> IterBytes for @[A] {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
self.as_slice().iter_bytes(lsb0, f)
}
}
impl<'a> IterBytes for &'a str {
#[inline]
fn iter_bytes(&self, _lsb0: bool, f: Cb) -> bool {

View File

@ -159,25 +159,6 @@ impl<A:ToStr> ToStr for ~[A] {
}
}
impl<A:ToStr> ToStr for @[A] {
#[inline]
fn to_str(&self) -> ~str {
let mut acc = ~"[";
let mut first = true;
for elt in self.iter() {
if first {
first = false;
}
else {
acc.push_str(", ");
}
acc.push_str(elt.to_str());
}
acc.push_char(']');
acc
}
}
#[cfg(test)]
mod tests {
use hashmap::HashMap;

View File

@ -56,7 +56,6 @@ pub trait Repr<T> {
impl<'a, T> Repr<Slice<T>> for &'a [T] {}
impl<'a> Repr<Slice<u8>> for &'a str {}
impl<T> Repr<*Box<T>> for @T {}
impl<T> Repr<*Box<Vec<T>>> for @[T] {}
impl<T> Repr<*Vec<T>> for ~[T] {}
impl Repr<*String> for ~str {}

View File

@ -646,13 +646,6 @@ pub mod traits {
fn ne(&self, other: &~[T]) -> bool { !self.eq(other) }
}
impl<T:Eq> Eq for @[T] {
#[inline]
fn eq(&self, other: &@[T]) -> bool { self.as_slice() == *other }
#[inline]
fn ne(&self, other: &@[T]) -> bool { !self.eq(other) }
}
impl<'a,T:TotalEq> TotalEq for &'a [T] {
fn equals(&self, other: & &'a [T]) -> bool {
self.len() == other.len() &&
@ -665,11 +658,6 @@ pub mod traits {
fn equals(&self, other: &~[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
}
impl<T:TotalEq> TotalEq for @[T] {
#[inline]
fn equals(&self, other: &@[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
}
impl<'a,T:Eq, V: Vector<T>> Equiv<V> for &'a [T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
@ -680,11 +668,6 @@ pub mod traits {
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'a,T:Eq, V: Vector<T>> Equiv<V> for @[T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'a,T:TotalOrd> TotalOrd for &'a [T] {
fn cmp(&self, other: & &'a [T]) -> Ordering {
order::cmp(self.iter(), other.iter())
@ -696,11 +679,6 @@ pub mod traits {
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<'a, T: Eq + Ord> Ord for &'a [T] {
fn lt(&self, other: & &'a [T]) -> bool {
order::lt(self.iter(), other.iter())
@ -730,17 +708,6 @@ pub mod traits {
fn gt(&self, other: &~[T]) -> bool { self.as_slice() > other.as_slice() }
}
impl<T: Eq + 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<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
@ -778,11 +745,6 @@ impl<T> Vector<T> for ~[T] {
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<'a, T> Container for &'a [T] {
/// Returns the length of a vector
#[inline]
@ -833,15 +795,6 @@ impl<T: Clone> CloneableVector<T> for ~[T] {
fn into_owned(self) -> ~[T] { self }
}
/// Extension methods for managed vectors
impl<T: Clone> CloneableVector<T> for @[T] {
#[inline]
fn to_owned(&self) -> ~[T] { self.as_slice().to_owned() }
#[inline(always)]
fn into_owned(self) -> ~[T] { self.to_owned() }
}
/// Extension methods for vectors
pub trait ImmutableVector<'a, T> {
/**
@ -2629,10 +2582,6 @@ impl<A> Default for ~[A] {
fn default() -> ~[A] { ~[] }
}
impl<A> Default for @[A] {
fn default() -> @[A] { @[] }
}
macro_rules! iterator {
(struct $name:ident -> $ptr:ty, $elem:ty) => {
/// An iterator for iterating over a vector.
@ -3109,14 +3058,6 @@ mod tests {
assert_eq!(v_b[0], 2);
assert_eq!(v_b[1], 3);
// Test on managed heap.
let vec_managed = @[1, 2, 3, 4, 5];
let v_c = vec_managed.slice(0u, 3u).to_owned();
assert_eq!(v_c.len(), 3u);
assert_eq!(v_c[0], 1);
assert_eq!(v_c[1], 2);
assert_eq!(v_c[2], 3);
// Test on exchange heap.
let vec_unique = ~[1, 2, 3, 4, 5, 6];
let v_d = vec_unique.slice(1u, 6u).to_owned();
@ -4052,7 +3993,6 @@ mod tests {
);
t!(&[int]);
t!(@[int]);
t!(~[int]);
}