Skip to content

Commit 4d736f1

Browse files
committed
---
yaml --- r: 15191 b: refs/heads/try c: b3d7823 h: refs/heads/master i: 15189: 91ced65 15187: 2df6433 15183: 1312154 v: v3
1 parent 427438c commit 4d736f1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 8e743b2981754f1aa7e0eb5b57d79e5a8ad7d172
5+
refs/heads/try: b3d7823381cd2d3d89ba4ca85a709df6622180ab
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/vec.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#[doc = "Vectors"];
22

33
import option::{some, none};
4-
import uint::next_power_of_two;
54
import ptr::addr_of;
65

76
export init_op;
87
export is_empty;
98
export is_not_empty;
109
export same_length;
1110
export reserve;
11+
export reserve_at_least;
1212
export len;
1313
export from_fn;
1414
export from_elem;
@@ -115,6 +115,25 @@ fn reserve<T>(&v: [const T], n: uint) {
115115
rustrt::vec_reserve_shared(sys::get_type_desc::<T>(), v, n);
116116
}
117117

118+
#[doc = "
119+
Reserves capacity for at least `n` elements in the given vector.
120+
121+
This function will over-allocate in order to amortize the allocation costs
122+
in scenarios where the caller may need to repeatedly reserve additional
123+
space.
124+
125+
If the capacity for `v` is already equal to or greater than the requested
126+
capacity, then no action is taken.
127+
128+
# Arguments
129+
130+
* v - A vector
131+
* n - The number of elements to reserve space for
132+
"]
133+
fn reserve_at_least<T>(&v: [const T], n: uint) {
134+
reserve(v, uint::next_power_of_two(n));
135+
}
136+
118137
#[doc = "Returns the length of a vector"]
119138
#[inline(always)]
120139
pure fn len<T>(&&v: [const T]) -> uint unsafe {
@@ -364,7 +383,7 @@ Expands a vector in place, initializing the new elements to a given value
364383
* initval - The value for the new elements
365384
"]
366385
fn grow<T: copy>(&v: [const T], n: uint, initval: T) {
367-
reserve(v, next_power_of_two(len(v) + n));
386+
reserve_at_least(v, len(v) + n);
368387
let mut i: uint = 0u;
369388
while i < n { v += [initval]; i += 1u; }
370389
}
@@ -383,7 +402,7 @@ Function `init_op` is called `n` times with the values [0..`n`)
383402
value
384403
"]
385404
fn grow_fn<T>(&v: [const T], n: uint, op: init_op<T>) {
386-
reserve(v, next_power_of_two(len(v) + n));
405+
reserve_at_least(v, len(v) + n);
387406
let mut i: uint = 0u;
388407
while i < n { v += [op(i)]; i += 1u; }
389408
}

0 commit comments

Comments
 (0)