Skip to content

Commit

Permalink
Rename reserve to reserve_exact and reserve_at_least to reserve
Browse files Browse the repository at this point in the history
Changes in std::{str,vec,hashmap} and extra::{priority_queue,ringbuf}.
Fixes #11949
  • Loading branch information
dmanescu committed Feb 4, 2014
1 parent 1d49419 commit 65f3578
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 53 deletions.
14 changes: 9 additions & 5 deletions src/libextra/priority_queue.rs
Expand Up @@ -51,10 +51,14 @@ impl<T:Ord> PriorityQueue<T> {
/// Returns the number of elements the queue can hold without reallocating
pub fn capacity(&self) -> uint { self.data.capacity() }

pub fn reserve(&mut self, n: uint) { self.data.reserve(n) }

pub fn reserve_at_least(&mut self, n: uint) {
self.data.reserve_at_least(n)
/// Reserve capacity for exactly n elements in the PriorityQueue.
/// Do nothing if the capacity is already sufficient.
pub fn reserve_exact(&mut self, n: uint) { self.data.reserve_exact(n) }

/// Reserve capacity for at least n elements in the PriorityQueue.
/// Do nothing if the capacity is already sufficient.
pub fn reserve(&mut self, n: uint) {
self.data.reserve(n)
}

/// Pop the greatest item from the queue - fails if empty
Expand Down Expand Up @@ -203,7 +207,7 @@ impl<T: Ord> Extendable<T> for PriorityQueue<T> {
let (lower, _) = iter.size_hint();

let len = self.capacity();
self.reserve_at_least(len + lower);
self.reserve(len + lower);

for elem in *iter {
self.push(elem);
Expand Down
20 changes: 10 additions & 10 deletions src/libextra/ringbuf.rs
Expand Up @@ -168,8 +168,8 @@ impl<T> RingBuf<T> {
/// # Arguments
///
/// * n - The number of elements to reserve space for
pub fn reserve(&mut self, n: uint) {
self.elts.reserve(n);
pub fn reserve_exact(&mut self, n: uint) {
self.elts.reserve_exact(n);
}

/// Reserve capacity for at least `n` elements in the given RingBuf,
Expand All @@ -182,8 +182,8 @@ impl<T> RingBuf<T> {
/// # Arguments
///
/// * n - The number of elements to reserve space for
pub fn reserve_at_least(&mut self, n: uint) {
self.elts.reserve_at_least(n);
pub fn reserve(&mut self, n: uint) {
self.elts.reserve(n);
}

/// Front-to-back iterator.
Expand Down Expand Up @@ -641,26 +641,26 @@ mod tests {
}

#[test]
fn test_reserve() {
fn test_reserve_exact() {
let mut d = RingBuf::new();
d.push_back(0u64);
d.reserve(50);
d.reserve_exact(50);
assert_eq!(d.elts.capacity(), 50);
let mut d = RingBuf::new();
d.push_back(0u32);
d.reserve(50);
d.reserve_exact(50);
assert_eq!(d.elts.capacity(), 50);
}

#[test]
fn test_reserve_at_least() {
fn test_reserve() {
let mut d = RingBuf::new();
d.push_back(0u64);
d.reserve_at_least(50);
d.reserve(50);
assert_eq!(d.elts.capacity(), 64);
let mut d = RingBuf::new();
d.push_back(0u32);
d.reserve_at_least(50);
d.reserve(50);
assert_eq!(d.elts.capacity(), 64);
}

Expand Down
6 changes: 3 additions & 3 deletions src/libstd/hashmap.rs
Expand Up @@ -384,7 +384,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
}

/// Reserve space for at least `n` elements in the hash table.
pub fn reserve_at_least(&mut self, n: uint) {
pub fn reserve(&mut self, n: uint) {
if n > self.buckets.len() {
let buckets = n * 4 / 3 + 1;
self.resize(num::next_power_of_two(buckets));
Expand Down Expand Up @@ -793,8 +793,8 @@ impl<T:Hash + Eq> HashSet<T> {
}

/// Reserve space for at least `n` elements in the hash table.
pub fn reserve_at_least(&mut self, n: uint) {
self.map.reserve_at_least(n)
pub fn reserve(&mut self, n: uint) {
self.map.reserve(n)
}

/// Returns true if the hash set contains a value equivalent to the
Expand Down
36 changes: 15 additions & 21 deletions src/libstd/str.rs
Expand Up @@ -104,7 +104,7 @@ use iter::{Iterator, FromIterator, Extendable, range};
use iter::{Filter, AdditiveIterator, Map};
use iter::{Rev, DoubleEndedIterator, ExactSize};
use libc;
use num::{Saturating, checked_next_power_of_two};
use num::Saturating;
use option::{None, Option, Some};
use ptr;
use ptr::RawPtr;
Expand Down Expand Up @@ -186,12 +186,7 @@ pub fn from_char(ch: char) -> ~str {

/// Convert a vector of chars to a string
pub fn from_chars(chs: &[char]) -> ~str {
let mut buf = ~"";
buf.reserve(chs.len());
for ch in chs.iter() {
buf.push_char(*ch)
}
buf
chs.iter().map(|c| *c).collect()
}

#[doc(hidden)]
Expand Down Expand Up @@ -875,8 +870,7 @@ pub fn utf16_chars(v: &[u16], f: |char|) {

/// Allocates a new string from the utf-16 slice provided
pub fn from_utf16(v: &[u16]) -> ~str {
let mut buf = ~"";
buf.reserve(v.len());
let mut buf = with_capacity(v.len());
utf16_chars(v, |ch| buf.push_char(ch));
buf
}
Expand Down Expand Up @@ -2166,17 +2160,15 @@ impl<'a> StrSlice<'a> for &'a str {
}

fn escape_default(&self) -> ~str {
let mut out: ~str = ~"";
out.reserve_at_least(self.len());
let mut out = with_capacity(self.len());
for c in self.chars() {
c.escape_default(|c| out.push_char(c));
}
out
}

fn escape_unicode(&self) -> ~str {
let mut out: ~str = ~"";
out.reserve_at_least(self.len());
let mut out = with_capacity(self.len());
for c in self.chars() {
c.escape_unicode(|c| out.push_char(c));
}
Expand Down Expand Up @@ -2508,7 +2500,7 @@ pub trait OwnedStr {
///
/// * s - A string
/// * n - The number of bytes to reserve space for
fn reserve(&mut self, n: uint);
fn reserve_exact(&mut self, n: uint);

/// Reserves capacity for at least `n` bytes in the given string.
///
Expand All @@ -2526,7 +2518,7 @@ pub trait OwnedStr {
///
/// * s - A string
/// * n - The number of bytes to reserve space for
fn reserve_at_least(&mut self, n: uint);
fn reserve(&mut self, n: uint);

/// Returns the number of single-byte characters the string can hold without
/// reallocating
Expand All @@ -2552,7 +2544,7 @@ impl OwnedStr for ~str {
#[inline]
fn push_str_no_overallocate(&mut self, rhs: &str) {
let new_cap = self.len() + rhs.len();
self.reserve(new_cap);
self.reserve_exact(new_cap);
self.push_str(rhs);
}

Expand Down Expand Up @@ -2631,15 +2623,17 @@ impl OwnedStr for ~str {
}

#[inline]
fn reserve(&mut self, n: uint) {
fn reserve_exact(&mut self, n: uint) {
unsafe {
raw::as_owned_vec(self).reserve(n)
raw::as_owned_vec(self).reserve_exact(n)
}
}

#[inline]
fn reserve_at_least(&mut self, n: uint) {
self.reserve(checked_next_power_of_two(n).unwrap_or(n))
fn reserve(&mut self, n: uint) {
unsafe {
raw::as_owned_vec(self).reserve(n)
}
}

#[inline]
Expand Down Expand Up @@ -2711,7 +2705,7 @@ impl Extendable<char> for ~str {
fn extend<T: Iterator<char>>(&mut self, iterator: &mut T) {
let (lower, _) = iterator.size_hint();
let reserve = lower + self.len();
self.reserve_at_least(reserve);
self.reserve(reserve);
for ch in *iterator {
self.push_char(ch)
}
Expand Down
28 changes: 14 additions & 14 deletions src/libstd/vec.rs
Expand Up @@ -1335,7 +1335,7 @@ pub trait OwnedVector<T> {
* This method always succeeds in reserving space for `n` elements, or it does
* not return.
*/
fn reserve(&mut self, n: uint);
fn reserve_exact(&mut self, n: uint);
/**
* Reserves capacity for at least `n` elements in the given vector.
*
Expand All @@ -1350,7 +1350,7 @@ pub trait OwnedVector<T> {
*
* * n - The number of elements to reserve space for
*/
fn reserve_at_least(&mut self, n: uint);
fn reserve(&mut self, n: uint);
/**
* Reserves capacity for at least `n` additional elements in the given vector.
*
Expand Down Expand Up @@ -1468,7 +1468,7 @@ impl<T> OwnedVector<T> for ~[T] {
self.move_iter().rev()
}

fn reserve(&mut self, n: uint) {
fn reserve_exact(&mut self, n: uint) {
// Only make the (slow) call into the runtime if we have to
if self.capacity() < n {
unsafe {
Expand All @@ -1486,16 +1486,16 @@ impl<T> OwnedVector<T> for ~[T] {
}

#[inline]
fn reserve_at_least(&mut self, n: uint) {
self.reserve(checked_next_power_of_two(n).unwrap_or(n));
fn reserve(&mut self, n: uint) {
self.reserve_exact(checked_next_power_of_two(n).unwrap_or(n));
}

#[inline]
fn reserve_additional(&mut self, n: uint) {
if self.capacity() - self.len() < n {
match self.len().checked_add(&n) {
None => fail!("vec::reserve_additional: `uint` overflow"),
Some(new_cap) => self.reserve_at_least(new_cap)
Some(new_cap) => self.reserve(new_cap)
}
}
}
Expand Down Expand Up @@ -1678,7 +1678,7 @@ impl<T> OwnedVector<T> for ~[T] {
}
fn grow_fn(&mut self, n: uint, op: |uint| -> T) {
let new_len = self.len() + n;
self.reserve_at_least(new_len);
self.reserve(new_len);
let mut i: uint = 0u;
while i < n {
self.push(op(i));
Expand Down Expand Up @@ -1737,15 +1737,15 @@ impl<T:Clone> OwnedCloneableVector<T> for ~[T] {
#[inline]
fn push_all(&mut self, rhs: &[T]) {
let new_len = self.len() + rhs.len();
self.reserve(new_len);
self.reserve_exact(new_len);

for elt in rhs.iter() {
self.push((*elt).clone())
}
}
fn grow(&mut self, n: uint, initval: &T) {
let new_len = self.len() + n;
self.reserve_at_least(new_len);
self.reserve(new_len);
let mut i: uint = 0u;

while i < n {
Expand Down Expand Up @@ -2900,7 +2900,7 @@ impl<A> Extendable<A> for ~[A] {
fn extend<T: Iterator<A>>(&mut self, iterator: &mut T) {
let (lower, _) = iterator.size_hint();
let len = self.len();
self.reserve(len + lower);
self.reserve_exact(len + lower);
for x in *iterator {
self.push(x);
}
Expand Down Expand Up @@ -3630,10 +3630,10 @@ mod tests {
#[test]
fn test_capacity() {
let mut v = ~[0u64];
v.reserve(10u);
v.reserve_exact(10u);
assert_eq!(v.capacity(), 10u);
let mut v = ~[0u32];
v.reserve(10u);
v.reserve_exact(10u);
assert_eq!(v.capacity(), 10u);
}

Expand Down Expand Up @@ -4070,7 +4070,7 @@ mod tests {
#[should_fail]
fn test_overflow_does_not_cause_segfault() {
let mut v = ~[];
v.reserve(-1);
v.reserve_exact(-1);
v.push(1);
v.push(2);
}
Expand All @@ -4080,7 +4080,7 @@ mod tests {
fn test_overflow_does_not_cause_segfault_managed() {
use rc::Rc;
let mut v = ~[Rc::new(1)];
v.reserve(-1);
v.reserve_exact(-1);
v.push(Rc::new(2));
}

Expand Down

9 comments on commit 65f3578

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at dmanescu@65f3578

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging dmanescu/rust/reserve-rename = 65f3578 into auto

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dmanescu/rust/reserve-rename = 65f3578 merged ok, testing candidate = 0131f692

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at dmanescu@65f3578

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging dmanescu/rust/reserve-rename = 65f3578 into auto

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dmanescu/rust/reserve-rename = 65f3578 merged ok, testing candidate = cdc6789

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 65f3578 Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = cdc6789

Please sign in to comment.