Convert vec::{reserve, reserve_at_least, capacity} to methods.
This commit is contained in:
parent
ae2f185349
commit
32d655916f
7 changed files with 88 additions and 90 deletions
|
@ -137,7 +137,7 @@ impl<T> Deque<T> {
|
|||
///
|
||||
/// * 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<T> Deque<T> {
|
|||
///
|
||||
/// * 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]
|
||||
|
|
|
@ -52,12 +52,12 @@ impl<T:Ord> PriorityQueue<T> {
|
|||
}
|
||||
|
||||
/// 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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -292,7 +292,7 @@ impl<T: Reader> 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 (|| {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -68,63 +68,6 @@ pub fn same_length<T, U>(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<T>(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::<T>();
|
||||
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<T>(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<T>(v: &const ~[T]) -> uint {
|
||||
unsafe {
|
||||
let repr: **raw::VecRepr = transmute(v);
|
||||
(**repr).unboxed.alloc / sys::nonzero_size_of::<T>()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and initializes an owned vector.
|
||||
*
|
||||
|
@ -179,7 +122,7 @@ pub fn to_owned<T:Copy>(t: &[T]) -> ~[T] {
|
|||
/// Creates a new vector with a capacity of `capacity`
|
||||
pub fn with_capacity<T>(capacity: uint) -> ~[T] {
|
||||
let mut vec = ~[];
|
||||
reserve(&mut vec, capacity);
|
||||
vec.reserve(capacity);
|
||||
vec
|
||||
}
|
||||
|
||||
|
@ -466,7 +409,7 @@ pub fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
|
|||
*/
|
||||
pub fn grow<T:Copy>(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<T:Copy>(v: &mut ~[T], n: uint, initval: &T) {
|
|||
*/
|
||||
pub fn grow_fn<T>(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<T> 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<T> for &'self [T] {
|
|||
|
||||
#[allow(missing_doc)]
|
||||
pub trait OwnedVector<T> {
|
||||
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<T> {
|
|||
}
|
||||
|
||||
impl<T> OwnedVector<T> 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::<T>();
|
||||
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::<T>()
|
||||
}
|
||||
}
|
||||
|
||||
/// Append an element to a vector
|
||||
#[inline]
|
||||
fn push(&mut self, t: T) {
|
||||
|
@ -1595,7 +1595,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||
#[inline(never)]
|
||||
fn reserve_no_inline<T>(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<T> OwnedVector<T> 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<T> OwnedVector<T> 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<T:Copy> OwnedCopyableVector<T> 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]
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue