Type dvec

type dvec<A> = {mut data: ~[mut 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 extensions for dvec<A>

Method swap

fn swap(f: fn(-~[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: ~[mut 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

Implementation extensions 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]

Gets a copy of the current contents.

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

Method []

fn [](idx: uint) -> A

Copy out an individual element

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 reach

fn reach(f: fn(A) -> bool)

Iterates over the elements in reverse order

Implementation extensions of iter::base_iter<A> for IMPL_T<A>

Method each

fn each(blk: fn(A) -> bool)

Method size_hint

fn size_hint() -> option<uint>

Method eachi

fn eachi(blk: fn(uint, A) -> bool)

Method all

fn all(blk: fn(A) -> bool) -> bool

Method any

fn any(blk: fn(A) -> bool) -> bool

Method foldl

fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B

Method contains

fn contains(x: A) -> bool

Method count

fn count(x: A) -> uint

Method position

fn position(f: fn(A) -> bool) -> option<uint>

Implementation extensions for IMPL_T<A>

Method filter_to_vec

fn filter_to_vec(pred: fn(A) -> bool) -> ~[A]

Method map_to_vec

fn map_to_vec<B>(op: fn(A) -> B) -> ~[B]

Method to_vec

fn to_vec() -> ~[A]

Method min

fn min() -> A

Method max

fn max() -> A

Method find

fn find(p: fn(A) -> bool) -> option<A>

Implementation extensions for *T

Extension methods for pointers

Method is_null

pure fn is_null() -> bool

Returns true if the pointer is equal to the null pointer.

Method is_not_null

pure fn is_not_null() -> bool

Returns true if the pointer is not equal to the null pointer.

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: ~[mut A]) -> dvec<A>

Creates a new dvec with the contents of a vector

Function unwrap

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

Consumes the vector and returns its contents