Trait std::slice::OwnedVector
pub trait OwnedVector<T> { fn move_iter(self) -> MoveItems<T>; fn move_rev_iter(self) -> RevMoveItems<T>; fn reserve_exact(&mut self, n: uint); fn reserve(&mut self, n: uint); fn reserve_additional(&mut self, n: uint); fn capacity(&self) -> uint; fn shrink_to_fit(&mut self); fn push(&mut self, t: T); fn push_all_move(&mut self, rhs: ~[T]); fn pop(&mut self) -> Option<T>; fn shift(&mut self) -> Option<T>; fn unshift(&mut self, x: T); fn insert(&mut self, i: uint, x: T); fn remove(&mut self, i: uint) -> Option<T>; fn swap_remove(&mut self, index: uint) -> Option<T>; fn truncate(&mut self, newlen: uint); fn retain(&mut self, f: |t: &T| -> bool); fn partition(self, f: |&T| -> bool) -> (~[T], ~[T]); fn grow_fn(&mut self, n: uint, op: |uint| -> T); unsafe fn set_len(&mut self, new_len: uint); }
Extension methods for owned vectors.
Required Methods
fn move_iter(self) -> MoveItems<T>
Creates a consuming iterator, that is, one that moves each value out of the vector (from start to end). The vector cannot be used after calling this.
Examples
let v = ~[~"a", ~"b"]; for s in v.move_iter() { // s has type ~str, not &~str println!("{}", s); }
fn move_rev_iter(self) -> RevMoveItems<T>
Creates a consuming iterator that moves out of the vector in reverse order.
fn reserve_exact(&mut self, n: uint)
Reserves capacity for exactly n
elements in the given vector.
If the capacity for self
is already equal to or greater than the requested
capacity, then no action is taken.
Arguments
- n - The number of elements to reserve space for
Failure
This method always succeeds in reserving space for n
elements, or it does
not return.
fn reserve(&mut self, n: uint)
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 self
is already equal to or greater than the requested
capacity, then no action is taken.
Arguments
- n - The number of elements to reserve space for
fn reserve_additional(&mut self, n: uint)
Reserves capacity for at least n
additional elements in the given vector.
Failure
Fails if the new required capacity overflows uint.
May also fail if reserve
fails.
fn capacity(&self) -> uint
Returns the number of elements the vector can hold without reallocating.
fn shrink_to_fit(&mut self)
Shrink the capacity of the vector to match the length
fn push(&mut self, t: T)
Append an element to a vector
fn push_all_move(&mut self, rhs: ~[T])
Takes ownership of the vector rhs
, moving all elements into
the current vector. This does not copy any elements, and it is
illegal to use the rhs
vector after calling this method
(because it is moved here).
Example
let mut a = ~[~1]; a.push_all_move(~[~2, ~3, ~4]); assert!(a == ~[~1, ~2, ~3, ~4]);
fn pop(&mut self) -> Option<T>
Remove the last element from a vector and return it, or None
if it is empty
fn shift(&mut self) -> Option<T>
Removes the first element from a vector and return it, or None
if it is empty
fn unshift(&mut self, x: T)
Prepend an element to the vector
fn insert(&mut self, i: uint, x: T)
Insert an element at position i within v, shifting all elements after position i one position to the right.
fn remove(&mut self, i: uint) -> Option<T>
Remove and return the element at position i
within v
,
shifting all elements after position i
one position to the
left. Returns None
if i
is out of bounds.
Example
let mut v = ~[1, 2, 3]; assert_eq!(v.remove(1), Some(2)); assert_eq!(v, ~[1, 3]); assert_eq!(v.remove(4), None); // v is unchanged: assert_eq!(v, ~[1, 3]);
fn swap_remove(&mut self, index: uint) -> Option<T>
Remove an element from anywhere in the vector and return it, replacing it with the last element. This does not preserve ordering, but is O(1).
Returns None
if index
is out of bounds.
Example
let mut v = ~[~"foo", ~"bar", ~"baz", ~"qux"]; assert_eq!(v.swap_remove(1), Some(~"bar")); assert_eq!(v, ~[~"foo", ~"qux", ~"baz"]); assert_eq!(v.swap_remove(0), Some(~"foo")); assert_eq!(v, ~[~"baz", ~"qux"]); assert_eq!(v.swap_remove(2), None);
fn truncate(&mut self, newlen: uint)
Shorten a vector, dropping excess elements.
fn retain(&mut self, f: |t: &T| -> bool)
Like filter()
, but in place. Preserves order of v
. Linear time.
fn partition(self, f: |&T| -> bool) -> (~[T], ~[T])
Partitions the vector into two vectors (A,B)
, where all
elements of A
satisfy f
and all elements of B
do not.
fn grow_fn(&mut self, n: uint, op: |uint| -> T)
Expands a vector in place, initializing the new elements to the result of a function.
Function init_op
is called n
times with the values [0..n
)
Arguments
- n - The number of elements to add
- init_op - A function to call to retrieve each appended element's value
unsafe fn set_len(&mut self, new_len: uint)
Sets the length of a vector
This will explicitly set the size of the vector, without actually modifying its buffers, so it is up to the caller to ensure that the vector is actually the specified size.
Implementors
impl<T> OwnedVector<T> for ~[T]