Trait std::vec::MutableVector
pub trait MutableVector<'a, T> { fn as_mut_slice(self) -> &'a mut [T]; fn mut_slice(self, start: uint, end: uint) -> &'a mut [T]; fn mut_slice_from(self, start: uint) -> &'a mut [T]; fn mut_slice_to(self, end: uint) -> &'a mut [T]; fn mut_iter(self) -> VecMutIterator<'a, T>; fn mut_rev_iter(self) -> MutRevIterator<'a, T>; fn mut_split(self, pred: 'a |&T| -> bool) -> MutSplitIterator<'a, T>; fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'a, T>; fn mut_shift_ref(&mut self) -> &'a mut T; fn mut_pop_ref(&mut self) -> &'a mut T; fn swap(self, a: uint, b: uint); fn mut_split_at(self, mid: uint) -> (&'a mut [T], &'a mut [T]); fn reverse(self); fn sort_by(self, compare: |&T, &T| -> Ordering); fn move_from(self, src: ~[T], start: uint, end: uint) -> uint; unsafe fn unsafe_mut_ref(self, index: uint) -> *mut T; fn as_mut_ptr(self) -> *mut T; unsafe fn unsafe_set(self, index: uint, val: T); unsafe fn init_elem(self, i: uint, val: T); unsafe fn copy_memory(self, src: &[T]); }
Extension methods for vectors such that their elements are mutable.
Required Methods
fn as_mut_slice(self) -> &'a mut [T]
Work with self
as a mut slice.
Primarily intended for getting a &mut [T] from a [T, ..N].
fn mut_slice(self, start: uint, end: uint) -> &'a mut [T]
Return a slice that points into another slice.
fn mut_slice_from(self, start: uint) -> &'a mut [T]
Returns a slice of self from start
to the end of the vec.
Fails when start
points outside the bounds of self.
fn mut_slice_to(self, end: uint) -> &'a mut [T]
Returns a slice of self from the start of the vec to end
.
Fails when end
points outside the bounds of self.
fn mut_iter(self) -> VecMutIterator<'a, T>
Returns an iterator that allows modifying each value
fn mut_rev_iter(self) -> MutRevIterator<'a, T>
Returns a reversed iterator that allows modifying each value
fn mut_split(self, pred: 'a |&T| -> bool) -> MutSplitIterator<'a, T>
Returns an iterator over the mutable subslices of the vector
which are separated by elements that match pred
. The
matched element is not contained in the subslices.
fn mut_chunks(self, chunk_size: uint) -> MutChunkIter<'a, T>
Returns an iterator over size
elements of the vector at a time.
The chunks are mutable and do not overlap. If size
does not divide the
length of the vector, then the last chunk will not have length
size
.
Failure
Fails if size
is 0.
fn mut_shift_ref(&mut self) -> &'a mut T
Returns a mutable reference to the first element in this slice and adjusts the slice in place so that it no longer contains that element. O(1).
Equivalent to:
let head = &mut self[0];
*self = self.mut_slice_from(1);
head
Fails if slice is empty.
fn mut_pop_ref(&mut self) -> &'a mut T
Returns a mutable reference to the last element in this slice and adjusts the slice in place so that it no longer contains that element. O(1).
Equivalent to:
let tail = &mut self[self.len() - 1];
*self = self.mut_slice_to(self.len() - 1);
tail
Fails if slice is empty.
fn swap(self, a: uint, b: uint)
Swaps two elements in a vector.
Fails if a
or b
are out of bounds.
Arguments
- a - The index of the first element
- b - The index of the second element
Example
let mut v = ["a", "b", "c", "d"];
v.swap(1, 3);
assert_eq!(v, ["a", "d", "c", "b"]);
fn mut_split_at(self, mid: uint) -> (&'a mut [T], &'a mut [T])
Divides one &mut
into two at an index.
The first will contain all indices from [0, mid)
(excluding
the index mid
itself) and the second will contain all
indices from [mid, len)
(excluding the index len
itself).
Fails if mid > len
.
Example
let mut v = [1, 2, 3, 4, 5, 6];
// scoped to restrict the lifetime of the borrows
{
let (left, right) = v.mut_split_at(0);
assert_eq!(left, &mut []);
assert_eq!(right, &mut [1, 2, 3, 4, 5, 6]);
}
{
let (left, right) = v.mut_split_at(2);
assert_eq!(left, &mut [1, 2]);
assert_eq!(right, &mut [3, 4, 5, 6]);
}
{
let (left, right) = v.mut_split_at(6);
assert_eq!(left, &mut [1, 2, 3, 4, 5, 6]);
assert_eq!(right, &mut []);
}
fn reverse(self)
Reverse the order of elements in a vector, in place.
Example
let mut v = [1, 2, 3];
v.reverse();
assert_eq!(v, [3, 2, 1]);
fn sort_by(self, compare: |&T, &T| -> Ordering)
Sort the vector, in place, using compare
to compare
elements.
This sort is O(n log n)
worst-case and stable, but allocates
approximately 2 * n
, where n
is the length of self
.
Example
let mut v = [5i, 4, 1, 3, 2];
v.sort_by(|a, b| a.cmp(b));
assert_eq!(v, [1, 2, 3, 4, 5]);
// reverse sorting
v.sort_by(|a, b| b.cmp(a));
assert_eq!(v, [5, 4, 3, 2, 1]);
fn move_from(self, src: ~[T], start: uint, end: uint) -> uint
Consumes src
and moves as many elements as it can into self
from the range [start,end).
Returns the number of elements copied (the shorter of self.len() and end - start).
Arguments
- src - A mutable vector of
T
- start - The index into
src
to start copying from - end - The index into
str
to stop copying from
unsafe fn unsafe_mut_ref(self, index: uint) -> *mut T
Returns an unsafe mutable pointer to the element in index
fn as_mut_ptr(self) -> *mut T
Return an unsafe mutable pointer to the vector's buffer.
The caller must ensure that the vector outlives the pointer this function returns, or else it will end up pointing to garbage.
Modifying the vector may cause its buffer to be reallocated, which would also make any pointers to it invalid.
unsafe fn unsafe_set(self, index: uint, val: T)
Unsafely sets the element in index to the value.
This performs no bounds checks, and it is undefined behaviour
if index
is larger than the length of self
. However, it
does run the destructor at index
. It is equivalent to
self[index] = val
.
Example
let mut v = ~[~"foo", ~"bar", ~"baz"];
unsafe {
// `~"baz"` is deallocated.
v.unsafe_set(2, ~"qux");
// Out of bounds: could cause a crash, or overwriting
// other data, or something else.
// v.unsafe_set(10, ~"oops");
}
unsafe fn init_elem(self, i: uint, val: T)
Unchecked vector index assignment. Does not drop the old value and hence is only suitable when the vector is newly allocated.
Example
let mut v = [~"foo", ~"bar"];
// memory leak! `~"bar"` is not deallocated.
unsafe { v.init_elem(1, ~"baz"); }
unsafe fn copy_memory(self, src: &[T])
Copies raw bytes from src
to self
.
This does not run destructors on the overwritten elements, and
ignores move semantics. self
and src
must not
overlap. Fails if self
is shorter than src
.
Implementors
impl<'a, T> MutableVector<'a, T> for &'a mut [T]