Jonathan S | 03609e5 | 2014-04-21 04:59:12 | [diff] [blame] | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
Graydon Hoare | 00c856c | 2012-12-04 00:48:01 | [diff] [blame] | 2 | // file at the top-level directory of this distribution and at |
| 3 | // http://rust-lang.org/COPYRIGHT. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | // option. This file may not be copied, modified, or distributed |
| 9 | // except according to those terms. |
| 10 | |
Steve Klabnik | 84030fd | 2014-08-30 21:11:22 | [diff] [blame] | 11 | //! This crate implements a double-ended queue with `O(1)` amortized inserts and removals from both |
| 12 | //! ends of the container. It also has `O(1)` indexing like a vector. The contained elements are |
| 13 | //! not required to be copyable, and the queue will be sendable if the contained type is sendable. |
Patrick Walton | f3723cf | 2013-05-17 22:28:44 | [diff] [blame] | 14 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 15 | #![stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | cb765ce | 2015-01-05 00:35:20 | [diff] [blame] | 16 | |
Alex Crichton | 6a58537 | 2014-05-30 01:50:12 | [diff] [blame] | 17 | use core::prelude::*; |
| 18 | |
Alex Crichton | 56290a0 | 2014-12-22 17:04:23 | [diff] [blame] | 19 | use core::cmp::Ordering; |
Tom Jakubowski | d6a3941 | 2014-06-09 07:30:04 | [diff] [blame] | 20 | use core::default::Default; |
Alex Crichton | 6a58537 | 2014-05-30 01:50:12 | [diff] [blame] | 21 | use core::fmt; |
Jorge Aparicio | a65d3f5 | 2015-01-08 03:01:05 | [diff] [blame] | 22 | use core::iter::{self, repeat, FromIterator, IntoIterator, RandomAccessIterator}; |
Nick Cameron | 9f07d05 | 2015-01-06 22:33:42 | [diff] [blame] | 23 | use core::marker; |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 24 | use core::mem; |
Colin Sherratt | 6277e3b | 2014-11-14 09:21:44 | [diff] [blame] | 25 | use core::num::{Int, UnsignedInt}; |
Alex Crichton | 56290a0 | 2014-12-22 17:04:23 | [diff] [blame] | 26 | use core::ops::{Index, IndexMut}; |
| 27 | use core::ptr; |
| 28 | use core::raw::Slice as RawSlice; |
Alex Crichton | 998fece | 2013-05-06 04:42:54 | [diff] [blame] | 29 | |
Alex Crichton | 511f0b8 | 2014-12-09 20:37:23 | [diff] [blame] | 30 | use std::hash::{Writer, Hash, Hasher}; |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 31 | use std::cmp; |
| 32 | |
| 33 | use alloc::heap; |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 34 | |
Alexis | 15fb06d | 2015-02-05 20:08:33 | [diff] [blame] | 35 | static INITIAL_CAPACITY: usize = 7; // 2^3 - 1 |
| 36 | static MINIMUM_CAPACITY: usize = 1; // 2 - 1 |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 37 | |
Tobias Bucher | 4a46f5e | 2014-12-09 22:05:16 | [diff] [blame] | 38 | /// `RingBuf` is a circular buffer, which can be used as a double-ended queue efficiently. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 39 | #[stable(feature = "rust1", since = "1.0.0")] |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 40 | pub struct RingBuf<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 41 | // tail and head are pointers into the buffer. Tail always points |
| 42 | // to the first element that could be read, Head always points |
| 43 | // to where data should be written. |
| 44 | // If tail == head the buffer is empty. The length of the ringbuf |
| 45 | // is defined as the distance between the two. |
| 46 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 47 | tail: usize, |
| 48 | head: usize, |
| 49 | cap: usize, |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 50 | ptr: *mut T |
| 51 | } |
| 52 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 53 | #[stable(feature = "rust1", since = "1.0.0")] |
Rohit Joshi | 8fb25ab | 2014-12-30 09:20:24 | [diff] [blame] | 54 | unsafe impl<T: Send> Send for RingBuf<T> {} |
| 55 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 56 | #[stable(feature = "rust1", since = "1.0.0")] |
Rohit Joshi | 8fb25ab | 2014-12-30 09:20:24 | [diff] [blame] | 57 | unsafe impl<T: Sync> Sync for RingBuf<T> {} |
| 58 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 59 | #[stable(feature = "rust1", since = "1.0.0")] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 60 | impl<T: Clone> Clone for RingBuf<T> { |
| 61 | fn clone(&self) -> RingBuf<T> { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 62 | self.iter().cloned().collect() |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | #[unsafe_destructor] |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 67 | #[stable(feature = "rust1", since = "1.0.0")] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 68 | impl<T> Drop for RingBuf<T> { |
| 69 | fn drop(&mut self) { |
| 70 | self.clear(); |
| 71 | unsafe { |
| 72 | if mem::size_of::<T>() != 0 { |
| 73 | heap::deallocate(self.ptr as *mut u8, |
| 74 | self.cap * mem::size_of::<T>(), |
| 75 | mem::min_align_of::<T>()) |
| 76 | } |
| 77 | } |
| 78 | } |
Marijn Haverbeke | 26610db | 2012-01-11 11:49:33 | [diff] [blame] | 79 | } |
Roy Frostig | 9c81889 | 2010-07-21 01:03:09 | [diff] [blame] | 80 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 81 | #[stable(feature = "rust1", since = "1.0.0")] |
Tom Jakubowski | d6a3941 | 2014-06-09 07:30:04 | [diff] [blame] | 82 | impl<T> Default for RingBuf<T> { |
| 83 | #[inline] |
| 84 | fn default() -> RingBuf<T> { RingBuf::new() } |
| 85 | } |
| 86 | |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 87 | impl<T> RingBuf<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 88 | /// Turn ptr into a slice |
| 89 | #[inline] |
Alexis Beingessner | 8dbaa71 | 2014-12-31 00:07:53 | [diff] [blame] | 90 | unsafe fn buffer_as_slice(&self) -> &[T] { |
we | 812ce6c | 2015-01-17 04:34:10 | [diff] [blame] | 91 | mem::transmute(RawSlice { data: self.ptr, len: self.cap }) |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /// Turn ptr into a mut slice |
| 95 | #[inline] |
Alexis Beingessner | 8dbaa71 | 2014-12-31 00:07:53 | [diff] [blame] | 96 | unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] { |
we | 812ce6c | 2015-01-17 04:34:10 | [diff] [blame] | 97 | mem::transmute(RawSlice { data: self.ptr, len: self.cap }) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// Moves an element out of the buffer |
| 101 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 102 | unsafe fn buffer_read(&mut self, off: usize) -> T { |
| 103 | ptr::read(self.ptr.offset(off as isize)) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /// Writes an element into the buffer, moving it. |
| 107 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 108 | unsafe fn buffer_write(&mut self, off: usize, t: T) { |
| 109 | ptr::write(self.ptr.offset(off as isize), t); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /// Returns true iff the buffer is at capacity |
| 113 | #[inline] |
| 114 | fn is_full(&self) -> bool { self.cap - self.len() == 1 } |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 115 | |
| 116 | /// Returns the index in the underlying buffer for a given logical element index. |
| 117 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 118 | fn wrap_index(&self, idx: usize) -> usize { wrap_index(idx, self.cap) } |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 119 | |
| 120 | /// Copies a contiguous block of memory len long from src to dst |
| 121 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 122 | unsafe fn copy(&self, dst: usize, src: usize, len: usize) { |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 123 | debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 124 | self.cap); |
| 125 | debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 126 | self.cap); |
| 127 | ptr::copy_memory( |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 128 | self.ptr.offset(dst as isize), |
| 129 | self.ptr.offset(src as isize), |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 130 | len); |
| 131 | } |
| 132 | |
| 133 | /// Copies a contiguous block of memory len long from src to dst |
| 134 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 135 | unsafe fn copy_nonoverlapping(&self, dst: usize, src: usize, len: usize) { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 136 | debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 137 | self.cap); |
| 138 | debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 139 | self.cap); |
| 140 | ptr::copy_nonoverlapping_memory( |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 141 | self.ptr.offset(dst as isize), |
| 142 | self.ptr.offset(src as isize), |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 143 | len); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 144 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | impl<T> RingBuf<T> { |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 148 | /// Creates an empty `RingBuf`. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 149 | #[stable(feature = "rust1", since = "1.0.0")] |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 150 | pub fn new() -> RingBuf<T> { |
| 151 | RingBuf::with_capacity(INITIAL_CAPACITY) |
| 152 | } |
| 153 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 154 | /// Creates an empty `RingBuf` with space for at least `n` elements. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 155 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 156 | pub fn with_capacity(n: usize) -> RingBuf<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 157 | // +1 since the ringbuffer always leaves one space empty |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 158 | let cap = cmp::max(n + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); |
| 159 | assert!(cap > n, "capacity overflow"); |
Colin Sherratt | 6277e3b | 2014-11-14 09:21:44 | [diff] [blame] | 160 | let size = cap.checked_mul(mem::size_of::<T>()) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 161 | .expect("capacity overflow"); |
| 162 | |
Colin Sherratt | ba24e33 | 2014-11-10 03:34:53 | [diff] [blame] | 163 | let ptr = if mem::size_of::<T>() != 0 { |
| 164 | unsafe { |
| 165 | let ptr = heap::allocate(size, mem::min_align_of::<T>()) as *mut T;; |
| 166 | if ptr.is_null() { ::alloc::oom() } |
| 167 | ptr |
| 168 | } |
| 169 | } else { |
| 170 | heap::EMPTY as *mut T |
| 171 | }; |
| 172 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 173 | RingBuf { |
| 174 | tail: 0, |
| 175 | head: 0, |
| 176 | cap: cap, |
Colin Sherratt | ba24e33 | 2014-11-10 03:34:53 | [diff] [blame] | 177 | ptr: ptr |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 178 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 179 | } |
| 180 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 181 | /// Retrieves an element in the `RingBuf` by index. |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 182 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 183 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 184 | /// |
| 185 | /// ```rust |
| 186 | /// use std::collections::RingBuf; |
| 187 | /// |
| 188 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 189 | /// buf.push_back(3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 190 | /// buf.push_back(4); |
| 191 | /// buf.push_back(5); |
| 192 | /// assert_eq!(buf.get(1).unwrap(), &4); |
| 193 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 194 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 195 | pub fn get(&self, i: usize) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 196 | if i < self.len() { |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 197 | let idx = self.wrap_index(self.tail + i); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 198 | unsafe { Some(&*self.ptr.offset(idx as isize)) } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 199 | } else { |
| 200 | None |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | /// Retrieves an element in the `RingBuf` mutably by index. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 205 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 206 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 207 | /// |
| 208 | /// ```rust |
| 209 | /// use std::collections::RingBuf; |
| 210 | /// |
| 211 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 212 | /// buf.push_back(3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 213 | /// buf.push_back(4); |
| 214 | /// buf.push_back(5); |
| 215 | /// match buf.get_mut(1) { |
| 216 | /// None => {} |
| 217 | /// Some(elem) => { |
| 218 | /// *elem = 7; |
| 219 | /// } |
| 220 | /// } |
| 221 | /// |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 222 | /// assert_eq!(buf[1], 7); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 223 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 224 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 225 | pub fn get_mut(&mut self, i: usize) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 226 | if i < self.len() { |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 227 | let idx = self.wrap_index(self.tail + i); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 228 | unsafe { Some(&mut *self.ptr.offset(idx as isize)) } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 229 | } else { |
| 230 | None |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 231 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 232 | } |
| 233 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 234 | /// Swaps elements at indices `i` and `j`. |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 235 | /// |
| 236 | /// `i` and `j` may be equal. |
| 237 | /// |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 238 | /// Fails if there is no element with either index. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 239 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 240 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 241 | /// |
| 242 | /// ```rust |
| 243 | /// use std::collections::RingBuf; |
| 244 | /// |
| 245 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 246 | /// buf.push_back(3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 247 | /// buf.push_back(4); |
| 248 | /// buf.push_back(5); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 249 | /// buf.swap(0, 2); |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 250 | /// assert_eq!(buf[0], 5); |
| 251 | /// assert_eq!(buf[2], 3); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 252 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 253 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 254 | pub fn swap(&mut self, i: usize, j: usize) { |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 255 | assert!(i < self.len()); |
| 256 | assert!(j < self.len()); |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 257 | let ri = self.wrap_index(self.tail + i); |
| 258 | let rj = self.wrap_index(self.tail + j); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 259 | unsafe { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 260 | ptr::swap(self.ptr.offset(ri as isize), self.ptr.offset(rj as isize)) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 261 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 262 | } |
| 263 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 264 | /// Returns the number of elements the `RingBuf` can hold without |
| 265 | /// reallocating. |
| 266 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 267 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 268 | /// |
| 269 | /// ``` |
| 270 | /// use std::collections::RingBuf; |
| 271 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 272 | /// let buf: RingBuf<i32> = RingBuf::with_capacity(10); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 273 | /// assert!(buf.capacity() >= 10); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 274 | /// ``` |
| 275 | #[inline] |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 276 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 277 | pub fn capacity(&self) -> usize { self.cap - 1 } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 278 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 279 | /// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the |
| 280 | /// given `RingBuf`. Does nothing if the capacity is already sufficient. |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 281 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 282 | /// Note that the allocator may give the collection more space than it requests. Therefore |
| 283 | /// capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future |
| 284 | /// insertions are expected. |
| 285 | /// |
| 286 | /// # Panics |
| 287 | /// |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 288 | /// Panics if the new capacity overflows `usize`. |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 289 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 290 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 291 | /// |
| 292 | /// ``` |
| 293 | /// use std::collections::RingBuf; |
| 294 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 295 | /// let mut buf: RingBuf<i32> = vec![1].into_iter().collect(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 296 | /// buf.reserve_exact(10); |
| 297 | /// assert!(buf.capacity() >= 11); |
| 298 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 299 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 300 | pub fn reserve_exact(&mut self, additional: usize) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 301 | self.reserve(additional); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /// Reserves capacity for at least `additional` more elements to be inserted in the given |
| 305 | /// `Ringbuf`. The collection may reserve more space to avoid frequent reallocations. |
| 306 | /// |
| 307 | /// # Panics |
| 308 | /// |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 309 | /// Panics if the new capacity overflows `usize`. |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 310 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 311 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 312 | /// |
| 313 | /// ``` |
| 314 | /// use std::collections::RingBuf; |
| 315 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 316 | /// let mut buf: RingBuf<i32> = vec![1].into_iter().collect(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 317 | /// buf.reserve(10); |
| 318 | /// assert!(buf.capacity() >= 11); |
| 319 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 320 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 321 | pub fn reserve(&mut self, additional: usize) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 322 | let new_len = self.len() + additional; |
| 323 | assert!(new_len + 1 > self.len(), "capacity overflow"); |
| 324 | if new_len > self.capacity() { |
Colin Sherratt | 6277e3b | 2014-11-14 09:21:44 | [diff] [blame] | 325 | let count = (new_len + 1).next_power_of_two(); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 326 | assert!(count >= new_len + 1); |
| 327 | |
| 328 | if mem::size_of::<T>() != 0 { |
| 329 | let old = self.cap * mem::size_of::<T>(); |
Colin Sherratt | 6277e3b | 2014-11-14 09:21:44 | [diff] [blame] | 330 | let new = count.checked_mul(mem::size_of::<T>()) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 331 | .expect("capacity overflow"); |
| 332 | unsafe { |
| 333 | self.ptr = heap::reallocate(self.ptr as *mut u8, |
| 334 | old, |
| 335 | new, |
| 336 | mem::min_align_of::<T>()) as *mut T; |
Colin Sherratt | ba24e33 | 2014-11-10 03:34:53 | [diff] [blame] | 337 | if self.ptr.is_null() { ::alloc::oom() } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | // Move the shortest contiguous section of the ring buffer |
| 342 | // T H |
| 343 | // [o o o o o o o . ] |
| 344 | // T H |
| 345 | // A [o o o o o o o . . . . . . . . . ] |
| 346 | // H T |
| 347 | // [o o . o o o o o ] |
| 348 | // T H |
| 349 | // B [. . . o o o o o o o . . . . . . ] |
| 350 | // H T |
| 351 | // [o o o o o . o o ] |
| 352 | // H T |
| 353 | // C [o o o o o . . . . . . . . . o o ] |
| 354 | |
| 355 | let oldcap = self.cap; |
| 356 | self.cap = count; |
| 357 | |
| 358 | if self.tail <= self.head { // A |
| 359 | // Nop |
| 360 | } else if self.head < oldcap - self.tail { // B |
| 361 | unsafe { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 362 | self.copy_nonoverlapping(oldcap, 0, self.head); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 363 | } |
| 364 | self.head += oldcap; |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 365 | debug_assert!(self.head > self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 366 | } else { // C |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 367 | let new_tail = count - (oldcap - self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 368 | unsafe { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 369 | self.copy_nonoverlapping(new_tail, self.tail, oldcap - self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 370 | } |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 371 | self.tail = new_tail; |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 372 | debug_assert!(self.head < self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 373 | } |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 374 | debug_assert!(self.head < self.cap); |
| 375 | debug_assert!(self.tail < self.cap); |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 376 | debug_assert!(self.cap.count_ones() == 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 377 | } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 378 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 379 | |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 380 | /// Shrinks the capacity of the ringbuf as much as possible. |
| 381 | /// |
| 382 | /// It will drop down as close as possible to the length but the allocator may still inform the |
| 383 | /// ringbuf that there is space for a few more elements. |
| 384 | /// |
| 385 | /// # Examples |
| 386 | /// |
| 387 | /// ``` |
| 388 | /// use std::collections::RingBuf; |
| 389 | /// |
| 390 | /// let mut buf = RingBuf::with_capacity(15); |
Alexis | e15538d | 2015-02-06 18:57:13 | [diff] [blame^] | 391 | /// buf.extend(0..4); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 392 | /// assert_eq!(buf.capacity(), 15); |
| 393 | /// buf.shrink_to_fit(); |
| 394 | /// assert!(buf.capacity() >= 4); |
| 395 | /// ``` |
| 396 | pub fn shrink_to_fit(&mut self) { |
| 397 | // +1 since the ringbuffer always leaves one space empty |
| 398 | // len + 1 can't overflow for an existing, well-formed ringbuf. |
| 399 | let target_cap = cmp::max(self.len() + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); |
| 400 | if target_cap < self.cap { |
| 401 | // There are three cases of interest: |
| 402 | // All elements are out of desired bounds |
| 403 | // Elements are contiguous, and head is out of desired bounds |
| 404 | // Elements are discontiguous, and tail is out of desired bounds |
| 405 | // |
| 406 | // At all other times, element positions are unaffected. |
| 407 | // |
| 408 | // Indicates that elements at the head should be moved. |
| 409 | let head_outside = self.head == 0 || self.head >= target_cap; |
| 410 | // Move elements from out of desired bounds (positions after target_cap) |
| 411 | if self.tail >= target_cap && head_outside { |
| 412 | // T H |
| 413 | // [. . . . . . . . o o o o o o o . ] |
| 414 | // T H |
| 415 | // [o o o o o o o . ] |
| 416 | unsafe { |
| 417 | self.copy_nonoverlapping(0, self.tail, self.len()); |
| 418 | } |
| 419 | self.head = self.len(); |
| 420 | self.tail = 0; |
| 421 | } else if self.tail != 0 && self.tail < target_cap && head_outside { |
| 422 | // T H |
| 423 | // [. . . o o o o o o o . . . . . . ] |
| 424 | // H T |
| 425 | // [o o . o o o o o ] |
| 426 | let len = self.wrap_index(self.head - target_cap); |
| 427 | unsafe { |
| 428 | self.copy_nonoverlapping(0, target_cap, len); |
| 429 | } |
| 430 | self.head = len; |
| 431 | debug_assert!(self.head < self.tail); |
| 432 | } else if self.tail >= target_cap { |
| 433 | // H T |
| 434 | // [o o o o o . . . . . . . . . o o ] |
| 435 | // H T |
| 436 | // [o o o o o . o o ] |
| 437 | debug_assert!(self.wrap_index(self.head - 1) < target_cap); |
| 438 | let len = self.cap - self.tail; |
| 439 | let new_tail = target_cap - len; |
| 440 | unsafe { |
| 441 | self.copy_nonoverlapping(new_tail, self.tail, len); |
| 442 | } |
| 443 | self.tail = new_tail; |
| 444 | debug_assert!(self.head < self.tail); |
| 445 | } |
| 446 | |
| 447 | if mem::size_of::<T>() != 0 { |
| 448 | let old = self.cap * mem::size_of::<T>(); |
| 449 | let new_size = target_cap * mem::size_of::<T>(); |
| 450 | unsafe { |
| 451 | self.ptr = heap::reallocate(self.ptr as *mut u8, |
| 452 | old, |
| 453 | new_size, |
| 454 | mem::min_align_of::<T>()) as *mut T; |
| 455 | if self.ptr.is_null() { ::alloc::oom() } |
| 456 | } |
| 457 | } |
| 458 | self.cap = target_cap; |
| 459 | debug_assert!(self.head < self.cap); |
| 460 | debug_assert!(self.tail < self.cap); |
| 461 | debug_assert!(self.cap.count_ones() == 1); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /// Shorten a ringbuf, dropping excess elements from the back. |
| 466 | /// |
| 467 | /// If `len` is greater than the ringbuf's current length, this has no |
| 468 | /// effect. |
| 469 | /// |
| 470 | /// # Examples |
| 471 | /// |
| 472 | /// ``` |
| 473 | /// use std::collections::RingBuf; |
| 474 | /// |
| 475 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 476 | /// buf.push_back(5); |
| 477 | /// buf.push_back(10); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 478 | /// buf.push_back(15); |
| 479 | /// buf.truncate(1); |
| 480 | /// assert_eq!(buf.len(), 1); |
| 481 | /// assert_eq!(Some(&5), buf.get(0)); |
| 482 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 483 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 484 | reason = "matches collection reform specification; waiting on panic semantics")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 485 | pub fn truncate(&mut self, len: usize) { |
Jorge Aparicio | efc97a5 | 2015-01-26 21:05:07 | [diff] [blame] | 486 | for _ in len..self.len() { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 487 | self.pop_back(); |
| 488 | } |
| 489 | } |
| 490 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 491 | /// Returns a front-to-back iterator. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 492 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 493 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 494 | /// |
| 495 | /// ```rust |
| 496 | /// use std::collections::RingBuf; |
| 497 | /// |
| 498 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 499 | /// buf.push_back(5); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 500 | /// buf.push_back(3); |
| 501 | /// buf.push_back(4); |
Nick Cameron | 52ef462 | 2014-08-06 09:59:40 | [diff] [blame] | 502 | /// let b: &[_] = &[&5, &3, &4]; |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 503 | /// assert_eq!(buf.iter().collect::<Vec<&i32>>().as_slice(), b); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 504 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 505 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 506 | pub fn iter(&self) -> Iter<T> { |
| 507 | Iter { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 508 | tail: self.tail, |
| 509 | head: self.head, |
| 510 | ring: unsafe { self.buffer_as_slice() } |
| 511 | } |
blake2-ppc | 3385e79 | 2013-07-15 23:13:26 | [diff] [blame] | 512 | } |
| 513 | |
Andrew Wagner | 8fcc832 | 2014-12-15 09:22:49 | [diff] [blame] | 514 | /// Returns a front-to-back iterator that returns mutable references. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 515 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 516 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 517 | /// |
| 518 | /// ```rust |
| 519 | /// use std::collections::RingBuf; |
| 520 | /// |
| 521 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 522 | /// buf.push_back(5); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 523 | /// buf.push_back(3); |
| 524 | /// buf.push_back(4); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 525 | /// for num in buf.iter_mut() { |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 526 | /// *num = *num - 2; |
| 527 | /// } |
Nick Cameron | 52ef462 | 2014-08-06 09:59:40 | [diff] [blame] | 528 | /// let b: &[_] = &[&mut 3, &mut 1, &mut 2]; |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 529 | /// assert_eq!(&buf.iter_mut().collect::<Vec<&mut i32>>()[], b); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 530 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 531 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 532 | pub fn iter_mut(&mut self) -> IterMut<T> { |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 533 | IterMut { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 534 | tail: self.tail, |
| 535 | head: self.head, |
| 536 | cap: self.cap, |
| 537 | ptr: self.ptr, |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 538 | marker: marker::ContravariantLifetime, |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 539 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 540 | } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 541 | |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 542 | /// Consumes the list into an iterator yielding elements by value. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 543 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 544 | pub fn into_iter(self) -> IntoIter<T> { |
| 545 | IntoIter { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 546 | inner: self, |
| 547 | } |
| 548 | } |
| 549 | |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 550 | /// Returns a pair of slices which contain, in order, the contents of the |
| 551 | /// `RingBuf`. |
| 552 | #[inline] |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 553 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 554 | reason = "matches collection reform specification, waiting for dust to settle")] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 555 | pub fn as_slices(&self) -> (&[T], &[T]) { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 556 | unsafe { |
| 557 | let contiguous = self.is_contiguous(); |
| 558 | let buf = self.buffer_as_slice(); |
| 559 | if contiguous { |
| 560 | let (empty, buf) = buf.split_at(0); |
Jorge Aparicio | 517f1cc | 2015-01-07 16:58:31 | [diff] [blame] | 561 | (&buf[self.tail..self.head], empty) |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 562 | } else { |
| 563 | let (mid, right) = buf.split_at(self.tail); |
| 564 | let (left, _) = mid.split_at(self.head); |
| 565 | (right, left) |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /// Returns a pair of slices which contain, in order, the contents of the |
| 571 | /// `RingBuf`. |
| 572 | #[inline] |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 573 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 574 | reason = "matches collection reform specification, waiting for dust to settle")] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 575 | pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 576 | unsafe { |
| 577 | let contiguous = self.is_contiguous(); |
| 578 | let head = self.head; |
| 579 | let tail = self.tail; |
| 580 | let buf = self.buffer_as_mut_slice(); |
| 581 | |
| 582 | if contiguous { |
| 583 | let (empty, buf) = buf.split_at_mut(0); |
Aaron Turon | a506d4c | 2015-01-18 00:15:52 | [diff] [blame] | 584 | (&mut buf[tail .. head], empty) |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 585 | } else { |
| 586 | let (mid, right) = buf.split_at_mut(tail); |
| 587 | let (left, _) = mid.split_at_mut(head); |
| 588 | |
| 589 | (right, left) |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 594 | /// Returns the number of elements in the `RingBuf`. |
| 595 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 596 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 597 | /// |
| 598 | /// ``` |
| 599 | /// use std::collections::RingBuf; |
| 600 | /// |
| 601 | /// let mut v = RingBuf::new(); |
| 602 | /// assert_eq!(v.len(), 0); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 603 | /// v.push_back(1); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 604 | /// assert_eq!(v.len(), 1); |
| 605 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 606 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 607 | pub fn len(&self) -> usize { count(self.tail, self.head, self.cap) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 608 | |
| 609 | /// Returns true if the buffer contains no elements |
| 610 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 611 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 612 | /// |
| 613 | /// ``` |
| 614 | /// use std::collections::RingBuf; |
| 615 | /// |
| 616 | /// let mut v = RingBuf::new(); |
| 617 | /// assert!(v.is_empty()); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 618 | /// v.push_front(1); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 619 | /// assert!(!v.is_empty()); |
| 620 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 621 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 622 | pub fn is_empty(&self) -> bool { self.len() == 0 } |
| 623 | |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 624 | /// Creates a draining iterator that clears the `RingBuf` and iterates over |
| 625 | /// the removed items from start to end. |
| 626 | /// |
| 627 | /// # Examples |
| 628 | /// |
| 629 | /// ``` |
| 630 | /// use std::collections::RingBuf; |
| 631 | /// |
| 632 | /// let mut v = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 633 | /// v.push_back(1); |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 634 | /// assert_eq!(v.drain().next(), Some(1)); |
| 635 | /// assert!(v.is_empty()); |
| 636 | /// ``` |
| 637 | #[inline] |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 638 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 639 | reason = "matches collection reform specification, waiting for dust to settle")] |
Alexis Beingessner | 8dbaa71 | 2014-12-31 00:07:53 | [diff] [blame] | 640 | pub fn drain(&mut self) -> Drain<T> { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 641 | Drain { |
| 642 | inner: self, |
| 643 | } |
| 644 | } |
| 645 | |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 646 | /// Clears the buffer, removing all values. |
| 647 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 648 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 649 | /// |
| 650 | /// ``` |
| 651 | /// use std::collections::RingBuf; |
| 652 | /// |
| 653 | /// let mut v = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 654 | /// v.push_back(1); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 655 | /// v.clear(); |
| 656 | /// assert!(v.is_empty()); |
| 657 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 658 | #[stable(feature = "rust1", since = "1.0.0")] |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 659 | #[inline] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 660 | pub fn clear(&mut self) { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 661 | self.drain(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /// Provides a reference to the front element, or `None` if the sequence is |
| 665 | /// empty. |
| 666 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 667 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 668 | /// |
| 669 | /// ``` |
| 670 | /// use std::collections::RingBuf; |
| 671 | /// |
| 672 | /// let mut d = RingBuf::new(); |
| 673 | /// assert_eq!(d.front(), None); |
| 674 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 675 | /// d.push_back(1); |
| 676 | /// d.push_back(2); |
| 677 | /// assert_eq!(d.front(), Some(&1)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 678 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 679 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 680 | pub fn front(&self) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 681 | if !self.is_empty() { Some(&self[0]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /// Provides a mutable reference to the front element, or `None` if the |
| 685 | /// sequence is empty. |
| 686 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 687 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 688 | /// |
| 689 | /// ``` |
| 690 | /// use std::collections::RingBuf; |
| 691 | /// |
| 692 | /// let mut d = RingBuf::new(); |
| 693 | /// assert_eq!(d.front_mut(), None); |
| 694 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 695 | /// d.push_back(1); |
| 696 | /// d.push_back(2); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 697 | /// match d.front_mut() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 698 | /// Some(x) => *x = 9, |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 699 | /// None => (), |
| 700 | /// } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 701 | /// assert_eq!(d.front(), Some(&9)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 702 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 703 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 704 | pub fn front_mut(&mut self) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 705 | if !self.is_empty() { Some(&mut self[0]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /// Provides a reference to the back element, or `None` if the sequence is |
| 709 | /// empty. |
| 710 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 711 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 712 | /// |
| 713 | /// ``` |
| 714 | /// use std::collections::RingBuf; |
| 715 | /// |
| 716 | /// let mut d = RingBuf::new(); |
| 717 | /// assert_eq!(d.back(), None); |
| 718 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 719 | /// d.push_back(1); |
| 720 | /// d.push_back(2); |
| 721 | /// assert_eq!(d.back(), Some(&2)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 722 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 723 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 724 | pub fn back(&self) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 725 | if !self.is_empty() { Some(&self[self.len() - 1]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /// Provides a mutable reference to the back element, or `None` if the |
| 729 | /// sequence is empty. |
| 730 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 731 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 732 | /// |
| 733 | /// ``` |
| 734 | /// use std::collections::RingBuf; |
| 735 | /// |
| 736 | /// let mut d = RingBuf::new(); |
| 737 | /// assert_eq!(d.back(), None); |
| 738 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 739 | /// d.push_back(1); |
| 740 | /// d.push_back(2); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 741 | /// match d.back_mut() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 742 | /// Some(x) => *x = 9, |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 743 | /// None => (), |
| 744 | /// } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 745 | /// assert_eq!(d.back(), Some(&9)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 746 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 747 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 748 | pub fn back_mut(&mut self) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 749 | let len = self.len(); |
| 750 | if !self.is_empty() { Some(&mut self[len - 1]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /// Removes the first element and returns it, or `None` if the sequence is |
| 754 | /// empty. |
| 755 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 756 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 757 | /// |
| 758 | /// ``` |
| 759 | /// use std::collections::RingBuf; |
| 760 | /// |
| 761 | /// let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 762 | /// d.push_back(1); |
| 763 | /// d.push_back(2); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 764 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 765 | /// assert_eq!(d.pop_front(), Some(1)); |
| 766 | /// assert_eq!(d.pop_front(), Some(2)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 767 | /// assert_eq!(d.pop_front(), None); |
| 768 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 769 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 770 | pub fn pop_front(&mut self) -> Option<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 771 | if self.is_empty() { |
| 772 | None |
| 773 | } else { |
| 774 | let tail = self.tail; |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 775 | self.tail = self.wrap_index(self.tail + 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 776 | unsafe { Some(self.buffer_read(tail)) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 777 | } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /// Inserts an element first in the sequence. |
| 781 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 782 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 783 | /// |
| 784 | /// ``` |
| 785 | /// use std::collections::RingBuf; |
| 786 | /// |
| 787 | /// let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 788 | /// d.push_front(1); |
| 789 | /// d.push_front(2); |
| 790 | /// assert_eq!(d.front(), Some(&2)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 791 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 792 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 793 | pub fn push_front(&mut self, t: T) { |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 794 | if self.is_full() { |
| 795 | self.reserve(1); |
| 796 | debug_assert!(!self.is_full()); |
| 797 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 798 | |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 799 | self.tail = self.wrap_index(self.tail - 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 800 | let tail = self.tail; |
| 801 | unsafe { self.buffer_write(tail, t); } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /// Appends an element to the back of a buffer |
| 805 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 806 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 807 | /// |
| 808 | /// ```rust |
| 809 | /// use std::collections::RingBuf; |
| 810 | /// |
| 811 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 812 | /// buf.push_back(1); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 813 | /// buf.push_back(3); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 814 | /// assert_eq!(3, *buf.back().unwrap()); |
| 815 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 816 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 817 | pub fn push_back(&mut self, t: T) { |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 818 | if self.is_full() { |
| 819 | self.reserve(1); |
| 820 | debug_assert!(!self.is_full()); |
| 821 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 822 | |
| 823 | let head = self.head; |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 824 | self.head = self.wrap_index(self.head + 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 825 | unsafe { self.buffer_write(head, t) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /// Removes the last element from a buffer and returns it, or `None` if |
| 829 | /// it is empty. |
| 830 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 831 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 832 | /// |
| 833 | /// ```rust |
| 834 | /// use std::collections::RingBuf; |
| 835 | /// |
| 836 | /// let mut buf = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 837 | /// assert_eq!(buf.pop_back(), None); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 838 | /// buf.push_back(1); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 839 | /// buf.push_back(3); |
| 840 | /// assert_eq!(buf.pop_back(), Some(3)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 841 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 842 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 843 | pub fn pop_back(&mut self) -> Option<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 844 | if self.is_empty() { |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 845 | None |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 846 | } else { |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 847 | self.head = self.wrap_index(self.head - 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 848 | let head = self.head; |
| 849 | unsafe { Some(self.buffer_read(head)) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 850 | } |
| 851 | } |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 852 | |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 853 | #[inline] |
| 854 | fn is_contiguous(&self) -> bool { |
| 855 | self.tail <= self.head |
| 856 | } |
| 857 | |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 858 | /// Removes an element from anywhere in the ringbuf and returns it, replacing it with the last |
| 859 | /// element. |
| 860 | /// |
| 861 | /// This does not preserve ordering, but is O(1). |
| 862 | /// |
| 863 | /// Returns `None` if `index` is out of bounds. |
| 864 | /// |
| 865 | /// # Examples |
| 866 | /// |
| 867 | /// ``` |
| 868 | /// use std::collections::RingBuf; |
| 869 | /// |
| 870 | /// let mut buf = RingBuf::new(); |
| 871 | /// assert_eq!(buf.swap_back_remove(0), None); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 872 | /// buf.push_back(5); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 873 | /// buf.push_back(99); |
| 874 | /// buf.push_back(15); |
| 875 | /// buf.push_back(20); |
| 876 | /// buf.push_back(10); |
| 877 | /// assert_eq!(buf.swap_back_remove(1), Some(99)); |
| 878 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 879 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 880 | reason = "the naming of this function may be altered")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 881 | pub fn swap_back_remove(&mut self, index: usize) -> Option<T> { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 882 | let length = self.len(); |
| 883 | if length > 0 && index < length - 1 { |
| 884 | self.swap(index, length - 1); |
| 885 | } else if index >= length { |
| 886 | return None; |
| 887 | } |
| 888 | self.pop_back() |
| 889 | } |
| 890 | |
| 891 | /// Removes an element from anywhere in the ringbuf and returns it, replacing it with the first |
| 892 | /// element. |
| 893 | /// |
| 894 | /// This does not preserve ordering, but is O(1). |
| 895 | /// |
| 896 | /// Returns `None` if `index` is out of bounds. |
| 897 | /// |
| 898 | /// # Examples |
| 899 | /// |
| 900 | /// ``` |
| 901 | /// use std::collections::RingBuf; |
| 902 | /// |
| 903 | /// let mut buf = RingBuf::new(); |
| 904 | /// assert_eq!(buf.swap_front_remove(0), None); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 905 | /// buf.push_back(15); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 906 | /// buf.push_back(5); |
| 907 | /// buf.push_back(10); |
| 908 | /// buf.push_back(99); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 909 | /// buf.push_back(20); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 910 | /// assert_eq!(buf.swap_front_remove(3), Some(99)); |
| 911 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 912 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 913 | reason = "the naming of this function may be altered")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 914 | pub fn swap_front_remove(&mut self, index: usize) -> Option<T> { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 915 | let length = self.len(); |
| 916 | if length > 0 && index < length && index != 0 { |
| 917 | self.swap(index, 0); |
| 918 | } else if index >= length { |
| 919 | return None; |
| 920 | } |
| 921 | self.pop_front() |
| 922 | } |
| 923 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 924 | /// Inserts an element at position `i` within the ringbuf. Whichever |
| 925 | /// end is closer to the insertion point will be moved to make room, |
| 926 | /// and all the affected elements will be moved to new positions. |
| 927 | /// |
| 928 | /// # Panics |
| 929 | /// |
| 930 | /// Panics if `i` is greater than ringbuf's length |
| 931 | /// |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 932 | /// # Examples |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 933 | /// ```rust |
| 934 | /// use std::collections::RingBuf; |
| 935 | /// |
| 936 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 937 | /// buf.push_back(10); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 938 | /// buf.push_back(12); |
| 939 | /// buf.insert(1,11); |
| 940 | /// assert_eq!(Some(&11), buf.get(1)); |
| 941 | /// ``` |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 942 | pub fn insert(&mut self, i: usize, t: T) { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 943 | assert!(i <= self.len(), "index out of bounds"); |
| 944 | if self.is_full() { |
| 945 | self.reserve(1); |
| 946 | debug_assert!(!self.is_full()); |
| 947 | } |
| 948 | |
| 949 | // Move the least number of elements in the ring buffer and insert |
| 950 | // the given object |
| 951 | // |
| 952 | // At most len/2 - 1 elements will be moved. O(min(n, n-i)) |
| 953 | // |
| 954 | // There are three main cases: |
| 955 | // Elements are contiguous |
| 956 | // - special case when tail is 0 |
| 957 | // Elements are discontiguous and the insert is in the tail section |
| 958 | // Elements are discontiguous and the insert is in the head section |
| 959 | // |
| 960 | // For each of those there are two more cases: |
| 961 | // Insert is closer to tail |
| 962 | // Insert is closer to head |
| 963 | // |
| 964 | // Key: H - self.head |
| 965 | // T - self.tail |
| 966 | // o - Valid element |
| 967 | // I - Insertion element |
| 968 | // A - The element that should be after the insertion point |
| 969 | // M - Indicates element was moved |
| 970 | |
| 971 | let idx = self.wrap_index(self.tail + i); |
| 972 | |
| 973 | let distance_to_tail = i; |
| 974 | let distance_to_head = self.len() - i; |
| 975 | |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 976 | let contiguous = self.is_contiguous(); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 977 | |
| 978 | match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { |
| 979 | (true, true, _) if i == 0 => { |
| 980 | // push_front |
| 981 | // |
| 982 | // T |
| 983 | // I H |
| 984 | // [A o o o o o o . . . . . . . . .] |
| 985 | // |
| 986 | // H T |
| 987 | // [A o o o o o o o . . . . . I] |
| 988 | // |
| 989 | |
| 990 | self.tail = self.wrap_index(self.tail - 1); |
| 991 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 992 | (true, true, _) => unsafe { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 993 | // contiguous, insert closer to tail: |
| 994 | // |
| 995 | // T I H |
| 996 | // [. . . o o A o o o o . . . . . .] |
| 997 | // |
| 998 | // T H |
| 999 | // [. . o o I A o o o o . . . . . .] |
| 1000 | // M M |
| 1001 | // |
| 1002 | // contiguous, insert closer to tail and tail is 0: |
| 1003 | // |
| 1004 | // |
| 1005 | // T I H |
| 1006 | // [o o A o o o o . . . . . . . . .] |
| 1007 | // |
| 1008 | // H T |
| 1009 | // [o I A o o o o o . . . . . . . o] |
| 1010 | // M M |
| 1011 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1012 | let new_tail = self.wrap_index(self.tail - 1); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1013 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1014 | self.copy(new_tail, self.tail, 1); |
| 1015 | // Already moved the tail, so we only copy `i - 1` elements. |
| 1016 | self.copy(self.tail, self.tail + 1, i - 1); |
| 1017 | |
| 1018 | self.tail = new_tail; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1019 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1020 | (true, false, _) => unsafe { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1021 | // contiguous, insert closer to head: |
| 1022 | // |
| 1023 | // T I H |
| 1024 | // [. . . o o o o A o o . . . . . .] |
| 1025 | // |
| 1026 | // T H |
| 1027 | // [. . . o o o o I A o o . . . . .] |
| 1028 | // M M M |
| 1029 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1030 | self.copy(idx + 1, idx, self.head - idx); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1031 | self.head = self.wrap_index(self.head + 1); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1032 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1033 | (false, true, true) => unsafe { |
| 1034 | // discontiguous, insert closer to tail, tail section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1035 | // |
| 1036 | // H T I |
| 1037 | // [o o o o o o . . . . . o o A o o] |
| 1038 | // |
| 1039 | // H T |
| 1040 | // [o o o o o o . . . . o o I A o o] |
| 1041 | // M M |
| 1042 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1043 | self.copy(self.tail - 1, self.tail, i); |
| 1044 | self.tail -= 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1045 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1046 | (false, false, true) => unsafe { |
| 1047 | // discontiguous, insert closer to head, tail section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1048 | // |
| 1049 | // H T I |
| 1050 | // [o o . . . . . . . o o o o o A o] |
| 1051 | // |
| 1052 | // H T |
| 1053 | // [o o o . . . . . . o o o o o I A] |
| 1054 | // M M M M |
| 1055 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1056 | // copy elements up to new head |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1057 | self.copy(1, 0, self.head); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1058 | |
| 1059 | // copy last element into empty spot at bottom of buffer |
| 1060 | self.copy(0, self.cap - 1, 1); |
| 1061 | |
| 1062 | // move elements from idx to end forward not including ^ element |
| 1063 | self.copy(idx + 1, idx, self.cap - 1 - idx); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1064 | |
| 1065 | self.head += 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1066 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1067 | (false, true, false) if idx == 0 => unsafe { |
| 1068 | // discontiguous, insert is closer to tail, head section, |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1069 | // and is at index zero in the internal buffer: |
| 1070 | // |
| 1071 | // I H T |
| 1072 | // [A o o o o o o o o o . . . o o o] |
| 1073 | // |
| 1074 | // H T |
| 1075 | // [A o o o o o o o o o . . o o o I] |
| 1076 | // M M M |
| 1077 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1078 | // copy elements up to new tail |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1079 | self.copy(self.tail - 1, self.tail, self.cap - self.tail); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1080 | |
| 1081 | // copy last element into empty spot at bottom of buffer |
| 1082 | self.copy(self.cap - 1, 0, 1); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1083 | |
| 1084 | self.tail -= 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1085 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1086 | (false, true, false) => unsafe { |
| 1087 | // discontiguous, insert closer to tail, head section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1088 | // |
| 1089 | // I H T |
| 1090 | // [o o o A o o o o o o . . . o o o] |
| 1091 | // |
| 1092 | // H T |
| 1093 | // [o o I A o o o o o o . . o o o o] |
| 1094 | // M M M M M M |
| 1095 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1096 | // copy elements up to new tail |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1097 | self.copy(self.tail - 1, self.tail, self.cap - self.tail); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1098 | |
| 1099 | // copy last element into empty spot at bottom of buffer |
| 1100 | self.copy(self.cap - 1, 0, 1); |
| 1101 | |
| 1102 | // move elements from idx-1 to end forward not including ^ element |
| 1103 | self.copy(0, 1, idx - 1); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1104 | |
| 1105 | self.tail -= 1; |
| 1106 | }, |
| 1107 | (false, false, false) => unsafe { |
| 1108 | // discontiguous, insert closer to head, head section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1109 | // |
| 1110 | // I H T |
| 1111 | // [o o o o A o o . . . . . . o o o] |
| 1112 | // |
| 1113 | // H T |
| 1114 | // [o o o o I A o o . . . . . o o o] |
| 1115 | // M M M |
| 1116 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1117 | self.copy(idx + 1, idx, self.head - idx); |
| 1118 | self.head += 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | // tail might've been changed so we need to recalculate |
| 1123 | let new_idx = self.wrap_index(self.tail + i); |
| 1124 | unsafe { |
| 1125 | self.buffer_write(new_idx, t); |
| 1126 | } |
| 1127 | } |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1128 | |
| 1129 | /// Removes and returns the element at position `i` from the ringbuf. |
| 1130 | /// Whichever end is closer to the removal point will be moved to make |
| 1131 | /// room, and all the affected elements will be moved to new positions. |
| 1132 | /// Returns `None` if `i` is out of bounds. |
| 1133 | /// |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1134 | /// # Examples |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1135 | /// ```rust |
| 1136 | /// use std::collections::RingBuf; |
| 1137 | /// |
| 1138 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1139 | /// buf.push_back(5); |
| 1140 | /// buf.push_back(10); |
| 1141 | /// buf.push_back(12); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1142 | /// buf.push_back(15); |
| 1143 | /// buf.remove(2); |
| 1144 | /// assert_eq!(Some(&15), buf.get(2)); |
| 1145 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1146 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1147 | pub fn remove(&mut self, i: usize) -> Option<T> { |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1148 | if self.is_empty() || self.len() <= i { |
| 1149 | return None; |
| 1150 | } |
| 1151 | |
| 1152 | // There are three main cases: |
| 1153 | // Elements are contiguous |
| 1154 | // Elements are discontiguous and the removal is in the tail section |
| 1155 | // Elements are discontiguous and the removal is in the head section |
| 1156 | // - special case when elements are technically contiguous, |
| 1157 | // but self.head = 0 |
| 1158 | // |
| 1159 | // For each of those there are two more cases: |
| 1160 | // Insert is closer to tail |
| 1161 | // Insert is closer to head |
| 1162 | // |
| 1163 | // Key: H - self.head |
| 1164 | // T - self.tail |
| 1165 | // o - Valid element |
| 1166 | // x - Element marked for removal |
| 1167 | // R - Indicates element that is being removed |
| 1168 | // M - Indicates element was moved |
| 1169 | |
| 1170 | let idx = self.wrap_index(self.tail + i); |
| 1171 | |
| 1172 | let elem = unsafe { |
| 1173 | Some(self.buffer_read(idx)) |
| 1174 | }; |
| 1175 | |
| 1176 | let distance_to_tail = i; |
| 1177 | let distance_to_head = self.len() - i; |
| 1178 | |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1179 | let contiguous = self.is_contiguous(); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1180 | |
| 1181 | match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { |
| 1182 | (true, true, _) => unsafe { |
| 1183 | // contiguous, remove closer to tail: |
| 1184 | // |
| 1185 | // T R H |
| 1186 | // [. . . o o x o o o o . . . . . .] |
| 1187 | // |
| 1188 | // T H |
| 1189 | // [. . . . o o o o o o . . . . . .] |
| 1190 | // M M |
| 1191 | |
| 1192 | self.copy(self.tail + 1, self.tail, i); |
| 1193 | self.tail += 1; |
| 1194 | }, |
| 1195 | (true, false, _) => unsafe { |
| 1196 | // contiguous, remove closer to head: |
| 1197 | // |
| 1198 | // T R H |
| 1199 | // [. . . o o o o x o o . . . . . .] |
| 1200 | // |
| 1201 | // T H |
| 1202 | // [. . . o o o o o o . . . . . . .] |
| 1203 | // M M |
| 1204 | |
| 1205 | self.copy(idx, idx + 1, self.head - idx - 1); |
| 1206 | self.head -= 1; |
| 1207 | }, |
| 1208 | (false, true, true) => unsafe { |
| 1209 | // discontiguous, remove closer to tail, tail section: |
| 1210 | // |
| 1211 | // H T R |
| 1212 | // [o o o o o o . . . . . o o x o o] |
| 1213 | // |
| 1214 | // H T |
| 1215 | // [o o o o o o . . . . . . o o o o] |
| 1216 | // M M |
| 1217 | |
| 1218 | self.copy(self.tail + 1, self.tail, i); |
| 1219 | self.tail = self.wrap_index(self.tail + 1); |
| 1220 | }, |
| 1221 | (false, false, false) => unsafe { |
| 1222 | // discontiguous, remove closer to head, head section: |
| 1223 | // |
| 1224 | // R H T |
| 1225 | // [o o o o x o o . . . . . . o o o] |
| 1226 | // |
| 1227 | // H T |
| 1228 | // [o o o o o o . . . . . . . o o o] |
| 1229 | // M M |
| 1230 | |
| 1231 | self.copy(idx, idx + 1, self.head - idx - 1); |
| 1232 | self.head -= 1; |
| 1233 | }, |
| 1234 | (false, false, true) => unsafe { |
| 1235 | // discontiguous, remove closer to head, tail section: |
| 1236 | // |
| 1237 | // H T R |
| 1238 | // [o o o . . . . . . o o o o o x o] |
| 1239 | // |
| 1240 | // H T |
| 1241 | // [o o . . . . . . . o o o o o o o] |
| 1242 | // M M M M |
| 1243 | // |
| 1244 | // or quasi-discontiguous, remove next to head, tail section: |
| 1245 | // |
| 1246 | // H T R |
| 1247 | // [. . . . . . . . . o o o o o x o] |
| 1248 | // |
| 1249 | // T H |
| 1250 | // [. . . . . . . . . o o o o o o .] |
| 1251 | // M |
| 1252 | |
| 1253 | // draw in elements in the tail section |
| 1254 | self.copy(idx, idx + 1, self.cap - idx - 1); |
| 1255 | |
| 1256 | // Prevents underflow. |
| 1257 | if self.head != 0 { |
| 1258 | // copy first element into empty spot |
| 1259 | self.copy(self.cap - 1, 0, 1); |
| 1260 | |
| 1261 | // move elements in the head section backwards |
| 1262 | self.copy(0, 1, self.head - 1); |
| 1263 | } |
| 1264 | |
| 1265 | self.head = self.wrap_index(self.head - 1); |
| 1266 | }, |
| 1267 | (false, true, false) => unsafe { |
| 1268 | // discontiguous, remove closer to tail, head section: |
| 1269 | // |
| 1270 | // R H T |
| 1271 | // [o o x o o o o o o o . . . o o o] |
| 1272 | // |
| 1273 | // H T |
| 1274 | // [o o o o o o o o o o . . . . o o] |
| 1275 | // M M M M M |
| 1276 | |
| 1277 | // draw in elements up to idx |
| 1278 | self.copy(1, 0, idx); |
| 1279 | |
| 1280 | // copy last element into empty spot |
| 1281 | self.copy(0, self.cap - 1, 1); |
| 1282 | |
| 1283 | // move elements from tail to end forward, excluding the last one |
| 1284 | self.copy(self.tail + 1, self.tail, self.cap - self.tail - 1); |
| 1285 | |
| 1286 | self.tail = self.wrap_index(self.tail + 1); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | return elem; |
| 1291 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 1292 | } |
| 1293 | |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1294 | impl<T: Clone> RingBuf<T> { |
| 1295 | /// Modifies the ringbuf in-place so that `len()` is equal to new_len, |
| 1296 | /// either by removing excess elements or by appending copies of a value to the back. |
| 1297 | /// |
| 1298 | /// # Examples |
| 1299 | /// |
| 1300 | /// ``` |
| 1301 | /// use std::collections::RingBuf; |
| 1302 | /// |
| 1303 | /// let mut buf = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1304 | /// buf.push_back(5); |
| 1305 | /// buf.push_back(10); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1306 | /// buf.push_back(15); |
| 1307 | /// buf.resize(2, 0); |
| 1308 | /// buf.resize(6, 20); |
| 1309 | /// for (a, b) in [5, 10, 20, 20, 20, 20].iter().zip(buf.iter()) { |
| 1310 | /// assert_eq!(a, b); |
| 1311 | /// } |
| 1312 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 1313 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 1314 | reason = "matches collection reform specification; waiting on panic semantics")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1315 | pub fn resize(&mut self, new_len: usize, value: T) { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1316 | let len = self.len(); |
| 1317 | |
| 1318 | if new_len > len { |
| 1319 | self.extend(repeat(value).take(new_len - len)) |
| 1320 | } else { |
| 1321 | self.truncate(new_len); |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1326 | /// Returns the index in the underlying buffer for a given logical element index. |
| 1327 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1328 | fn wrap_index(index: usize, size: usize) -> usize { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1329 | // size is always a power of 2 |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 1330 | index & (size - 1) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | /// Calculate the number of elements left to be read in the buffer |
| 1334 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1335 | fn count(tail: usize, head: usize, size: usize) -> usize { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1336 | // size is always a power of 2 |
| 1337 | (head - tail) & (size - 1) |
| 1338 | } |
| 1339 | |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 1340 | /// `RingBuf` iterator. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1341 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 1342 | pub struct Iter<'a, T:'a> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1343 | ring: &'a [T], |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1344 | tail: usize, |
| 1345 | head: usize |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 1346 | } |
| 1347 | |
Jorge Aparicio | 351409a | 2015-01-04 03:54:18 | [diff] [blame] | 1348 | // FIXME(#19839) Remove in favor of `#[derive(Clone)]` |
Huon Wilson | b7832ed | 2014-12-30 10:01:36 | [diff] [blame] | 1349 | impl<'a, T> Clone for Iter<'a, T> { |
| 1350 | fn clone(&self) -> Iter<'a, T> { |
| 1351 | Iter { |
| 1352 | ring: self.ring, |
| 1353 | tail: self.tail, |
| 1354 | head: self.head |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1359 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1360 | impl<'a, T> Iterator for Iter<'a, T> { |
| 1361 | type Item = &'a T; |
| 1362 | |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1363 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 1364 | fn next(&mut self) -> Option<&'a T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1365 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1366 | return None; |
| 1367 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1368 | let tail = self.tail; |
| 1369 | self.tail = wrap_index(self.tail + 1, self.ring.len()); |
Aaron Turon | 6abfac0 | 2014-12-30 18:51:18 | [diff] [blame] | 1370 | unsafe { Some(self.ring.get_unchecked(tail)) } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1374 | fn size_hint(&self) -> (usize, Option<usize>) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1375 | let len = count(self.tail, self.head, self.ring.len()); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1376 | (len, Some(len)) |
| 1377 | } |
| 1378 | } |
| 1379 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1380 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1381 | impl<'a, T> DoubleEndedIterator for Iter<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1382 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 1383 | fn next_back(&mut self) -> Option<&'a T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1384 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1385 | return None; |
| 1386 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1387 | self.head = wrap_index(self.head - 1, self.ring.len()); |
Aaron Turon | 6abfac0 | 2014-12-30 18:51:18 | [diff] [blame] | 1388 | unsafe { Some(self.ring.get_unchecked(self.head)) } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1389 | } |
| 1390 | } |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 1391 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1392 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1393 | impl<'a, T> ExactSizeIterator for Iter<'a, T> {} |
blake2-ppc | 7c369ee7 | 2013-09-01 16:20:24 | [diff] [blame] | 1394 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1395 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1396 | impl<'a, T> RandomAccessIterator for Iter<'a, T> { |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1397 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1398 | fn indexable(&self) -> usize { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1399 | let (len, _) = self.size_hint(); |
| 1400 | len |
| 1401 | } |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1402 | |
| 1403 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1404 | fn idx(&mut self, j: usize) -> Option<&'a T> { |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1405 | if j >= self.indexable() { |
| 1406 | None |
| 1407 | } else { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1408 | let idx = wrap_index(self.tail + j, self.ring.len()); |
Aaron Turon | 6abfac0 | 2014-12-30 18:51:18 | [diff] [blame] | 1409 | unsafe { Some(self.ring.get_unchecked(idx)) } |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 1414 | // FIXME This was implemented differently from Iter because of a problem |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1415 | // with returning the mutable reference. I couldn't find a way to |
| 1416 | // make the lifetime checker happy so, but there should be a way. |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 1417 | /// `RingBuf` mutable iterator. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1418 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 1419 | pub struct IterMut<'a, T:'a> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1420 | ptr: *mut T, |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1421 | tail: usize, |
| 1422 | head: usize, |
| 1423 | cap: usize, |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1424 | marker: marker::ContravariantLifetime<'a>, |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 1425 | } |
| 1426 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1427 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1428 | impl<'a, T> Iterator for IterMut<'a, T> { |
| 1429 | type Item = &'a mut T; |
| 1430 | |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1431 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 1432 | fn next(&mut self) -> Option<&'a mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1433 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1434 | return None; |
| 1435 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1436 | let tail = self.tail; |
| 1437 | self.tail = wrap_index(self.tail + 1, self.cap); |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1438 | |
| 1439 | unsafe { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1440 | Some(&mut *self.ptr.offset(tail as isize)) |
Alex Crichton | 9d5d97b | 2014-10-15 06:05:01 | [diff] [blame] | 1441 | } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1445 | fn size_hint(&self) -> (usize, Option<usize>) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1446 | let len = count(self.tail, self.head, self.cap); |
| 1447 | (len, Some(len)) |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1451 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1452 | impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1453 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 1454 | fn next_back(&mut self) -> Option<&'a mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1455 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1456 | return None; |
| 1457 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1458 | self.head = wrap_index(self.head - 1, self.cap); |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1459 | |
| 1460 | unsafe { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1461 | Some(&mut *self.ptr.offset(self.head as isize)) |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1462 | } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1463 | } |
| 1464 | } |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 1465 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1466 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1467 | impl<'a, T> ExactSizeIterator for IterMut<'a, T> {} |
blake2-ppc | 7c369ee7 | 2013-09-01 16:20:24 | [diff] [blame] | 1468 | |
Alexis Beingessner | 8dbaa71 | 2014-12-31 00:07:53 | [diff] [blame] | 1469 | /// A by-value RingBuf iterator |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1470 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 1471 | pub struct IntoIter<T> { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1472 | inner: RingBuf<T>, |
| 1473 | } |
| 1474 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1475 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1476 | impl<T> Iterator for IntoIter<T> { |
| 1477 | type Item = T; |
| 1478 | |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1479 | #[inline] |
| 1480 | fn next(&mut self) -> Option<T> { |
| 1481 | self.inner.pop_front() |
| 1482 | } |
| 1483 | |
| 1484 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1485 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1486 | let len = self.inner.len(); |
| 1487 | (len, Some(len)) |
| 1488 | } |
| 1489 | } |
| 1490 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1491 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1492 | impl<T> DoubleEndedIterator for IntoIter<T> { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1493 | #[inline] |
| 1494 | fn next_back(&mut self) -> Option<T> { |
| 1495 | self.inner.pop_back() |
| 1496 | } |
| 1497 | } |
| 1498 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1499 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1500 | impl<T> ExactSizeIterator for IntoIter<T> {} |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1501 | |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1502 | /// A draining RingBuf iterator |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 1503 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 1504 | reason = "matches collection reform specification, waiting for dust to settle")] |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1505 | pub struct Drain<'a, T: 'a> { |
| 1506 | inner: &'a mut RingBuf<T>, |
| 1507 | } |
| 1508 | |
| 1509 | #[unsafe_destructor] |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1510 | #[stable(feature = "rust1", since = "1.0.0")] |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1511 | impl<'a, T: 'a> Drop for Drain<'a, T> { |
| 1512 | fn drop(&mut self) { |
Jorge Aparicio | f9865ea | 2015-01-11 02:50:07 | [diff] [blame] | 1513 | for _ in self.by_ref() {} |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1514 | self.inner.head = 0; |
| 1515 | self.inner.tail = 0; |
| 1516 | } |
| 1517 | } |
| 1518 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1519 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1520 | impl<'a, T: 'a> Iterator for Drain<'a, T> { |
| 1521 | type Item = T; |
| 1522 | |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1523 | #[inline] |
| 1524 | fn next(&mut self) -> Option<T> { |
| 1525 | self.inner.pop_front() |
| 1526 | } |
| 1527 | |
| 1528 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1529 | fn size_hint(&self) -> (usize, Option<usize>) { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1530 | let len = self.inner.len(); |
| 1531 | (len, Some(len)) |
| 1532 | } |
| 1533 | } |
| 1534 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1535 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1536 | impl<'a, T: 'a> DoubleEndedIterator for Drain<'a, T> { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1537 | #[inline] |
| 1538 | fn next_back(&mut self) -> Option<T> { |
| 1539 | self.inner.pop_back() |
| 1540 | } |
| 1541 | } |
| 1542 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1543 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1544 | impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {} |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1545 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1546 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 748bc3c | 2014-05-30 00:45:07 | [diff] [blame] | 1547 | impl<A: PartialEq> PartialEq for RingBuf<A> { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1548 | fn eq(&self, other: &RingBuf<A>) -> bool { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1549 | self.len() == other.len() && |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1550 | self.iter().zip(other.iter()).all(|(a, b)| a.eq(b)) |
| 1551 | } |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1552 | } |
| 1553 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1554 | #[stable(feature = "rust1", since = "1.0.0")] |
nham | 25acfde | 2014-08-01 20:05:03 | [diff] [blame] | 1555 | impl<A: Eq> Eq for RingBuf<A> {} |
| 1556 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1557 | #[stable(feature = "rust1", since = "1.0.0")] |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 1558 | impl<A: PartialOrd> PartialOrd for RingBuf<A> { |
| 1559 | fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> { |
| 1560 | iter::order::partial_cmp(self.iter(), other.iter()) |
| 1561 | } |
| 1562 | } |
| 1563 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1564 | #[stable(feature = "rust1", since = "1.0.0")] |
nham | 3737c53 | 2014-08-01 20:22:48 | [diff] [blame] | 1565 | impl<A: Ord> Ord for RingBuf<A> { |
| 1566 | #[inline] |
| 1567 | fn cmp(&self, other: &RingBuf<A>) -> Ordering { |
| 1568 | iter::order::cmp(self.iter(), other.iter()) |
| 1569 | } |
| 1570 | } |
| 1571 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1572 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 511f0b8 | 2014-12-09 20:37:23 | [diff] [blame] | 1573 | impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for RingBuf<A> { |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 1574 | fn hash(&self, state: &mut S) { |
nham | 9fa4424 | 2014-07-27 16:37:32 | [diff] [blame] | 1575 | self.len().hash(state); |
Jorge Aparicio | d5d7e65 | 2015-01-31 17:20:46 | [diff] [blame] | 1576 | for elt in self { |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 1577 | elt.hash(state); |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1582 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1583 | impl<A> Index<usize> for RingBuf<A> { |
Jorge Aparicio | 32dd592 | 2015-01-03 15:40:10 | [diff] [blame] | 1584 | type Output = A; |
| 1585 | |
| 1586 | #[inline] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 1587 | fn index(&self, i: &usize) -> &A { |
Jorge Aparicio | 32dd592 | 2015-01-03 15:40:10 | [diff] [blame] | 1588 | self.get(*i).expect("Out of bounds access") |
| 1589 | } |
| 1590 | } |
| 1591 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1592 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1593 | impl<A> IndexMut<usize> for RingBuf<A> { |
Jorge Aparicio | 32dd592 | 2015-01-03 15:40:10 | [diff] [blame] | 1594 | type Output = A; |
| 1595 | |
| 1596 | #[inline] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 1597 | fn index_mut(&mut self, i: &usize) -> &mut A { |
Jorge Aparicio | 32dd592 | 2015-01-03 15:40:10 | [diff] [blame] | 1598 | self.get_mut(*i).expect("Out of bounds access") |
| 1599 | } |
| 1600 | } |
| 1601 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1602 | #[stable(feature = "rust1", since = "1.0.0")] |
Huon Wilson | 53487a0 | 2013-08-13 13:08:14 | [diff] [blame] | 1603 | impl<A> FromIterator<A> for RingBuf<A> { |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1604 | fn from_iter<T: Iterator<Item=A>>(iterator: T) -> RingBuf<A> { |
blake2-ppc | f8ae526 | 2013-07-30 00:06:49 | [diff] [blame] | 1605 | let (lower, _) = iterator.size_hint(); |
| 1606 | let mut deq = RingBuf::with_capacity(lower); |
| 1607 | deq.extend(iterator); |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 1608 | deq |
| 1609 | } |
| 1610 | } |
| 1611 | |
Jorge Aparicio | a65d3f5 | 2015-01-08 03:01:05 | [diff] [blame] | 1612 | impl<T> IntoIterator for RingBuf<T> { |
| 1613 | type Iter = IntoIter<T>; |
| 1614 | |
| 1615 | fn into_iter(self) -> IntoIter<T> { |
| 1616 | self.into_iter() |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | impl<'a, T> IntoIterator for &'a RingBuf<T> { |
| 1621 | type Iter = Iter<'a, T>; |
| 1622 | |
| 1623 | fn into_iter(self) -> Iter<'a, T> { |
| 1624 | self.iter() |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | impl<'a, T> IntoIterator for &'a mut RingBuf<T> { |
| 1629 | type Iter = IterMut<'a, T>; |
| 1630 | |
| 1631 | fn into_iter(mut self) -> IterMut<'a, T> { |
| 1632 | self.iter_mut() |
| 1633 | } |
| 1634 | } |
| 1635 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1636 | #[stable(feature = "rust1", since = "1.0.0")] |
gamazeps | 16c8cd9 | 2014-11-08 00:39:39 | [diff] [blame] | 1637 | impl<A> Extend<A> for RingBuf<A> { |
Jorge Aparicio | 3484706 | 2015-01-31 14:17:50 | [diff] [blame] | 1638 | fn extend<T: Iterator<Item=A>>(&mut self, iterator: T) { |
Marvin Löbel | 6200e76 | 2014-03-20 13:12:56 | [diff] [blame] | 1639 | for elt in iterator { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1640 | self.push_back(elt); |
blake2-ppc | f8ae526 | 2013-07-30 00:06:49 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1645 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 3cb9fa2 | 2015-01-20 23:45:07 | [diff] [blame] | 1646 | impl<T: fmt::Debug> fmt::Debug for RingBuf<T> { |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1647 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Sean McArthur | 44440e5 | 2014-12-20 08:09:35 | [diff] [blame] | 1648 | try!(write!(f, "RingBuf [")); |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1649 | |
| 1650 | for (i, e) in self.iter().enumerate() { |
| 1651 | if i != 0 { try!(write!(f, ", ")); } |
Sean McArthur | 44440e5 | 2014-12-20 08:09:35 | [diff] [blame] | 1652 | try!(write!(f, "{:?}", *e)); |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | write!(f, "]") |
| 1656 | } |
| 1657 | } |
| 1658 | |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1659 | #[cfg(test)] |
| 1660 | mod tests { |
Steven Fackler | 3dcd215 | 2014-11-06 08:05:53 | [diff] [blame] | 1661 | use self::Taggy::*; |
| 1662 | use self::Taggypar::*; |
Eduard Burtescu | b45d30d | 2014-12-19 12:02:22 | [diff] [blame] | 1663 | use prelude::*; |
Eduard Burtescu | b45d30d | 2014-12-19 12:02:22 | [diff] [blame] | 1664 | use core::iter; |
Alex Crichton | 3cb9fa2 | 2015-01-20 23:45:07 | [diff] [blame] | 1665 | use std::fmt::Debug; |
Alex Crichton | 511f0b8 | 2014-12-09 20:37:23 | [diff] [blame] | 1666 | use std::hash::{self, SipHasher}; |
Alex Crichton | 760b93a | 2014-05-30 02:03:06 | [diff] [blame] | 1667 | use test::Bencher; |
| 1668 | use test; |
| 1669 | |
Alex Crichton | f47e4b2 | 2014-01-07 06:33:50 | [diff] [blame] | 1670 | use super::RingBuf; |
Patrick Walton | fa5ee93 | 2012-12-28 02:24:18 | [diff] [blame] | 1671 | |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1672 | #[test] |
Victor Berger | 52ea83d | 2014-09-22 17:30:06 | [diff] [blame] | 1673 | #[allow(deprecated)] |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1674 | fn test_simple() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1675 | let mut d = RingBuf::new(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1676 | assert_eq!(d.len(), 0); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1677 | d.push_front(17); |
| 1678 | d.push_front(42); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1679 | d.push_back(137); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1680 | assert_eq!(d.len(), 3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1681 | d.push_back(137); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1682 | assert_eq!(d.len(), 4); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1683 | assert_eq!(*d.front().unwrap(), 42); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1684 | assert_eq!(*d.back().unwrap(), 137); |
| 1685 | let mut i = d.pop_front(); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1686 | assert_eq!(i, Some(42)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1687 | i = d.pop_back(); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1688 | assert_eq!(i, Some(137)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1689 | i = d.pop_back(); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1690 | assert_eq!(i, Some(137)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1691 | i = d.pop_back(); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1692 | assert_eq!(i, Some(17)); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1693 | assert_eq!(d.len(), 0); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1694 | d.push_back(3); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1695 | assert_eq!(d.len(), 1); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1696 | d.push_front(2); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1697 | assert_eq!(d.len(), 2); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1698 | d.push_back(4); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1699 | assert_eq!(d.len(), 3); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1700 | d.push_front(1); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1701 | assert_eq!(d.len(), 4); |
Alex Crichton | 9d5d97b | 2014-10-15 06:05:01 | [diff] [blame] | 1702 | debug!("{}", d[0]); |
| 1703 | debug!("{}", d[1]); |
| 1704 | debug!("{}", d[2]); |
| 1705 | debug!("{}", d[3]); |
| 1706 | assert_eq!(d[0], 1); |
| 1707 | assert_eq!(d[1], 2); |
| 1708 | assert_eq!(d[2], 3); |
| 1709 | assert_eq!(d[3], 4); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1710 | } |
| 1711 | |
Felix S. Klock II | a636f51 | 2013-05-01 23:32:37 | [diff] [blame] | 1712 | #[cfg(test)] |
Alex Crichton | 3cb9fa2 | 2015-01-20 23:45:07 | [diff] [blame] | 1713 | fn test_parameterized<T:Clone + PartialEq + Debug>(a: T, b: T, c: T, d: T) { |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 1714 | let mut deq = RingBuf::new(); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1715 | assert_eq!(deq.len(), 0); |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 1716 | deq.push_front(a.clone()); |
| 1717 | deq.push_front(b.clone()); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1718 | deq.push_back(c.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1719 | assert_eq!(deq.len(), 3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1720 | deq.push_back(d.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1721 | assert_eq!(deq.len(), 4); |
Marvin Löbel | 0ac7a21 | 2013-08-03 23:59:24 | [diff] [blame] | 1722 | assert_eq!((*deq.front().unwrap()).clone(), b.clone()); |
| 1723 | assert_eq!((*deq.back().unwrap()).clone(), d.clone()); |
| 1724 | assert_eq!(deq.pop_front().unwrap(), b.clone()); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1725 | assert_eq!(deq.pop_back().unwrap(), d.clone()); |
| 1726 | assert_eq!(deq.pop_back().unwrap(), c.clone()); |
| 1727 | assert_eq!(deq.pop_back().unwrap(), a.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1728 | assert_eq!(deq.len(), 0); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1729 | deq.push_back(c.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1730 | assert_eq!(deq.len(), 1); |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 1731 | deq.push_front(b.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1732 | assert_eq!(deq.len(), 2); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1733 | deq.push_back(d.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1734 | assert_eq!(deq.len(), 3); |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 1735 | deq.push_front(a.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 1736 | assert_eq!(deq.len(), 4); |
NODA, Kai | f27ad3d | 2014-10-05 10:11:17 | [diff] [blame] | 1737 | assert_eq!(deq[0].clone(), a.clone()); |
| 1738 | assert_eq!(deq[1].clone(), b.clone()); |
| 1739 | assert_eq!(deq[2].clone(), c.clone()); |
| 1740 | assert_eq!(deq[3].clone(), d.clone()); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1741 | } |
| 1742 | |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1743 | #[test] |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1744 | fn test_push_front_grow() { |
| 1745 | let mut deq = RingBuf::new(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1746 | for i in 0..66 { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1747 | deq.push_front(i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1748 | } |
| 1749 | assert_eq!(deq.len(), 66); |
| 1750 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1751 | for i in 0..66 { |
NODA, Kai | f27ad3d | 2014-10-05 10:11:17 | [diff] [blame] | 1752 | assert_eq!(deq[i], 65 - i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1753 | } |
| 1754 | |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1755 | let mut deq = RingBuf::new(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1756 | for i in 0..66 { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1757 | deq.push_back(i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1758 | } |
| 1759 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1760 | for i in 0..66 { |
NODA, Kai | f27ad3d | 2014-10-05 10:11:17 | [diff] [blame] | 1761 | assert_eq!(deq[i], i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 1765 | #[test] |
| 1766 | fn test_index() { |
| 1767 | let mut deq = RingBuf::new(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1768 | for i in 1..4 { |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 1769 | deq.push_front(i); |
| 1770 | } |
| 1771 | assert_eq!(deq[1], 2); |
| 1772 | } |
| 1773 | |
| 1774 | #[test] |
| 1775 | #[should_fail] |
| 1776 | fn test_index_out_of_bounds() { |
| 1777 | let mut deq = RingBuf::new(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1778 | for i in 1..4 { |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 1779 | deq.push_front(i); |
| 1780 | } |
| 1781 | deq[3]; |
| 1782 | } |
| 1783 | |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1784 | #[bench] |
Liigo Zhuang | 408f484 | 2014-04-01 01:16:35 | [diff] [blame] | 1785 | fn bench_new(b: &mut test::Bencher) { |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1786 | b.iter(|| { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1787 | let ring: RingBuf<i32> = RingBuf::new(); |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1788 | test::black_box(ring); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1789 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | #[bench] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1793 | fn bench_push_back_100(b: &mut test::Bencher) { |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1794 | let mut deq = RingBuf::with_capacity(101); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1795 | b.iter(|| { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1796 | for i in 0..100 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1797 | deq.push_back(i); |
| 1798 | } |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1799 | deq.head = 0; |
| 1800 | deq.tail = 0; |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1801 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | #[bench] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1805 | fn bench_push_front_100(b: &mut test::Bencher) { |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1806 | let mut deq = RingBuf::with_capacity(101); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1807 | b.iter(|| { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1808 | for i in 0..100 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1809 | deq.push_front(i); |
| 1810 | } |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1811 | deq.head = 0; |
| 1812 | deq.tail = 0; |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1813 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | #[bench] |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1817 | fn bench_pop_back_100(b: &mut test::Bencher) { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1818 | let mut deq= RingBuf::<i32>::with_capacity(101); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1819 | |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1820 | b.iter(|| { |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1821 | deq.head = 100; |
| 1822 | deq.tail = 0; |
| 1823 | while !deq.is_empty() { |
| 1824 | test::black_box(deq.pop_back()); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1825 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1826 | }) |
| 1827 | } |
| 1828 | |
| 1829 | #[bench] |
| 1830 | fn bench_pop_front_100(b: &mut test::Bencher) { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1831 | let mut deq = RingBuf::<i32>::with_capacity(101); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1832 | |
| 1833 | b.iter(|| { |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1834 | deq.head = 100; |
| 1835 | deq.tail = 0; |
| 1836 | while !deq.is_empty() { |
| 1837 | test::black_box(deq.pop_front()); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1838 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1839 | }) |
| 1840 | } |
| 1841 | |
| 1842 | #[bench] |
| 1843 | fn bench_grow_1025(b: &mut test::Bencher) { |
| 1844 | b.iter(|| { |
| 1845 | let mut deq = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1846 | for i in 0..1025 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1847 | deq.push_front(i); |
Brendan Zabarauskas | 729060d | 2014-01-30 00:20:34 | [diff] [blame] | 1848 | } |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1849 | test::black_box(deq); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1850 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1851 | } |
| 1852 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1853 | #[bench] |
| 1854 | fn bench_iter_1000(b: &mut test::Bencher) { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1855 | let ring: RingBuf<_> = (0..1000).collect(); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1856 | |
| 1857 | b.iter(|| { |
| 1858 | let mut sum = 0; |
Jorge Aparicio | d5d7e65 | 2015-01-31 17:20:46 | [diff] [blame] | 1859 | for &i in &ring { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1860 | sum += i; |
| 1861 | } |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1862 | test::black_box(sum); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1863 | }) |
| 1864 | } |
| 1865 | |
| 1866 | #[bench] |
| 1867 | fn bench_mut_iter_1000(b: &mut test::Bencher) { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1868 | let mut ring: RingBuf<_> = (0..1000).collect(); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1869 | |
| 1870 | b.iter(|| { |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1871 | let mut sum = 0; |
Jorge Aparicio | d5f61b4 | 2015-02-01 01:02:00 | [diff] [blame] | 1872 | for i in &mut ring { |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1873 | sum += *i; |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1874 | } |
Colin Sherratt | 5e549d8 | 2014-11-11 02:57:52 | [diff] [blame] | 1875 | test::black_box(sum); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1876 | }) |
| 1877 | } |
| 1878 | |
Jorge Aparicio | 788181d | 2015-01-28 13:34:18 | [diff] [blame] | 1879 | #[derive(Clone, PartialEq, Debug)] |
Patrick Walton | 99b33f7 | 2013-07-02 19:47:32 | [diff] [blame] | 1880 | enum Taggy { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1881 | One(i32), |
| 1882 | Two(i32, i32), |
| 1883 | Three(i32, i32, i32), |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1884 | } |
| 1885 | |
Jorge Aparicio | 788181d | 2015-01-28 13:34:18 | [diff] [blame] | 1886 | #[derive(Clone, PartialEq, Debug)] |
Patrick Walton | 99b33f7 | 2013-07-02 19:47:32 | [diff] [blame] | 1887 | enum Taggypar<T> { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1888 | Onepar(i32), |
| 1889 | Twopar(i32, i32), |
| 1890 | Threepar(i32, i32, i32), |
Patrick Walton | 99b33f7 | 2013-07-02 19:47:32 | [diff] [blame] | 1891 | } |
| 1892 | |
Jorge Aparicio | 788181d | 2015-01-28 13:34:18 | [diff] [blame] | 1893 | #[derive(Clone, PartialEq, Debug)] |
Erick Tryzelaar | e84576b | 2013-01-22 16:44:24 | [diff] [blame] | 1894 | struct RecCy { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1895 | x: i32, |
| 1896 | y: i32, |
Patrick Walton | eb4d39e | 2013-01-26 00:57:39 | [diff] [blame] | 1897 | t: Taggy |
Patrick Walton | 9117dcb | 2012-09-20 01:00:26 | [diff] [blame] | 1898 | } |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1899 | |
| 1900 | #[test] |
| 1901 | fn test_param_int() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1902 | test_parameterized::<i32>(5, 72, 64, 175); |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | #[test] |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1906 | fn test_param_taggy() { |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 1907 | test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42)); |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | #[test] |
| 1911 | fn test_param_taggypar() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1912 | test_parameterized::<Taggypar<i32>>(Onepar::<i32>(1), |
| 1913 | Twopar::<i32>(1, 2), |
| 1914 | Threepar::<i32>(1, 2, 3), |
| 1915 | Twopar::<i32>(17, 42)); |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1916 | } |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1917 | |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1918 | #[test] |
| 1919 | fn test_param_reccy() { |
Erick Tryzelaar | e84576b | 2013-01-22 16:44:24 | [diff] [blame] | 1920 | let reccy1 = RecCy { x: 1, y: 2, t: One(1) }; |
| 1921 | let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) }; |
| 1922 | let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) }; |
| 1923 | let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) }; |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1924 | test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1925 | } |
Erick Tryzelaar | 909d8f0 | 2013-03-30 01:02:44 | [diff] [blame] | 1926 | |
| 1927 | #[test] |
blake2-ppc | 0ff5c17 | 2013-07-06 03:42:45 | [diff] [blame] | 1928 | fn test_with_capacity() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1929 | let mut d = RingBuf::with_capacity(0); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1930 | d.push_back(1); |
blake2-ppc | 0ff5c17 | 2013-07-06 03:42:45 | [diff] [blame] | 1931 | assert_eq!(d.len(), 1); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1932 | let mut d = RingBuf::with_capacity(50); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1933 | d.push_back(1); |
blake2-ppc | 0ff5c17 | 2013-07-06 03:42:45 | [diff] [blame] | 1934 | assert_eq!(d.len(), 1); |
| 1935 | } |
| 1936 | |
| 1937 | #[test] |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1938 | fn test_with_capacity_non_power_two() { |
| 1939 | let mut d3 = RingBuf::with_capacity(3); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1940 | d3.push_back(1); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1941 | |
| 1942 | // X = None, | = lo |
| 1943 | // [|1, X, X] |
| 1944 | assert_eq!(d3.pop_front(), Some(1)); |
| 1945 | // [X, |X, X] |
| 1946 | assert_eq!(d3.front(), None); |
| 1947 | |
| 1948 | // [X, |3, X] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1949 | d3.push_back(3); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1950 | // [X, |3, 6] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1951 | d3.push_back(6); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1952 | // [X, X, |6] |
| 1953 | assert_eq!(d3.pop_front(), Some(3)); |
| 1954 | |
| 1955 | // Pushing the lo past half way point to trigger |
| 1956 | // the 'B' scenario for growth |
| 1957 | // [9, X, |6] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1958 | d3.push_back(9); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1959 | // [9, 12, |6] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1960 | d3.push_back(12); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1961 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1962 | d3.push_back(15); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1963 | // There used to be a bug here about how the |
| 1964 | // RingBuf made growth assumptions about the |
| 1965 | // underlying Vec which didn't hold and lead |
| 1966 | // to corruption. |
| 1967 | // (Vec grows to next power of two) |
| 1968 | //good- [9, 12, 15, X, X, X, X, |6] |
| 1969 | //bug- [15, 12, X, X, X, |6, X, X] |
| 1970 | assert_eq!(d3.pop_front(), Some(6)); |
| 1971 | |
| 1972 | // Which leads us to the following state which |
| 1973 | // would be a failure case. |
| 1974 | //bug- [15, 12, X, X, X, X, |X, X] |
| 1975 | assert_eq!(d3.front(), Some(&9)); |
| 1976 | } |
| 1977 | |
| 1978 | #[test] |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1979 | fn test_reserve_exact() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1980 | let mut d = RingBuf::new(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1981 | d.push_back(0); |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1982 | d.reserve_exact(50); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1983 | assert!(d.capacity() >= 51); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | #[test] |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1987 | fn test_reserve() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1988 | let mut d = RingBuf::new(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1989 | d.push_back(0); |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1990 | d.reserve(50); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1991 | assert!(d.capacity() >= 51); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 1992 | } |
| 1993 | |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1994 | #[test] |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 1995 | fn test_swap() { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1996 | let mut d: RingBuf<_> = (0..5).collect(); |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 1997 | d.pop_front(); |
| 1998 | d.swap(0, 3); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1999 | assert_eq!(d.iter().cloned().collect::<Vec<_>>(), vec!(4, 2, 3, 1)); |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | #[test] |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 2003 | fn test_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2004 | let mut d = RingBuf::new(); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2005 | assert_eq!(d.iter().next(), None); |
blake2-ppc | 9ccf443 | 2013-07-14 20:30:22 | [diff] [blame] | 2006 | assert_eq!(d.iter().size_hint(), (0, Some(0))); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2007 | |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2008 | for i in 0..5 { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2009 | d.push_back(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 2010 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 2011 | { |
| 2012 | let b: &[_] = &[&0,&1,&2,&3,&4]; |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2013 | assert_eq!(d.iter().collect::<Vec<_>>(), b); |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 2014 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 2015 | |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2016 | for i in 6..9 { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2017 | d.push_front(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 2018 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 2019 | { |
| 2020 | let b: &[_] = &[&8,&7,&6,&0,&1,&2,&3,&4]; |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2021 | assert_eq!(d.iter().collect::<Vec<_>>(), b); |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 2022 | } |
blake2-ppc | 9ccf443 | 2013-07-14 20:30:22 | [diff] [blame] | 2023 | |
| 2024 | let mut it = d.iter(); |
| 2025 | let mut len = d.len(); |
| 2026 | loop { |
| 2027 | match it.next() { |
| 2028 | None => break, |
| 2029 | _ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) } |
| 2030 | } |
| 2031 | } |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | #[test] |
| 2035 | fn test_rev_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2036 | let mut d = RingBuf::new(); |
Jonathan S | 03609e5 | 2014-04-21 04:59:12 | [diff] [blame] | 2037 | assert_eq!(d.iter().rev().next(), None); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2038 | |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2039 | for i in 0..5 { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2040 | d.push_back(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 2041 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 2042 | { |
| 2043 | let b: &[_] = &[&4,&3,&2,&1,&0]; |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2044 | assert_eq!(d.iter().rev().collect::<Vec<_>>(), b); |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 2045 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 2046 | |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2047 | for i in 6..9 { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2048 | d.push_front(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 2049 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 2050 | let b: &[_] = &[&4,&3,&2,&1,&0,&6,&7,&8]; |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2051 | assert_eq!(d.iter().rev().collect::<Vec<_>>(), b); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 2052 | } |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 2053 | |
| 2054 | #[test] |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 2055 | fn test_mut_rev_iter_wrap() { |
| 2056 | let mut d = RingBuf::with_capacity(3); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 2057 | assert!(d.iter_mut().rev().next().is_none()); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 2058 | |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2059 | d.push_back(1); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2060 | d.push_back(2); |
| 2061 | d.push_back(3); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 2062 | assert_eq!(d.pop_front(), Some(1)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2063 | d.push_back(4); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 2064 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2065 | assert_eq!(d.iter_mut().rev().cloned().collect::<Vec<_>>(), |
| 2066 | vec![4, 3, 2]); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | #[test] |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2070 | fn test_mut_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2071 | let mut d = RingBuf::new(); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 2072 | assert!(d.iter_mut().next().is_none()); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2073 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2074 | for i in 0..3 { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2075 | d.push_front(i); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2076 | } |
| 2077 | |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 2078 | for (i, elt) in d.iter_mut().enumerate() { |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2079 | assert_eq!(*elt, 2 - i); |
| 2080 | *elt = i; |
| 2081 | } |
| 2082 | |
| 2083 | { |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 2084 | let mut it = d.iter_mut(); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2085 | assert_eq!(*it.next().unwrap(), 0); |
| 2086 | assert_eq!(*it.next().unwrap(), 1); |
| 2087 | assert_eq!(*it.next().unwrap(), 2); |
| 2088 | assert!(it.next().is_none()); |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | #[test] |
| 2093 | fn test_mut_rev_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2094 | let mut d = RingBuf::new(); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 2095 | assert!(d.iter_mut().rev().next().is_none()); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2096 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2097 | for i in 0..3 { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2098 | d.push_front(i); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2099 | } |
| 2100 | |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 2101 | for (i, elt) in d.iter_mut().rev().enumerate() { |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2102 | assert_eq!(*elt, i); |
| 2103 | *elt = i; |
| 2104 | } |
| 2105 | |
| 2106 | { |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 2107 | let mut it = d.iter_mut().rev(); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 2108 | assert_eq!(*it.next().unwrap(), 0); |
| 2109 | assert_eq!(*it.next().unwrap(), 1); |
| 2110 | assert_eq!(*it.next().unwrap(), 2); |
| 2111 | assert!(it.next().is_none()); |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | #[test] |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2116 | fn test_into_iter() { |
| 2117 | |
| 2118 | // Empty iter |
| 2119 | { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2120 | let d: RingBuf<i32> = RingBuf::new(); |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2121 | let mut iter = d.into_iter(); |
| 2122 | |
| 2123 | assert_eq!(iter.size_hint(), (0, Some(0))); |
| 2124 | assert_eq!(iter.next(), None); |
| 2125 | assert_eq!(iter.size_hint(), (0, Some(0))); |
| 2126 | } |
| 2127 | |
| 2128 | // simple iter |
| 2129 | { |
| 2130 | let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2131 | for i in 0..5 { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2132 | d.push_back(i); |
| 2133 | } |
| 2134 | |
| 2135 | let b = vec![0,1,2,3,4]; |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2136 | assert_eq!(d.into_iter().collect::<Vec<_>>(), b); |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | // wrapped iter |
| 2140 | { |
| 2141 | let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2142 | for i in 0..5 { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2143 | d.push_back(i); |
| 2144 | } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2145 | for i in 6..9 { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2146 | d.push_front(i); |
| 2147 | } |
| 2148 | |
| 2149 | let b = vec![8,7,6,0,1,2,3,4]; |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2150 | assert_eq!(d.into_iter().collect::<Vec<_>>(), b); |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | // partially used |
| 2154 | { |
| 2155 | let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2156 | for i in 0..5 { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2157 | d.push_back(i); |
| 2158 | } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2159 | for i in 6..9 { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 2160 | d.push_front(i); |
| 2161 | } |
| 2162 | |
| 2163 | let mut it = d.into_iter(); |
| 2164 | assert_eq!(it.size_hint(), (8, Some(8))); |
| 2165 | assert_eq!(it.next(), Some(8)); |
| 2166 | assert_eq!(it.size_hint(), (7, Some(7))); |
| 2167 | assert_eq!(it.next_back(), Some(4)); |
| 2168 | assert_eq!(it.size_hint(), (6, Some(6))); |
| 2169 | assert_eq!(it.next(), Some(7)); |
| 2170 | assert_eq!(it.size_hint(), (5, Some(5))); |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | #[test] |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2175 | fn test_drain() { |
| 2176 | |
| 2177 | // Empty iter |
| 2178 | { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2179 | let mut d: RingBuf<i32> = RingBuf::new(); |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2180 | |
| 2181 | { |
| 2182 | let mut iter = d.drain(); |
| 2183 | |
| 2184 | assert_eq!(iter.size_hint(), (0, Some(0))); |
| 2185 | assert_eq!(iter.next(), None); |
| 2186 | assert_eq!(iter.size_hint(), (0, Some(0))); |
| 2187 | } |
| 2188 | |
| 2189 | assert!(d.is_empty()); |
| 2190 | } |
| 2191 | |
| 2192 | // simple iter |
| 2193 | { |
| 2194 | let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2195 | for i in 0..5 { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2196 | d.push_back(i); |
| 2197 | } |
| 2198 | |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2199 | assert_eq!(d.drain().collect::<Vec<_>>(), [0, 1, 2, 3, 4]); |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2200 | assert!(d.is_empty()); |
| 2201 | } |
| 2202 | |
| 2203 | // wrapped iter |
| 2204 | { |
| 2205 | let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2206 | for i in 0..5 { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2207 | d.push_back(i); |
| 2208 | } |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2209 | for i in 6..9 { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2210 | d.push_front(i); |
| 2211 | } |
| 2212 | |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2213 | assert_eq!(d.drain().collect::<Vec<_>>(), [8,7,6,0,1,2,3,4]); |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2214 | assert!(d.is_empty()); |
| 2215 | } |
| 2216 | |
| 2217 | // partially used |
| 2218 | { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2219 | let mut d: RingBuf<_> = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2220 | for i in 0..5 { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2221 | d.push_back(i); |
| 2222 | } |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2223 | for i in 6..9 { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 2224 | d.push_front(i); |
| 2225 | } |
| 2226 | |
| 2227 | { |
| 2228 | let mut it = d.drain(); |
| 2229 | assert_eq!(it.size_hint(), (8, Some(8))); |
| 2230 | assert_eq!(it.next(), Some(8)); |
| 2231 | assert_eq!(it.size_hint(), (7, Some(7))); |
| 2232 | assert_eq!(it.next_back(), Some(4)); |
| 2233 | assert_eq!(it.size_hint(), (6, Some(6))); |
| 2234 | assert_eq!(it.next(), Some(7)); |
| 2235 | assert_eq!(it.size_hint(), (5, Some(5))); |
| 2236 | } |
| 2237 | assert!(d.is_empty()); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | #[test] |
Brian Anderson | ee05219 | 2014-03-31 04:45:55 | [diff] [blame] | 2242 | fn test_from_iter() { |
Eduard Burtescu | b45d30d | 2014-12-19 12:02:22 | [diff] [blame] | 2243 | use core::iter; |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2244 | let v = vec!(1,2,3,4,5,6,7); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2245 | let deq: RingBuf<_> = v.iter().cloned().collect(); |
| 2246 | let u: Vec<_> = deq.iter().cloned().collect(); |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 2247 | assert_eq!(u, v); |
| 2248 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2249 | let seq = iter::count(0, 2).take(256); |
| 2250 | let deq: RingBuf<_> = seq.collect(); |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 2251 | for (i, &x) in deq.iter().enumerate() { |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 2252 | assert_eq!(2*i, x); |
| 2253 | } |
| 2254 | assert_eq!(deq.len(), 256); |
| 2255 | } |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 2256 | |
| 2257 | #[test] |
| 2258 | fn test_clone() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2259 | let mut d = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2260 | d.push_front(17); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2261 | d.push_front(42); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2262 | d.push_back(137); |
| 2263 | d.push_back(137); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2264 | assert_eq!(d.len(), 4); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 2265 | let mut e = d.clone(); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2266 | assert_eq!(e.len(), 4); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 2267 | while !d.is_empty() { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2268 | assert_eq!(d.pop_back(), e.pop_back()); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 2269 | } |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2270 | assert_eq!(d.len(), 0); |
| 2271 | assert_eq!(e.len(), 0); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | #[test] |
| 2275 | fn test_eq() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2276 | let mut d = RingBuf::new(); |
Alex Crichton | 02882fb | 2014-02-28 09:23:06 | [diff] [blame] | 2277 | assert!(d == RingBuf::with_capacity(0)); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2278 | d.push_front(137); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2279 | d.push_front(17); |
| 2280 | d.push_front(42); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2281 | d.push_back(137); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 2282 | let mut e = RingBuf::with_capacity(0); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2283 | e.push_back(42); |
| 2284 | e.push_back(17); |
| 2285 | e.push_back(137); |
| 2286 | e.push_back(137); |
Alex Crichton | 02882fb | 2014-02-28 09:23:06 | [diff] [blame] | 2287 | assert!(&e == &d); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2288 | e.pop_back(); |
| 2289 | e.push_back(0); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 2290 | assert!(e != d); |
| 2291 | e.clear(); |
Alex Crichton | 02882fb | 2014-02-28 09:23:06 | [diff] [blame] | 2292 | assert!(e == RingBuf::new()); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 2293 | } |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 2294 | |
| 2295 | #[test] |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 2296 | fn test_hash() { |
| 2297 | let mut x = RingBuf::new(); |
| 2298 | let mut y = RingBuf::new(); |
| 2299 | |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2300 | x.push_back(1); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2301 | x.push_back(2); |
| 2302 | x.push_back(3); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 2303 | |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2304 | y.push_back(0); |
| 2305 | y.push_back(1); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 2306 | y.pop_front(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2307 | y.push_back(2); |
| 2308 | y.push_back(3); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 2309 | |
Alex Crichton | 511f0b8 | 2014-12-09 20:37:23 | [diff] [blame] | 2310 | assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y)); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | #[test] |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 2314 | fn test_ord() { |
| 2315 | let x = RingBuf::new(); |
| 2316 | let mut y = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2317 | y.push_back(1); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 2318 | y.push_back(2); |
| 2319 | y.push_back(3); |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 2320 | assert!(x < y); |
| 2321 | assert!(y > x); |
| 2322 | assert!(x <= x); |
| 2323 | assert!(x >= x); |
| 2324 | } |
| 2325 | |
| 2326 | #[test] |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 2327 | fn test_show() { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2328 | let ringbuf: RingBuf<_> = (0..10).collect(); |
Alex Crichton | 3cb9fa2 | 2015-01-20 23:45:07 | [diff] [blame] | 2329 | assert_eq!(format!("{:?}", ringbuf), "RingBuf [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"); |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 2330 | |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2331 | let ringbuf: RingBuf<_> = vec!["just", "one", "test", "more"].iter() |
| 2332 | .cloned() |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 2333 | .collect(); |
Alex Crichton | a640008 | 2015-01-07 00:16:35 | [diff] [blame] | 2334 | assert_eq!(format!("{:?}", ringbuf), "RingBuf [\"just\", \"one\", \"test\", \"more\"]"); |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 2335 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2336 | |
| 2337 | #[test] |
| 2338 | fn test_drop() { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2339 | static mut drops: i32 = 0; |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2340 | struct Elem; |
| 2341 | impl Drop for Elem { |
| 2342 | fn drop(&mut self) { |
| 2343 | unsafe { drops += 1; } |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | let mut ring = RingBuf::new(); |
| 2348 | ring.push_back(Elem); |
| 2349 | ring.push_front(Elem); |
| 2350 | ring.push_back(Elem); |
| 2351 | ring.push_front(Elem); |
| 2352 | drop(ring); |
| 2353 | |
| 2354 | assert_eq!(unsafe {drops}, 4); |
| 2355 | } |
| 2356 | |
| 2357 | #[test] |
| 2358 | fn test_drop_with_pop() { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2359 | static mut drops: i32 = 0; |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2360 | struct Elem; |
| 2361 | impl Drop for Elem { |
| 2362 | fn drop(&mut self) { |
| 2363 | unsafe { drops += 1; } |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | let mut ring = RingBuf::new(); |
| 2368 | ring.push_back(Elem); |
| 2369 | ring.push_front(Elem); |
| 2370 | ring.push_back(Elem); |
| 2371 | ring.push_front(Elem); |
| 2372 | |
| 2373 | drop(ring.pop_back()); |
| 2374 | drop(ring.pop_front()); |
| 2375 | assert_eq!(unsafe {drops}, 2); |
| 2376 | |
| 2377 | drop(ring); |
| 2378 | assert_eq!(unsafe {drops}, 4); |
| 2379 | } |
| 2380 | |
| 2381 | #[test] |
| 2382 | fn test_drop_clear() { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 2383 | static mut drops: i32 = 0; |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2384 | struct Elem; |
| 2385 | impl Drop for Elem { |
| 2386 | fn drop(&mut self) { |
| 2387 | unsafe { drops += 1; } |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | let mut ring = RingBuf::new(); |
| 2392 | ring.push_back(Elem); |
| 2393 | ring.push_front(Elem); |
| 2394 | ring.push_back(Elem); |
| 2395 | ring.push_front(Elem); |
| 2396 | ring.clear(); |
| 2397 | assert_eq!(unsafe {drops}, 4); |
| 2398 | |
| 2399 | drop(ring); |
| 2400 | assert_eq!(unsafe {drops}, 4); |
| 2401 | } |
| 2402 | |
| 2403 | #[test] |
| 2404 | fn test_reserve_grow() { |
| 2405 | // test growth path A |
| 2406 | // [T o o H] -> [T o o H . . . . ] |
| 2407 | let mut ring = RingBuf::with_capacity(4); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2408 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2409 | ring.push_back(i); |
| 2410 | } |
| 2411 | ring.reserve(7); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2412 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2413 | assert_eq!(ring.pop_front(), Some(i)); |
| 2414 | } |
| 2415 | |
| 2416 | // test growth path B |
| 2417 | // [H T o o] -> [. T o o H . . . ] |
| 2418 | let mut ring = RingBuf::with_capacity(4); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2419 | for i in 0..1 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2420 | ring.push_back(i); |
| 2421 | assert_eq!(ring.pop_front(), Some(i)); |
| 2422 | } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2423 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2424 | ring.push_back(i); |
| 2425 | } |
| 2426 | ring.reserve(7); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2427 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2428 | assert_eq!(ring.pop_front(), Some(i)); |
| 2429 | } |
| 2430 | |
| 2431 | // test growth path C |
| 2432 | // [o o H T] -> [o o H . . . . T ] |
| 2433 | let mut ring = RingBuf::with_capacity(4); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2434 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2435 | ring.push_back(i); |
| 2436 | assert_eq!(ring.pop_front(), Some(i)); |
| 2437 | } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2438 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2439 | ring.push_back(i); |
| 2440 | } |
| 2441 | ring.reserve(7); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2442 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2443 | assert_eq!(ring.pop_front(), Some(i)); |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | #[test] |
| 2448 | fn test_get() { |
| 2449 | let mut ring = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2450 | ring.push_back(0); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2451 | assert_eq!(ring.get(0), Some(&0)); |
| 2452 | assert_eq!(ring.get(1), None); |
| 2453 | |
| 2454 | ring.push_back(1); |
| 2455 | assert_eq!(ring.get(0), Some(&0)); |
| 2456 | assert_eq!(ring.get(1), Some(&1)); |
| 2457 | assert_eq!(ring.get(2), None); |
| 2458 | |
| 2459 | ring.push_back(2); |
| 2460 | assert_eq!(ring.get(0), Some(&0)); |
| 2461 | assert_eq!(ring.get(1), Some(&1)); |
| 2462 | assert_eq!(ring.get(2), Some(&2)); |
| 2463 | assert_eq!(ring.get(3), None); |
| 2464 | |
| 2465 | assert_eq!(ring.pop_front(), Some(0)); |
| 2466 | assert_eq!(ring.get(0), Some(&1)); |
| 2467 | assert_eq!(ring.get(1), Some(&2)); |
| 2468 | assert_eq!(ring.get(2), None); |
| 2469 | |
| 2470 | assert_eq!(ring.pop_front(), Some(1)); |
| 2471 | assert_eq!(ring.get(0), Some(&2)); |
| 2472 | assert_eq!(ring.get(1), None); |
| 2473 | |
| 2474 | assert_eq!(ring.pop_front(), Some(2)); |
| 2475 | assert_eq!(ring.get(0), None); |
| 2476 | assert_eq!(ring.get(1), None); |
| 2477 | } |
| 2478 | |
| 2479 | #[test] |
| 2480 | fn test_get_mut() { |
| 2481 | let mut ring = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2482 | for i in 0..3 { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 2483 | ring.push_back(i); |
| 2484 | } |
| 2485 | |
| 2486 | match ring.get_mut(1) { |
| 2487 | Some(x) => *x = -1, |
| 2488 | None => () |
| 2489 | }; |
| 2490 | |
| 2491 | assert_eq!(ring.get_mut(0), Some(&mut 0)); |
| 2492 | assert_eq!(ring.get_mut(1), Some(&mut -1)); |
| 2493 | assert_eq!(ring.get_mut(2), Some(&mut 2)); |
| 2494 | assert_eq!(ring.get_mut(3), None); |
| 2495 | |
| 2496 | assert_eq!(ring.pop_front(), Some(0)); |
| 2497 | assert_eq!(ring.get_mut(0), Some(&mut -1)); |
| 2498 | assert_eq!(ring.get_mut(1), Some(&mut 2)); |
| 2499 | assert_eq!(ring.get_mut(2), None); |
| 2500 | } |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2501 | |
| 2502 | #[test] |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2503 | fn test_swap_front_back_remove() { |
| 2504 | fn test(back: bool) { |
| 2505 | // This test checks that every single combination of tail position and length is tested. |
| 2506 | // Capacity 15 should be large enough to cover every case. |
| 2507 | let mut tester = RingBuf::with_capacity(15); |
| 2508 | let usable_cap = tester.capacity(); |
| 2509 | let final_len = usable_cap / 2; |
| 2510 | |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2511 | for len in 0..final_len { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2512 | let expected = if back { |
Jorge Aparicio | c300d68 | 2015-01-26 20:44:22 | [diff] [blame] | 2513 | (0..len).collect() |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2514 | } else { |
Jorge Aparicio | c300d68 | 2015-01-26 20:44:22 | [diff] [blame] | 2515 | (0..len).rev().collect() |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2516 | }; |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2517 | for tail_pos in 0..usable_cap { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2518 | tester.tail = tail_pos; |
| 2519 | tester.head = tail_pos; |
| 2520 | if back { |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2521 | for i in 0..len * 2 { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2522 | tester.push_front(i); |
| 2523 | } |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2524 | for i in 0..len { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2525 | assert_eq!(tester.swap_back_remove(i), Some(len * 2 - 1 - i)); |
| 2526 | } |
| 2527 | } else { |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2528 | for i in 0..len * 2 { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2529 | tester.push_back(i); |
| 2530 | } |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2531 | for i in 0..len { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2532 | let idx = tester.len() - 1 - i; |
| 2533 | assert_eq!(tester.swap_front_remove(idx), Some(len * 2 - 1 - i)); |
| 2534 | } |
| 2535 | } |
| 2536 | assert!(tester.tail < tester.cap); |
| 2537 | assert!(tester.head < tester.cap); |
| 2538 | assert_eq!(tester, expected); |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | test(true); |
| 2543 | test(false); |
| 2544 | } |
| 2545 | |
| 2546 | #[test] |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2547 | fn test_insert() { |
| 2548 | // This test checks that every single combination of tail position, length, and |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 2549 | // insertion position is tested. Capacity 15 should be large enough to cover every case. |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2550 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 2551 | let mut tester = RingBuf::with_capacity(15); |
| 2552 | // can't guarantee we got 15, so have to get what we got. |
| 2553 | // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2554 | // this test isn't covering what it wants to |
| 2555 | let cap = tester.capacity(); |
| 2556 | |
| 2557 | |
| 2558 | // len is the length *after* insertion |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2559 | for len in 1..cap { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2560 | // 0, 1, 2, .., len - 1 |
| 2561 | let expected = iter::count(0, 1).take(len).collect(); |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2562 | for tail_pos in 0..cap { |
| 2563 | for to_insert in 0..len { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2564 | tester.tail = tail_pos; |
| 2565 | tester.head = tail_pos; |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2566 | for i in 0..len { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2567 | if i != to_insert { |
| 2568 | tester.push_back(i); |
| 2569 | } |
| 2570 | } |
| 2571 | tester.insert(to_insert, to_insert); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 2572 | assert!(tester.tail < tester.cap); |
| 2573 | assert!(tester.head < tester.cap); |
| 2574 | assert_eq!(tester, expected); |
| 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | #[test] |
| 2581 | fn test_remove() { |
| 2582 | // This test checks that every single combination of tail position, length, and |
| 2583 | // removal position is tested. Capacity 15 should be large enough to cover every case. |
| 2584 | |
| 2585 | let mut tester = RingBuf::with_capacity(15); |
| 2586 | // can't guarantee we got 15, so have to get what we got. |
| 2587 | // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else |
| 2588 | // this test isn't covering what it wants to |
| 2589 | let cap = tester.capacity(); |
| 2590 | |
| 2591 | // len is the length *after* removal |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2592 | for len in 0..cap - 1 { |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 2593 | // 0, 1, 2, .., len - 1 |
| 2594 | let expected = iter::count(0, 1).take(len).collect(); |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2595 | for tail_pos in 0..cap { |
| 2596 | for to_remove in 0..len + 1 { |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 2597 | tester.tail = tail_pos; |
| 2598 | tester.head = tail_pos; |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2599 | for i in 0..len { |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 2600 | if i == to_remove { |
| 2601 | tester.push_back(1234); |
| 2602 | } |
| 2603 | tester.push_back(i); |
| 2604 | } |
| 2605 | if to_remove == len { |
| 2606 | tester.push_back(1234); |
| 2607 | } |
| 2608 | tester.remove(to_remove); |
| 2609 | assert!(tester.tail < tester.cap); |
| 2610 | assert!(tester.head < tester.cap); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2611 | assert_eq!(tester, expected); |
| 2612 | } |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | #[test] |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2618 | fn test_shrink_to_fit() { |
| 2619 | // This test checks that every single combination of head and tail position, |
| 2620 | // is tested. Capacity 15 should be large enough to cover every case. |
| 2621 | |
| 2622 | let mut tester = RingBuf::with_capacity(15); |
| 2623 | // can't guarantee we got 15, so have to get what we got. |
| 2624 | // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else |
| 2625 | // this test isn't covering what it wants to |
| 2626 | let cap = tester.capacity(); |
| 2627 | tester.reserve(63); |
| 2628 | let max_cap = tester.capacity(); |
| 2629 | |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2630 | for len in 0..cap + 1 { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2631 | // 0, 1, 2, .., len - 1 |
| 2632 | let expected = iter::count(0, 1).take(len).collect(); |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2633 | for tail_pos in 0..max_cap + 1 { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2634 | tester.tail = tail_pos; |
| 2635 | tester.head = tail_pos; |
| 2636 | tester.reserve(63); |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2637 | for i in 0..len { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 2638 | tester.push_back(i); |
| 2639 | } |
| 2640 | tester.shrink_to_fit(); |
| 2641 | assert!(tester.capacity() <= cap); |
| 2642 | assert!(tester.tail < tester.cap); |
| 2643 | assert!(tester.head < tester.cap); |
| 2644 | assert_eq!(tester, expected); |
| 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | #[test] |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2650 | fn test_front() { |
| 2651 | let mut ring = RingBuf::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2652 | ring.push_back(10); |
| 2653 | ring.push_back(20); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 2654 | assert_eq!(ring.front(), Some(&10)); |
| 2655 | ring.pop_front(); |
| 2656 | assert_eq!(ring.front(), Some(&20)); |
| 2657 | ring.pop_front(); |
| 2658 | assert_eq!(ring.front(), None); |
| 2659 | } |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2660 | |
| 2661 | #[test] |
| 2662 | fn test_as_slices() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2663 | let mut ring: RingBuf<i32> = RingBuf::with_capacity(127); |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2664 | let cap = ring.capacity() as i32; |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2665 | let first = cap/2; |
| 2666 | let last = cap - first; |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2667 | for i in 0..first { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2668 | ring.push_back(i); |
| 2669 | |
| 2670 | let (left, right) = ring.as_slices(); |
Jorge Aparicio | c300d68 | 2015-01-26 20:44:22 | [diff] [blame] | 2671 | let expected: Vec<_> = (0..i+1).collect(); |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2672 | assert_eq!(left, expected); |
| 2673 | assert_eq!(right, []); |
| 2674 | } |
| 2675 | |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2676 | for j in -last..0 { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2677 | ring.push_front(j); |
| 2678 | let (left, right) = ring.as_slices(); |
Jorge Aparicio | c300d68 | 2015-01-26 20:44:22 | [diff] [blame] | 2679 | let expected_left: Vec<_> = (-last..j+1).rev().collect(); |
| 2680 | let expected_right: Vec<_> = (0..first).collect(); |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2681 | assert_eq!(left, expected_left); |
| 2682 | assert_eq!(right, expected_right); |
| 2683 | } |
| 2684 | |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2685 | assert_eq!(ring.len() as i32, cap); |
| 2686 | assert_eq!(ring.capacity() as i32, cap); |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | #[test] |
| 2690 | fn test_as_mut_slices() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 2691 | let mut ring: RingBuf<i32> = RingBuf::with_capacity(127); |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2692 | let cap = ring.capacity() as i32; |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2693 | let first = cap/2; |
| 2694 | let last = cap - first; |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2695 | for i in 0..first { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2696 | ring.push_back(i); |
| 2697 | |
| 2698 | let (left, right) = ring.as_mut_slices(); |
Jorge Aparicio | c300d68 | 2015-01-26 20:44:22 | [diff] [blame] | 2699 | let expected: Vec<_> = (0..i+1).collect(); |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2700 | assert_eq!(left, expected); |
| 2701 | assert_eq!(right, []); |
| 2702 | } |
| 2703 | |
Jorge Aparicio | 7d661af | 2015-01-26 20:46:12 | [diff] [blame] | 2704 | for j in -last..0 { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2705 | ring.push_front(j); |
| 2706 | let (left, right) = ring.as_mut_slices(); |
Jorge Aparicio | c300d68 | 2015-01-26 20:44:22 | [diff] [blame] | 2707 | let expected_left: Vec<_> = (-last..j+1).rev().collect(); |
| 2708 | let expected_right: Vec<_> = (0..first).collect(); |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2709 | assert_eq!(left, expected_left); |
| 2710 | assert_eq!(right, expected_right); |
| 2711 | } |
| 2712 | |
Alex Crichton | 3a2530d | 2015-01-30 20:26:44 | [diff] [blame] | 2713 | assert_eq!(ring.len() as i32, cap); |
| 2714 | assert_eq!(ring.capacity() as i32, cap); |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 2715 | } |
Michael Sullivan | c854d6e | 2012-07-03 17:52:32 | [diff] [blame] | 2716 | } |