dvec
- A growable, modifiable vector type that accumulates elements into a unique vector.extensions for dvec<A>
extensions for dvec<A>
extensions of iter::base_iter<A> for IMPL_T<A>
extensions for IMPL_T<A>
extensions for *T
- Extension methods for pointersdvec
- 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
type dvec<A> = {mut data: ~[mut 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.
extensions for dvec<A>
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.
len
fn len() -> uint
Returns the number of elements currently in the dvec
set
fn set(+w: ~[mut 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
extensions for 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]
Gets a copy of the current contents.
See unwrap()
if you do not wish to copy the contents.
[]
fn [](idx: uint) -> A
Copy out an individual element
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
reach
fn reach(f: fn(A) -> bool)
Iterates over the elements in reverse order
extensions of iter::base_iter<A> for IMPL_T<A>
each
fn each(blk: fn(A) -> bool)
size_hint
fn size_hint() -> option<uint>
eachi
fn eachi(blk: fn(uint, A) -> bool)
all
fn all(blk: fn(A) -> bool) -> bool
any
fn any(blk: fn(A) -> bool) -> bool
foldl
fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B
contains
fn contains(x: A) -> bool
count
fn count(x: A) -> uint
position
fn position(f: fn(A) -> bool) -> option<uint>
extensions for IMPL_T<A>
filter_to_vec
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A]
map_to_vec
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B]
to_vec
fn to_vec() -> ~[A]
min
fn min() -> A
max
fn max() -> A
find
fn find(p: fn(A) -> bool) -> option<A>
extensions for *T
Extension methods for pointers
is_null
pure fn is_null() -> bool
Returns true if the pointer is equal to the null pointer.
is_not_null
pure fn is_not_null() -> bool
Returns true if the pointer is not equal to the null pointer.
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: ~[mut A]) -> dvec<A>
Creates a new dvec with the contents of a vector
unwrap
fn unwrap<A>(-d: dvec<A>) -> ~[mut A]
Consumes the vector and returns its contents