2012-12-03 18:48:01 -06: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-09-20 17:33:52 -05:00
|
|
|
//! Managed vectors
|
2012-07-17 18:31:19 -05:00
|
|
|
|
2012-09-26 14:28:30 -05:00
|
|
|
use cast::transmute;
|
2013-01-08 21:37:25 -06:00
|
|
|
use kinds::Copy;
|
2012-12-23 16:41:37 -06:00
|
|
|
use iter;
|
|
|
|
use libc;
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::Option;
|
2012-10-01 14:47:02 -05:00
|
|
|
use ptr::addr_of;
|
2012-12-23 16:41:37 -06:00
|
|
|
use sys;
|
|
|
|
use uint;
|
|
|
|
use vec;
|
2012-07-17 18:31:19 -05:00
|
|
|
|
|
|
|
/// Code for dealing with @-vectors. This is pretty incomplete, and
|
|
|
|
/// contains a bunch of duplication from the code for ~-vectors.
|
|
|
|
|
|
|
|
#[abi = "cdecl"]
|
2012-12-23 16:41:37 -06:00
|
|
|
pub extern mod rustrt {
|
2013-01-29 13:47:18 -06:00
|
|
|
pub unsafe fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
|
|
|
|
++v: **vec::raw::VecRepr,
|
|
|
|
++n: libc::size_t);
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of elements the vector can hold without reallocating
|
|
|
|
#[inline(always)]
|
2012-09-26 12:33:55 -05:00
|
|
|
pub pure fn capacity<T>(v: @[const T]) -> uint {
|
2012-07-17 18:31:19 -05:00
|
|
|
unsafe {
|
2012-09-18 13:18:56 -05:00
|
|
|
let repr: **raw::VecRepr =
|
2012-09-28 23:51:14 -05:00
|
|
|
::cast::reinterpret_cast(&addr_of(&v));
|
2012-09-14 21:09:38 -05:00
|
|
|
(**repr).unboxed.alloc / sys::size_of::<T>()
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a vector by calling a provided function with an argument
|
|
|
|
* function that pushes an element to the back of a vector.
|
|
|
|
* This version takes an initial size for the vector.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * size - An initial size of the vector to reserve
|
|
|
|
* * builder - A function that will construct the vector. It recieves
|
|
|
|
* as an argument a function that will push an element
|
|
|
|
* onto the vector being constructed.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2012-09-25 19:52:46 -05:00
|
|
|
pub pure fn build_sized<A>(size: uint,
|
2012-10-02 13:37:37 -05:00
|
|
|
builder: &fn(push: pure fn(v: A))) -> @[A] {
|
2012-09-26 14:28:30 -05:00
|
|
|
let mut vec: @[const A] = @[];
|
|
|
|
unsafe { raw::reserve(&mut vec, size); }
|
2012-12-12 17:38:50 -06:00
|
|
|
builder(|+x| unsafe { raw::push(&mut vec, x) });
|
2012-09-26 14:28:30 -05:00
|
|
|
return unsafe { transmute(vec) };
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a vector by calling a provided function with an argument
|
|
|
|
* function that pushes an element to the back of a vector.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * builder - A function that will construct the vector. It recieves
|
|
|
|
* as an argument a function that will push an element
|
|
|
|
* onto the vector being constructed.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2012-10-02 13:37:37 -05:00
|
|
|
pub pure fn build<A>(builder: &fn(push: pure fn(v: A))) -> @[A] {
|
2012-07-17 18:31:19 -05:00
|
|
|
build_sized(4, builder)
|
|
|
|
}
|
|
|
|
|
2012-08-23 12:22:14 -05:00
|
|
|
/**
|
|
|
|
* Builds a vector by calling a provided function with an argument
|
|
|
|
* function that pushes an element to the back of a vector.
|
|
|
|
* This version takes an initial size for the vector.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * size - An option, maybe containing initial size of the vector to reserve
|
|
|
|
* * builder - A function that will construct the vector. It recieves
|
|
|
|
* as an argument a function that will push an element
|
|
|
|
* onto the vector being constructed.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2012-10-02 13:37:37 -05:00
|
|
|
pub pure fn build_sized_opt<A>(size: Option<uint>,
|
|
|
|
builder: &fn(push: pure fn(v: A))) -> @[A] {
|
2013-01-04 18:01:26 -06:00
|
|
|
build_sized(size.get_or_default(4), builder)
|
2012-08-23 12:22:14 -05:00
|
|
|
}
|
|
|
|
|
2012-08-08 16:30:30 -05:00
|
|
|
// Appending
|
|
|
|
#[inline(always)]
|
2013-02-20 19:07:17 -06:00
|
|
|
pub pure fn append<T:Copy>(lhs: @[T], rhs: &[const T]) -> @[T] {
|
2012-08-08 16:30:30 -05:00
|
|
|
do build_sized(lhs.len() + rhs.len()) |push| {
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::each(lhs) |x| { push(*x); }
|
2012-08-08 16:30:30 -05:00
|
|
|
for uint::range(0, rhs.len()) |i| { push(rhs[i]); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-17 18:31:19 -05:00
|
|
|
/// Apply a function to each element of a vector and return the results
|
2012-09-26 12:33:55 -05:00
|
|
|
pub pure fn map<T, U>(v: &[T], f: &fn(x: &T) -> U) -> @[U] {
|
2012-07-17 18:31:19 -05:00
|
|
|
do build_sized(v.len()) |push| {
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(v) |elem| {
|
2012-09-26 12:33:55 -05:00
|
|
|
push(f(elem));
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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`.
|
|
|
|
*/
|
2012-09-25 19:52:46 -05:00
|
|
|
pub pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> @[T] {
|
2012-07-17 18:31:19 -05:00
|
|
|
do build_sized(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`.
|
|
|
|
*/
|
2013-02-20 19:07:17 -06:00
|
|
|
pub pure fn from_elem<T:Copy>(n_elts: uint, t: T) -> @[T] {
|
2012-07-17 18:31:19 -05:00
|
|
|
do build_sized(n_elts) |push| {
|
|
|
|
let mut i: uint = 0u;
|
2012-09-26 14:28:30 -05:00
|
|
|
while i < n_elts { push(copy t); i += 1u; }
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 16:52:20 -06:00
|
|
|
/**
|
|
|
|
* Creates and initializes an immutable managed vector by moving all the
|
|
|
|
* elements from an owned vector.
|
|
|
|
*/
|
2013-01-23 16:30:56 -06:00
|
|
|
pub fn from_owned<T>(v: ~[T]) -> @[T] {
|
2013-01-22 16:52:20 -06:00
|
|
|
let mut av = @[];
|
|
|
|
unsafe {
|
|
|
|
raw::reserve(&mut av, v.len());
|
|
|
|
do vec::consume(v) |_i, x| {
|
|
|
|
raw::push(&mut av, x);
|
|
|
|
}
|
|
|
|
transmute(av)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and initializes an immutable managed vector by copying all the
|
|
|
|
* elements of a slice.
|
|
|
|
*/
|
2013-01-23 16:30:56 -06:00
|
|
|
pub fn from_slice<T:Copy>(v: &[T]) -> @[T] {
|
2013-01-22 16:52:20 -06:00
|
|
|
from_fn(v.len(), |i| v[i])
|
|
|
|
}
|
|
|
|
|
2012-08-08 16:30:30 -05:00
|
|
|
#[cfg(notest)]
|
2012-09-26 16:23:27 -05:00
|
|
|
pub mod traits {
|
2013-01-08 21:37:25 -06:00
|
|
|
use at_vec::append;
|
|
|
|
use kinds::Copy;
|
|
|
|
use ops::Add;
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
pub impl<T:Copy> Add<&[const T],@[T]> for @[T] {
|
2012-11-28 15:51:50 -06:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn add(&self, rhs: & &self/[const T]) -> @[T] {
|
|
|
|
append(*self, (*rhs))
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 16:30:30 -05:00
|
|
|
}
|
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
#[cfg(test)]
|
2012-11-03 15:23:01 -05:00
|
|
|
pub mod traits {}
|
2012-07-17 18:31:19 -05:00
|
|
|
|
2012-09-25 19:52:46 -05:00
|
|
|
pub mod raw {
|
2013-02-20 10:57:15 -06:00
|
|
|
use at_vec::{capacity, rustrt};
|
2013-01-08 21:37:25 -06:00
|
|
|
use cast::transmute;
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
2013-02-20 10:57:15 -06:00
|
|
|
use private::intrinsics::{move_val_init};
|
2013-01-08 21:37:25 -06:00
|
|
|
use ptr::addr_of;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ptr;
|
|
|
|
use sys;
|
|
|
|
use uint;
|
|
|
|
use vec;
|
|
|
|
|
2012-09-25 19:52:46 -05:00
|
|
|
pub type VecRepr = vec::raw::VecRepr;
|
|
|
|
pub type SliceRepr = vec::raw::SliceRepr;
|
2012-07-17 18:31:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the length of a vector
|
|
|
|
*
|
|
|
|
* This will explicitly set the size of the vector, without actually
|
|
|
|
* modifing its buffers, so it is up to the caller to ensure that
|
|
|
|
* the vector is actually the specified size.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2012-09-26 12:33:55 -05:00
|
|
|
pub unsafe fn set_len<T>(v: @[const T], new_len: uint) {
|
2012-09-28 23:51:14 -05:00
|
|
|
let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(&v));
|
2012-09-14 21:09:38 -05:00
|
|
|
(**repr).unboxed.fill = new_len * sys::size_of::<T>();
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-10-02 13:37:37 -05:00
|
|
|
pub unsafe fn push<T>(v: &mut @[const T], initval: T) {
|
2012-09-26 14:28:30 -05:00
|
|
|
let repr: **VecRepr = ::cast::reinterpret_cast(&v);
|
2012-09-14 21:09:38 -05:00
|
|
|
let fill = (**repr).unboxed.fill;
|
|
|
|
if (**repr).unboxed.alloc > fill {
|
2012-12-12 17:38:50 -06:00
|
|
|
push_fast(v, initval);
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-12-12 17:38:50 -06:00
|
|
|
push_slow(v, initval);
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-05 16:58:42 -05:00
|
|
|
|
|
|
|
#[inline(always)] // really pretty please
|
|
|
|
pub unsafe fn push_fast<T>(v: &mut @[const T], initval: T) {
|
|
|
|
let repr: **VecRepr = ::cast::reinterpret_cast(&v);
|
|
|
|
let fill = (**repr).unboxed.fill;
|
|
|
|
(**repr).unboxed.fill += sys::size_of::<T>();
|
|
|
|
let p = addr_of(&((**repr).unboxed.data));
|
|
|
|
let p = ptr::offset(p, fill) as *mut T;
|
2013-02-20 10:57:15 -06:00
|
|
|
move_val_init(&mut(*p), initval);
|
2012-10-05 16:58:42 -05:00
|
|
|
}
|
2012-07-30 13:21:40 -05:00
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
pub unsafe fn push_slow<T>(v: &mut @[const T], initval: T) {
|
2013-01-11 23:01:42 -06:00
|
|
|
reserve_at_least(&mut *v, v.len() + 1u);
|
2013-02-15 02:51:28 -06:00
|
|
|
push_fast(v, initval);
|
2012-07-17 18:31:19 -05:00
|
|
|
}
|
2012-07-30 13:21:40 -05:00
|
|
|
|
2012-07-17 18:31:19 -05:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-26 14:28:30 -05:00
|
|
|
pub unsafe fn reserve<T>(v: &mut @[const T], n: uint) {
|
2012-07-17 18:31:19 -05:00
|
|
|
// Only make the (slow) call into the runtime if we have to
|
2012-09-26 14:28:30 -05:00
|
|
|
if capacity(*v) < n {
|
2013-01-11 23:01:42 -06:00
|
|
|
let ptr: **VecRepr = transmute(v);
|
2012-07-17 18:31:19 -05:00
|
|
|
rustrt::vec_reserve_shared_actual(sys::get_type_desc::<T>(),
|
|
|
|
ptr, n as libc::size_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-26 14:28:30 -05:00
|
|
|
pub unsafe fn reserve_at_least<T>(v: &mut @[const T], n: uint) {
|
2012-07-17 18:31:19 -05:00
|
|
|
reserve(v, uint::next_power_of_two(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-07-31 19:30:38 -05:00
|
|
|
|
|
|
|
#[test]
|
2012-09-25 19:52:46 -05:00
|
|
|
pub fn test() {
|
2012-07-31 19:30:38 -05:00
|
|
|
// Some code that could use that, then:
|
|
|
|
fn seq_range(lo: uint, hi: uint) -> @[uint] {
|
|
|
|
do build |push| {
|
|
|
|
for uint::range(lo, hi) |i| {
|
|
|
|
push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert seq_range(10, 15) == @[10, 11, 12, 13, 14];
|
|
|
|
assert from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5];
|
|
|
|
assert from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14];
|
2012-08-08 16:30:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-25 19:52:46 -05:00
|
|
|
pub fn append_test() {
|
2012-08-08 16:30:30 -05:00
|
|
|
assert @[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6];
|
|
|
|
}
|
2013-01-22 16:52:20 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_from_owned() {
|
|
|
|
assert from_owned::<int>(~[]) == @[];
|
|
|
|
assert from_owned(~[true]) == @[true];
|
|
|
|
assert from_owned(~[1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5];
|
|
|
|
assert from_owned(~[~"abc", ~"123"]) == @[~"abc", ~"123"];
|
|
|
|
assert from_owned(~[~[42]]) == @[~[42]];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_from_slice() {
|
|
|
|
assert from_slice::<int>([]) == @[];
|
|
|
|
assert from_slice([true]) == @[true];
|
|
|
|
assert from_slice([1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5];
|
|
|
|
assert from_slice([@"abc", @"123"]) == @[@"abc", @"123"];
|
|
|
|
assert from_slice([@[42]]) == @[@[42]];
|
|
|
|
}
|
|
|
|
|