Trait std::slice::SliceExtUnstable
[-]
[+]
[src]
pub trait SliceExt<T> { fn sort_by<F>(&mut self, compare: F); fn move_from(&mut self, src: Vec<T>, start: uint, end: uint) -> uint; fn slice(&self, start: uint, end: uint) -> &[T]; fn slice_from(&self, start: uint) -> &[T]; fn slice_to(&self, end: uint) -> &[T]; fn split_at(&self, mid: uint) -> (&[T], &[T]); fn iter(&self) -> Iter<T>; fn split<F>(&self, pred: F) -> Split<T, F>; fn splitn<F>(&self, n: uint, pred: F) -> SplitN<T, F>; fn rsplitn<F>(&self, n: uint, pred: F) -> RSplitN<T, F>; fn windows(&self, size: uint) -> Windows<T>; fn chunks(&self, size: uint) -> Chunks<T>; fn get(&self, index: uint) -> Option<&T>; fn first(&self) -> Option<&T>; fn tail(&self) -> &[T]; fn init(&self) -> &[T]; fn last(&self) -> Option<&T>; unsafe fn get_unchecked(&self, index: uint) -> &T; fn as_ptr(&self) -> *const T; fn binary_search_by<F>(&self, f: F) -> Result<uint, uint>; fn len(&self) -> uint; fn get_mut(&mut self, index: uint) -> Option<&mut T>; fn as_mut_slice(&mut self) -> &mut [T]; fn slice_mut(&mut self, start: uint, end: uint) -> &mut [T]; fn slice_from_mut(&mut self, start: uint) -> &mut [T]; fn slice_to_mut(&mut self, end: uint) -> &mut [T]; fn iter_mut(&mut self) -> IterMut<T>; fn first_mut(&mut self) -> Option<&mut T>; fn tail_mut(&mut self) -> &mut [T]; fn init_mut(&mut self) -> &mut [T]; fn last_mut(&mut self) -> Option<&mut T>; fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F>; fn splitn_mut<F>(&mut self, n: uint, pred: F) -> SplitNMut<T, F>; fn rsplitn_mut<F>(&mut self, n: uint, pred: F) -> RSplitNMut<T, F>; fn chunks_mut(&mut self, chunk_size: uint) -> ChunksMut<T>; fn swap(&mut self, a: uint, b: uint); fn split_at_mut(&mut self, mid: uint) -> (&mut [T], &mut [T]); fn reverse(&mut self); unsafe fn get_unchecked_mut(&mut self, index: uint) -> &mut T; fn as_mut_ptr(&mut self) -> *mut T; fn head(&self) -> Option<&T> { ... } unsafe fn unsafe_get(&self, index: uint) -> &T { ... } fn is_empty(&self) -> bool { ... } fn head_mut(&mut self) -> Option<&mut T> { ... } unsafe fn unchecked_mut(&mut self, index: uint) -> &mut T { ... } }
Allocating extension methods for slices.
Required Methods
fn sort_by<F>(&mut self, compare: F)
Sorts the slice, 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
.
Examples
fn main() { let mut v = [5i, 4, 1, 3, 2]; v.sort_by(|a, b| a.cmp(b)); assert!(v == [1, 2, 3, 4, 5]); // reverse sorting v.sort_by(|a, b| b.cmp(a)); assert!(v == [5, 4, 3, 2, 1]); }let mut v = [5i, 4, 1, 3, 2]; v.sort_by(|a, b| a.cmp(b)); assert!(v == [1, 2, 3, 4, 5]); // reverse sorting v.sort_by(|a, b| b.cmp(a)); assert!(v == [5, 4, 3, 2, 1]);
fn move_from(&mut self, src: Vec<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
src
to stop copying from
Examples
fn main() { let mut a = [1i, 2, 3, 4, 5]; let b = vec![6i, 7, 8]; let num_moved = a.move_from(b, 0, 3); assert_eq!(num_moved, 3); assert!(a == [6i, 7, 8, 4, 5]); }let mut a = [1i, 2, 3, 4, 5]; let b = vec![6i, 7, 8]; let num_moved = a.move_from(b, 0, 3); assert_eq!(num_moved, 3); assert!(a == [6i, 7, 8, 4, 5]);
fn slice(&self, start: uint, end: uint) -> &[T]
Returns a subslice spanning the interval [start
, end
).
Panics when the end of the new slice lies beyond the end of the
original slice (i.e. when end > self.len()
) or when start > end
.
Slicing with start
equal to end
yields an empty slice.
fn slice_from(&self, start: uint) -> &[T]
Returns a subslice from start
to the end of the slice.
Panics when start
is strictly greater than the length of the original slice.
Slicing from self.len()
yields an empty slice.
fn slice_to(&self, end: uint) -> &[T]
Returns a subslice from the start of the slice to end
.
Panics when end
is strictly greater than the length of the original slice.
Slicing to 0
yields an empty slice.
fn split_at(&self, mid: uint) -> (&[T], &[T])
Divides one slice 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).
Panics if mid > len
.
fn iter(&self) -> Iter<T>
Returns an iterator over the slice
fn split<F>(&self, pred: F) -> Split<T, F>
Returns an iterator over subslices separated by elements that match
pred
. The matched element is not contained in the subslices.
fn splitn<F>(&self, n: uint, pred: F) -> SplitN<T, F>
Returns an iterator over subslices separated by elements that match
pred
, limited to splitting at most n
times. The matched element is
not contained in the subslices.
fn rsplitn<F>(&self, n: uint, pred: F) -> RSplitN<T, F>
Returns an iterator over subslices separated by elements that match
pred
limited to splitting at most n
times. This starts at the end of
the slice and works backwards. The matched element is not contained in
the subslices.
fn windows(&self, size: uint) -> Windows<T>
Returns an iterator over all contiguous windows of length
size
. The windows overlap. If the slice is shorter than
size
, the iterator returns no values.
Panics
Panics if size
is 0.
Example
Print the adjacent pairs of a slice (i.e. [1,2]
, [2,3]
,
[3,4]
):
let v = &[1i, 2, 3, 4]; for win in v.windows(2) { println!("{}", win); }
fn chunks(&self, size: uint) -> Chunks<T>
Returns an iterator over size
elements of the slice at a
time. The chunks do not overlap. If size
does not divide the
length of the slice, then the last chunk will not have length
size
.
Panics
Panics if size
is 0.
Example
Print the slice two elements at a time (i.e. [1,2]
,
[3,4]
, [5]
):
let v = &[1i, 2, 3, 4, 5]; for win in v.chunks(2) { println!("{}", win); }
fn get(&self, index: uint) -> Option<&T>
Returns the element of a slice at the given index, or None
if the
index is out of bounds.
fn first(&self) -> Option<&T>
Returns the first element of a slice, or None
if it is empty.
fn tail(&self) -> &[T]
Returns all but the first element of a slice.
fn init(&self) -> &[T]
Returns all but the last element of a slice.
fn last(&self) -> Option<&T>
Returns the last element of a slice, or None
if it is empty.
unsafe fn get_unchecked(&self, index: uint) -> &T
Returns a pointer to the element at the given index, without doing bounds checking.
fn as_ptr(&self) -> *const T
Returns an unsafe pointer to the slice's buffer
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Modifying the slice may cause its buffer to be reallocated, which would also make any pointers to it invalid.
fn binary_search_by<F>(&self, f: F) -> Result<uint, uint>
Binary search a sorted slice with a comparator function.
The comparator function should implement an order consistent
with the sort order of the underlying slice, returning an
order code that indicates whether its argument is Less
,
Equal
or Greater
the desired target.
If a matching value is found then returns Ok
, containing
the index for the matched element; if no match is found then
Err
is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
Example
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1,4]
.
let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let s = s.as_slice(); let seek = 13; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9)); let seek = 4; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7)); let seek = 100; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13)); let seek = 1; let r = s.binary_search_by(|probe| probe.cmp(&seek)); assert!(match r { Ok(1...4) => true, _ => false, });
fn len(&self) -> uint
Return the number of elements in the slice
Example
fn main() { let a = [1i, 2, 3]; assert_eq!(a.len(), 3); }let a = [1i, 2, 3]; assert_eq!(a.len(), 3);
fn get_mut(&mut self, index: uint) -> Option<&mut T>
Returns a mutable reference to the element at the given index,
or None
if the index is out of bounds
fn as_mut_slice(&mut self) -> &mut [T]
Work with self
as a mut slice.
Primarily intended for getting a &mut [T] from a [T, ..N].
fn slice_mut(&mut self, start: uint, end: uint) -> &mut [T]
Returns a mutable subslice spanning the interval [start
, end
).
Panics when the end of the new slice lies beyond the end of the
original slice (i.e. when end > self.len()
) or when start > end
.
Slicing with start
equal to end
yields an empty slice.
fn slice_from_mut(&mut self, start: uint) -> &mut [T]
Returns a mutable subslice from start
to the end of the slice.
Panics when start
is strictly greater than the length of the original slice.
Slicing from self.len()
yields an empty slice.
fn slice_to_mut(&mut self, end: uint) -> &mut [T]
Returns a mutable subslice from the start of the slice to end
.
Panics when end
is strictly greater than the length of the original slice.
Slicing to 0
yields an empty slice.
fn iter_mut(&mut self) -> IterMut<T>
Returns an iterator that allows modifying each value
fn first_mut(&mut self) -> Option<&mut T>
Returns a mutable pointer to the first element of a slice, or None
if it is empty
fn tail_mut(&mut self) -> &mut [T]
Returns all but the first element of a mutable slice
fn init_mut(&mut self) -> &mut [T]
Returns all but the last element of a mutable slice
fn last_mut(&mut self) -> Option<&mut T>
Returns a mutable pointer to the last item in the slice.
fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F>
Returns an iterator over mutable subslices separated by elements that
match pred
. The matched element is not contained in the subslices.
fn splitn_mut<F>(&mut self, n: uint, pred: F) -> SplitNMut<T, F>
Returns an iterator over subslices separated by elements that match
pred
, limited to splitting at most n
times. The matched element is
not contained in the subslices.
fn rsplitn_mut<F>(&mut self, n: uint, pred: F) -> RSplitNMut<T, F>
Returns an iterator over subslices separated by elements that match
pred
limited to splitting at most n
times. This starts at the end of
the slice and works backwards. The matched element is not contained in
the subslices.
fn chunks_mut(&mut self, chunk_size: uint) -> ChunksMut<T>
Returns an iterator over chunk_size
elements of the slice at a time.
The chunks are mutable and do not overlap. If chunk_size
does
not divide the length of the slice, then the last chunk will not
have length chunk_size
.
Panics
Panics if chunk_size
is 0.
fn swap(&mut self, a: uint, b: uint)
Swaps two elements in a slice.
Arguments
- a - The index of the first element
- b - The index of the second element
Panics
Panics if a
or b
are out of bounds.
Example
fn main() { let mut v = ["a", "b", "c", "d"]; v.swap(1, 3); assert!(v == ["a", "d", "c", "b"]); }let mut v = ["a", "b", "c", "d"]; v.swap(1, 3); assert!(v == ["a", "d", "c", "b"]);
fn split_at_mut(&mut self, mid: uint) -> (&mut [T], &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).
Panics
Panics if mid > len
.
Example
fn main() { let mut v = [1i, 2, 3, 4, 5, 6]; // scoped to restrict the lifetime of the borrows { let (left, right) = v.split_at_mut(0); assert!(left == []); assert!(right == [1i, 2, 3, 4, 5, 6]); } { let (left, right) = v.split_at_mut(2); assert!(left == [1i, 2]); assert!(right == [3i, 4, 5, 6]); } { let (left, right) = v.split_at_mut(6); assert!(left == [1i, 2, 3, 4, 5, 6]); assert!(right == []); } }let mut v = [1i, 2, 3, 4, 5, 6]; // scoped to restrict the lifetime of the borrows { let (left, right) = v.split_at_mut(0); assert!(left == []); assert!(right == [1i, 2, 3, 4, 5, 6]); } { let (left, right) = v.split_at_mut(2); assert!(left == [1i, 2]); assert!(right == [3i, 4, 5, 6]); } { let (left, right) = v.split_at_mut(6); assert!(left == [1i, 2, 3, 4, 5, 6]); assert!(right == []); }
fn reverse(&mut self)
Reverse the order of elements in a slice, in place.
Example
fn main() { let mut v = [1i, 2, 3]; v.reverse(); assert!(v == [3i, 2, 1]); }let mut v = [1i, 2, 3]; v.reverse(); assert!(v == [3i, 2, 1]);
unsafe fn get_unchecked_mut(&mut self, index: uint) -> &mut T
Returns an unsafe mutable pointer to the element in index
fn as_mut_ptr(&mut self) -> *mut T
Return an unsafe mutable pointer to the slice's buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Modifying the slice may cause its buffer to be reallocated, which would also make any pointers to it invalid.
Provided Methods
fn head(&self) -> Option<&T>
Deprecated: renamed to first
.
unsafe fn unsafe_get(&self, index: uint) -> &T
Deprecated: renamed to get_unchecked
.
fn is_empty(&self) -> bool
Returns true if the slice has a length of 0
Example
fn main() { let a = [1i, 2, 3]; assert!(!a.is_empty()); }let a = [1i, 2, 3]; assert!(!a.is_empty());
fn head_mut(&mut self) -> Option<&mut T>
Depreated: renamed to first_mut
.
unsafe fn unchecked_mut(&mut self, index: uint) -> &mut T
Deprecated: renamed to get_unchecked_mut
.