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. |
| 14 | //! Its interface `Deque` is defined in `collections`. |
Patrick Walton | f3723cf | 2013-05-17 22:28:44 | [diff] [blame] | 15 | |
Alex Crichton | 6a58537 | 2014-05-30 01:50:12 | [diff] [blame] | 16 | use core::prelude::*; |
| 17 | |
Tom Jakubowski | d6a3941 | 2014-06-09 07:30:04 | [diff] [blame] | 18 | use core::default::Default; |
Alex Crichton | 6a58537 | 2014-05-30 01:50:12 | [diff] [blame] | 19 | use core::fmt; |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 20 | use core::iter; |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 21 | use core::raw::Slice as RawSlice; |
| 22 | use core::ptr; |
| 23 | use core::kinds::marker; |
| 24 | use core::mem; |
| 25 | use core::num; |
Alex Crichton | 998fece | 2013-05-06 04:42:54 | [diff] [blame] | 26 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 27 | use std::hash::{Writer, Hash}; |
| 28 | use std::cmp; |
| 29 | |
| 30 | use alloc::heap; |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 31 | |
blake2-ppc | 0ff5c17 | 2013-07-06 03:42:45 | [diff] [blame] | 32 | static INITIAL_CAPACITY: uint = 8u; // 2^3 |
| 33 | static MINIMUM_CAPACITY: uint = 2u; |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 34 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 35 | // FIXME(conventions): implement shrink_to_fit. Awkward with the current design, but it should |
| 36 | // be scrapped anyway. Defer to rewrite? |
| 37 | // FIXME(conventions): implement into_iter |
| 38 | |
| 39 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 40 | /// `RingBuf` is a circular buffer that implements `Deque`. |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 41 | pub struct RingBuf<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 42 | // tail and head are pointers into the buffer. Tail always points |
| 43 | // to the first element that could be read, Head always points |
| 44 | // to where data should be written. |
| 45 | // If tail == head the buffer is empty. The length of the ringbuf |
| 46 | // is defined as the distance between the two. |
| 47 | |
| 48 | tail: uint, |
| 49 | head: uint, |
| 50 | cap: uint, |
| 51 | ptr: *mut T |
| 52 | } |
| 53 | |
| 54 | impl<T: Clone> Clone for RingBuf<T> { |
| 55 | fn clone(&self) -> RingBuf<T> { |
| 56 | self.iter().map(|t| t.clone()).collect() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[unsafe_destructor] |
| 61 | impl<T> Drop for RingBuf<T> { |
| 62 | fn drop(&mut self) { |
| 63 | self.clear(); |
| 64 | unsafe { |
| 65 | if mem::size_of::<T>() != 0 { |
| 66 | heap::deallocate(self.ptr as *mut u8, |
| 67 | self.cap * mem::size_of::<T>(), |
| 68 | mem::min_align_of::<T>()) |
| 69 | } |
| 70 | } |
| 71 | } |
Marijn Haverbeke | 26610db | 2012-01-11 11:49:33 | [diff] [blame] | 72 | } |
Roy Frostig | 9c81889 | 2010-07-21 01:03:09 | [diff] [blame] | 73 | |
Tom Jakubowski | d6a3941 | 2014-06-09 07:30:04 | [diff] [blame] | 74 | impl<T> Default for RingBuf<T> { |
| 75 | #[inline] |
| 76 | fn default() -> RingBuf<T> { RingBuf::new() } |
| 77 | } |
| 78 | |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 79 | impl<T> RingBuf<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 80 | /// Turn ptr into a slice |
| 81 | #[inline] |
| 82 | unsafe fn buffer_as_slice(&self) -> &[T] { |
| 83 | mem::transmute(RawSlice { data: self.ptr as *const T, len: self.cap }) |
| 84 | } |
| 85 | |
| 86 | /// Moves an element out of the buffer |
| 87 | #[inline] |
| 88 | unsafe fn buffer_read(&mut self, off: uint) -> T { |
| 89 | ptr::read(self.ptr.offset(off as int) as *const T) |
| 90 | } |
| 91 | |
| 92 | /// Writes an element into the buffer, moving it. |
| 93 | #[inline] |
| 94 | unsafe fn buffer_write(&mut self, off: uint, t: T) { |
| 95 | ptr::write(self.ptr.offset(off as int), t); |
| 96 | } |
| 97 | |
| 98 | /// Returns true iff the buffer is at capacity |
| 99 | #[inline] |
| 100 | fn is_full(&self) -> bool { self.cap - self.len() == 1 } |
| 101 | } |
| 102 | |
| 103 | impl<T> RingBuf<T> { |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 104 | /// Creates an empty `RingBuf`. |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 105 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 106 | pub fn new() -> RingBuf<T> { |
| 107 | RingBuf::with_capacity(INITIAL_CAPACITY) |
| 108 | } |
| 109 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 110 | /// Creates an empty `RingBuf` with space for at least `n` elements. |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 111 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 112 | pub fn with_capacity(n: uint) -> RingBuf<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 113 | // +1 since the ringbuffer always leaves one space empty |
| 114 | let cap = num::next_power_of_two(cmp::max(n + 1, MINIMUM_CAPACITY)); |
| 115 | let size = cap.checked_mul(&mem::size_of::<T>()) |
| 116 | .expect("capacity overflow"); |
| 117 | |
| 118 | RingBuf { |
| 119 | tail: 0, |
| 120 | head: 0, |
| 121 | cap: cap, |
| 122 | ptr: if mem::size_of::<T>() != 0 { |
| 123 | unsafe { heap::allocate(size, mem::min_align_of::<T>()) as *mut T } |
| 124 | } else { |
| 125 | heap::EMPTY as *mut T |
| 126 | } |
| 127 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 128 | } |
| 129 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 130 | /// Retrieves an element in the `RingBuf` by index. |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 131 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 132 | /// # Example |
| 133 | /// |
| 134 | /// ```rust |
| 135 | /// use std::collections::RingBuf; |
| 136 | /// |
| 137 | /// let mut buf = RingBuf::new(); |
| 138 | /// buf.push_back(3i); |
| 139 | /// buf.push_back(4); |
| 140 | /// buf.push_back(5); |
| 141 | /// assert_eq!(buf.get(1).unwrap(), &4); |
| 142 | /// ``` |
| 143 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
| 144 | pub fn get(&self, i: uint) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 145 | if i < self.len() { |
| 146 | let idx = wrap_index(self.tail + i, self.cap); |
| 147 | unsafe { Some(&*self.ptr.offset(idx as int)) } |
| 148 | } else { |
| 149 | None |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | /// Retrieves an element in the `RingBuf` mutably by index. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 154 | /// |
| 155 | /// # Example |
| 156 | /// |
| 157 | /// ```rust |
| 158 | /// use std::collections::RingBuf; |
| 159 | /// |
| 160 | /// let mut buf = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 161 | /// buf.push_back(3i); |
| 162 | /// buf.push_back(4); |
| 163 | /// buf.push_back(5); |
| 164 | /// match buf.get_mut(1) { |
| 165 | /// None => {} |
| 166 | /// Some(elem) => { |
| 167 | /// *elem = 7; |
| 168 | /// } |
| 169 | /// } |
| 170 | /// |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 171 | /// assert_eq!(buf[1], 7); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 172 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 173 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
| 174 | pub fn get_mut(&mut self, i: uint) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 175 | if i < self.len() { |
| 176 | let idx = wrap_index(self.tail + i, self.cap); |
| 177 | unsafe { Some(&mut *self.ptr.offset(idx as int)) } |
| 178 | } else { |
| 179 | None |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 180 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 181 | } |
| 182 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 183 | /// Swaps elements at indices `i` and `j`. |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 184 | /// |
| 185 | /// `i` and `j` may be equal. |
| 186 | /// |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 187 | /// Fails if there is no element with either index. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 188 | /// |
| 189 | /// # Example |
| 190 | /// |
| 191 | /// ```rust |
| 192 | /// use std::collections::RingBuf; |
| 193 | /// |
| 194 | /// let mut buf = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 195 | /// buf.push_back(3i); |
| 196 | /// buf.push_back(4); |
| 197 | /// buf.push_back(5); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 198 | /// buf.swap(0, 2); |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 199 | /// assert_eq!(buf[0], 5); |
| 200 | /// assert_eq!(buf[2], 3); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 201 | /// ``` |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 202 | pub fn swap(&mut self, i: uint, j: uint) { |
| 203 | assert!(i < self.len()); |
| 204 | assert!(j < self.len()); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 205 | let ri = wrap_index(self.tail + i, self.cap); |
| 206 | let rj = wrap_index(self.tail + j, self.cap); |
| 207 | unsafe { |
| 208 | ptr::swap(self.ptr.offset(ri as int), self.ptr.offset(rj as int)) |
| 209 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 210 | } |
| 211 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 212 | /// Returns the number of elements the `RingBuf` can hold without |
| 213 | /// reallocating. |
| 214 | /// |
| 215 | /// # Example |
| 216 | /// |
| 217 | /// ``` |
| 218 | /// use std::collections::RingBuf; |
| 219 | /// |
| 220 | /// let buf: RingBuf<int> = RingBuf::with_capacity(10); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 221 | /// assert!(buf.capacity() >= 10); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 222 | /// ``` |
| 223 | #[inline] |
| 224 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 225 | pub fn capacity(&self) -> uint { self.cap - 1 } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 226 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 227 | /// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the |
| 228 | /// given `RingBuf`. Does nothing if the capacity is already sufficient. |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 229 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 230 | /// Note that the allocator may give the collection more space than it requests. Therefore |
| 231 | /// capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future |
| 232 | /// insertions are expected. |
| 233 | /// |
| 234 | /// # Panics |
| 235 | /// |
| 236 | /// Panics if the new capacity overflows `uint`. |
| 237 | /// |
| 238 | /// # Example |
| 239 | /// |
| 240 | /// ``` |
| 241 | /// use std::collections::RingBuf; |
| 242 | /// |
| 243 | /// let mut buf: RingBuf<int> = vec![1].into_iter().collect(); |
| 244 | /// buf.reserve_exact(10); |
| 245 | /// assert!(buf.capacity() >= 11); |
| 246 | /// ``` |
| 247 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
| 248 | pub fn reserve_exact(&mut self, additional: uint) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 249 | self.reserve(additional); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /// Reserves capacity for at least `additional` more elements to be inserted in the given |
| 253 | /// `Ringbuf`. The collection may reserve more space to avoid frequent reallocations. |
| 254 | /// |
| 255 | /// # Panics |
| 256 | /// |
| 257 | /// Panics if the new capacity overflows `uint`. |
| 258 | /// |
| 259 | /// # Example |
| 260 | /// |
| 261 | /// ``` |
| 262 | /// use std::collections::RingBuf; |
| 263 | /// |
| 264 | /// let mut buf: RingBuf<int> = vec![1].into_iter().collect(); |
| 265 | /// buf.reserve(10); |
| 266 | /// assert!(buf.capacity() >= 11); |
| 267 | /// ``` |
| 268 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
| 269 | pub fn reserve(&mut self, additional: uint) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 270 | let new_len = self.len() + additional; |
| 271 | assert!(new_len + 1 > self.len(), "capacity overflow"); |
| 272 | if new_len > self.capacity() { |
| 273 | let count = num::next_power_of_two(new_len + 1); |
| 274 | assert!(count >= new_len + 1); |
| 275 | |
| 276 | if mem::size_of::<T>() != 0 { |
| 277 | let old = self.cap * mem::size_of::<T>(); |
| 278 | let new = count.checked_mul(&mem::size_of::<T>()) |
| 279 | .expect("capacity overflow"); |
| 280 | unsafe { |
| 281 | self.ptr = heap::reallocate(self.ptr as *mut u8, |
| 282 | old, |
| 283 | new, |
| 284 | mem::min_align_of::<T>()) as *mut T; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Move the shortest contiguous section of the ring buffer |
| 289 | // T H |
| 290 | // [o o o o o o o . ] |
| 291 | // T H |
| 292 | // A [o o o o o o o . . . . . . . . . ] |
| 293 | // H T |
| 294 | // [o o . o o o o o ] |
| 295 | // T H |
| 296 | // B [. . . o o o o o o o . . . . . . ] |
| 297 | // H T |
| 298 | // [o o o o o . o o ] |
| 299 | // H T |
| 300 | // C [o o o o o . . . . . . . . . o o ] |
| 301 | |
| 302 | let oldcap = self.cap; |
| 303 | self.cap = count; |
| 304 | |
| 305 | if self.tail <= self.head { // A |
| 306 | // Nop |
| 307 | } else if self.head < oldcap - self.tail { // B |
| 308 | unsafe { |
| 309 | ptr::copy_nonoverlapping_memory( |
| 310 | self.ptr.offset(oldcap as int), |
| 311 | self.ptr as *const T, |
| 312 | self.head |
| 313 | ); |
| 314 | } |
| 315 | self.head += oldcap; |
| 316 | } else { // C |
| 317 | unsafe { |
| 318 | ptr::copy_nonoverlapping_memory( |
| 319 | self.ptr.offset((count - (oldcap - self.tail)) as int), |
| 320 | self.ptr.offset(self.tail as int) as *const T, |
| 321 | oldcap - self.tail |
| 322 | ); |
| 323 | } |
| 324 | self.tail = count - (oldcap - self.tail); |
| 325 | } |
| 326 | } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 327 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 328 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 329 | /// Returns a front-to-back iterator. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 330 | /// |
| 331 | /// # Example |
| 332 | /// |
| 333 | /// ```rust |
| 334 | /// use std::collections::RingBuf; |
| 335 | /// |
| 336 | /// let mut buf = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 337 | /// buf.push_back(5i); |
| 338 | /// buf.push_back(3); |
| 339 | /// buf.push_back(4); |
Nick Cameron | 52ef462 | 2014-08-06 09:59:40 | [diff] [blame] | 340 | /// let b: &[_] = &[&5, &3, &4]; |
| 341 | /// assert_eq!(buf.iter().collect::<Vec<&int>>().as_slice(), b); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 342 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 343 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 344 | pub fn iter(&self) -> Items<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 345 | Items { |
| 346 | tail: self.tail, |
| 347 | head: self.head, |
| 348 | ring: unsafe { self.buffer_as_slice() } |
| 349 | } |
blake2-ppc | 3385e79 | 2013-07-15 23:13:26 | [diff] [blame] | 350 | } |
| 351 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 352 | /// Returns a front-to-back iterator which returns mutable references. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 353 | /// |
| 354 | /// # Example |
| 355 | /// |
| 356 | /// ```rust |
| 357 | /// use std::collections::RingBuf; |
| 358 | /// |
| 359 | /// let mut buf = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 360 | /// buf.push_back(5i); |
| 361 | /// buf.push_back(3); |
| 362 | /// buf.push_back(4); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 363 | /// for num in buf.iter_mut() { |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 364 | /// *num = *num - 2; |
| 365 | /// } |
Nick Cameron | 52ef462 | 2014-08-06 09:59:40 | [diff] [blame] | 366 | /// let b: &[_] = &[&mut 3, &mut 1, &mut 2]; |
Nick Cameron | 5997694 | 2014-09-24 11:41:09 | [diff] [blame] | 367 | /// assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>()[], b); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 368 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 369 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 370 | pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { |
| 371 | MutItems { |
| 372 | tail: self.tail, |
| 373 | head: self.head, |
| 374 | cap: self.cap, |
| 375 | ptr: self.ptr, |
| 376 | marker: marker::ContravariantLifetime::<'a>, |
| 377 | marker2: marker::NoCopy |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 378 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 379 | } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 380 | |
| 381 | /// Returns the number of elements in the `RingBuf`. |
| 382 | /// |
| 383 | /// # Example |
| 384 | /// |
| 385 | /// ``` |
| 386 | /// use std::collections::RingBuf; |
| 387 | /// |
| 388 | /// let mut v = RingBuf::new(); |
| 389 | /// assert_eq!(v.len(), 0); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 390 | /// v.push_back(1i); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 391 | /// assert_eq!(v.len(), 1); |
| 392 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 393 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 394 | pub fn len(&self) -> uint { count(self.tail, self.head, self.cap) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 395 | |
| 396 | /// Returns true if the buffer contains no elements |
| 397 | /// |
| 398 | /// # Example |
| 399 | /// |
| 400 | /// ``` |
| 401 | /// use std::collections::RingBuf; |
| 402 | /// |
| 403 | /// let mut v = RingBuf::new(); |
| 404 | /// assert!(v.is_empty()); |
| 405 | /// v.push_front(1i); |
| 406 | /// assert!(!v.is_empty()); |
| 407 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 408 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 409 | pub fn is_empty(&self) -> bool { self.len() == 0 } |
| 410 | |
| 411 | /// Clears the buffer, removing all values. |
| 412 | /// |
| 413 | /// # Example |
| 414 | /// |
| 415 | /// ``` |
| 416 | /// use std::collections::RingBuf; |
| 417 | /// |
| 418 | /// let mut v = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 419 | /// v.push_back(1i); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 420 | /// v.clear(); |
| 421 | /// assert!(v.is_empty()); |
| 422 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 423 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 424 | pub fn clear(&mut self) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 425 | while !self.is_empty() { |
| 426 | self.pop_front(); |
| 427 | } |
| 428 | self.head = 0; |
| 429 | self.tail = 0; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /// Provides a reference to the front element, or `None` if the sequence is |
| 433 | /// empty. |
| 434 | /// |
| 435 | /// # Example |
| 436 | /// |
| 437 | /// ``` |
| 438 | /// use std::collections::RingBuf; |
| 439 | /// |
| 440 | /// let mut d = RingBuf::new(); |
| 441 | /// assert_eq!(d.front(), None); |
| 442 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 443 | /// d.push_back(1i); |
| 444 | /// d.push_back(2i); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 445 | /// assert_eq!(d.front(), Some(&1i)); |
| 446 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 447 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 448 | pub fn front(&self) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 449 | if !self.is_empty() { Some(&self[0]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// Provides a mutable reference to the front element, or `None` if the |
| 453 | /// sequence is empty. |
| 454 | /// |
| 455 | /// # Example |
| 456 | /// |
| 457 | /// ``` |
| 458 | /// use std::collections::RingBuf; |
| 459 | /// |
| 460 | /// let mut d = RingBuf::new(); |
| 461 | /// assert_eq!(d.front_mut(), None); |
| 462 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 463 | /// d.push_back(1i); |
| 464 | /// d.push_back(2i); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 465 | /// match d.front_mut() { |
| 466 | /// Some(x) => *x = 9i, |
| 467 | /// None => (), |
| 468 | /// } |
| 469 | /// assert_eq!(d.front(), Some(&9i)); |
| 470 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 471 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 472 | pub fn front_mut(&mut self) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 473 | if !self.is_empty() { Some(&mut self[0]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /// Provides a reference to the back element, or `None` if the sequence is |
| 477 | /// empty. |
| 478 | /// |
| 479 | /// # Example |
| 480 | /// |
| 481 | /// ``` |
| 482 | /// use std::collections::RingBuf; |
| 483 | /// |
| 484 | /// let mut d = RingBuf::new(); |
| 485 | /// assert_eq!(d.back(), None); |
| 486 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 487 | /// d.push_back(1i); |
| 488 | /// d.push_back(2i); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 489 | /// assert_eq!(d.back(), Some(&2i)); |
| 490 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 491 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 492 | pub fn back(&self) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 493 | if !self.is_empty() { Some(&self[self.len() - 1]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /// Provides a mutable reference to the back element, or `None` if the |
| 497 | /// sequence is empty. |
| 498 | /// |
| 499 | /// # Example |
| 500 | /// |
| 501 | /// ``` |
| 502 | /// use std::collections::RingBuf; |
| 503 | /// |
| 504 | /// let mut d = RingBuf::new(); |
| 505 | /// assert_eq!(d.back(), None); |
| 506 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 507 | /// d.push_back(1i); |
| 508 | /// d.push_back(2i); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 509 | /// match d.back_mut() { |
| 510 | /// Some(x) => *x = 9i, |
| 511 | /// None => (), |
| 512 | /// } |
| 513 | /// assert_eq!(d.back(), Some(&9i)); |
| 514 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 515 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 516 | pub fn back_mut(&mut self) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 517 | let len = self.len(); |
| 518 | if !self.is_empty() { Some(&mut self[len - 1]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /// Removes the first element and returns it, or `None` if the sequence is |
| 522 | /// empty. |
| 523 | /// |
| 524 | /// # Example |
| 525 | /// |
| 526 | /// ``` |
| 527 | /// use std::collections::RingBuf; |
| 528 | /// |
| 529 | /// let mut d = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 530 | /// d.push_back(1i); |
| 531 | /// d.push_back(2i); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 532 | /// |
| 533 | /// assert_eq!(d.pop_front(), Some(1i)); |
| 534 | /// assert_eq!(d.pop_front(), Some(2i)); |
| 535 | /// assert_eq!(d.pop_front(), None); |
| 536 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 537 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 538 | pub fn pop_front(&mut self) -> Option<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 539 | if self.is_empty() { |
| 540 | None |
| 541 | } else { |
| 542 | let tail = self.tail; |
| 543 | self.tail = wrap_index(self.tail + 1, self.cap); |
| 544 | unsafe { Some(self.buffer_read(tail)) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 545 | } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /// Inserts an element first in the sequence. |
| 549 | /// |
| 550 | /// # Example |
| 551 | /// |
| 552 | /// ``` |
| 553 | /// use std::collections::RingBuf; |
| 554 | /// |
| 555 | /// let mut d = RingBuf::new(); |
| 556 | /// d.push_front(1i); |
| 557 | /// d.push_front(2i); |
| 558 | /// assert_eq!(d.front(), Some(&2i)); |
| 559 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 560 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 561 | pub fn push_front(&mut self, t: T) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 562 | if self.is_full() { self.reserve(1) } |
| 563 | |
| 564 | self.tail = wrap_index(self.tail - 1, self.cap); |
| 565 | let tail = self.tail; |
| 566 | unsafe { self.buffer_write(tail, t); } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 567 | } |
| 568 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 569 | /// Deprecated: Renamed to `push_back`. |
| 570 | #[deprecated = "Renamed to `push_back`"] |
| 571 | pub fn push(&mut self, t: T) { |
| 572 | self.push_back(t) |
| 573 | } |
| 574 | |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 575 | /// Appends an element to the back of a buffer |
| 576 | /// |
| 577 | /// # Example |
| 578 | /// |
| 579 | /// ```rust |
| 580 | /// use std::collections::RingBuf; |
| 581 | /// |
| 582 | /// let mut buf = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 583 | /// buf.push_back(1i); |
| 584 | /// buf.push_back(3); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 585 | /// assert_eq!(3, *buf.back().unwrap()); |
| 586 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 587 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
| 588 | pub fn push_back(&mut self, t: T) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 589 | if self.is_full() { self.reserve(1) } |
| 590 | |
| 591 | let head = self.head; |
| 592 | self.head = wrap_index(self.head + 1, self.cap); |
| 593 | unsafe { self.buffer_write(head, t) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 594 | } |
| 595 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 596 | /// Deprecated: Renamed to `pop_back`. |
| 597 | #[deprecated = "Renamed to `pop_back`"] |
| 598 | pub fn pop(&mut self) -> Option<T> { |
| 599 | self.pop_back() |
| 600 | } |
| 601 | |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 602 | /// Removes the last element from a buffer and returns it, or `None` if |
| 603 | /// it is empty. |
| 604 | /// |
| 605 | /// # Example |
| 606 | /// |
| 607 | /// ```rust |
| 608 | /// use std::collections::RingBuf; |
| 609 | /// |
| 610 | /// let mut buf = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 611 | /// assert_eq!(buf.pop_back(), None); |
| 612 | /// buf.push_back(1i); |
| 613 | /// buf.push_back(3); |
| 614 | /// assert_eq!(buf.pop_back(), Some(3)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 615 | /// ``` |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 616 | #[unstable = "matches collection reform specification, waiting for dust to settle"] |
| 617 | pub fn pop_back(&mut self) -> Option<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 618 | if self.is_empty() { |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 619 | None |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 620 | } else { |
| 621 | self.head = wrap_index(self.head - 1, self.cap); |
| 622 | let head = self.head; |
| 623 | unsafe { Some(self.buffer_read(head)) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 624 | } |
| 625 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 626 | } |
| 627 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 628 | /// Returns the index in the underlying buffer for a given logical element index. |
| 629 | #[inline] |
| 630 | fn wrap_index(index: uint, size: uint) -> uint { |
| 631 | // size is always a power of 2 |
| 632 | index & (size - 1) |
| 633 | } |
| 634 | |
| 635 | /// Calculate the number of elements left to be read in the buffer |
| 636 | #[inline] |
| 637 | fn count(tail: uint, head: uint, size: uint) -> uint { |
| 638 | // size is always a power of 2 |
| 639 | (head - tail) & (size - 1) |
| 640 | } |
| 641 | |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 642 | /// `RingBuf` iterator. |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 643 | pub struct Items<'a, T:'a> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 644 | ring: &'a [T], |
| 645 | tail: uint, |
| 646 | head: uint |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 647 | } |
| 648 | |
Palmer Cox | 3fd8c8b | 2014-01-15 03:32:24 | [diff] [blame] | 649 | impl<'a, T> Iterator<&'a T> for Items<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 650 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 651 | fn next(&mut self) -> Option<&'a T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 652 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 653 | return None; |
| 654 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 655 | let tail = self.tail; |
| 656 | self.tail = wrap_index(self.tail + 1, self.ring.len()); |
| 657 | unsafe { Some(self.ring.unsafe_get(tail)) } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | #[inline] |
| 661 | fn size_hint(&self) -> (uint, Option<uint>) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 662 | let len = count(self.tail, self.head, self.ring.len()); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 663 | (len, Some(len)) |
| 664 | } |
| 665 | } |
| 666 | |
Palmer Cox | 3fd8c8b | 2014-01-15 03:32:24 | [diff] [blame] | 667 | impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 668 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 669 | fn next_back(&mut self) -> Option<&'a T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 670 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 671 | return None; |
| 672 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 673 | self.head = wrap_index(self.head - 1, self.ring.len()); |
| 674 | unsafe { Some(self.ring.unsafe_get(self.head)) } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 675 | } |
| 676 | } |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 677 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 678 | |
Palmer Cox | 3fd8c8b | 2014-01-15 03:32:24 | [diff] [blame] | 679 | impl<'a, T> ExactSize<&'a T> for Items<'a, T> {} |
blake2-ppc | 7c369ee7 | 2013-09-01 16:20:24 | [diff] [blame] | 680 | |
Palmer Cox | 3fd8c8b | 2014-01-15 03:32:24 | [diff] [blame] | 681 | impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> { |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 682 | #[inline] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 683 | fn indexable(&self) -> uint { |
| 684 | let (len, _) = self.size_hint(); |
| 685 | len |
| 686 | } |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 687 | |
| 688 | #[inline] |
Alex Crichton | f4083a2 | 2014-04-22 05:15:42 | [diff] [blame] | 689 | fn idx(&mut self, j: uint) -> Option<&'a T> { |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 690 | if j >= self.indexable() { |
| 691 | None |
| 692 | } else { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 693 | let idx = wrap_index(self.tail + j, self.ring.len()); |
| 694 | unsafe { Some(self.ring.unsafe_get(idx)) } |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 699 | // FIXME This was implemented differently from Items because of a problem |
| 700 | // with returning the mutable reference. I couldn't find a way to |
| 701 | // make the lifetime checker happy so, but there should be a way. |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 702 | /// `RingBuf` mutable iterator. |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 703 | pub struct MutItems<'a, T:'a> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 704 | ptr: *mut T, |
| 705 | tail: uint, |
| 706 | head: uint, |
| 707 | cap: uint, |
| 708 | marker: marker::ContravariantLifetime<'a>, |
| 709 | marker2: marker::NoCopy |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 710 | } |
| 711 | |
Palmer Cox | 3fd8c8b | 2014-01-15 03:32:24 | [diff] [blame] | 712 | impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 713 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 714 | fn next(&mut self) -> Option<&'a mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 715 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 716 | return None; |
| 717 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 718 | let tail = self.tail; |
| 719 | self.tail = wrap_index(self.tail + 1, self.cap); |
| 720 | if mem::size_of::<T>() != 0 { |
| 721 | unsafe { Some(&mut *self.ptr.offset(tail as int)) } |
| 722 | } else { |
| 723 | // use a none zero pointer |
| 724 | Some(unsafe { mem::transmute(1u) }) |
Alex Crichton | 9d5d97b | 2014-10-15 06:05:01 | [diff] [blame] | 725 | } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | #[inline] |
| 729 | fn size_hint(&self) -> (uint, Option<uint>) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 730 | let len = count(self.tail, self.head, self.cap); |
| 731 | (len, Some(len)) |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Palmer Cox | 3fd8c8b | 2014-01-15 03:32:24 | [diff] [blame] | 735 | impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 736 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 737 | fn next_back(&mut self) -> Option<&'a mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 738 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 739 | return None; |
| 740 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 741 | self.head = wrap_index(self.head - 1, self.cap); |
| 742 | unsafe { Some(&mut *self.ptr.offset(self.head as int)) } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 743 | } |
| 744 | } |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 745 | |
Palmer Cox | 3fd8c8b | 2014-01-15 03:32:24 | [diff] [blame] | 746 | impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {} |
blake2-ppc | 7c369ee7 | 2013-09-01 16:20:24 | [diff] [blame] | 747 | |
Alex Crichton | 748bc3c | 2014-05-30 00:45:07 | [diff] [blame] | 748 | impl<A: PartialEq> PartialEq for RingBuf<A> { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 749 | fn eq(&self, other: &RingBuf<A>) -> bool { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 750 | self.len() == other.len() && |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 751 | self.iter().zip(other.iter()).all(|(a, b)| a.eq(b)) |
| 752 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 753 | fn ne(&self, other: &RingBuf<A>) -> bool { |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 754 | !self.eq(other) |
| 755 | } |
| 756 | } |
| 757 | |
nham | 25acfde | 2014-08-01 20:05:03 | [diff] [blame] | 758 | impl<A: Eq> Eq for RingBuf<A> {} |
| 759 | |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 760 | impl<A: PartialOrd> PartialOrd for RingBuf<A> { |
| 761 | fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> { |
| 762 | iter::order::partial_cmp(self.iter(), other.iter()) |
| 763 | } |
| 764 | } |
| 765 | |
nham | 3737c53 | 2014-08-01 20:22:48 | [diff] [blame] | 766 | impl<A: Ord> Ord for RingBuf<A> { |
| 767 | #[inline] |
| 768 | fn cmp(&self, other: &RingBuf<A>) -> Ordering { |
| 769 | iter::order::cmp(self.iter(), other.iter()) |
| 770 | } |
| 771 | } |
| 772 | |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 773 | impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> { |
| 774 | fn hash(&self, state: &mut S) { |
nham | 9fa4424 | 2014-07-27 16:37:32 | [diff] [blame] | 775 | self.len().hash(state); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 776 | for elt in self.iter() { |
| 777 | elt.hash(state); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 782 | impl<A> Index<uint, A> for RingBuf<A> { |
| 783 | #[inline] |
| 784 | fn index<'a>(&'a self, i: &uint) -> &'a A { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 785 | self.get(*i).expect("Out of bounds access") |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
Alex Crichton | 1d35662 | 2014-10-23 15:42:21 | [diff] [blame] | 789 | impl<A> IndexMut<uint, A> for RingBuf<A> { |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 790 | #[inline] |
Alex Crichton | 1d35662 | 2014-10-23 15:42:21 | [diff] [blame] | 791 | fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut A { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 792 | self.get_mut(*i).expect("Out of bounds access") |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 793 | } |
Alex Crichton | 1d35662 | 2014-10-23 15:42:21 | [diff] [blame] | 794 | } |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 795 | |
Huon Wilson | 53487a0 | 2013-08-13 13:08:14 | [diff] [blame] | 796 | impl<A> FromIterator<A> for RingBuf<A> { |
Brian Anderson | ee05219 | 2014-03-31 04:45:55 | [diff] [blame] | 797 | fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> { |
blake2-ppc | f8ae526 | 2013-07-30 00:06:49 | [diff] [blame] | 798 | let (lower, _) = iterator.size_hint(); |
| 799 | let mut deq = RingBuf::with_capacity(lower); |
| 800 | deq.extend(iterator); |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 801 | deq |
| 802 | } |
| 803 | } |
| 804 | |
gamazeps | 16c8cd9 | 2014-11-08 00:39:39 | [diff] [blame] | 805 | impl<A> Extend<A> for RingBuf<A> { |
Marvin Löbel | 6200e76 | 2014-03-20 13:12:56 | [diff] [blame] | 806 | fn extend<T: Iterator<A>>(&mut self, mut iterator: T) { |
| 807 | for elt in iterator { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 808 | self.push_back(elt); |
blake2-ppc | f8ae526 | 2013-07-30 00:06:49 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
Alex Crichton | 6a58537 | 2014-05-30 01:50:12 | [diff] [blame] | 813 | impl<T: fmt::Show> fmt::Show for RingBuf<T> { |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 814 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 815 | try!(write!(f, "[")); |
| 816 | |
| 817 | for (i, e) in self.iter().enumerate() { |
| 818 | if i != 0 { try!(write!(f, ", ")); } |
| 819 | try!(write!(f, "{}", *e)); |
| 820 | } |
| 821 | |
| 822 | write!(f, "]") |
| 823 | } |
| 824 | } |
| 825 | |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 826 | #[cfg(test)] |
| 827 | mod tests { |
Alex Crichton | 02882fb | 2014-02-28 09:23:06 | [diff] [blame] | 828 | use std::fmt::Show; |
Alex Crichton | 760b93a | 2014-05-30 02:03:06 | [diff] [blame] | 829 | use std::prelude::*; |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 830 | use std::hash; |
Alex Crichton | 760b93a | 2014-05-30 02:03:06 | [diff] [blame] | 831 | use test::Bencher; |
| 832 | use test; |
| 833 | |
Alex Crichton | f47e4b2 | 2014-01-07 06:33:50 | [diff] [blame] | 834 | use super::RingBuf; |
Alex Crichton | 760b93a | 2014-05-30 02:03:06 | [diff] [blame] | 835 | use vec::Vec; |
Patrick Walton | fa5ee93 | 2012-12-28 02:24:18 | [diff] [blame] | 836 | |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 837 | #[test] |
Victor Berger | 52ea83d | 2014-09-22 17:30:06 | [diff] [blame] | 838 | #[allow(deprecated)] |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 839 | fn test_simple() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 840 | let mut d = RingBuf::new(); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 841 | assert_eq!(d.len(), 0u); |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 842 | d.push_front(17i); |
| 843 | d.push_front(42i); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 844 | d.push_back(137); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 845 | assert_eq!(d.len(), 3u); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 846 | d.push_back(137); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 847 | assert_eq!(d.len(), 4u); |
Luqman Aden | 3ef9aa0 | 2014-10-15 07:22:55 | [diff] [blame] | 848 | debug!("{}", d.front()); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 849 | assert_eq!(*d.front().unwrap(), 42); |
Luqman Aden | 3ef9aa0 | 2014-10-15 07:22:55 | [diff] [blame] | 850 | debug!("{}", d.back()); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 851 | assert_eq!(*d.back().unwrap(), 137); |
| 852 | let mut i = d.pop_front(); |
Luqman Aden | 3ef9aa0 | 2014-10-15 07:22:55 | [diff] [blame] | 853 | debug!("{}", i); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 854 | assert_eq!(i, Some(42)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 855 | i = d.pop_back(); |
Luqman Aden | 3ef9aa0 | 2014-10-15 07:22:55 | [diff] [blame] | 856 | debug!("{}", i); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 857 | assert_eq!(i, Some(137)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 858 | i = d.pop_back(); |
Luqman Aden | 3ef9aa0 | 2014-10-15 07:22:55 | [diff] [blame] | 859 | debug!("{}", i); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 860 | assert_eq!(i, Some(137)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 861 | i = d.pop_back(); |
Luqman Aden | 3ef9aa0 | 2014-10-15 07:22:55 | [diff] [blame] | 862 | debug!("{}", i); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 863 | assert_eq!(i, Some(17)); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 864 | assert_eq!(d.len(), 0u); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 865 | d.push_back(3); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 866 | assert_eq!(d.len(), 1u); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 867 | d.push_front(2); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 868 | assert_eq!(d.len(), 2u); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 869 | d.push_back(4); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 870 | assert_eq!(d.len(), 3u); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 871 | d.push_front(1); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 872 | assert_eq!(d.len(), 4u); |
Alex Crichton | 9d5d97b | 2014-10-15 06:05:01 | [diff] [blame] | 873 | debug!("{}", d[0]); |
| 874 | debug!("{}", d[1]); |
| 875 | debug!("{}", d[2]); |
| 876 | debug!("{}", d[3]); |
| 877 | assert_eq!(d[0], 1); |
| 878 | assert_eq!(d[1], 2); |
| 879 | assert_eq!(d[2], 3); |
| 880 | assert_eq!(d[3], 4); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 881 | } |
| 882 | |
Felix S. Klock II | a636f51 | 2013-05-01 23:32:37 | [diff] [blame] | 883 | #[cfg(test)] |
Alex Crichton | 748bc3c | 2014-05-30 00:45:07 | [diff] [blame] | 884 | fn test_parameterized<T:Clone + PartialEq + Show>(a: T, b: T, c: T, d: T) { |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 885 | let mut deq = RingBuf::new(); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 886 | assert_eq!(deq.len(), 0); |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 887 | deq.push_front(a.clone()); |
| 888 | deq.push_front(b.clone()); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 889 | deq.push_back(c.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 890 | assert_eq!(deq.len(), 3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 891 | deq.push_back(d.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 892 | assert_eq!(deq.len(), 4); |
Marvin Löbel | 0ac7a21 | 2013-08-03 23:59:24 | [diff] [blame] | 893 | assert_eq!((*deq.front().unwrap()).clone(), b.clone()); |
| 894 | assert_eq!((*deq.back().unwrap()).clone(), d.clone()); |
| 895 | assert_eq!(deq.pop_front().unwrap(), b.clone()); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 896 | assert_eq!(deq.pop_back().unwrap(), d.clone()); |
| 897 | assert_eq!(deq.pop_back().unwrap(), c.clone()); |
| 898 | assert_eq!(deq.pop_back().unwrap(), a.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 899 | assert_eq!(deq.len(), 0); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 900 | deq.push_back(c.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 901 | assert_eq!(deq.len(), 1); |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 902 | deq.push_front(b.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 903 | assert_eq!(deq.len(), 2); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 904 | deq.push_back(d.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 905 | assert_eq!(deq.len(), 3); |
Patrick Walton | dc4bf17 | 2013-07-13 04:05:59 | [diff] [blame] | 906 | deq.push_front(a.clone()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 907 | assert_eq!(deq.len(), 4); |
NODA, Kai | f27ad3d | 2014-10-05 10:11:17 | [diff] [blame] | 908 | assert_eq!(deq[0].clone(), a.clone()); |
| 909 | assert_eq!(deq[1].clone(), b.clone()); |
| 910 | assert_eq!(deq[2].clone(), c.clone()); |
| 911 | assert_eq!(deq[3].clone(), d.clone()); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 912 | } |
| 913 | |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 914 | #[test] |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 915 | fn test_push_front_grow() { |
| 916 | let mut deq = RingBuf::new(); |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 917 | for i in range(0u, 66) { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 918 | deq.push_front(i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 919 | } |
| 920 | assert_eq!(deq.len(), 66); |
| 921 | |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 922 | for i in range(0u, 66) { |
NODA, Kai | f27ad3d | 2014-10-05 10:11:17 | [diff] [blame] | 923 | assert_eq!(deq[i], 65 - i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 924 | } |
| 925 | |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 926 | let mut deq = RingBuf::new(); |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 927 | for i in range(0u, 66) { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 928 | deq.push_back(i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 929 | } |
| 930 | |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 931 | for i in range(0u, 66) { |
NODA, Kai | f27ad3d | 2014-10-05 10:11:17 | [diff] [blame] | 932 | assert_eq!(deq[i], i); |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 936 | #[test] |
| 937 | fn test_index() { |
| 938 | let mut deq = RingBuf::new(); |
| 939 | for i in range(1u, 4) { |
| 940 | deq.push_front(i); |
| 941 | } |
| 942 | assert_eq!(deq[1], 2); |
| 943 | } |
| 944 | |
| 945 | #[test] |
| 946 | #[should_fail] |
| 947 | fn test_index_out_of_bounds() { |
| 948 | let mut deq = RingBuf::new(); |
| 949 | for i in range(1u, 4) { |
| 950 | deq.push_front(i); |
| 951 | } |
| 952 | deq[3]; |
| 953 | } |
| 954 | |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 955 | #[bench] |
Liigo Zhuang | 408f484 | 2014-04-01 01:16:35 | [diff] [blame] | 956 | fn bench_new(b: &mut test::Bencher) { |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 957 | b.iter(|| { |
Patrick Walton | 8693943 | 2013-08-08 18:38:10 | [diff] [blame] | 958 | let _: RingBuf<u64> = RingBuf::new(); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 959 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | #[bench] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 963 | fn bench_push_back_100(b: &mut test::Bencher) { |
| 964 | let mut deq = RingBuf::with_capacity(100); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 965 | b.iter(|| { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 966 | for i in range(0i, 100) { |
| 967 | deq.push_back(i); |
| 968 | } |
| 969 | deq.clear(); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 970 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | #[bench] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 974 | fn bench_push_front_100(b: &mut test::Bencher) { |
| 975 | let mut deq = RingBuf::with_capacity(100); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 976 | b.iter(|| { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 977 | for i in range(0i, 100) { |
| 978 | deq.push_front(i); |
| 979 | } |
| 980 | deq.clear(); |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 981 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | #[bench] |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 985 | fn bench_pop_100(b: &mut test::Bencher) { |
| 986 | let mut deq = RingBuf::with_capacity(100); |
| 987 | |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 988 | b.iter(|| { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 989 | for i in range(0i, 100) { |
| 990 | deq.push_back(i); |
| 991 | } |
| 992 | while None != deq.pop_back() {} |
| 993 | }) |
| 994 | } |
| 995 | |
| 996 | #[bench] |
| 997 | fn bench_pop_front_100(b: &mut test::Bencher) { |
| 998 | let mut deq = RingBuf::with_capacity(100); |
| 999 | |
| 1000 | b.iter(|| { |
| 1001 | for i in range(0i, 100) { |
| 1002 | deq.push_back(i); |
| 1003 | } |
| 1004 | while None != deq.pop_front() {} |
| 1005 | }) |
| 1006 | } |
| 1007 | |
| 1008 | #[bench] |
| 1009 | fn bench_grow_1025(b: &mut test::Bencher) { |
| 1010 | b.iter(|| { |
| 1011 | let mut deq = RingBuf::new(); |
| 1012 | for i in range(0i, 1025) { |
| 1013 | deq.push_front(i); |
Brendan Zabarauskas | 729060d | 2014-01-30 00:20:34 | [diff] [blame] | 1014 | } |
Patrick Walton | 38efa17 | 2013-11-22 03:20:48 | [diff] [blame] | 1015 | }) |
blake2-ppc | 81933ed | 2013-07-06 03:42:45 | [diff] [blame] | 1016 | } |
| 1017 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 1018 | #[bench] |
| 1019 | fn bench_iter_1000(b: &mut test::Bencher) { |
| 1020 | let ring: RingBuf<int> = range(0i, 1000).collect(); |
| 1021 | |
| 1022 | b.iter(|| { |
| 1023 | let mut sum = 0; |
| 1024 | for &i in ring.iter() { |
| 1025 | sum += i; |
| 1026 | } |
| 1027 | sum |
| 1028 | }) |
| 1029 | } |
| 1030 | |
| 1031 | #[bench] |
| 1032 | fn bench_mut_iter_1000(b: &mut test::Bencher) { |
| 1033 | let mut ring: RingBuf<int> = range(0i, 1000).collect(); |
| 1034 | |
| 1035 | b.iter(|| { |
| 1036 | for i in ring.iter_mut() { |
| 1037 | *i += 1; |
| 1038 | } |
| 1039 | }) |
| 1040 | } |
| 1041 | |
| 1042 | |
Alex Crichton | 748bc3c | 2014-05-30 00:45:07 | [diff] [blame] | 1043 | #[deriving(Clone, PartialEq, Show)] |
Patrick Walton | 99b33f7 | 2013-07-02 19:47:32 | [diff] [blame] | 1044 | enum Taggy { |
| 1045 | One(int), |
| 1046 | Two(int, int), |
| 1047 | Three(int, int, int), |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1048 | } |
| 1049 | |
Alex Crichton | 748bc3c | 2014-05-30 00:45:07 | [diff] [blame] | 1050 | #[deriving(Clone, PartialEq, Show)] |
Patrick Walton | 99b33f7 | 2013-07-02 19:47:32 | [diff] [blame] | 1051 | enum Taggypar<T> { |
| 1052 | Onepar(int), |
| 1053 | Twopar(int, int), |
| 1054 | Threepar(int, int, int), |
| 1055 | } |
| 1056 | |
Alex Crichton | 748bc3c | 2014-05-30 00:45:07 | [diff] [blame] | 1057 | #[deriving(Clone, PartialEq, Show)] |
Erick Tryzelaar | e84576b | 2013-01-22 16:44:24 | [diff] [blame] | 1058 | struct RecCy { |
| 1059 | x: int, |
| 1060 | y: int, |
Patrick Walton | eb4d39e | 2013-01-26 00:57:39 | [diff] [blame] | 1061 | t: Taggy |
Patrick Walton | 9117dcb | 2012-09-20 01:00:26 | [diff] [blame] | 1062 | } |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1063 | |
| 1064 | #[test] |
| 1065 | fn test_param_int() { |
| 1066 | test_parameterized::<int>(5, 72, 64, 175); |
| 1067 | } |
| 1068 | |
| 1069 | #[test] |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1070 | fn test_param_taggy() { |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 1071 | 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] | 1072 | } |
| 1073 | |
| 1074 | #[test] |
| 1075 | fn test_param_taggypar() { |
| 1076 | test_parameterized::<Taggypar<int>>(Onepar::<int>(1), |
Ben Striegel | a605fd0 | 2012-08-11 14:08:42 | [diff] [blame] | 1077 | Twopar::<int>(1, 2), |
| 1078 | Threepar::<int>(1, 2, 3), |
| 1079 | Twopar::<int>(17, 42)); |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1080 | } |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1081 | |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1082 | #[test] |
| 1083 | fn test_param_reccy() { |
Erick Tryzelaar | e84576b | 2013-01-22 16:44:24 | [diff] [blame] | 1084 | let reccy1 = RecCy { x: 1, y: 2, t: One(1) }; |
| 1085 | let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) }; |
| 1086 | let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) }; |
| 1087 | let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) }; |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 1088 | test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 1089 | } |
Erick Tryzelaar | 909d8f0 | 2013-03-30 01:02:44 | [diff] [blame] | 1090 | |
| 1091 | #[test] |
blake2-ppc | 0ff5c17 | 2013-07-06 03:42:45 | [diff] [blame] | 1092 | fn test_with_capacity() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1093 | let mut d = RingBuf::with_capacity(0); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1094 | d.push_back(1i); |
blake2-ppc | 0ff5c17 | 2013-07-06 03:42:45 | [diff] [blame] | 1095 | assert_eq!(d.len(), 1); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1096 | let mut d = RingBuf::with_capacity(50); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1097 | d.push_back(1i); |
blake2-ppc | 0ff5c17 | 2013-07-06 03:42:45 | [diff] [blame] | 1098 | assert_eq!(d.len(), 1); |
| 1099 | } |
| 1100 | |
| 1101 | #[test] |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1102 | fn test_with_capacity_non_power_two() { |
| 1103 | let mut d3 = RingBuf::with_capacity(3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1104 | d3.push_back(1i); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1105 | |
| 1106 | // X = None, | = lo |
| 1107 | // [|1, X, X] |
| 1108 | assert_eq!(d3.pop_front(), Some(1)); |
| 1109 | // [X, |X, X] |
| 1110 | assert_eq!(d3.front(), None); |
| 1111 | |
| 1112 | // [X, |3, X] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1113 | d3.push_back(3); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1114 | // [X, |3, 6] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1115 | d3.push_back(6); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1116 | // [X, X, |6] |
| 1117 | assert_eq!(d3.pop_front(), Some(3)); |
| 1118 | |
| 1119 | // Pushing the lo past half way point to trigger |
| 1120 | // the 'B' scenario for growth |
| 1121 | // [9, X, |6] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1122 | d3.push_back(9); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1123 | // [9, 12, |6] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1124 | d3.push_back(12); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1125 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1126 | d3.push_back(15); |
Kevin Butler | 64896d6 | 2014-08-07 01:11:13 | [diff] [blame] | 1127 | // There used to be a bug here about how the |
| 1128 | // RingBuf made growth assumptions about the |
| 1129 | // underlying Vec which didn't hold and lead |
| 1130 | // to corruption. |
| 1131 | // (Vec grows to next power of two) |
| 1132 | //good- [9, 12, 15, X, X, X, X, |6] |
| 1133 | //bug- [15, 12, X, X, X, |6, X, X] |
| 1134 | assert_eq!(d3.pop_front(), Some(6)); |
| 1135 | |
| 1136 | // Which leads us to the following state which |
| 1137 | // would be a failure case. |
| 1138 | //bug- [15, 12, X, X, X, X, |X, X] |
| 1139 | assert_eq!(d3.front(), Some(&9)); |
| 1140 | } |
| 1141 | |
| 1142 | #[test] |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1143 | fn test_reserve_exact() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1144 | let mut d = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1145 | d.push_back(0u64); |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1146 | d.reserve_exact(50); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1147 | assert!(d.capacity() >= 51); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1148 | let mut d = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1149 | d.push_back(0u32); |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1150 | d.reserve_exact(50); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1151 | assert!(d.capacity() >= 51); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | #[test] |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1155 | fn test_reserve() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1156 | let mut d = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1157 | d.push_back(0u64); |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1158 | d.reserve(50); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 1159 | assert!(d.capacity() >= 51); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1160 | let mut d = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1161 | d.push_back(0u32); |
David Manescu | 65f3578 | 2014-01-31 13:03:20 | [diff] [blame] | 1162 | d.reserve(50); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 1163 | assert!(d.capacity() >= 51); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 1164 | } |
| 1165 | |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1166 | #[test] |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 1167 | fn test_swap() { |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1168 | let mut d: RingBuf<int> = range(0i, 5).collect(); |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 1169 | d.pop_front(); |
| 1170 | d.swap(0, 3); |
Huon Wilson | 4b9a7a2 | 2014-04-05 05:45:42 | [diff] [blame] | 1171 | assert_eq!(d.iter().map(|&x|x).collect::<Vec<int>>(), vec!(4, 2, 3, 1)); |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | #[test] |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1175 | fn test_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1176 | let mut d = RingBuf::new(); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1177 | assert_eq!(d.iter().next(), None); |
blake2-ppc | 9ccf443 | 2013-07-14 20:30:22 | [diff] [blame] | 1178 | assert_eq!(d.iter().size_hint(), (0, Some(0))); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1179 | |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1180 | for i in range(0i, 5) { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1181 | d.push_back(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1182 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 1183 | { |
| 1184 | let b: &[_] = &[&0,&1,&2,&3,&4]; |
| 1185 | assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b); |
| 1186 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 1187 | |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1188 | for i in range(6i, 9) { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1189 | d.push_front(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1190 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 1191 | { |
| 1192 | let b: &[_] = &[&8,&7,&6,&0,&1,&2,&3,&4]; |
| 1193 | assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b); |
| 1194 | } |
blake2-ppc | 9ccf443 | 2013-07-14 20:30:22 | [diff] [blame] | 1195 | |
| 1196 | let mut it = d.iter(); |
| 1197 | let mut len = d.len(); |
| 1198 | loop { |
| 1199 | match it.next() { |
| 1200 | None => break, |
| 1201 | _ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) } |
| 1202 | } |
| 1203 | } |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | #[test] |
| 1207 | fn test_rev_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1208 | let mut d = RingBuf::new(); |
Jonathan S | 03609e5 | 2014-04-21 04:59:12 | [diff] [blame] | 1209 | assert_eq!(d.iter().rev().next(), None); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1210 | |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1211 | for i in range(0i, 5) { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1212 | d.push_back(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1213 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 1214 | { |
| 1215 | let b: &[_] = &[&4,&3,&2,&1,&0]; |
| 1216 | assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b); |
| 1217 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 1218 | |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1219 | for i in range(6i, 9) { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1220 | d.push_front(i); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1221 | } |
Nick Cameron | 37a94b8 | 2014-08-04 12:19:02 | [diff] [blame] | 1222 | let b: &[_] = &[&4,&3,&2,&1,&0,&6,&7,&8]; |
| 1223 | assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 1224 | } |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 1225 | |
| 1226 | #[test] |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1227 | fn test_mut_rev_iter_wrap() { |
| 1228 | let mut d = RingBuf::with_capacity(3); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1229 | assert!(d.iter_mut().rev().next().is_none()); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1230 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1231 | d.push_back(1i); |
| 1232 | d.push_back(2); |
| 1233 | d.push_back(3); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1234 | assert_eq!(d.pop_front(), Some(1)); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1235 | d.push_back(4); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1236 | |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1237 | assert_eq!(d.iter_mut().rev().map(|x| *x).collect::<Vec<int>>(), |
Huon Wilson | 4b9a7a2 | 2014-04-05 05:45:42 | [diff] [blame] | 1238 | vec!(4, 3, 2)); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | #[test] |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1242 | fn test_mut_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1243 | let mut d = RingBuf::new(); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1244 | assert!(d.iter_mut().next().is_none()); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1245 | |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 1246 | for i in range(0u, 3) { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1247 | d.push_front(i); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1248 | } |
| 1249 | |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1250 | for (i, elt) in d.iter_mut().enumerate() { |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1251 | assert_eq!(*elt, 2 - i); |
| 1252 | *elt = i; |
| 1253 | } |
| 1254 | |
| 1255 | { |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1256 | let mut it = d.iter_mut(); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1257 | assert_eq!(*it.next().unwrap(), 0); |
| 1258 | assert_eq!(*it.next().unwrap(), 1); |
| 1259 | assert_eq!(*it.next().unwrap(), 2); |
| 1260 | assert!(it.next().is_none()); |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | #[test] |
| 1265 | fn test_mut_rev_iter() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1266 | let mut d = RingBuf::new(); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1267 | assert!(d.iter_mut().rev().next().is_none()); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1268 | |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 1269 | for i in range(0u, 3) { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1270 | d.push_front(i); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1271 | } |
| 1272 | |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1273 | for (i, elt) in d.iter_mut().rev().enumerate() { |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1274 | assert_eq!(*elt, i); |
| 1275 | *elt = i; |
| 1276 | } |
| 1277 | |
| 1278 | { |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 1279 | let mut it = d.iter_mut().rev(); |
blake2-ppc | f88d532 | 2013-07-06 03:42:45 | [diff] [blame] | 1280 | assert_eq!(*it.next().unwrap(), 0); |
| 1281 | assert_eq!(*it.next().unwrap(), 1); |
| 1282 | assert_eq!(*it.next().unwrap(), 2); |
| 1283 | assert!(it.next().is_none()); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | #[test] |
Brian Anderson | ee05219 | 2014-03-31 04:45:55 | [diff] [blame] | 1288 | fn test_from_iter() { |
Daniel Micay | 6919cf5 | 2013-09-08 15:01:16 | [diff] [blame] | 1289 | use std::iter; |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1290 | let v = vec!(1i,2,3,4,5,6,7); |
Erick Tryzelaar | 68f40d2 | 2013-08-10 03:09:47 | [diff] [blame] | 1291 | let deq: RingBuf<int> = v.iter().map(|&x| x).collect(); |
Huon Wilson | 4b9a7a2 | 2014-04-05 05:45:42 | [diff] [blame] | 1292 | let u: Vec<int> = deq.iter().map(|&x| x).collect(); |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 1293 | assert_eq!(u, v); |
| 1294 | |
Daniel Micay | 6919cf5 | 2013-09-08 15:01:16 | [diff] [blame] | 1295 | let mut seq = iter::count(0u, 2).take(256); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1296 | let deq: RingBuf<uint> = seq.collect(); |
Daniel Micay | 10089455 | 2013-08-03 16:45:23 | [diff] [blame] | 1297 | for (i, &x) in deq.iter().enumerate() { |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 1298 | assert_eq!(2*i, x); |
| 1299 | } |
| 1300 | assert_eq!(deq.len(), 256); |
| 1301 | } |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1302 | |
| 1303 | #[test] |
| 1304 | fn test_clone() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1305 | let mut d = RingBuf::new(); |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1306 | d.push_front(17i); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1307 | d.push_front(42); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1308 | d.push_back(137); |
| 1309 | d.push_back(137); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1310 | assert_eq!(d.len(), 4u); |
| 1311 | let mut e = d.clone(); |
| 1312 | assert_eq!(e.len(), 4u); |
| 1313 | while !d.is_empty() { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1314 | assert_eq!(d.pop_back(), e.pop_back()); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1315 | } |
| 1316 | assert_eq!(d.len(), 0u); |
| 1317 | assert_eq!(e.len(), 0u); |
| 1318 | } |
| 1319 | |
| 1320 | #[test] |
| 1321 | fn test_eq() { |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1322 | let mut d = RingBuf::new(); |
Alex Crichton | 02882fb | 2014-02-28 09:23:06 | [diff] [blame] | 1323 | assert!(d == RingBuf::with_capacity(0)); |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1324 | d.push_front(137i); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1325 | d.push_front(17); |
| 1326 | d.push_front(42); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1327 | d.push_back(137); |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 1328 | let mut e = RingBuf::with_capacity(0); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1329 | e.push_back(42); |
| 1330 | e.push_back(17); |
| 1331 | e.push_back(137); |
| 1332 | e.push_back(137); |
Alex Crichton | 02882fb | 2014-02-28 09:23:06 | [diff] [blame] | 1333 | assert!(&e == &d); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1334 | e.pop_back(); |
| 1335 | e.push_back(0); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1336 | assert!(e != d); |
| 1337 | e.clear(); |
Alex Crichton | 02882fb | 2014-02-28 09:23:06 | [diff] [blame] | 1338 | assert!(e == RingBuf::new()); |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1339 | } |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1340 | |
| 1341 | #[test] |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 1342 | fn test_hash() { |
| 1343 | let mut x = RingBuf::new(); |
| 1344 | let mut y = RingBuf::new(); |
| 1345 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1346 | x.push_back(1i); |
| 1347 | x.push_back(2); |
| 1348 | x.push_back(3); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 1349 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1350 | y.push_back(0i); |
| 1351 | y.push_back(1i); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 1352 | y.pop_front(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1353 | y.push_back(2); |
| 1354 | y.push_back(3); |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 1355 | |
| 1356 | assert!(hash::hash(&x) == hash::hash(&y)); |
| 1357 | } |
| 1358 | |
| 1359 | #[test] |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 1360 | fn test_ord() { |
| 1361 | let x = RingBuf::new(); |
| 1362 | let mut y = RingBuf::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1363 | y.push_back(1i); |
| 1364 | y.push_back(2); |
| 1365 | y.push_back(3); |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 1366 | assert!(x < y); |
| 1367 | assert!(y > x); |
| 1368 | assert!(x <= x); |
| 1369 | assert!(x >= x); |
| 1370 | } |
| 1371 | |
| 1372 | #[test] |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1373 | fn test_show() { |
Niko Matsakis | 9e3d0b0 | 2014-04-21 21:58:52 | [diff] [blame] | 1374 | let ringbuf: RingBuf<int> = range(0i, 10).collect(); |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1375 | assert!(format!("{}", ringbuf).as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"); |
| 1376 | |
| 1377 | let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter() |
| 1378 | .map(|&s| s) |
| 1379 | .collect(); |
| 1380 | assert!(format!("{}", ringbuf).as_slice() == "[just, one, test, more]"); |
| 1381 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame^] | 1382 | |
| 1383 | #[test] |
| 1384 | fn test_drop() { |
| 1385 | static mut drops: uint = 0; |
| 1386 | struct Elem; |
| 1387 | impl Drop for Elem { |
| 1388 | fn drop(&mut self) { |
| 1389 | unsafe { drops += 1; } |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | let mut ring = RingBuf::new(); |
| 1394 | ring.push_back(Elem); |
| 1395 | ring.push_front(Elem); |
| 1396 | ring.push_back(Elem); |
| 1397 | ring.push_front(Elem); |
| 1398 | drop(ring); |
| 1399 | |
| 1400 | assert_eq!(unsafe {drops}, 4); |
| 1401 | } |
| 1402 | |
| 1403 | #[test] |
| 1404 | fn test_drop_with_pop() { |
| 1405 | static mut drops: uint = 0; |
| 1406 | struct Elem; |
| 1407 | impl Drop for Elem { |
| 1408 | fn drop(&mut self) { |
| 1409 | unsafe { drops += 1; } |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | let mut ring = RingBuf::new(); |
| 1414 | ring.push_back(Elem); |
| 1415 | ring.push_front(Elem); |
| 1416 | ring.push_back(Elem); |
| 1417 | ring.push_front(Elem); |
| 1418 | |
| 1419 | drop(ring.pop_back()); |
| 1420 | drop(ring.pop_front()); |
| 1421 | assert_eq!(unsafe {drops}, 2); |
| 1422 | |
| 1423 | drop(ring); |
| 1424 | assert_eq!(unsafe {drops}, 4); |
| 1425 | } |
| 1426 | |
| 1427 | #[test] |
| 1428 | fn test_drop_clear() { |
| 1429 | static mut drops: uint = 0; |
| 1430 | struct Elem; |
| 1431 | impl Drop for Elem { |
| 1432 | fn drop(&mut self) { |
| 1433 | unsafe { drops += 1; } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | let mut ring = RingBuf::new(); |
| 1438 | ring.push_back(Elem); |
| 1439 | ring.push_front(Elem); |
| 1440 | ring.push_back(Elem); |
| 1441 | ring.push_front(Elem); |
| 1442 | ring.clear(); |
| 1443 | assert_eq!(unsafe {drops}, 4); |
| 1444 | |
| 1445 | drop(ring); |
| 1446 | assert_eq!(unsafe {drops}, 4); |
| 1447 | } |
| 1448 | |
| 1449 | #[test] |
| 1450 | fn test_reserve_grow() { |
| 1451 | // test growth path A |
| 1452 | // [T o o H] -> [T o o H . . . . ] |
| 1453 | let mut ring = RingBuf::with_capacity(4); |
| 1454 | for i in range(0i, 3) { |
| 1455 | ring.push_back(i); |
| 1456 | } |
| 1457 | ring.reserve(7); |
| 1458 | for i in range(0i, 3) { |
| 1459 | assert_eq!(ring.pop_front(), Some(i)); |
| 1460 | } |
| 1461 | |
| 1462 | // test growth path B |
| 1463 | // [H T o o] -> [. T o o H . . . ] |
| 1464 | let mut ring = RingBuf::with_capacity(4); |
| 1465 | for i in range(0i, 1) { |
| 1466 | ring.push_back(i); |
| 1467 | assert_eq!(ring.pop_front(), Some(i)); |
| 1468 | } |
| 1469 | for i in range(0i, 3) { |
| 1470 | ring.push_back(i); |
| 1471 | } |
| 1472 | ring.reserve(7); |
| 1473 | for i in range(0i, 3) { |
| 1474 | assert_eq!(ring.pop_front(), Some(i)); |
| 1475 | } |
| 1476 | |
| 1477 | // test growth path C |
| 1478 | // [o o H T] -> [o o H . . . . T ] |
| 1479 | let mut ring = RingBuf::with_capacity(4); |
| 1480 | for i in range(0i, 3) { |
| 1481 | ring.push_back(i); |
| 1482 | assert_eq!(ring.pop_front(), Some(i)); |
| 1483 | } |
| 1484 | for i in range(0i, 3) { |
| 1485 | ring.push_back(i); |
| 1486 | } |
| 1487 | ring.reserve(7); |
| 1488 | for i in range(0i, 3) { |
| 1489 | assert_eq!(ring.pop_front(), Some(i)); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | #[test] |
| 1494 | fn test_get() { |
| 1495 | let mut ring = RingBuf::new(); |
| 1496 | ring.push_back(0i); |
| 1497 | assert_eq!(ring.get(0), Some(&0)); |
| 1498 | assert_eq!(ring.get(1), None); |
| 1499 | |
| 1500 | ring.push_back(1); |
| 1501 | assert_eq!(ring.get(0), Some(&0)); |
| 1502 | assert_eq!(ring.get(1), Some(&1)); |
| 1503 | assert_eq!(ring.get(2), None); |
| 1504 | |
| 1505 | ring.push_back(2); |
| 1506 | assert_eq!(ring.get(0), Some(&0)); |
| 1507 | assert_eq!(ring.get(1), Some(&1)); |
| 1508 | assert_eq!(ring.get(2), Some(&2)); |
| 1509 | assert_eq!(ring.get(3), None); |
| 1510 | |
| 1511 | assert_eq!(ring.pop_front(), Some(0)); |
| 1512 | assert_eq!(ring.get(0), Some(&1)); |
| 1513 | assert_eq!(ring.get(1), Some(&2)); |
| 1514 | assert_eq!(ring.get(2), None); |
| 1515 | |
| 1516 | assert_eq!(ring.pop_front(), Some(1)); |
| 1517 | assert_eq!(ring.get(0), Some(&2)); |
| 1518 | assert_eq!(ring.get(1), None); |
| 1519 | |
| 1520 | assert_eq!(ring.pop_front(), Some(2)); |
| 1521 | assert_eq!(ring.get(0), None); |
| 1522 | assert_eq!(ring.get(1), None); |
| 1523 | } |
| 1524 | |
| 1525 | #[test] |
| 1526 | fn test_get_mut() { |
| 1527 | let mut ring = RingBuf::new(); |
| 1528 | for i in range(0i, 3) { |
| 1529 | ring.push_back(i); |
| 1530 | } |
| 1531 | |
| 1532 | match ring.get_mut(1) { |
| 1533 | Some(x) => *x = -1, |
| 1534 | None => () |
| 1535 | }; |
| 1536 | |
| 1537 | assert_eq!(ring.get_mut(0), Some(&mut 0)); |
| 1538 | assert_eq!(ring.get_mut(1), Some(&mut -1)); |
| 1539 | assert_eq!(ring.get_mut(2), Some(&mut 2)); |
| 1540 | assert_eq!(ring.get_mut(3), None); |
| 1541 | |
| 1542 | assert_eq!(ring.pop_front(), Some(0)); |
| 1543 | assert_eq!(ring.get_mut(0), Some(&mut -1)); |
| 1544 | assert_eq!(ring.get_mut(1), Some(&mut 2)); |
| 1545 | assert_eq!(ring.get_mut(2), None); |
| 1546 | } |
Michael Sullivan | c854d6e | 2012-07-03 17:52:32 | [diff] [blame] | 1547 | } |