Skip to content

Commit 32d6559

Browse files
committed
Convert vec::{reserve, reserve_at_least, capacity} to methods.
1 parent ae2f185 commit 32d6559

File tree

7 files changed

+88
-90
lines changed

7 files changed

+88
-90
lines changed

src/libextra/deque.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<T> Deque<T> {
137137
///
138138
/// * n - The number of elements to reserve space for
139139
pub fn reserve(&mut self, n: uint) {
140-
vec::reserve(&mut self.elts, n);
140+
self.elts.reserve(n);
141141
}
142142

143143
/// Reserve capacity for at least `n` elements in the given deque,
@@ -151,7 +151,7 @@ impl<T> Deque<T> {
151151
///
152152
/// * n - The number of elements to reserve space for
153153
pub fn reserve_at_least(&mut self, n: uint) {
154-
vec::reserve_at_least(&mut self.elts, n);
154+
self.elts.reserve_at_least(n);
155155
}
156156

157157
/// Front-to-back iterator.
@@ -256,7 +256,6 @@ mod tests {
256256
use super::*;
257257
use core::cmp::Eq;
258258
use core::kinds::Copy;
259-
use core::vec::capacity;
260259
use core;
261260

262261
#[test]
@@ -442,23 +441,23 @@ mod tests {
442441
let mut d = Deque::new();
443442
d.add_back(0u64);
444443
d.reserve(50);
445-
assert_eq!(capacity(&mut d.elts), 50);
444+
assert_eq!(d.elts.capacity(), 50);
446445
let mut d = Deque::new();
447446
d.add_back(0u32);
448447
d.reserve(50);
449-
assert_eq!(capacity(&mut d.elts), 50);
448+
assert_eq!(d.elts.capacity(), 50);
450449
}
451450

452451
#[test]
453452
fn test_reserve_at_least() {
454453
let mut d = Deque::new();
455454
d.add_back(0u64);
456455
d.reserve_at_least(50);
457-
assert_eq!(capacity(&mut d.elts), 64);
456+
assert_eq!(d.elts.capacity(), 64);
458457
let mut d = Deque::new();
459458
d.add_back(0u32);
460459
d.reserve_at_least(50);
461-
assert_eq!(capacity(&mut d.elts), 64);
460+
assert_eq!(d.elts.capacity(), 64);
462461
}
463462

464463
#[test]

src/libextra/priority_queue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ impl<T:Ord> PriorityQueue<T> {
5252
}
5353

5454
/// Returns the number of elements the queue can hold without reallocating
55-
pub fn capacity(&self) -> uint { vec::capacity(&self.data) }
55+
pub fn capacity(&self) -> uint { self.data.capacity() }
5656

57-
pub fn reserve(&mut self, n: uint) { vec::reserve(&mut self.data, n) }
57+
pub fn reserve(&mut self, n: uint) { self.data.reserve(n) }
5858

5959
pub fn reserve_at_least(&mut self, n: uint) {
60-
vec::reserve_at_least(&mut self.data, n)
60+
self.data.reserve_at_least(n)
6161
}
6262

6363
/// Pop the greatest item from the queue - fails if empty

src/libstd/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ impl Writer for BytesWriter {
16581658

16591659
let bytes = &mut *self.bytes;
16601660
let count = uint::max(bytes.len(), *self.pos + v_len);
1661-
vec::reserve(bytes, count);
1661+
bytes.reserve(count);
16621662

16631663
unsafe {
16641664
vec::raw::set_len(bytes, count);

src/libstd/rt/io/extensions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<T: Reader> ReaderUtil for T {
292292
let start_len = buf.len();
293293
let mut total_read = 0;
294294

295-
vec::reserve_at_least(buf, start_len + len);
295+
buf.reserve_at_least(start_len + len);
296296
vec::raw::set_len(buf, start_len + len);
297297

298298
do (|| {

src/libstd/str.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,7 @@ impl OwnedStr for ~str {
20812081
pub fn reserve(&mut self, n: uint) {
20822082
unsafe {
20832083
let v: *mut ~[u8] = cast::transmute(self);
2084-
vec::reserve(&mut *v, n + 1);
2084+
(*v).reserve(n + 1);
20852085
}
20862086
}
20872087
@@ -2115,8 +2115,8 @@ impl OwnedStr for ~str {
21152115
* reallocating
21162116
*/
21172117
fn capacity(&self) -> uint {
2118-
let buf: &const ~[u8] = unsafe { cast::transmute(self) };
2119-
let vcap = vec::capacity(buf);
2118+
let buf: &~[u8] = unsafe { cast::transmute(self) };
2119+
let vcap = buf.capacity();
21202120
assert!(vcap > 0u);
21212121
vcap - 1u
21222122
}

src/libstd/vec.rs

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -68,63 +68,6 @@ pub fn same_length<T, U>(xs: &const [T], ys: &const [U]) -> bool {
6868
xs.len() == ys.len()
6969
}
7070

71-
/**
72-
* Reserves capacity for exactly `n` elements in the given vector.
73-
*
74-
* If the capacity for `v` is already equal to or greater than the requested
75-
* capacity, then no action is taken.
76-
*
77-
* # Arguments
78-
*
79-
* * v - A vector
80-
* * n - The number of elements to reserve space for
81-
*/
82-
#[inline]
83-
pub fn reserve<T>(v: &mut ~[T], n: uint) {
84-
// Only make the (slow) call into the runtime if we have to
85-
use managed;
86-
if capacity(v) < n {
87-
unsafe {
88-
let ptr: **raw::VecRepr = cast::transmute(v);
89-
let td = get_tydesc::<T>();
90-
if ((**ptr).box_header.ref_count ==
91-
managed::raw::RC_MANAGED_UNIQUE) {
92-
rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t);
93-
} else {
94-
rustrt::vec_reserve_shared(td, ptr, n as libc::size_t);
95-
}
96-
}
97-
}
98-
}
99-
100-
/**
101-
* Reserves capacity for at least `n` elements in the given vector.
102-
*
103-
* This function will over-allocate in order to amortize the allocation costs
104-
* in scenarios where the caller may need to repeatedly reserve additional
105-
* space.
106-
*
107-
* If the capacity for `v` is already equal to or greater than the requested
108-
* capacity, then no action is taken.
109-
*
110-
* # Arguments
111-
*
112-
* * v - A vector
113-
* * n - The number of elements to reserve space for
114-
*/
115-
pub fn reserve_at_least<T>(v: &mut ~[T], n: uint) {
116-
reserve(v, uint::next_power_of_two(n));
117-
}
118-
119-
/// Returns the number of elements the vector can hold without reallocating
120-
#[inline]
121-
pub fn capacity<T>(v: &const ~[T]) -> uint {
122-
unsafe {
123-
let repr: **raw::VecRepr = transmute(v);
124-
(**repr).unboxed.alloc / sys::nonzero_size_of::<T>()
125-
}
126-
}
127-
12871
/**
12972
* Creates and initializes an owned vector.
13073
*
@@ -179,7 +122,7 @@ pub fn to_owned<T:Copy>(t: &[T]) -> ~[T] {
179122
/// Creates a new vector with a capacity of `capacity`
180123
pub fn with_capacity<T>(capacity: uint) -> ~[T] {
181124
let mut vec = ~[];
182-
reserve(&mut vec, capacity);
125+
vec.reserve(capacity);
183126
vec
184127
}
185128

@@ -466,7 +409,7 @@ pub fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
466409
*/
467410
pub fn grow<T:Copy>(v: &mut ~[T], n: uint, initval: &T) {
468411
let new_len = v.len() + n;
469-
reserve_at_least(&mut *v, new_len);
412+
v.reserve_at_least(new_len);
470413
let mut i: uint = 0u;
471414

472415
while i < n {
@@ -490,7 +433,7 @@ pub fn grow<T:Copy>(v: &mut ~[T], n: uint, initval: &T) {
490433
*/
491434
pub fn grow_fn<T>(v: &mut ~[T], n: uint, op: &fn(uint) -> T) {
492435
let new_len = v.len() + n;
493-
reserve_at_least(&mut *v, new_len);
436+
v.reserve_at_least(new_len);
494437
let mut i: uint = 0u;
495438
while i < n {
496439
v.push(op(i));
@@ -1298,13 +1241,11 @@ impl<'self,T:Copy> CopyableVector<T> for &'self [T] {
12981241
/// Returns a copy of `v`.
12991242
#[inline]
13001243
fn to_owned(&self) -> ~[T] {
1301-
let mut result = ~[];
1302-
reserve(&mut result, self.len());
1244+
let mut result = with_capacity(self.len());
13031245
for self.iter().advance |e| {
13041246
result.push(copy *e);
13051247
}
13061248
result
1307-
13081249
}
13091250
}
13101251

@@ -1555,6 +1496,10 @@ impl<'self,T:Copy> ImmutableCopyableVector<T> for &'self [T] {
15551496

15561497
#[allow(missing_doc)]
15571498
pub trait OwnedVector<T> {
1499+
fn reserve(&mut self, n: uint);
1500+
fn reserve_at_least(&mut self, n: uint);
1501+
fn capacity(&self) -> uint;
1502+
15581503
fn push(&mut self, t: T);
15591504
unsafe fn push_fast(&mut self, t: T);
15601505

@@ -1575,6 +1520,61 @@ pub trait OwnedVector<T> {
15751520
}
15761521

15771522
impl<T> OwnedVector<T> for ~[T] {
1523+
/**
1524+
* Reserves capacity for exactly `n` elements in the given vector.
1525+
*
1526+
* If the capacity for `self` is already equal to or greater than the requested
1527+
* capacity, then no action is taken.
1528+
*
1529+
* # Arguments
1530+
*
1531+
* * n - The number of elements to reserve space for
1532+
*/
1533+
#[inline]
1534+
fn reserve(&mut self, n: uint) {
1535+
// Only make the (slow) call into the runtime if we have to
1536+
use managed;
1537+
if self.capacity() < n {
1538+
unsafe {
1539+
let ptr: **raw::VecRepr = cast::transmute(self);
1540+
let td = get_tydesc::<T>();
1541+
if ((**ptr).box_header.ref_count ==
1542+
managed::raw::RC_MANAGED_UNIQUE) {
1543+
rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t);
1544+
} else {
1545+
rustrt::vec_reserve_shared(td, ptr, n as libc::size_t);
1546+
}
1547+
}
1548+
}
1549+
}
1550+
1551+
/**
1552+
* Reserves capacity for at least `n` elements in the given vector.
1553+
*
1554+
* This function will over-allocate in order to amortize the allocation costs
1555+
* in scenarios where the caller may need to repeatedly reserve additional
1556+
* space.
1557+
*
1558+
* If the capacity for `self` is already equal to or greater than the requested
1559+
* capacity, then no action is taken.
1560+
*
1561+
* # Arguments
1562+
*
1563+
* * n - The number of elements to reserve space for
1564+
*/
1565+
fn reserve_at_least(&mut self, n: uint) {
1566+
self.reserve(uint::next_power_of_two(n));
1567+
}
1568+
1569+
/// Returns the number of elements the vector can hold without reallocating.
1570+
#[inline]
1571+
fn capacity(&self) -> uint {
1572+
unsafe {
1573+
let repr: **raw::VecRepr = transmute(self);
1574+
(**repr).unboxed.alloc / sys::nonzero_size_of::<T>()
1575+
}
1576+
}
1577+
15781578
/// Append an element to a vector
15791579
#[inline]
15801580
fn push(&mut self, t: T) {
@@ -1595,7 +1595,7 @@ impl<T> OwnedVector<T> for ~[T] {
15951595
#[inline(never)]
15961596
fn reserve_no_inline<T>(v: &mut ~[T]) {
15971597
let new_len = v.len() + 1;
1598-
reserve_at_least(v, new_len);
1598+
v.reserve_at_least(new_len);
15991599
}
16001600
}
16011601

@@ -1625,7 +1625,7 @@ impl<T> OwnedVector<T> for ~[T] {
16251625
#[inline]
16261626
fn push_all_move(&mut self, mut rhs: ~[T]) {
16271627
let new_len = self.len() + rhs.len();
1628-
reserve(self, new_len);
1628+
self.reserve(new_len);
16291629
unsafe {
16301630
do as_mut_buf(rhs) |p, len| {
16311631
for uint::range(0, len) |i| {
@@ -1672,7 +1672,7 @@ impl<T> OwnedVector<T> for ~[T] {
16721672
// Save the last element. We're going to overwrite its position
16731673
let work_elt = self.pop();
16741674
// We still should have room to work where what last element was
1675-
assert!(capacity(self) >= ln);
1675+
assert!(self.capacity() >= ln);
16761676
// Pretend like we have the original length so we can use
16771677
// the vector copy_memory to overwrite the hole we just made
16781678
raw::set_len(self, ln);
@@ -1859,7 +1859,7 @@ impl<T:Copy> OwnedCopyableVector<T> for ~[T] {
18591859
#[inline]
18601860
fn push_all(&mut self, rhs: &const [T]) {
18611861
let new_len = self.len() + rhs.len();
1862-
reserve(self, new_len);
1862+
self.reserve(new_len);
18631863

18641864
for uint::range(0u, rhs.len()) |i| {
18651865
self.push(unsafe { raw::get(rhs, i) })
@@ -3333,11 +3333,11 @@ mod tests {
33333333
#[test]
33343334
fn test_capacity() {
33353335
let mut v = ~[0u64];
3336-
reserve(&mut v, 10u);
3337-
assert_eq!(capacity(&v), 10u);
3336+
v.reserve(10u);
3337+
assert_eq!(v.capacity(), 10u);
33383338
let mut v = ~[0u32];
3339-
reserve(&mut v, 10u);
3340-
assert_eq!(capacity(&v), 10u);
3339+
v.reserve(10u);
3340+
assert_eq!(v.capacity(), 10u);
33413341
}
33423342

33433343
#[test]

src/test/bench/shootout-reverse-complement.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::cast::transmute;
55
use std::libc::{STDOUT_FILENO, c_int, fdopen, fgets, fopen, fputc, fwrite};
66
use std::libc::{size_t};
77
use std::ptr::null;
8-
use std::vec::{capacity, reserve, reserve_at_least};
98
use std::vec::raw::set_len;
109

1110
static LINE_LEN: u32 = 80;
@@ -103,13 +102,13 @@ fn main() {
103102
let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0]));
104103

105104
let mut out: ~[u8] = ~[];
106-
reserve(&mut out, 12777888);
105+
out.reserve(12777888);
107106
let mut pos = 0;
108107

109108
loop {
110109
let needed = pos + (LINE_LEN as uint) + 1;
111-
if capacity(&out) < needed {
112-
reserve_at_least(&mut out, needed);
110+
if out.capacity() < needed {
111+
out.reserve_at_least(needed);
113112
}
114113

115114
let mut ptr = out.unsafe_mut_ref(pos);

0 commit comments

Comments
 (0)