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