提交 9d4aab80 编写于 作者: E Erick Tryzelaar 提交者: Graydon Hoare

libcore: add vec::{mut_view, const_view}.

上级 a762c725
......@@ -27,7 +27,7 @@
export last;
export last_opt;
export slice;
export view;
export view, mut_view, const_view;
export split;
export splitn;
export rsplit;
......@@ -325,6 +325,30 @@ fn reserve_at_least<T>(&v: ~[const T], n: uint) {
}
}
/// Return a slice that points into another slice.
pure fn mut_view<T>(v: &[mut T], start: uint, end: uint) -> &[mut T] {
assert (start <= end);
assert (end <= len(v));
do unpack_slice(v) |p, _len| {
unsafe {
::unsafe::reinterpret_cast(
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
}
}
}
/// Return a slice that points into another slice.
pure fn const_view<T>(v: &[const T], start: uint, end: uint) -> &[const T] {
assert (start <= end);
assert (end <= len(v));
do unpack_slice(v) |p, _len| {
unsafe {
::unsafe::reinterpret_cast(
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
}
}
}
/// Split the vector `v` by applying each element against the predicate `f`.
fn split<T: copy>(v: &[T], f: fn(T) -> bool) -> ~[~[T]] {
let ln = len(v);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册