Skip to content

Commit 689f6ce

Browse files
Dretchbrson
authored andcommitted
Add insert and remove methods to vecs - as proposed in issue #4028.
1 parent aee0b76 commit 689f6ce

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/libcore/vec.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,34 @@ pub fn unshift<T>(v: &mut ~[T], x: T) {
419419
v.push_all_move(move vv);
420420
}
421421

422+
/// Insert an element at position i within v, shifting all
423+
/// elements after position i one position to the right.
424+
pub fn insert<T>(v: &mut ~[T], i: uint, x: T) {
425+
let len = v.len();
426+
assert i <= len;
427+
428+
v.push(move x);
429+
let mut j = len;
430+
while j > i {
431+
v[j] <-> v[j - 1];
432+
j -= 1;
433+
}
434+
}
435+
436+
/// Remove and return the element at position i within v, shifting
437+
/// all elements after position i one position to the left.
438+
pub fn remove<T>(v: &mut ~[T], i: uint) -> T {
439+
let len = v.len();
440+
assert i < len;
441+
442+
let mut j = i;
443+
while j < len - 1 {
444+
v[j] <-> v[j + 1];
445+
j += 1;
446+
}
447+
move v.pop()
448+
}
449+
422450
pub fn consume<T>(v: ~[T], f: fn(uint, v: T)) unsafe {
423451
let mut v = move v; // FIXME(#3488)
424452

@@ -1685,6 +1713,8 @@ pub trait MutableVector<T> {
16851713
fn pop(&mut self) -> T;
16861714
fn shift(&mut self) -> T;
16871715
fn unshift(&mut self, x: T);
1716+
fn insert(&mut self, i: uint, x:T);
1717+
fn remove(&mut self, i: uint) -> T;
16881718
fn swap_remove(&mut self, index: uint) -> T;
16891719
fn truncate(&mut self, newlen: uint);
16901720
fn retain(&mut self, f: pure fn(t: &T) -> bool);
@@ -1722,6 +1752,14 @@ impl<T> ~[T]: MutableVector<T> {
17221752
unshift(self, move x)
17231753
}
17241754

1755+
fn insert(&mut self, i: uint, x:T) {
1756+
insert(self, i, move x)
1757+
}
1758+
1759+
fn remove(&mut self, i: uint) -> T {
1760+
remove(self, i)
1761+
}
1762+
17251763
fn swap_remove(&mut self, index: uint) -> T {
17261764
swap_remove(self, index)
17271765
}
@@ -2925,6 +2963,54 @@ mod tests {
29252963
assert x == ~[0, 1, 2, 3];
29262964
}
29272965

2966+
#[test]
2967+
fn test_insert() {
2968+
let mut a = ~[1, 2, 4];
2969+
a.insert(2, 3);
2970+
assert a == ~[1, 2, 3, 4];
2971+
2972+
let mut a = ~[1, 2, 3];
2973+
a.insert(0, 0);
2974+
assert a == ~[0, 1, 2, 3];
2975+
2976+
let mut a = ~[1, 2, 3];
2977+
a.insert(3, 4);
2978+
assert a == ~[1, 2, 3, 4];
2979+
2980+
let mut a = ~[];
2981+
a.insert(0, 1);
2982+
assert a == ~[1];
2983+
}
2984+
2985+
#[test]
2986+
#[should_fail]
2987+
fn test_insert_oob() {
2988+
let mut a = ~[1, 2, 3];
2989+
a.insert(4, 5);
2990+
}
2991+
2992+
#[test]
2993+
fn test_remove() {
2994+
let mut a = ~[1, 2, 3, 4];
2995+
a.remove(2);
2996+
assert a == ~[1, 2, 4];
2997+
2998+
let mut a = ~[1, 2, 3];
2999+
a.remove(0);
3000+
assert a == ~[2, 3];
3001+
3002+
let mut a = ~[1];
3003+
a.remove(0);
3004+
assert a == ~[];
3005+
}
3006+
3007+
#[test]
3008+
#[should_fail]
3009+
fn test_remove_oob() {
3010+
let mut a = ~[1, 2, 3];
3011+
a.remove(3);
3012+
}
3013+
29283014
#[test]
29293015
fn test_capacity() {
29303016
let mut v = ~[0u64];

0 commit comments

Comments
 (0)