From d0512b1055eac15db86d83c994fb546cbfa62676 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 19:48:50 +1000 Subject: Convert vec::[mut_]slice to methods, remove vec::const_slice. --- src/libstd/io.rs | 11 +++-- src/libstd/os.rs | 2 +- src/libstd/rt/io/extensions.rs | 2 +- src/libstd/rt/io/mem.rs | 2 +- src/libstd/vec.rs | 100 +++++++++++++++-------------------------- 5 files changed, 45 insertions(+), 72 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/io.rs b/src/libstd/io.rs index a78be9c8b2b..4d9c08f25da 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -65,7 +65,7 @@ use str::StrSlice; use to_str::ToStr; use uint; use vec; -use vec::{ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector}; +use vec::{MutableVector, ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector}; #[allow(non_camel_case_types)] // not sure what to do about this pub type fd_t = c_int; @@ -698,7 +698,7 @@ impl ReaderUtil for T { // over-read by reading 1-byte per char needed nbread = if ncreq > nbreq { ncreq } else { nbreq }; if nbread > 0 { - bytes = vec::slice(bytes, offset, bytes.len()).to_owned(); + bytes = bytes.slice(offset, bytes.len()).to_owned(); } } chars @@ -1053,7 +1053,7 @@ impl Reader for BytesReader { fn read(&self, bytes: &mut [u8], len: uint) -> uint { let count = uint::min(len, self.bytes.len() - *self.pos); - let view = vec::slice(self.bytes, *self.pos, self.bytes.len()); + let view = self.bytes.slice(*self.pos, self.bytes.len()); vec::bytes::copy_memory(bytes, view, count); *self.pos += count; @@ -1663,7 +1663,7 @@ impl Writer for BytesWriter { unsafe { vec::raw::set_len(bytes, count); - let view = vec::mut_slice(*bytes, *self.pos, count); + let view = bytes.mut_slice(*self.pos, count); vec::bytes::copy_memory(view, v, v_len); } @@ -1909,8 +1909,7 @@ mod tests { if len <= ivals.len() { assert_eq!(res.len(), len); } - assert!(vec::slice(ivals, 0u, res.len()) == - vec::map(res, |x| *x as int)); + assert!(ivals.slice(0u, res.len()) == vec::map(res, |x| *x as int)); } } let mut i = 0; diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 112540c405d..5534c5befc2 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -134,7 +134,7 @@ pub mod win32 { } } if k != 0 && done { - let sub = vec::slice(buf, 0u, k as uint); + let sub = buf.slice(0, k as uint); res = option::Some(str::from_utf16(sub)); } } diff --git a/src/libstd/rt/io/extensions.rs b/src/libstd/rt/io/extensions.rs index 55861f127bb..5320bd0f42e 100644 --- a/src/libstd/rt/io/extensions.rs +++ b/src/libstd/rt/io/extensions.rs @@ -298,7 +298,7 @@ impl ReaderUtil for T { do (|| { while total_read < len { let len = buf.len(); - let slice = vec::mut_slice(*buf, start_len + total_read, len); + let slice = buf.mut_slice(start_len + total_read, len); match self.read(slice) { Some(nread) => { total_read += nread; diff --git a/src/libstd/rt/io/mem.rs b/src/libstd/rt/io/mem.rs index bd9cff76e57..c93945a6a9a 100644 --- a/src/libstd/rt/io/mem.rs +++ b/src/libstd/rt/io/mem.rs @@ -86,7 +86,7 @@ impl Reader for MemReader { let write_len = min(buf.len(), self.buf.len() - self.pos); { let input = self.buf.slice(self.pos, self.pos + write_len); - let output = vec::mut_slice(buf, 0, write_len); + let output = buf.mut_slice(0, write_len); assert_eq!(input.len(), output.len()); vec::bytes::copy_memory(output, input, write_len); } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 56e6bacf93e..c8dc5aa7f79 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -251,17 +251,17 @@ pub fn head_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { } /// Returns a vector containing all but the first element of a slice -pub fn tail<'r,T>(v: &'r [T]) -> &'r [T] { slice(v, 1, v.len()) } +pub fn tail<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(1, v.len()) } /// Returns a vector containing all but the first `n` elements of a slice -pub fn tailn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { slice(v, n, v.len()) } +pub fn tailn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { v.slice(n, v.len()) } /// Returns a vector containing all but the last element of a slice -pub fn init<'r,T>(v: &'r [T]) -> &'r [T] { slice(v, 0, v.len() - 1) } +pub fn init<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(0, v.len() - 1) } /// Returns a vector containing all but the last `n' elements of a slice pub fn initn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { - slice(v, 0, v.len() - n) + v.slice(0, v.len() - n) } /// Returns the last element of the slice `v`, failing if the slice is empty. @@ -276,47 +276,6 @@ pub fn last_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { if v.len() == 0 { None } else { Some(&v[v.len() - 1]) } } -/// Return a slice that points into another slice. -#[inline] -pub fn slice<'r,T>(v: &'r [T], start: uint, end: uint) -> &'r [T] { - assert!(start <= end); - assert!(end <= v.len()); - do as_imm_buf(v) |p, _len| { - unsafe { - transmute((ptr::offset(p, start), - (end - start) * sys::nonzero_size_of::())) - } - } -} - -/// Return a slice that points into another slice. -#[inline] -pub fn mut_slice<'r,T>(v: &'r mut [T], start: uint, end: uint) - -> &'r mut [T] { - assert!(start <= end); - assert!(end <= v.len()); - do as_mut_buf(v) |p, _len| { - unsafe { - transmute((ptr::mut_offset(p, start), - (end - start) * sys::nonzero_size_of::())) - } - } -} - -/// Return a slice that points into another slice. -#[inline] -pub fn const_slice<'r,T>(v: &'r const [T], start: uint, end: uint) - -> &'r const [T] { - assert!(start <= end); - assert!(end <= v.len()); - do as_const_buf(v) |p, _len| { - unsafe { - transmute((ptr::const_offset(p, start), - (end - start) * sys::nonzero_size_of::())) - } - } -} - /// Copies /// Split the vector `v` by applying each element against the predicate `f`. @@ -330,12 +289,12 @@ pub fn split(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { match position_between(v, start, ln, f) { None => break, Some(i) => { - result.push(slice(v, start, i).to_owned()); + result.push(v.slice(start, i).to_owned()); start = i + 1u; } } } - result.push(slice(v, start, ln).to_owned()); + result.push(v.slice(start, ln).to_owned()); result } @@ -354,14 +313,14 @@ pub fn splitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { match position_between(v, start, ln, f) { None => break, Some(i) => { - result.push(slice(v, start, i).to_owned()); + result.push(v.slice(start, i).to_owned()); // Make sure to skip the separator. start = i + 1u; count -= 1u; } } } - result.push(slice(v, start, ln).to_owned()); + result.push(v.slice(start, ln).to_owned()); result } @@ -379,12 +338,12 @@ pub fn rsplit(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { match rposition_between(v, 0, end, f) { None => break, Some(i) => { - result.push(slice(v, i + 1, end).to_owned()); + result.push(v.slice(i + 1, end).to_owned()); end = i; } } } - result.push(slice(v, 0u, end).to_owned()); + result.push(v.slice(0u, end).to_owned()); reverse(result); result } @@ -404,14 +363,14 @@ pub fn rsplitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { match rposition_between(v, 0u, end, f) { None => break, Some(i) => { - result.push(slice(v, i + 1u, end).to_owned()); + result.push(v.slice(i + 1u, end).to_owned()); // Make sure to skip the separator. end = i; count -= 1u; } } } - result.push(slice(v, 0u, end).to_owned()); + result.push(v.slice(0u, end).to_owned()); reverse(result); result } @@ -487,15 +446,15 @@ pub fn shift(v: &mut ~[T]) -> T { // popped. For the moment it unsafely exists at both the head and last // positions { - let first_slice = slice(*v, 0, 1); - let last_slice = slice(*v, next_ln, ln); + let first_slice = v.slice(0, 1); + let last_slice = v.slice(next_ln, ln); raw::copy_memory(transmute(last_slice), first_slice, 1); } // Memcopy everything to the left one element { - let init_slice = slice(*v, 0, next_ln); - let tail_slice = slice(*v, 1, ln); + let init_slice = v.slice(0, next_ln); + let tail_slice = v.slice(1, ln); raw::copy_memory(transmute(init_slice), tail_slice, next_ln); @@ -1689,7 +1648,14 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { /// Return a slice that points into another slice. #[inline] fn slice(&self, start: uint, end: uint) -> &'self [T] { - slice(*self, start, end) + assert!(start <= end); + assert!(end <= self.len()); + do as_imm_buf(*self) |p, _len| { + unsafe { + transmute((ptr::offset(p, start), + (end - start) * sys::nonzero_size_of::())) + } + } } #[inline] @@ -2042,9 +2008,17 @@ pub trait MutableVector<'self, T> { } impl<'self,T> MutableVector<'self, T> for &'self mut [T] { + /// Return a slice that points into another slice. #[inline] fn mut_slice(self, start: uint, end: uint) -> &'self mut [T] { - mut_slice(self, start, end) + assert!(start <= end); + assert!(end <= self.len()); + do as_mut_buf(self) |p, _len| { + unsafe { + transmute((ptr::mut_offset(p, start), + (end - start) * sys::nonzero_size_of::())) + } + } } #[inline] @@ -2713,7 +2687,7 @@ mod tests { fn test_slice() { // Test fixed length vector. let vec_fixed = [1, 2, 3, 4]; - let v_a = slice(vec_fixed, 1u, vec_fixed.len()).to_owned(); + let v_a = vec_fixed.slice(1u, vec_fixed.len()).to_owned(); assert_eq!(v_a.len(), 3u); assert_eq!(v_a[0], 2); assert_eq!(v_a[1], 3); @@ -2721,14 +2695,14 @@ mod tests { // Test on stack. let vec_stack = &[1, 2, 3]; - let v_b = slice(vec_stack, 1u, 3u).to_owned(); + let v_b = vec_stack.slice(1u, 3u).to_owned(); assert_eq!(v_b.len(), 2u); assert_eq!(v_b[0], 2); assert_eq!(v_b[1], 3); // Test on managed heap. let vec_managed = @[1, 2, 3, 4, 5]; - let v_c = slice(vec_managed, 0u, 3u).to_owned(); + let v_c = vec_managed.slice(0u, 3u).to_owned(); assert_eq!(v_c.len(), 3u); assert_eq!(v_c[0], 1); assert_eq!(v_c[1], 2); @@ -2736,7 +2710,7 @@ mod tests { // Test on exchange heap. let vec_unique = ~[1, 2, 3, 4, 5, 6]; - let v_d = slice(vec_unique, 1u, 6u).to_owned(); + let v_d = vec_unique.slice(1u, 6u).to_owned(); assert_eq!(v_d.len(), 5u); assert_eq!(v_d[0], 2); assert_eq!(v_d[1], 3); -- cgit 1.4.1-3-g733a5 From d2e3e1e52ba008c58ecfa801cb5d127365d20ae5 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 22:36:27 +1000 Subject: Convert vec::{head, tail, init, last} (and similar fns) to methods. --- src/compiletest/compiletest.rs | 2 +- src/libextra/test.rs | 2 +- src/librustc/middle/trans/cabi_x86_64.rs | 2 +- src/librustc/middle/trans/callee.rs | 3 +- src/librustc/middle/trans/meth.rs | 5 +-- src/librustc/middle/ty.rs | 2 +- src/libstd/vec.rs | 76 +++++++++++--------------------- 7 files changed, 33 insertions(+), 59 deletions(-) (limited to 'src/libstd') diff --git a/src/compiletest/compiletest.rs b/src/compiletest/compiletest.rs index e8876c4851b..b2ba05c550d 100644 --- a/src/compiletest/compiletest.rs +++ b/src/compiletest/compiletest.rs @@ -75,7 +75,7 @@ pub fn parse_config(args: ~[~str]) -> config { ]; assert!(!args.is_empty()); - let args_ = vec::tail(args); + let args_ = args.tail(); let matches = &match getopts::getopts(args_, opts) { Ok(m) => m, diff --git a/src/libextra/test.rs b/src/libextra/test.rs index 72e70943ce1..bb03e3ab9bb 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -139,7 +139,7 @@ type OptRes = Either; // Parses command line arguments into test options pub fn parse_opts(args: &[~str]) -> OptRes { - let args_ = vec::tail(args); + let args_ = args.tail(); let opts = ~[getopts::optflag("ignored"), getopts::optflag("test"), getopts::optflag("bench"), diff --git a/src/librustc/middle/trans/cabi_x86_64.rs b/src/librustc/middle/trans/cabi_x86_64.rs index 14ab17f5030..73323634c2b 100644 --- a/src/librustc/middle/trans/cabi_x86_64.rs +++ b/src/librustc/middle/trans/cabi_x86_64.rs @@ -312,7 +312,7 @@ fn llreg_ty(cls: &[RegClass]) -> Type { tys.push(Type::i64()); } SSEFv => { - let vec_len = llvec_len(vec::tailn(cls, i + 1u)) * 2u; + let vec_len = llvec_len(cls.tailn(i + 1u)) * 2u; let vec_ty = Type::vector(&Type::f32(), vec_len as u64); tys.push(vec_ty); i += vec_len; diff --git a/src/librustc/middle/trans/callee.rs b/src/librustc/middle/trans/callee.rs index cb475550638..064a457c712 100644 --- a/src/librustc/middle/trans/callee.rs +++ b/src/librustc/middle/trans/callee.rs @@ -47,7 +47,6 @@ use util::ppaux::Repr; use middle::trans::type_::Type; -use core::vec; use syntax::ast; use syntax::ast_map; use syntax::visit; @@ -503,7 +502,7 @@ pub fn trans_call_inner(in_cx: block, do base::with_scope(in_cx, call_info, "call") |cx| { let ret_in_loop = match args { ArgExprs(args) => { - args.len() > 0u && match vec::last(args).node { + args.len() > 0u && match args.last().node { ast::expr_loop_body(@ast::expr { node: ast::expr_fn_block(_, ref body), _ diff --git a/src/librustc/middle/trans/meth.rs b/src/librustc/middle/trans/meth.rs index 38e4f087b0e..b6911a7eb96 100644 --- a/src/librustc/middle/trans/meth.rs +++ b/src/librustc/middle/trans/meth.rs @@ -492,8 +492,7 @@ pub fn combine_impl_and_methods_tps(bcx: block, debug!("rcvr_substs=%?", rcvr_substs.map(|t| bcx.ty_to_str(*t))); let ty_substs = vec::append(rcvr_substs.to_owned(), - vec::tailn(node_substs, - node_substs.len() - n_m_tps)); + node_substs.tailn(node_substs.len() - n_m_tps)); debug!("n_m_tps=%?", n_m_tps); debug!("node_substs=%?", node_substs.map(|t| bcx.ty_to_str(*t))); debug!("ty_substs=%?", ty_substs.map(|t| bcx.ty_to_str(*t))); @@ -540,7 +539,7 @@ pub fn combine_impl_and_methods_origins(bcx: block, }; // Extract those that belong to method: - let m_origins = vec::tailn(*r_m_origins, r_m_origins.len() - m_vtables); + let m_origins = r_m_origins.tailn(r_m_origins.len() - m_vtables); // Combine rcvr + method to find the final result: @vec::append(/*bad*/copy *rcvr_origins, m_origins) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 4cea4c94972..939fecfe40a 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3954,7 +3954,7 @@ pub fn item_path(cx: ctxt, id: ast::def_id) -> ast_map::path { } ast_map::node_variant(ref variant, _, path) => { - vec::append_one(vec::to_owned(vec::init(*path)), + vec::append_one(vec::to_owned(path.init()), ast_map::path_name((*variant).node.name)) } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index c8dc5aa7f79..3dae32d717d 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -238,44 +238,6 @@ pub fn build_sized_opt(size: Option, // Accessors -/// Returns the first element of a vector -pub fn head<'r,T>(v: &'r [T]) -> &'r T { - if v.len() == 0 { fail!("head: empty vector") } - &v[0] -} - -/// Returns `Some(x)` where `x` is the first element of the slice `v`, -/// or `None` if the vector is empty. -pub fn head_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { - if v.len() == 0 { None } else { Some(&v[0]) } -} - -/// Returns a vector containing all but the first element of a slice -pub fn tail<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(1, v.len()) } - -/// Returns a vector containing all but the first `n` elements of a slice -pub fn tailn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { v.slice(n, v.len()) } - -/// Returns a vector containing all but the last element of a slice -pub fn init<'r,T>(v: &'r [T]) -> &'r [T] { v.slice(0, v.len() - 1) } - -/// Returns a vector containing all but the last `n' elements of a slice -pub fn initn<'r,T>(v: &'r [T], n: uint) -> &'r [T] { - v.slice(0, v.len() - n) -} - -/// Returns the last element of the slice `v`, failing if the slice is empty. -pub fn last<'r,T>(v: &'r [T]) -> &'r T { - if v.len() == 0 { fail!("last: empty vector") } - &v[v.len() - 1] -} - -/// Returns `Some(x)` where `x` is the last element of the slice `v`, or -/// `None` if the vector is empty. -pub fn last_opt<'r,T>(v: &'r [T]) -> Option<&'r T> { - if v.len() == 0 { None } else { Some(&v[v.len() - 1]) } -} - /// Copies /// Split the vector `v` by applying each element against the predicate `f`. @@ -1678,35 +1640,49 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { /// Returns the first element of a vector, failing if the vector is empty. #[inline] - fn head(&self) -> &'self T { head(*self) } + fn head(&self) -> &'self T { + if self.len() == 0 { fail!("head: empty vector") } + &self[0] + } - /// Returns the first element of a vector + /// Returns the first element of a vector, or `None` if it is empty #[inline] - fn head_opt(&self) -> Option<&'self T> { head_opt(*self) } + fn head_opt(&self) -> Option<&'self T> { + if self.len() == 0 { None } else { Some(&self[0]) } + } /// Returns all but the first element of a vector #[inline] - fn tail(&self) -> &'self [T] { tail(*self) } + fn tail(&self) -> &'self [T] { self.slice(1, self.len()) } /// Returns all but the first `n' elements of a vector #[inline] - fn tailn(&self, n: uint) -> &'self [T] { tailn(*self, n) } + fn tailn(&self, n: uint) -> &'self [T] { self.slice(n, self.len()) } - /// Returns all but the last elemnt of a vector + /// Returns all but the last element of a vector #[inline] - fn init(&self) -> &'self [T] { init(*self) } + fn init(&self) -> &'self [T] { + self.slice(0, self.len() - 1) + } /// Returns all but the last `n' elemnts of a vector #[inline] - fn initn(&self, n: uint) -> &'self [T] { initn(*self, n) } + fn initn(&self, n: uint) -> &'self [T] { + self.slice(0, self.len() - n) + } - /// Returns the last element of a `v`, failing if the vector is empty. + /// Returns the last element of a vector, failing if the vector is empty. #[inline] - fn last(&self) -> &'self T { last(*self) } + fn last(&self) -> &'self T { + if self.len() == 0 { fail!("last: empty vector") } + &self[self.len() - 1] + } - /// Returns the last element of a `v`, failing if the vector is empty. + /// Returns the last element of a vector, or `None` if it is empty. #[inline] - fn last_opt(&self) -> Option<&'self T> { last_opt(*self) } + fn last_opt(&self) -> Option<&'self T> { + if self.len() == 0 { None } else { Some(&self[self.len() - 1]) } + } /** * Find the last index matching some predicate -- cgit 1.4.1-3-g733a5 From 1cb0a567d1209855b476689b0e449a832035f05b Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 22:59:52 +1000 Subject: Convert vec::{pop, shift, unshift, insert, remove, swap_remove} to methods. --- src/libextra/treemap.rs | 3 +- src/librustc/middle/resolve.rs | 2 +- src/libstd/vec.rs | 234 ++++++++++++++++++----------------------- 3 files changed, 103 insertions(+), 136 deletions(-) (limited to 'src/libstd') diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index 33ec4ae94ba..4622b8c7284 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -695,7 +695,6 @@ mod test_treemap { use core::rand::RngUtil; use core::rand; - use core::vec; #[test] fn find_empty() { @@ -848,7 +847,7 @@ mod test_treemap { for 30.times { let r = rng.gen_uint_range(0, ctrl.len()); - let (key, _) = vec::remove(&mut ctrl, r); + let (key, _) = ctrl.remove(r); assert!(map.remove(&key)); check_structure(&map); check_equal(ctrl, &map); diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index e06fd8f9717..7cd64e863d2 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -4882,7 +4882,7 @@ impl Resolver { values[smallest] <= max_distance && name != maybes[smallest] { - Some(vec::swap_remove(&mut maybes, smallest)) + Some(maybes.swap_remove(smallest)) } else { None diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 3dae32d717d..bc933e70b37 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -377,96 +377,6 @@ pub fn partitioned(v: &[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) { (lefts, rights) } -// Mutators - -/// Removes the first element from a vector and return it -pub fn shift(v: &mut ~[T]) -> T { - unsafe { - assert!(!v.is_empty()); - - if v.len() == 1 { return v.pop() } - - if v.len() == 2 { - let last = v.pop(); - let first = v.pop(); - v.push(last); - return first; - } - - let ln = v.len(); - let next_ln = v.len() - 1; - - // Save the last element. We're going to overwrite its position - let work_elt = v.pop(); - // We still should have room to work where what last element was - assert!(capacity(v) >= ln); - // Pretend like we have the original length so we can use - // the vector copy_memory to overwrite the hole we just made - raw::set_len(&mut *v, ln); - - // Memcopy the head element (the one we want) to the location we just - // popped. For the moment it unsafely exists at both the head and last - // positions - { - let first_slice = v.slice(0, 1); - let last_slice = v.slice(next_ln, ln); - raw::copy_memory(transmute(last_slice), first_slice, 1); - } - - // Memcopy everything to the left one element - { - let init_slice = v.slice(0, next_ln); - let tail_slice = v.slice(1, ln); - raw::copy_memory(transmute(init_slice), - tail_slice, - next_ln); - } - - // Set the new length. Now the vector is back to normal - raw::set_len(&mut *v, next_ln); - - // Swap out the element we want from the end - let vp = raw::to_mut_ptr(*v); - let vp = ptr::mut_offset(vp, next_ln - 1); - - ptr::replace_ptr(vp, work_elt) - } -} - -/// Prepend an element to the vector -pub fn unshift(v: &mut ~[T], x: T) { - let vv = util::replace(v, ~[x]); - v.push_all_move(vv); -} - -/// Insert an element at position i within v, shifting all -/// elements after position i one position to the right. -pub fn insert(v: &mut ~[T], i: uint, x: T) { - let len = v.len(); - assert!(i <= len); - - v.push(x); - let mut j = len; - while j > i { - swap(*v, j, j - 1); - j -= 1; - } -} - -/// Remove and return the element at position i within v, shifting -/// all elements after position i one position to the left. -pub fn remove(v: &mut ~[T], i: uint) -> T { - let len = v.len(); - assert!(i < len); - - let mut j = i; - while j < len - 1 { - swap(*v, j, j + 1); - j += 1; - } - v.pop() -} - /// Consumes all elements, in a vector, moving them out into the / closure /// provided. The vector is traversed from the start to the end. /// @@ -528,37 +438,6 @@ pub fn consume_reverse(mut v: ~[T], f: &fn(uint, v: T)) { } } -/// Remove the last element from a vector and return it -pub fn pop(v: &mut ~[T]) -> T { - let ln = v.len(); - if ln == 0 { - fail!("sorry, cannot vec::pop an empty vector") - } - let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]); - unsafe { - let val = ptr::replace_ptr(valptr, intrinsics::init()); - raw::set_len(v, ln - 1u); - val - } -} - -/** - * Remove an element from anywhere in the vector and return it, replacing it - * with the last element. This does not preserve ordering, but is O(1). - * - * Fails if index >= length. - */ -pub fn swap_remove(v: &mut ~[T], index: uint) -> T { - let ln = v.len(); - if index >= ln { - fail!("vec::swap_remove - index %u >= length %u", index, ln); - } - if index < ln - 1 { - swap(*v, index, ln - 1); - } - v.pop() -} - /// Append an element to a vector #[inline] pub fn push(v: &mut ~[T], initval: T) { @@ -1847,34 +1726,123 @@ impl OwnedVector for ~[T] { push_all_move(self, rhs); } - #[inline] + /// Remove the last element from a vector and return it fn pop(&mut self) -> T { - pop(self) + let ln = self.len(); + if ln == 0 { + fail!("sorry, cannot pop an empty vector") + } + let valptr = ptr::to_mut_unsafe_ptr(&mut self[ln - 1u]); + unsafe { + let val = ptr::replace_ptr(valptr, intrinsics::init()); + raw::set_len(self, ln - 1u); + val + } } - #[inline] + /// Removes the first element from a vector and return it fn shift(&mut self) -> T { - shift(self) + unsafe { + assert!(!self.is_empty()); + + if self.len() == 1 { return self.pop() } + + if self.len() == 2 { + let last = self.pop(); + let first = self.pop(); + self.push(last); + return first; + } + + let ln = self.len(); + let next_ln = self.len() - 1; + + // Save the last element. We're going to overwrite its position + let work_elt = self.pop(); + // We still should have room to work where what last element was + assert!(capacity(self) >= ln); + // Pretend like we have the original length so we can use + // the vector copy_memory to overwrite the hole we just made + raw::set_len(self, ln); + + // Memcopy the head element (the one we want) to the location we just + // popped. For the moment it unsafely exists at both the head and last + // positions + { + let first_slice = self.slice(0, 1); + let last_slice = self.slice(next_ln, ln); + raw::copy_memory(transmute(last_slice), first_slice, 1); + } + + // Memcopy everything to the left one element + { + let init_slice = self.slice(0, next_ln); + let tail_slice = self.slice(1, ln); + raw::copy_memory(transmute(init_slice), + tail_slice, + next_ln); + } + + // Set the new length. Now the vector is back to normal + raw::set_len(self, next_ln); + + // Swap out the element we want from the end + let vp = raw::to_mut_ptr(*self); + let vp = ptr::mut_offset(vp, next_ln - 1); + + ptr::replace_ptr(vp, work_elt) + } } - #[inline] + /// Prepend an element to the vector fn unshift(&mut self, x: T) { - unshift(self, x) + let v = util::replace(self, ~[x]); + self.push_all_move(v); } - #[inline] + /// Insert an element at position i within v, shifting all + /// elements after position i one position to the right. fn insert(&mut self, i: uint, x:T) { - insert(self, i, x) + let len = self.len(); + assert!(i <= len); + + self.push(x); + let mut j = len; + while j > i { + swap(*self, j, j - 1); + j -= 1; + } } - #[inline] + /// Remove and return the element at position i within v, shifting + /// all elements after position i one position to the left. fn remove(&mut self, i: uint) -> T { - remove(self, i) + let len = self.len(); + assert!(i < len); + + let mut j = i; + while j < len - 1 { + swap(*self, j, j + 1); + j += 1; + } + self.pop() } - #[inline] + /** + * Remove an element from anywhere in the vector and return it, replacing it + * with the last element. This does not preserve ordering, but is O(1). + * + * Fails if index >= length. + */ fn swap_remove(&mut self, index: uint) -> T { - swap_remove(self, index) + let ln = self.len(); + if index >= ln { + fail!("vec::swap_remove - index %u >= length %u", index, ln); + } + if index < ln - 1 { + swap(*self, index, ln - 1); + } + self.pop() } #[inline] -- cgit 1.4.1-3-g733a5 From 29b0649a6af8c4821f0d69c544569a9529a68431 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 23:53:37 +1000 Subject: Convert vec::{push, push_all, push_all_move} to methods. --- doc/rust.md | 2 +- src/librustc/metadata/decoder.rs | 3 +- src/librustc/middle/resolve.rs | 4 +- src/librustc/middle/typeck/coherence.rs | 2 +- src/libstd/os.rs | 5 +- src/libstd/vec.rs | 160 +++++++++++++++----------------- 6 files changed, 82 insertions(+), 94 deletions(-) (limited to 'src/libstd') diff --git a/doc/rust.md b/doc/rust.md index 0939664fc79..8f742d0d210 100644 --- a/doc/rust.md +++ b/doc/rust.md @@ -2620,7 +2620,7 @@ assert!(b != "world"); The vector type constructor represents a homogeneous array of values of a given type. A vector has a fixed size. -(Operations like `vec::push` operate solely on owned vectors.) +(Operations like `vec.push` operate solely on owned vectors.) A vector type can be annotated with a _definite_ size, written with a trailing asterisk and integer literal, such as `[int * 10]`. Such a definite-sized vector type is a first-class type, since its size is known statically. diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 39cce41b386..d7c20ed2d50 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -763,8 +763,7 @@ pub fn get_provided_trait_methods(intr: @ident_interner, cdata: cmd, if item_method_sort(mth) != 'p' { loop; } - vec::push(&mut result, - @get_method(intr, cdata, did.node, tcx)); + result.push(@get_method(intr, cdata, did.node, tcx)); } return result; diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 7cd64e863d2..b839e22f906 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -4862,8 +4862,8 @@ impl Resolver { while j != 0 { j -= 1; for this.value_ribs[j].bindings.each_key |&k| { - vec::push(&mut maybes, this.session.str_of(k)); - vec::push(&mut values, uint::max_value); + maybes.push(this.session.str_of(k)); + values.push(uint::max_value); } } diff --git a/src/librustc/middle/typeck/coherence.rs b/src/librustc/middle/typeck/coherence.rs index bd99a8e150b..5d0fbfcb1ba 100644 --- a/src/librustc/middle/typeck/coherence.rs +++ b/src/librustc/middle/typeck/coherence.rs @@ -788,7 +788,7 @@ impl CoherenceChecker { `%s` to impl", provided_method.method_info .ident.repr(self.crate_context.tcx)); - vec::push(all_methods, provided_method.method_info); + all_methods.push(provided_method.method_info); } } } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 5534c5befc2..165996f935e 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -1140,7 +1140,7 @@ pub fn set_exit_status(code: int) { unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] { let mut args = ~[]; for uint::range(0, argc as uint) |i| { - vec::push(&mut args, str::raw::from_c_str(*argv.offset(i))); + args.push(str::raw::from_c_str(*argv.offset(i))); } args } @@ -1186,8 +1186,7 @@ pub fn real_args() -> ~[~str] { while *ptr.offset(len) != 0 { len += 1; } // Push it onto the list. - vec::push(&mut args, - vec::raw::buf_as_slice(ptr, len, + args.push(vec::raw::buf_as_slice(ptr, len, str::from_utf16)); } } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index bc933e70b37..f0c81f9c04b 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -438,86 +438,6 @@ pub fn consume_reverse(mut v: ~[T], f: &fn(uint, v: T)) { } } -/// Append an element to a vector -#[inline] -pub fn push(v: &mut ~[T], initval: T) { - unsafe { - let repr: **raw::VecRepr = transmute(&mut *v); - let fill = (**repr).unboxed.fill; - if (**repr).unboxed.alloc > fill { - push_fast(v, initval); - } - else { - push_slow(v, initval); - } - } -} - -// This doesn't bother to make sure we have space. -#[inline] // really pretty please -unsafe fn push_fast(v: &mut ~[T], initval: T) { - let repr: **mut raw::VecRepr = transmute(v); - let fill = (**repr).unboxed.fill; - (**repr).unboxed.fill += sys::nonzero_size_of::(); - let p = to_unsafe_ptr(&((**repr).unboxed.data)); - let p = ptr::offset(p, fill) as *mut T; - intrinsics::move_val_init(&mut(*p), initval); -} - -#[inline(never)] -fn push_slow(v: &mut ~[T], initval: T) { - let new_len = v.len() + 1; - reserve_at_least(&mut *v, new_len); - unsafe { push_fast(v, initval) } -} - -/// Iterates over the slice `rhs`, copies each element, and then appends it to -/// the vector provided `v`. The `rhs` vector is traversed in-order. -/// -/// # Example -/// -/// ~~~ {.rust} -/// let mut a = ~[1]; -/// vec::push_all(&mut a, [2, 3, 4]); -/// assert!(a == ~[1, 2, 3, 4]); -/// ~~~ -#[inline] -pub fn push_all(v: &mut ~[T], rhs: &const [T]) { - let new_len = v.len() + rhs.len(); - reserve(&mut *v, new_len); - - for uint::range(0u, rhs.len()) |i| { - push(&mut *v, unsafe { raw::get(rhs, i) }) - } -} - -/// Takes ownership of the vector `rhs`, moving all elements into the specified -/// vector `v`. This does not copy any elements, and it is illegal to use the -/// `rhs` vector after calling this method (because it is moved here). -/// -/// # Example -/// -/// ~~~ {.rust} -/// let mut a = ~[~1]; -/// vec::push_all_move(&mut a, ~[~2, ~3, ~4]); -/// assert!(a == ~[~1, ~2, ~3, ~4]); -/// ~~~ -#[inline] -pub fn push_all_move(v: &mut ~[T], mut rhs: ~[T]) { - let new_len = v.len() + rhs.len(); - reserve(&mut *v, new_len); - unsafe { - do as_mut_buf(rhs) |p, len| { - for uint::range(0, len) |i| { - let x = ptr::replace_ptr(ptr::mut_offset(p, i), - intrinsics::uninit()); - push(&mut *v, x); - } - } - raw::set_len(&mut rhs, 0); - } -} - /// Shorten a vector, dropping excess elements. pub fn truncate(v: &mut ~[T], newlen: uint) { do as_mut_buf(*v) |p, oldlen| { @@ -1699,6 +1619,8 @@ impl<'self,T:Copy> ImmutableCopyableVector for &'self [T] { #[allow(missing_doc)] pub trait OwnedVector { fn push(&mut self, t: T); + unsafe fn push_fast(&mut self, t: T); + fn push_all_move(&mut self, rhs: ~[T]); fn pop(&mut self) -> T; fn shift(&mut self) -> T; @@ -1716,14 +1638,67 @@ pub trait OwnedVector { } impl OwnedVector for ~[T] { + /// Append an element to a vector #[inline] fn push(&mut self, t: T) { - push(self, t); + unsafe { + let repr: **raw::VecRepr = transmute(&mut *self); + let fill = (**repr).unboxed.fill; + if (**repr).unboxed.alloc <= fill { + // need more space + reserve_no_inline(self); + } + + self.push_fast(t); + } + + // this peculiar function is because reserve_at_least is very + // large (because of reserve), and will be inlined, which + // makes push too large. + #[inline(never)] + fn reserve_no_inline(v: &mut ~[T]) { + let new_len = v.len() + 1; + reserve_at_least(v, new_len); + } } - #[inline] - fn push_all_move(&mut self, rhs: ~[T]) { - push_all_move(self, rhs); + // This doesn't bother to make sure we have space. + #[inline] // really pretty please + unsafe fn push_fast(&mut self, t: T) { + let repr: **mut raw::VecRepr = transmute(self); + let fill = (**repr).unboxed.fill; + (**repr).unboxed.fill += sys::nonzero_size_of::(); + let p = to_unsafe_ptr(&((**repr).unboxed.data)); + let p = ptr::offset(p, fill) as *mut T; + intrinsics::move_val_init(&mut(*p), t); + } + + /// Takes ownership of the vector `rhs`, moving all elements into + /// the current vector. This does not copy any elements, and it is + /// illegal to use the `rhs` vector after calling this method + /// (because it is moved here). + /// + /// # Example + /// + /// ~~~ {.rust} + /// let mut a = ~[~1]; + /// a.push_all_move(~[~2, ~3, ~4]); + /// assert!(a == ~[~1, ~2, ~3, ~4]); + /// ~~~ + #[inline] + fn push_all_move(&mut self, mut rhs: ~[T]) { + let new_len = self.len() + rhs.len(); + reserve(self, new_len); + unsafe { + do as_mut_buf(rhs) |p, len| { + for uint::range(0, len) |i| { + let x = ptr::replace_ptr(ptr::mut_offset(p, i), + intrinsics::uninit()); + self.push(x); + } + } + raw::set_len(&mut rhs, 0); + } } /// Remove the last element from a vector and return it @@ -1898,9 +1873,24 @@ pub trait OwnedCopyableVector { } impl OwnedCopyableVector for ~[T] { + /// Iterates over the slice `rhs`, copies each element, and then appends it to + /// the vector provided `v`. The `rhs` vector is traversed in-order. + /// + /// # Example + /// + /// ~~~ {.rust} + /// let mut a = ~[1]; + /// a.push_all([2, 3, 4]); + /// assert!(a == ~[1, 2, 3, 4]); + /// ~~~ #[inline] fn push_all(&mut self, rhs: &const [T]) { - push_all(self, rhs); + let new_len = self.len() + rhs.len(); + reserve(self, new_len); + + for uint::range(0u, rhs.len()) |i| { + self.push(unsafe { raw::get(rhs, i) }) + } } #[inline] -- cgit 1.4.1-3-g733a5 From 4470d14388b1637a1e4862c0650baddf6ed7c430 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 27 Jun 2013 23:58:07 +1000 Subject: Convert vec::truncate to a method. --- src/libstd/vec.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index f0c81f9c04b..2cc27861207 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -438,20 +438,6 @@ pub fn consume_reverse(mut v: ~[T], f: &fn(uint, v: T)) { } } -/// Shorten a vector, dropping excess elements. -pub fn truncate(v: &mut ~[T], newlen: uint) { - do as_mut_buf(*v) |p, oldlen| { - assert!(newlen <= oldlen); - unsafe { - // This loop is optimized out for non-drop types. - for uint::range(newlen, oldlen) |i| { - ptr::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit()); - } - } - } - unsafe { raw::set_len(&mut *v, newlen); } -} - /** * Remove consecutive repeated elements from a vector; if the vector is * sorted, this removes all duplicates. @@ -1820,9 +1806,18 @@ impl OwnedVector for ~[T] { self.pop() } - #[inline] + /// Shorten a vector, dropping excess elements. fn truncate(&mut self, newlen: uint) { - truncate(self, newlen); + do as_mut_buf(*self) |p, oldlen| { + assert!(newlen <= oldlen); + unsafe { + // This loop is optimized out for non-drop types. + for uint::range(newlen, oldlen) |i| { + ptr::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit()); + } + } + } + unsafe { raw::set_len(self, newlen); } } #[inline] -- cgit 1.4.1-3-g733a5 From 206d4f00dc628cf0c7680ae2dc5b3ab6771c32e4 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 00:01:21 +1000 Subject: Convert vec::retain to a method. --- src/libstd/vec.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 2cc27861207..65c394f032c 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -687,26 +687,6 @@ pub fn filtered(v: &[T], f: &fn(t: &T) -> bool) -> ~[T] { result } -/** - * Like `filter()`, but in place. Preserves order of `v`. Linear time. - */ -pub fn retain(v: &mut ~[T], f: &fn(t: &T) -> bool) { - let len = v.len(); - let mut deleted: uint = 0; - - for uint::range(0, len) |i| { - if !f(&v[i]) { - deleted += 1; - } else if deleted > 0 { - swap(*v, i - deleted, i); - } - } - - if deleted > 0 { - v.truncate(len - deleted); - } -} - /// Flattens a vector of vectors of T into a single vector of T. pub fn concat(v: &[~[T]]) -> ~[T] { v.concat_vec() } @@ -1820,9 +1800,25 @@ impl OwnedVector for ~[T] { unsafe { raw::set_len(self, newlen); } } - #[inline] + + /** + * Like `filter()`, but in place. Preserves order of `v`. Linear time. + */ fn retain(&mut self, f: &fn(t: &T) -> bool) { - retain(self, f); + let len = self.len(); + let mut deleted: uint = 0; + + for uint::range(0, len) |i| { + if !f(&self[i]) { + deleted += 1; + } else if deleted > 0 { + swap(*self, i - deleted, i); + } + } + + if deleted > 0 { + self.truncate(len - deleted); + } } #[inline] -- cgit 1.4.1-3-g733a5 From ae2f1853491540b9e70be2209b235f6c920706a8 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 00:10:18 +1000 Subject: Convert vec::{partition, partitioned} to methods. --- src/libextra/test.rs | 2 +- src/libstd/vec.rs | 75 ++++++++++++++++++++-------------------------------- 2 files changed, 29 insertions(+), 48 deletions(-) (limited to 'src/libstd') diff --git a/src/libextra/test.rs b/src/libextra/test.rs index bb03e3ab9bb..7b68298a8dd 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -431,7 +431,7 @@ fn run_tests(opts: &TestOpts, callback(TeFiltered(filtered_descs)); let (filtered_tests, filtered_benchs) = - do vec::partition(filtered_tests) |e| { + do filtered_tests.partition |e| { match e.testfn { StaticTestFn(_) | DynTestFn(_) => true, StaticBenchFn(_) | DynBenchFn(_) => false diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 65c394f032c..4dbc0c4e3e0 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -337,46 +337,6 @@ pub fn rsplitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { result } -/** - * Partitions a vector into two new vectors: those that satisfies the - * predicate, and those that do not. - */ -pub fn partition(v: ~[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) { - let mut lefts = ~[]; - let mut rights = ~[]; - - // FIXME (#4355 maybe): using v.consume here crashes - // do v.consume |_, elt| { - do consume(v) |_, elt| { - if f(&elt) { - lefts.push(elt); - } else { - rights.push(elt); - } - } - - (lefts, rights) -} - -/** - * Partitions a vector into two new vectors: those that satisfies the - * predicate, and those that do not. - */ -pub fn partitioned(v: &[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) { - let mut lefts = ~[]; - let mut rights = ~[]; - - for v.iter().advance |elt| { - if f(elt) { - lefts.push(copy *elt); - } else { - rights.push(copy *elt); - } - } - - (lefts, rights) -} - /// Consumes all elements, in a vector, moving them out into the / closure /// provided. The vector is traversed from the start to the end. /// @@ -1572,7 +1532,18 @@ impl<'self,T:Copy> ImmutableCopyableVector for &'self [T] { */ #[inline] fn partitioned(&self, f: &fn(&T) -> bool) -> (~[T], ~[T]) { - partitioned(*self, f) + let mut lefts = ~[]; + let mut rights = ~[]; + + for self.iter().advance |elt| { + if f(elt) { + lefts.push(copy *elt); + } else { + rights.push(copy *elt); + } + } + + (lefts, rights) } /// Returns the element at the given index, without doing bounds checking. @@ -1842,7 +1813,18 @@ impl OwnedVector for ~[T] { */ #[inline] fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T]) { - partition(self, f) + let mut lefts = ~[]; + let mut rights = ~[]; + + do self.consume |_, elt| { + if f(&elt) { + lefts.push(elt); + } else { + rights.push(elt); + } + } + + (lefts, rights) } #[inline] @@ -3228,11 +3210,10 @@ mod tests { #[test] fn test_partition() { - // FIXME (#4355 maybe): using v.partition here crashes - assert_eq!(partition(~[], |x: &int| *x < 3), (~[], ~[])); - assert_eq!(partition(~[1, 2, 3], |x: &int| *x < 4), (~[1, 2, 3], ~[])); - assert_eq!(partition(~[1, 2, 3], |x: &int| *x < 2), (~[1], ~[2, 3])); - assert_eq!(partition(~[1, 2, 3], |x: &int| *x < 0), (~[], ~[1, 2, 3])); + assert_eq!((~[]).partition(|x: &int| *x < 3), (~[], ~[])); + assert_eq!((~[1, 2, 3]).partition(|x: &int| *x < 4), (~[1, 2, 3], ~[])); + assert_eq!((~[1, 2, 3]).partition(|x: &int| *x < 2), (~[1], ~[2, 3])); + assert_eq!((~[1, 2, 3]).partition(|x: &int| *x < 0), (~[], ~[1, 2, 3])); } #[test] -- cgit 1.4.1-3-g733a5 From 32d655916f1c3365a521616b57d9d0efc2bae643 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 00:40:47 +1000 Subject: Convert vec::{reserve, reserve_at_least, capacity} to methods. --- src/libextra/deque.rs | 13 ++- src/libextra/priority_queue.rs | 6 +- src/libstd/io.rs | 2 +- src/libstd/rt/io/extensions.rs | 2 +- src/libstd/str.rs | 6 +- src/libstd/vec.rs | 142 +++++++++++++------------- src/test/bench/shootout-reverse-complement.rs | 7 +- 7 files changed, 88 insertions(+), 90 deletions(-) (limited to 'src/libstd') diff --git a/src/libextra/deque.rs b/src/libextra/deque.rs index e6a7dd64837..c70c87b6ea1 100644 --- a/src/libextra/deque.rs +++ b/src/libextra/deque.rs @@ -137,7 +137,7 @@ impl Deque { /// /// * n - The number of elements to reserve space for pub fn reserve(&mut self, n: uint) { - vec::reserve(&mut self.elts, n); + self.elts.reserve(n); } /// Reserve capacity for at least `n` elements in the given deque, @@ -151,7 +151,7 @@ impl Deque { /// /// * n - The number of elements to reserve space for pub fn reserve_at_least(&mut self, n: uint) { - vec::reserve_at_least(&mut self.elts, n); + self.elts.reserve_at_least(n); } /// Front-to-back iterator. @@ -256,7 +256,6 @@ mod tests { use super::*; use core::cmp::Eq; use core::kinds::Copy; - use core::vec::capacity; use core; #[test] @@ -442,11 +441,11 @@ mod tests { let mut d = Deque::new(); d.add_back(0u64); d.reserve(50); - assert_eq!(capacity(&mut d.elts), 50); + assert_eq!(d.elts.capacity(), 50); let mut d = Deque::new(); d.add_back(0u32); d.reserve(50); - assert_eq!(capacity(&mut d.elts), 50); + assert_eq!(d.elts.capacity(), 50); } #[test] @@ -454,11 +453,11 @@ mod tests { let mut d = Deque::new(); d.add_back(0u64); d.reserve_at_least(50); - assert_eq!(capacity(&mut d.elts), 64); + assert_eq!(d.elts.capacity(), 64); let mut d = Deque::new(); d.add_back(0u32); d.reserve_at_least(50); - assert_eq!(capacity(&mut d.elts), 64); + assert_eq!(d.elts.capacity(), 64); } #[test] diff --git a/src/libextra/priority_queue.rs b/src/libextra/priority_queue.rs index af891edf9e5..fbb4be0febb 100644 --- a/src/libextra/priority_queue.rs +++ b/src/libextra/priority_queue.rs @@ -52,12 +52,12 @@ impl PriorityQueue { } /// Returns the number of elements the queue can hold without reallocating - pub fn capacity(&self) -> uint { vec::capacity(&self.data) } + pub fn capacity(&self) -> uint { self.data.capacity() } - pub fn reserve(&mut self, n: uint) { vec::reserve(&mut self.data, n) } + pub fn reserve(&mut self, n: uint) { self.data.reserve(n) } pub fn reserve_at_least(&mut self, n: uint) { - vec::reserve_at_least(&mut self.data, n) + self.data.reserve_at_least(n) } /// Pop the greatest item from the queue - fails if empty diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 4d9c08f25da..36920bd2488 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1658,7 +1658,7 @@ impl Writer for BytesWriter { let bytes = &mut *self.bytes; let count = uint::max(bytes.len(), *self.pos + v_len); - vec::reserve(bytes, count); + bytes.reserve(count); unsafe { vec::raw::set_len(bytes, count); diff --git a/src/libstd/rt/io/extensions.rs b/src/libstd/rt/io/extensions.rs index 5320bd0f42e..1f82a9cd963 100644 --- a/src/libstd/rt/io/extensions.rs +++ b/src/libstd/rt/io/extensions.rs @@ -292,7 +292,7 @@ impl ReaderUtil for T { let start_len = buf.len(); let mut total_read = 0; - vec::reserve_at_least(buf, start_len + len); + buf.reserve_at_least(start_len + len); vec::raw::set_len(buf, start_len + len); do (|| { diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 16c287c1da8..58cdc6631f0 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -2081,7 +2081,7 @@ impl OwnedStr for ~str { pub fn reserve(&mut self, n: uint) { unsafe { let v: *mut ~[u8] = cast::transmute(self); - vec::reserve(&mut *v, n + 1); + (*v).reserve(n + 1); } } @@ -2115,8 +2115,8 @@ impl OwnedStr for ~str { * reallocating */ fn capacity(&self) -> uint { - let buf: &const ~[u8] = unsafe { cast::transmute(self) }; - let vcap = vec::capacity(buf); + let buf: &~[u8] = unsafe { cast::transmute(self) }; + let vcap = buf.capacity(); assert!(vcap > 0u); vcap - 1u } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 4dbc0c4e3e0..5dfea811c23 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -68,63 +68,6 @@ pub fn same_length(xs: &const [T], ys: &const [U]) -> bool { xs.len() == ys.len() } -/** - * Reserves capacity for exactly `n` elements in the given vector. - * - * If the capacity for `v` is already equal to or greater than the requested - * capacity, then no action is taken. - * - * # Arguments - * - * * v - A vector - * * n - The number of elements to reserve space for - */ -#[inline] -pub fn reserve(v: &mut ~[T], n: uint) { - // Only make the (slow) call into the runtime if we have to - use managed; - if capacity(v) < n { - unsafe { - let ptr: **raw::VecRepr = cast::transmute(v); - let td = get_tydesc::(); - if ((**ptr).box_header.ref_count == - managed::raw::RC_MANAGED_UNIQUE) { - rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t); - } else { - rustrt::vec_reserve_shared(td, ptr, n as libc::size_t); - } - } - } -} - -/** - * Reserves capacity for at least `n` elements in the given vector. - * - * This function will over-allocate in order to amortize the allocation costs - * in scenarios where the caller may need to repeatedly reserve additional - * space. - * - * If the capacity for `v` is already equal to or greater than the requested - * capacity, then no action is taken. - * - * # Arguments - * - * * v - A vector - * * n - The number of elements to reserve space for - */ -pub fn reserve_at_least(v: &mut ~[T], n: uint) { - reserve(v, uint::next_power_of_two(n)); -} - -/// Returns the number of elements the vector can hold without reallocating -#[inline] -pub fn capacity(v: &const ~[T]) -> uint { - unsafe { - let repr: **raw::VecRepr = transmute(v); - (**repr).unboxed.alloc / sys::nonzero_size_of::() - } -} - /** * Creates and initializes an owned vector. * @@ -179,7 +122,7 @@ pub fn to_owned(t: &[T]) -> ~[T] { /// Creates a new vector with a capacity of `capacity` pub fn with_capacity(capacity: uint) -> ~[T] { let mut vec = ~[]; - reserve(&mut vec, capacity); + vec.reserve(capacity); vec } @@ -466,7 +409,7 @@ pub fn append_one(lhs: ~[T], x: T) -> ~[T] { */ pub fn grow(v: &mut ~[T], n: uint, initval: &T) { let new_len = v.len() + n; - reserve_at_least(&mut *v, new_len); + v.reserve_at_least(new_len); let mut i: uint = 0u; while i < n { @@ -490,7 +433,7 @@ pub fn grow(v: &mut ~[T], n: uint, initval: &T) { */ pub fn grow_fn(v: &mut ~[T], n: uint, op: &fn(uint) -> T) { let new_len = v.len() + n; - reserve_at_least(&mut *v, new_len); + v.reserve_at_least(new_len); let mut i: uint = 0u; while i < n { v.push(op(i)); @@ -1298,13 +1241,11 @@ impl<'self,T:Copy> CopyableVector for &'self [T] { /// Returns a copy of `v`. #[inline] fn to_owned(&self) -> ~[T] { - let mut result = ~[]; - reserve(&mut result, self.len()); + let mut result = with_capacity(self.len()); for self.iter().advance |e| { result.push(copy *e); } result - } } @@ -1555,6 +1496,10 @@ impl<'self,T:Copy> ImmutableCopyableVector for &'self [T] { #[allow(missing_doc)] pub trait OwnedVector { + fn reserve(&mut self, n: uint); + fn reserve_at_least(&mut self, n: uint); + fn capacity(&self) -> uint; + fn push(&mut self, t: T); unsafe fn push_fast(&mut self, t: T); @@ -1575,6 +1520,61 @@ pub trait OwnedVector { } impl OwnedVector for ~[T] { + /** + * Reserves capacity for exactly `n` elements in the given vector. + * + * If the capacity for `self` is already equal to or greater than the requested + * capacity, then no action is taken. + * + * # Arguments + * + * * n - The number of elements to reserve space for + */ + #[inline] + fn reserve(&mut self, n: uint) { + // Only make the (slow) call into the runtime if we have to + use managed; + if self.capacity() < n { + unsafe { + let ptr: **raw::VecRepr = cast::transmute(self); + let td = get_tydesc::(); + if ((**ptr).box_header.ref_count == + managed::raw::RC_MANAGED_UNIQUE) { + rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t); + } else { + rustrt::vec_reserve_shared(td, ptr, n as libc::size_t); + } + } + } + } + + /** + * Reserves capacity for at least `n` elements in the given vector. + * + * This function will over-allocate in order to amortize the allocation costs + * in scenarios where the caller may need to repeatedly reserve additional + * space. + * + * If the capacity for `self` is already equal to or greater than the requested + * capacity, then no action is taken. + * + * # Arguments + * + * * n - The number of elements to reserve space for + */ + fn reserve_at_least(&mut self, n: uint) { + self.reserve(uint::next_power_of_two(n)); + } + + /// Returns the number of elements the vector can hold without reallocating. + #[inline] + fn capacity(&self) -> uint { + unsafe { + let repr: **raw::VecRepr = transmute(self); + (**repr).unboxed.alloc / sys::nonzero_size_of::() + } + } + /// Append an element to a vector #[inline] fn push(&mut self, t: T) { @@ -1595,7 +1595,7 @@ impl OwnedVector for ~[T] { #[inline(never)] fn reserve_no_inline(v: &mut ~[T]) { let new_len = v.len() + 1; - reserve_at_least(v, new_len); + v.reserve_at_least(new_len); } } @@ -1625,7 +1625,7 @@ impl OwnedVector for ~[T] { #[inline] fn push_all_move(&mut self, mut rhs: ~[T]) { let new_len = self.len() + rhs.len(); - reserve(self, new_len); + self.reserve(new_len); unsafe { do as_mut_buf(rhs) |p, len| { for uint::range(0, len) |i| { @@ -1672,7 +1672,7 @@ impl OwnedVector for ~[T] { // Save the last element. We're going to overwrite its position let work_elt = self.pop(); // We still should have room to work where what last element was - assert!(capacity(self) >= ln); + assert!(self.capacity() >= ln); // Pretend like we have the original length so we can use // the vector copy_memory to overwrite the hole we just made raw::set_len(self, ln); @@ -1859,7 +1859,7 @@ impl OwnedCopyableVector for ~[T] { #[inline] fn push_all(&mut self, rhs: &const [T]) { let new_len = self.len() + rhs.len(); - reserve(self, new_len); + self.reserve(new_len); for uint::range(0u, rhs.len()) |i| { self.push(unsafe { raw::get(rhs, i) }) @@ -3333,11 +3333,11 @@ mod tests { #[test] fn test_capacity() { let mut v = ~[0u64]; - reserve(&mut v, 10u); - assert_eq!(capacity(&v), 10u); + v.reserve(10u); + assert_eq!(v.capacity(), 10u); let mut v = ~[0u32]; - reserve(&mut v, 10u); - assert_eq!(capacity(&v), 10u); + v.reserve(10u); + assert_eq!(v.capacity(), 10u); } #[test] diff --git a/src/test/bench/shootout-reverse-complement.rs b/src/test/bench/shootout-reverse-complement.rs index 9893785ecfa..e57dee06c75 100644 --- a/src/test/bench/shootout-reverse-complement.rs +++ b/src/test/bench/shootout-reverse-complement.rs @@ -5,7 +5,6 @@ use std::cast::transmute; use std::libc::{STDOUT_FILENO, c_int, fdopen, fgets, fopen, fputc, fwrite}; use std::libc::{size_t}; use std::ptr::null; -use std::vec::{capacity, reserve, reserve_at_least}; use std::vec::raw::set_len; static LINE_LEN: u32 = 80; @@ -103,13 +102,13 @@ fn main() { let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0])); let mut out: ~[u8] = ~[]; - reserve(&mut out, 12777888); + out.reserve(12777888); let mut pos = 0; loop { let needed = pos + (LINE_LEN as uint) + 1; - if capacity(&out) < needed { - reserve_at_least(&mut out, needed); + if out.capacity() < needed { + out.reserve_at_least(needed); } let mut ptr = out.unsafe_mut_ref(pos); -- cgit 1.4.1-3-g733a5 From 366ca44cc8f79704f8781adb15e74d3c2a0e5572 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 28 Jun 2013 01:45:24 +1000 Subject: std: silence some test warnings. --- src/libstd/iterator.rs | 1 - src/libstd/local_data.rs | 23 ++++++++++------------- src/libstd/rt/uv/timer.rs | 4 ++-- src/libstd/str.rs | 3 ++- src/libstd/task/mod.rs | 14 ++++++-------- src/libstd/vec.rs | 4 ++-- 6 files changed, 22 insertions(+), 27 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index ab433a9a79d..7de02a9f815 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -980,7 +980,6 @@ mod tests { use super::*; use prelude::*; - use iter; use uint; #[test] diff --git a/src/libstd/local_data.rs b/src/libstd/local_data.rs index 82c01c998cf..33b4e3f1963 100644 --- a/src/libstd/local_data.rs +++ b/src/libstd/local_data.rs @@ -92,14 +92,12 @@ fn test_tls_multitask() { fn my_key(_x: @~str) { } local_data_set(my_key, @~"parent data"); do task::spawn { - unsafe { - // TLS shouldn't carry over. - assert!(local_data_get(my_key).is_none()); - local_data_set(my_key, @~"child data"); - assert!(*(local_data_get(my_key).get()) == + // TLS shouldn't carry over. + assert!(local_data_get(my_key).is_none()); + local_data_set(my_key, @~"child data"); + assert!(*(local_data_get(my_key).get()) == ~"child data"); - // should be cleaned up for us - } + // should be cleaned up for us } // Must work multiple times assert!(*(local_data_get(my_key).get()) == ~"parent data"); @@ -206,12 +204,11 @@ fn test_tls_cleanup_on_failure() { local_data_set(str_key, @~"parent data"); local_data_set(box_key, @@()); do task::spawn { - unsafe { // spawn_linked - local_data_set(str_key, @~"string data"); - local_data_set(box_key, @@()); - local_data_set(int_key, @42); - fail!(); - } + // spawn_linked + local_data_set(str_key, @~"string data"); + local_data_set(box_key, @@()); + local_data_set(int_key, @42); + fail!(); } // Not quite nondeterministic. local_data_set(int_key, @31337); diff --git a/src/libstd/rt/uv/timer.rs b/src/libstd/rt/uv/timer.rs index cd6fc5c0a25..14465eb7dfd 100644 --- a/src/libstd/rt/uv/timer.rs +++ b/src/libstd/rt/uv/timer.rs @@ -160,14 +160,14 @@ mod test { let mut timer2 = TimerWatcher::new(&mut loop_); do timer2.start(10, 0) |timer2, _| { - unsafe { *count_ptr += 1; } + *count_ptr += 1; timer2.close(||()); // Restart the original timer let mut timer = timer; do timer.start(1, 0) |timer, _| { - unsafe { *count_ptr += 1; } + *count_ptr += 1; timer.close(||()); } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 58cdc6631f0..b4292a30541 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -2249,7 +2249,7 @@ mod tests { assert!("" <= ""); assert!("" <= "foo"); assert!("foo" <= "foo"); - assert!("foo" != ~"bar"); + assert!("foo" != "bar"); } #[test] @@ -3156,6 +3156,7 @@ mod tests { #[test] fn test_add() { + #[allow(unnecessary_allocation)]; macro_rules! t ( ($s1:expr, $s2:expr, $e:expr) => { assert_eq!($s1 + $s2, $e); diff --git a/src/libstd/task/mod.rs b/src/libstd/task/mod.rs index 223afbce091..b558b9d53a3 100644 --- a/src/libstd/task/mod.rs +++ b/src/libstd/task/mod.rs @@ -934,17 +934,15 @@ fn test_spawn_sched_blocking() { let lock = testrt::rust_dbg_lock_create(); do spawn_sched(SingleThreaded) { - unsafe { - testrt::rust_dbg_lock_lock(lock); + testrt::rust_dbg_lock_lock(lock); - start_ch.send(()); + start_ch.send(()); - // Block the scheduler thread - testrt::rust_dbg_lock_wait(lock); - testrt::rust_dbg_lock_unlock(lock); + // Block the scheduler thread + testrt::rust_dbg_lock_wait(lock); + testrt::rust_dbg_lock_unlock(lock); - fin_ch.send(()); - } + fin_ch.send(()); }; // Wait until the other task has its lock diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 5dfea811c23..8cbd9309cc6 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -3861,11 +3861,11 @@ mod tests { fn test_vec_zero() { use num::Zero; macro_rules! t ( - ($ty:ty) => { + ($ty:ty) => {{ let v: $ty = Zero::zero(); assert!(v.is_empty()); assert!(v.is_zero()); - } + }} ); t!(&[int]); -- cgit 1.4.1-3-g733a5