Dynamic vector

A growable vector that makes use of unique pointers so that the result can be sent between tasks and so forth.

Note that recursive use is not permitted.

Struct DVec

pub struct DVec <A>{
    mut data: ~[A],
}

A growable, modifiable vector type that accumulates elements into a unique vector.

Limitations on recursive use

This class works by swapping the unique vector out of the data structure whenever it is to be used. Therefore, recursive use is not permitted. That is, while iterating through a vector, you cannot access the vector in any other way or else the program will fail. If you wish, you can use the swap() method to gain access to the raw vector and transform it or use it any way you like. Eventually, we may permit read-only access during iteration or other use.

WARNING

For maximum performance, this type is implemented using some rather unsafe code. In particular, this innocent looking ~[mut A] pointer may be null! Therefore, it is important you not reach into the data structure manually but instead use the provided extensions.

The reason that I did not use an unsafe pointer in the structure itself is that I wanted to ensure that the vector would be freed when the dvec is dropped. The reason that I did not use an Option<T> instead of a nullable pointer is that I found experimentally that it becomes approximately 50% slower. This can probably be improved through optimization. You can run your own experiments using src/test/bench/vec-append.rs. My own tests found that using null pointers achieved about 103 million pushes/second. Using an option type could only produce 47 million pushes/second.

Implementation for DVec<A>

Method check_not_borrowed

fn check_not_borrowed()

Method check_out

fn check_out<B>(f: &fn(v: ~[A]) -> B) -> B

Method give_back

fn give_back(data: ~[A])

Method unwrap

fn unwrap() -> ~[A]

Implementation for DVec<A>

Method reserve

fn reserve(count: uint)

Reserves space for N elements

Method swap

fn swap(f: &fn(v: ~[A]) -> ~[A])

Swaps out the current vector and hands it off to a user-provided function f. The function should transform it however is desired and return a new vector to replace it with.

Method swap_mut

fn swap_mut(f: &fn(v: ~[mut A]) -> ~[mut A])

Swaps out the current vector and hands it off to a user-provided function f. The function should transform it however is desired and return a new vector to replace it with.

Method len

fn len() -> uint

Returns the number of elements currently in the dvec

Method set

fn set(w: ~[A])

Overwrite the current contents

Method pop

fn pop() -> A

Remove and return the last element

Method unshift

fn unshift(t: A)

Insert a single item at the front of the list

Method push

fn push(t: A)

Append a single item to the end of the list

Method shift

fn shift() -> A

Remove and return the first element

Method reverse

fn reverse()

Reverse the elements in the list, in place

Method borrow

fn borrow<R>(op: &fn(x: &[A]) -> R) -> R

Gives access to the vector as a slice with immutable contents

Method borrow_mut

fn borrow_mut<R>(op: &fn(x: &[mut A]) -> R) -> R

Gives access to the vector as a slice with mutable contents

Implementation for DVec<A>

Method push_all

fn push_all(ts: &[const A])

Append all elements of a vector to the end of the list

Equivalent to append_iter() but potentially more efficient.

Method push_slice

fn push_slice(ts: &[const A], from_idx: uint, to_idx: uint)

Appends elements from from_idx to to_idx (exclusive)

Method get

fn get() -> ~[A]

Append all elements of an iterable.

Failure will occur if the iterable's each() method attempts to access this vector. Gets a copy of the current contents.

See unwrap() if you do not wish to copy the contents.

Method get_elt

fn get_elt(idx: uint) -> A

Copy out an individual element

Method set_elt

fn set_elt(idx: uint, a: A)

Overwrites the contents of the element at idx with a

Method grow_set_elt

fn grow_set_elt(idx: uint, initval: &A, val: A)

Overwrites the contents of the element at idx with a, growing the vector if necessary. New elements will be initialized with initval

Method last

fn last() -> A

Returns the last element, failing if the vector is empty

Method rev_each

fn rev_each(f: &fn(v: &A) -> bool)

Iterates over the elements in reverse order

Method rev_eachi

fn rev_eachi(f: &fn(uint, v: &A) -> bool)

Iterates over the elements and indices in reverse order

Implementation of Index<uint, A> for DVec<A>

Method index

fn index(idx: uint) -> A

Function DVec

fn DVec<A>() -> DVec<A>

Creates a new, empty dvec

Function from_elem

fn from_elem<A>(e: A) -> DVec<A>

Creates a new dvec with a single element

Function from_vec

fn from_vec<A>(v: ~[A]) -> DVec<A>

Creates a new dvec with the contents of a vector

Function unwrap

fn unwrap<A>(d: DVec<A>) -> ~[A]

Consumes the vector and returns its contents