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.
DVec
- A growable, modifiable vector type that accumulates elements into a unique vector.for DVec<A>
for DVec<A>
for DVec<A>
of Index<uint, A> for DVec<A>
DVec
- Creates a new, empty dvecfrom_elem
- Creates a new dvec with a single elementfrom_vec
- Creates a new dvec with the contents of a vectorunwrap
- Consumes the vector and returns its contentsDVec
pub struct DVec <A>{
mut data: ~[A],
}
A growable, modifiable vector type that accumulates elements into a unique vector.
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.
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.
DVec<A>
check_not_borrowed
fn check_not_borrowed()
check_out
fn check_out<B>(f: &fn(v: ~[A]) -> B) -> B
give_back
fn give_back(data: ~[A])
unwrap
fn unwrap() -> ~[A]
DVec<A>
reserve
fn reserve(count: uint)
Reserves space for N elements
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.
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.
len
fn len() -> uint
Returns the number of elements currently in the dvec
set
fn set(w: ~[A])
Overwrite the current contents
pop
fn pop() -> A
Remove and return the last element
unshift
fn unshift(t: A)
Insert a single item at the front of the list
push
fn push(t: A)
Append a single item to the end of the list
shift
fn shift() -> A
Remove and return the first element
reverse
fn reverse()
Reverse the elements in the list, in place
borrow
fn borrow<R>(op: &fn(x: &[A]) -> R) -> R
Gives access to the vector as a slice with immutable contents
borrow_mut
fn borrow_mut<R>(op: &fn(x: &[mut A]) -> R) -> R
Gives access to the vector as a slice with mutable contents
DVec<A>
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.
push_slice
fn push_slice(ts: &[const A], from_idx: uint, to_idx: uint)
Appends elements from from_idx
to to_idx
(exclusive)
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.
get_elt
fn get_elt(idx: uint) -> A
Copy out an individual element
set_elt
fn set_elt(idx: uint, a: A)
Overwrites the contents of the element at idx
with a
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
last
fn last() -> A
Returns the last element, failing if the vector is empty
rev_each
fn rev_each(f: &fn(v: &A) -> bool)
Iterates over the elements in reverse order
rev_eachi
fn rev_eachi(f: &fn(uint, v: &A) -> bool)
Iterates over the elements and indices in reverse order
Index<uint, A>
for DVec<A>
index
fn index(idx: uint) -> A
DVec
fn DVec<A>() -> DVec<A>
Creates a new, empty dvec
from_elem
fn from_elem<A>(e: A) -> DVec<A>
Creates a new dvec with a single element
from_vec
fn from_vec<A>(v: ~[A]) -> DVec<A>
Creates a new dvec with the contents of a vector
unwrap
fn unwrap<A>(d: DVec<A>) -> ~[A]
Consumes the vector and returns its contents