Skip to content

Commit 7bafedb

Browse files
author
blake2-ppc
committed
---
yaml --- r: 81149 b: refs/heads/snap-stage3 c: 57757a8 h: refs/heads/master i: 81147: 2669bc6 v: v3
1 parent e13c835 commit 7bafedb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 4c6bf4872012c010f84dc7fa2cdfe87522533f89
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 5adfa1038787cb042ca5bbe634082201d71a3b72
4+
refs/heads/snap-stage3: 57757a8051101eb64eb7a330d7c673422e06a57b
55
refs/heads/try: 70152ff55722878cde684ee6462c14c65f2c4729
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libextra/ringbuf.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ impl<T> RingBuf<T> {
143143
}
144144
}
145145

146+
/// Swap elements at indices `i` and `j`
147+
///
148+
/// `i` and `j` may be equal.
149+
///
150+
/// Fails if there is no element with the given index
151+
pub fn swap(&mut self, i: uint, j: uint) {
152+
assert!(i < self.len());
153+
assert!(j < self.len());
154+
let ri = self.raw_index(i);
155+
let rj = self.raw_index(j);
156+
self.elts.swap(ri, rj);
157+
}
158+
146159
/// Return index in underlying vec for a given logical element index
147160
fn raw_index(&self, idx: uint) -> uint {
148161
raw_index(self.lo, self.elts.len(), idx)
@@ -604,6 +617,14 @@ mod tests {
604617
assert_eq!(d.elts.capacity(), 64);
605618
}
606619

620+
#[test]
621+
fn test_swap() {
622+
let mut d: RingBuf<int> = range(0, 5).collect();
623+
d.pop_front();
624+
d.swap(0, 3);
625+
assert_eq!(d.iter().map(|&x|x).collect::<~[int]>(), ~[4, 2, 3, 1]);
626+
}
627+
607628
#[test]
608629
fn test_iter() {
609630
let mut d = RingBuf::new();

0 commit comments

Comments
 (0)