diff options
| author | bors <bors@rust-lang.org> | 2013-09-26 13:21:08 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-09-26 13:21:08 -0700 |
| commit | f210a1671883d5fe97a2b06ebbe9270fa9a6943a (patch) | |
| tree | 08c98c9b674128c00451b34ce630c933febe6800 /src | |
| parent | 47f2e80b65bf814953c0ceda3b48e46802883f4b (diff) | |
| parent | 57757a8051101eb64eb7a330d7c673422e06a57b (diff) | |
| download | rust-f210a1671883d5fe97a2b06ebbe9270fa9a6943a.tar.gz rust-f210a1671883d5fe97a2b06ebbe9270fa9a6943a.zip | |
auto merge of #9520 : blake2-ppc/rust/ringbuf-swap, r=thestinger
extra::ringbuf: Implement method `.swap(uint, uint)` just like vector RingBuf::swap(&mut self, i, j) swaps the element at indices `i` and `j` if both elements are in bounds, otherwise it fails.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libextra/ringbuf.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs index ea8537caeb5..1e396291405 100644 --- a/src/libextra/ringbuf.rs +++ b/src/libextra/ringbuf.rs @@ -143,6 +143,19 @@ impl<T> RingBuf<T> { } } + /// Swap elements at indices `i` and `j` + /// + /// `i` and `j` may be equal. + /// + /// Fails if there is no element with the given index + pub fn swap(&mut self, i: uint, j: uint) { + assert!(i < self.len()); + assert!(j < self.len()); + let ri = self.raw_index(i); + let rj = self.raw_index(j); + self.elts.swap(ri, rj); + } + /// Return index in underlying vec for a given logical element index fn raw_index(&self, idx: uint) -> uint { raw_index(self.lo, self.elts.len(), idx) @@ -605,6 +618,14 @@ mod tests { } #[test] + fn test_swap() { + let mut d: RingBuf<int> = range(0, 5).collect(); + d.pop_front(); + d.swap(0, 3); + assert_eq!(d.iter().map(|&x|x).collect::<~[int]>(), ~[4, 2, 3, 1]); + } + + #[test] fn test_iter() { let mut d = RingBuf::new(); assert_eq!(d.iter().next(), None); |
