Added vec::view, for creating subslices.
This commit is contained in:
parent
0eed37da29
commit
c568cf6099
1 changed files with 22 additions and 0 deletions
|
@ -22,6 +22,7 @@ export init;
|
||||||
export last;
|
export last;
|
||||||
export last_opt;
|
export last_opt;
|
||||||
export slice;
|
export slice;
|
||||||
|
export view;
|
||||||
export split;
|
export split;
|
||||||
export splitn;
|
export splitn;
|
||||||
export rsplit;
|
export rsplit;
|
||||||
|
@ -254,6 +255,18 @@ fn slice<T: copy>(v: [const T], start: uint, end: uint) -> [T] {
|
||||||
ret result;
|
ret result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc = "Return a slice that points into another slice."]
|
||||||
|
fn view<T: copy>(v: [const T]/&, start: uint, end: uint) -> [T]/&a {
|
||||||
|
assert (start <= end);
|
||||||
|
assert (end <= len(v));
|
||||||
|
unpack_slice(v) {|p, _len|
|
||||||
|
unsafe {
|
||||||
|
::unsafe::reinterpret_cast(
|
||||||
|
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
Split the vector `v` by applying each element against the predicate `f`.
|
Split the vector `v` by applying each element against the predicate `f`.
|
||||||
"]
|
"]
|
||||||
|
@ -2029,6 +2042,15 @@ mod tests {
|
||||||
reserve(v, 10u);
|
reserve(v, 10u);
|
||||||
assert capacity(v) == 10u;
|
assert capacity(v) == 10u;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_view() {
|
||||||
|
let v = [1, 2, 3, 4, 5];
|
||||||
|
let v = view(v, 1u, 3u);
|
||||||
|
assert(len(v) == 2u);
|
||||||
|
assert(v[0] == 2);
|
||||||
|
assert(v[1] == 3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue