Trait std::slice::ImmutableVector
pub trait ImmutableVector<'a, T> { fn slice(&self, start: uint, end: uint) -> &'a [T]; fn slice_from(&self, start: uint) -> &'a [T]; fn slice_to(&self, end: uint) -> &'a [T]; fn iter(self) -> Items<'a, T>; fn rev_iter(self) -> RevItems<'a, T>; fn split(self, pred: 'a |&T| -> bool) -> Splits<'a, T>; fn splitn(self, n: uint, pred: 'a |&T| -> bool) -> Splits<'a, T>; fn rsplit(self, pred: 'a |&T| -> bool) -> RevSplits<'a, T>; fn rsplitn(self, n: uint, pred: 'a |&T| -> bool) -> RevSplits<'a, T>; fn windows(self, size: uint) -> Windows<'a, T>; fn chunks(self, size: uint) -> Chunks<'a, T>; fn get(&self, index: uint) -> Option<&'a T>; fn head(&self) -> Option<&'a T>; fn tail(&self) -> &'a [T]; fn tailn(&self, n: uint) -> &'a [T]; fn init(&self) -> &'a [T]; fn initn(&self, n: uint) -> &'a [T]; fn last(&self) -> Option<&'a T>; unsafe fn unsafe_ref(self, index: uint) -> &'a T; fn as_ptr(&self) -> *T; fn bsearch(&self, f: |&T| -> Ordering) -> Option<uint>; fn shift_ref(&mut self) -> Option<&'a T>; fn pop_ref(&mut self) -> Option<&'a T>; }
Extension methods for vectors
Required Methods
fn slice(&self, start: uint, end: uint) -> &'a [T]
Returns a slice of self between start
and end
.
Fails when start
or end
point outside the bounds of self,
or when start
> end
.
fn slice_from(&self, start: uint) -> &'a [T]
Returns a slice of self from start
to the end of the vec.
Fails when start
points outside the bounds of self.
fn slice_to(&self, end: uint) -> &'a [T]
Returns a slice of self from the start of the vec to end
.
Fails when end
points outside the bounds of self.
fn iter(self) -> Items<'a, T>
Returns an iterator over the vector
fn rev_iter(self) -> RevItems<'a, T>
Returns a reversed iterator over a vector
fn split(self, pred: 'a |&T| -> bool) -> Splits<'a, T>
Returns an iterator over the subslices of the vector which are
separated by elements that match pred
. The matched element
is not contained in the subslices.
fn splitn(self, n: uint, pred: 'a |&T| -> bool) -> Splits<'a, T>
Returns an iterator over the subslices of the vector which are
separated by elements that match pred
, limited to splitting
at most n
times. The matched element is not contained in
the subslices.
fn rsplit(self, pred: 'a |&T| -> bool) -> RevSplits<'a, T>
Returns an iterator over the subslices of the vector which are
separated by elements that match pred
. This starts at the
end of the vector and works backwards. The matched element is
not contained in the subslices.
fn rsplitn(self, n: uint, pred: 'a |&T| -> bool) -> RevSplits<'a, T>
Returns an iterator over the subslices of the vector which are
separated by elements that match pred
limited to splitting
at most n
times. This starts at the end of the vector and
works backwards. The matched element is not contained in the
subslices.
fn windows(self, size: uint) -> Windows<'a, T>
Returns an iterator over all contiguous windows of length
size
. The windows overlap. If the vector is shorter than
size
, the iterator returns no values.
Failure
Fails if size
is 0.
Example
Print the adjacent pairs of a vector (i.e. [1,2]
, [2,3]
,
[3,4]
):
let v = &[1,2,3,4]; for win in v.windows(2) { println!("{:?}", win); }
fn chunks(self, size: uint) -> Chunks<'a, T>
Returns an iterator over size
elements of the vector at a
time. The chunks 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.
Example
Print the vector two elements at a time (i.e. [1,2]
,
[3,4]
, [5]
):
let v = &[1,2,3,4,5]; for win in v.chunks(2) { println!("{:?}", win); }
fn get(&self, index: uint) -> Option<&'a T>
Returns the element of a vector at the given index, or None
if the
index is out of bounds
fn head(&self) -> Option<&'a T>
Returns the first element of a vector, or None
if it is empty
fn tail(&self) -> &'a [T]
Returns all but the first element of a vector
fn tailn(&self, n: uint) -> &'a [T]
Returns all but the first `n' elements of a vector
fn init(&self) -> &'a [T]
Returns all but the last element of a vector
fn initn(&self, n: uint) -> &'a [T]
Returns all but the last `n' elements of a vector
fn last(&self) -> Option<&'a T>
Returns the last element of a vector, or None
if it is empty.
unsafe fn unsafe_ref(self, index: uint) -> &'a T
Returns a pointer to the element at the given index, without doing bounds checking.
fn as_ptr(&self) -> *T
Returns an unsafe 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.
fn bsearch(&self, f: |&T| -> Ordering) -> Option<uint>
Binary search a sorted vector with a comparator function.
The comparator function should implement an order consistent
with the sort order of the underlying vector, returning an
order code that indicates whether its argument is Less
,
Equal
or Greater
the desired target.
Returns the index where the comparator returned Equal
, or None
if
not found.
fn shift_ref(&mut self) -> Option<&'a 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:
if self.len() == 0 { return None } let head = &self[0]; *self = self.slice_from(1); Some(head)
Returns None
if vector is empty
fn pop_ref(&mut self) -> Option<&'a 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:
if self.len() == 0 { return None; } let tail = &self[self.len() - 1]; *self = self.slice_to(self.len() - 1); Some(tail)
Returns None
if slice is empty.
Implementors
impl<'a, T> ImmutableVector<'a, T> for &'a [T]