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; |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 28 | use core::num::wrapping::WrappingOps; |
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}; |
Oliver Schneider | 6584ae5 | 2015-03-13 08:56:18 | [diff] [blame] | 31 | use core::slice; |
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 | |
Florian Zeitz | f35f973 | 2015-02-27 14:36:53 | [diff] [blame] | 42 | const INITIAL_CAPACITY: usize = 7; // 2^3 - 1 |
| 43 | const 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 | impl<T: Clone> Clone for VecDeque<T> { |
| 63 | fn clone(&self) -> VecDeque<T> { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 64 | self.iter().cloned().collect() |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | #[unsafe_destructor] |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 69 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 70 | impl<T> Drop for VecDeque<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 71 | fn drop(&mut self) { |
| 72 | self.clear(); |
| 73 | unsafe { |
| 74 | if mem::size_of::<T>() != 0 { |
Niko Matsakis | 8dbdcdb | 2015-02-12 15:38:45 | [diff] [blame] | 75 | heap::deallocate(*self.ptr as *mut u8, |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 76 | self.cap * mem::size_of::<T>(), |
| 77 | mem::min_align_of::<T>()) |
| 78 | } |
| 79 | } |
| 80 | } |
Marijn Haverbeke | 26610db | 2012-01-11 11:49:33 | [diff] [blame] | 81 | } |
Roy Frostig | 9c81889 | 2010-07-21 01:03:09 | [diff] [blame] | 82 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 83 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 84 | impl<T> Default for VecDeque<T> { |
Tom Jakubowski | d6a3941 | 2014-06-09 07:30:04 | [diff] [blame] | 85 | #[inline] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 86 | fn default() -> VecDeque<T> { VecDeque::new() } |
Tom Jakubowski | d6a3941 | 2014-06-09 07:30:04 | [diff] [blame] | 87 | } |
| 88 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 89 | impl<T> VecDeque<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 90 | /// Turn ptr into a slice |
| 91 | #[inline] |
Alexis Beingessner | 8dbaa71 | 2014-12-31 00:07:53 | [diff] [blame] | 92 | unsafe fn buffer_as_slice(&self) -> &[T] { |
Oliver Schneider | 6584ae5 | 2015-03-13 08:56:18 | [diff] [blame] | 93 | slice::from_raw_parts(*self.ptr, self.cap) |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /// Turn ptr into a mut slice |
| 97 | #[inline] |
Alexis Beingessner | 8dbaa71 | 2014-12-31 00:07:53 | [diff] [blame] | 98 | unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] { |
Oliver Schneider | 6584ae5 | 2015-03-13 08:56:18 | [diff] [blame] | 99 | slice::from_raw_parts_mut(*self.ptr, self.cap) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /// Moves an element out of the buffer |
| 103 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 104 | unsafe fn buffer_read(&mut self, off: usize) -> T { |
| 105 | ptr::read(self.ptr.offset(off as isize)) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /// Writes an element into the buffer, moving it. |
| 109 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 110 | unsafe fn buffer_write(&mut self, off: usize, t: T) { |
| 111 | ptr::write(self.ptr.offset(off as isize), t); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /// Returns true iff the buffer is at capacity |
| 115 | #[inline] |
| 116 | fn is_full(&self) -> bool { self.cap - self.len() == 1 } |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 117 | |
Alex Crichton | 665ea96 | 2015-02-18 03:00:20 | [diff] [blame] | 118 | /// Returns the index in the underlying buffer for a given logical element |
| 119 | /// index. |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 120 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 121 | fn wrap_index(&self, idx: usize) -> usize { wrap_index(idx, self.cap) } |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 122 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 123 | /// Returns the index in the underlying buffer for a given logical element |
| 124 | /// index + addend. |
| 125 | #[inline] |
| 126 | fn wrap_add(&self, idx: usize, addend: usize) -> usize { |
| 127 | wrap_index(idx.wrapping_add(addend), self.cap) |
| 128 | } |
| 129 | |
| 130 | /// Returns the index in the underlying buffer for a given logical element |
| 131 | /// index - subtrahend. |
| 132 | #[inline] |
| 133 | fn wrap_sub(&self, idx: usize, subtrahend: usize) -> usize { |
| 134 | wrap_index(idx.wrapping_sub(subtrahend), self.cap) |
| 135 | } |
| 136 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 137 | /// Copies a contiguous block of memory len long from src to dst |
| 138 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 139 | unsafe fn copy(&self, dst: usize, src: usize, len: usize) { |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 140 | debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 141 | self.cap); |
| 142 | debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 143 | self.cap); |
Alex Crichton | ab45694 | 2015-02-23 19:39:16 | [diff] [blame] | 144 | ptr::copy( |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 145 | self.ptr.offset(src as isize), |
Alex Crichton | acd48a2 | 2015-03-27 18:12:28 | [diff] [blame^] | 146 | self.ptr.offset(dst as isize), |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 147 | len); |
| 148 | } |
| 149 | |
| 150 | /// Copies a contiguous block of memory len long from src to dst |
| 151 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 152 | unsafe fn copy_nonoverlapping(&self, dst: usize, src: usize, len: usize) { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 153 | debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 154 | self.cap); |
| 155 | debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len, |
| 156 | self.cap); |
Alex Crichton | ab45694 | 2015-02-23 19:39:16 | [diff] [blame] | 157 | ptr::copy_nonoverlapping( |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 158 | self.ptr.offset(src as isize), |
Alex Crichton | acd48a2 | 2015-03-27 18:12:28 | [diff] [blame^] | 159 | self.ptr.offset(dst as isize), |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 160 | len); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 161 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 162 | } |
| 163 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 164 | impl<T> VecDeque<T> { |
| 165 | /// Creates an empty `VecDeque`. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 166 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 167 | pub fn new() -> VecDeque<T> { |
| 168 | VecDeque::with_capacity(INITIAL_CAPACITY) |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 169 | } |
| 170 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 171 | /// Creates an empty `VecDeque` with space for at least `n` elements. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 172 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 173 | pub fn with_capacity(n: usize) -> VecDeque<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 174 | // +1 since the ringbuffer always leaves one space empty |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 175 | let cap = cmp::max(n + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); |
| 176 | assert!(cap > n, "capacity overflow"); |
Colin Sherratt | 6277e3b | 2014-11-14 09:21:44 | [diff] [blame] | 177 | let size = cap.checked_mul(mem::size_of::<T>()) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 178 | .expect("capacity overflow"); |
| 179 | |
Niko Matsakis | 8dbdcdb | 2015-02-12 15:38:45 | [diff] [blame] | 180 | let ptr = unsafe { |
| 181 | if mem::size_of::<T>() != 0 { |
Colin Sherratt | ba24e33 | 2014-11-10 03:34:53 | [diff] [blame] | 182 | let ptr = heap::allocate(size, mem::min_align_of::<T>()) as *mut T;; |
| 183 | if ptr.is_null() { ::alloc::oom() } |
Niko Matsakis | 8dbdcdb | 2015-02-12 15:38:45 | [diff] [blame] | 184 | Unique::new(ptr) |
| 185 | } else { |
| 186 | Unique::new(heap::EMPTY as *mut T) |
Colin Sherratt | ba24e33 | 2014-11-10 03:34:53 | [diff] [blame] | 187 | } |
Colin Sherratt | ba24e33 | 2014-11-10 03:34:53 | [diff] [blame] | 188 | }; |
| 189 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 190 | VecDeque { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 191 | tail: 0, |
| 192 | head: 0, |
| 193 | cap: cap, |
Niko Matsakis | 8dbdcdb | 2015-02-12 15:38:45 | [diff] [blame] | 194 | ptr: ptr, |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 195 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 196 | } |
| 197 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 198 | /// Retrieves an element in the `VecDeque` by index. |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 199 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 200 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 201 | /// |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 202 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 203 | /// use std::collections::VecDeque; |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 204 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 205 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 206 | /// buf.push_back(3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 207 | /// buf.push_back(4); |
| 208 | /// buf.push_back(5); |
| 209 | /// assert_eq!(buf.get(1).unwrap(), &4); |
| 210 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 211 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 212 | pub fn get(&self, i: usize) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 213 | if i < self.len() { |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 214 | let idx = self.wrap_add(self.tail, i); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 215 | unsafe { Some(&*self.ptr.offset(idx as isize)) } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 216 | } else { |
| 217 | None |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 221 | /// Retrieves an element in the `VecDeque` mutably by index. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 222 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 223 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 224 | /// |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 225 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 226 | /// use std::collections::VecDeque; |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 227 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 228 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 229 | /// buf.push_back(3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 230 | /// buf.push_back(4); |
| 231 | /// buf.push_back(5); |
| 232 | /// match buf.get_mut(1) { |
| 233 | /// None => {} |
| 234 | /// Some(elem) => { |
| 235 | /// *elem = 7; |
| 236 | /// } |
| 237 | /// } |
| 238 | /// |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 239 | /// assert_eq!(buf[1], 7); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 240 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 241 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 242 | pub fn get_mut(&mut self, i: usize) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 243 | if i < self.len() { |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 244 | let idx = self.wrap_add(self.tail, i); |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 245 | unsafe { Some(&mut *self.ptr.offset(idx as isize)) } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 246 | } else { |
| 247 | None |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 248 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 249 | } |
| 250 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 251 | /// Swaps elements at indices `i` and `j`. |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 252 | /// |
| 253 | /// `i` and `j` may be equal. |
| 254 | /// |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 255 | /// Fails if there is no element with either index. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 256 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 257 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 258 | /// |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 259 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 260 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 261 | /// use std::collections::VecDeque; |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 262 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 263 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 264 | /// buf.push_back(3); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 265 | /// buf.push_back(4); |
| 266 | /// buf.push_back(5); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 267 | /// buf.swap(0, 2); |
P1start | fd10d20 | 2014-08-02 06:39:39 | [diff] [blame] | 268 | /// assert_eq!(buf[0], 5); |
| 269 | /// assert_eq!(buf[2], 3); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 270 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 271 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 272 | pub fn swap(&mut self, i: usize, j: usize) { |
blake2-ppc | 57757a8 | 2013-09-26 07:19:26 | [diff] [blame] | 273 | assert!(i < self.len()); |
| 274 | assert!(j < self.len()); |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 275 | let ri = self.wrap_add(self.tail, i); |
| 276 | let rj = self.wrap_add(self.tail, j); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 277 | unsafe { |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 278 | 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] | 279 | } |
blake2-ppc | 7052371 | 2013-07-10 13:27:14 | [diff] [blame] | 280 | } |
| 281 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 282 | /// Returns the number of elements the `VecDeque` can hold without |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 283 | /// reallocating. |
| 284 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 285 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 286 | /// |
| 287 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 288 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 289 | /// use std::collections::VecDeque; |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 290 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 291 | /// let buf: VecDeque<i32> = VecDeque::with_capacity(10); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 292 | /// assert!(buf.capacity() >= 10); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 293 | /// ``` |
| 294 | #[inline] |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 295 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 296 | pub fn capacity(&self) -> usize { self.cap - 1 } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 297 | |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 298 | /// 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] | 299 | /// given `VecDeque`. Does nothing if the capacity is already sufficient. |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 300 | /// |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 301 | /// Note that the allocator may give the collection more space than it requests. Therefore |
| 302 | /// capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future |
| 303 | /// insertions are expected. |
| 304 | /// |
| 305 | /// # Panics |
| 306 | /// |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 307 | /// Panics if the new capacity overflows `usize`. |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 308 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 309 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 310 | /// |
| 311 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 312 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 313 | /// use std::collections::VecDeque; |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 314 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 315 | /// let mut buf: VecDeque<i32> = vec![1].into_iter().collect(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 316 | /// buf.reserve_exact(10); |
| 317 | /// assert!(buf.capacity() >= 11); |
| 318 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 319 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 320 | pub fn reserve_exact(&mut self, additional: usize) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 321 | self.reserve(additional); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /// Reserves capacity for at least `additional` more elements to be inserted in the given |
| 325 | /// `Ringbuf`. The collection may reserve more space to avoid frequent reallocations. |
| 326 | /// |
| 327 | /// # Panics |
| 328 | /// |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 329 | /// Panics if the new capacity overflows `usize`. |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 330 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 331 | /// # Examples |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 332 | /// |
| 333 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 334 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 335 | /// use std::collections::VecDeque; |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 336 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 337 | /// let mut buf: VecDeque<i32> = vec![1].into_iter().collect(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 338 | /// buf.reserve(10); |
| 339 | /// assert!(buf.capacity() >= 11); |
| 340 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 341 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 342 | pub fn reserve(&mut self, additional: usize) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 343 | let new_len = self.len() + additional; |
| 344 | assert!(new_len + 1 > self.len(), "capacity overflow"); |
| 345 | if new_len > self.capacity() { |
Colin Sherratt | 6277e3b | 2014-11-14 09:21:44 | [diff] [blame] | 346 | let count = (new_len + 1).next_power_of_two(); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 347 | assert!(count >= new_len + 1); |
| 348 | |
| 349 | if mem::size_of::<T>() != 0 { |
| 350 | let old = self.cap * mem::size_of::<T>(); |
Colin Sherratt | 6277e3b | 2014-11-14 09:21:44 | [diff] [blame] | 351 | let new = count.checked_mul(mem::size_of::<T>()) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 352 | .expect("capacity overflow"); |
| 353 | unsafe { |
Niko Matsakis | 8dbdcdb | 2015-02-12 15:38:45 | [diff] [blame] | 354 | let ptr = heap::reallocate(*self.ptr as *mut u8, |
| 355 | old, |
| 356 | new, |
| 357 | mem::min_align_of::<T>()) as *mut T; |
| 358 | if ptr.is_null() { ::alloc::oom() } |
| 359 | self.ptr = Unique::new(ptr); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | // Move the shortest contiguous section of the ring buffer |
| 364 | // T H |
| 365 | // [o o o o o o o . ] |
| 366 | // T H |
| 367 | // A [o o o o o o o . . . . . . . . . ] |
| 368 | // H T |
| 369 | // [o o . o o o o o ] |
| 370 | // T H |
| 371 | // B [. . . o o o o o o o . . . . . . ] |
| 372 | // H T |
| 373 | // [o o o o o . o o ] |
| 374 | // H T |
| 375 | // C [o o o o o . . . . . . . . . o o ] |
| 376 | |
| 377 | let oldcap = self.cap; |
| 378 | self.cap = count; |
| 379 | |
| 380 | if self.tail <= self.head { // A |
| 381 | // Nop |
| 382 | } else if self.head < oldcap - self.tail { // B |
| 383 | unsafe { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 384 | self.copy_nonoverlapping(oldcap, 0, self.head); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 385 | } |
| 386 | self.head += oldcap; |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 387 | debug_assert!(self.head > self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 388 | } else { // C |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 389 | let new_tail = count - (oldcap - self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 390 | unsafe { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 391 | self.copy_nonoverlapping(new_tail, self.tail, oldcap - self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 392 | } |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 393 | self.tail = new_tail; |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 394 | debug_assert!(self.head < self.tail); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 395 | } |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 396 | debug_assert!(self.head < self.cap); |
| 397 | debug_assert!(self.tail < self.cap); |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 398 | debug_assert!(self.cap.count_ones() == 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 399 | } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 400 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 401 | |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 402 | /// Shrinks the capacity of the ringbuf as much as possible. |
| 403 | /// |
| 404 | /// It will drop down as close as possible to the length but the allocator may still inform the |
| 405 | /// ringbuf that there is space for a few more elements. |
| 406 | /// |
| 407 | /// # Examples |
| 408 | /// |
| 409 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 410 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 411 | /// use std::collections::VecDeque; |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 412 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 413 | /// let mut buf = VecDeque::with_capacity(15); |
Alexis | e15538d | 2015-02-06 18:57:13 | [diff] [blame] | 414 | /// buf.extend(0..4); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 415 | /// assert_eq!(buf.capacity(), 15); |
| 416 | /// buf.shrink_to_fit(); |
| 417 | /// assert!(buf.capacity() >= 4); |
| 418 | /// ``` |
| 419 | pub fn shrink_to_fit(&mut self) { |
| 420 | // +1 since the ringbuffer always leaves one space empty |
| 421 | // len + 1 can't overflow for an existing, well-formed ringbuf. |
| 422 | let target_cap = cmp::max(self.len() + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); |
| 423 | if target_cap < self.cap { |
| 424 | // There are three cases of interest: |
| 425 | // All elements are out of desired bounds |
| 426 | // Elements are contiguous, and head is out of desired bounds |
| 427 | // Elements are discontiguous, and tail is out of desired bounds |
| 428 | // |
| 429 | // At all other times, element positions are unaffected. |
| 430 | // |
| 431 | // Indicates that elements at the head should be moved. |
| 432 | let head_outside = self.head == 0 || self.head >= target_cap; |
| 433 | // Move elements from out of desired bounds (positions after target_cap) |
| 434 | if self.tail >= target_cap && head_outside { |
| 435 | // T H |
| 436 | // [. . . . . . . . o o o o o o o . ] |
| 437 | // T H |
| 438 | // [o o o o o o o . ] |
| 439 | unsafe { |
| 440 | self.copy_nonoverlapping(0, self.tail, self.len()); |
| 441 | } |
| 442 | self.head = self.len(); |
| 443 | self.tail = 0; |
| 444 | } else if self.tail != 0 && self.tail < target_cap && head_outside { |
| 445 | // T H |
| 446 | // [. . . o o o o o o o . . . . . . ] |
| 447 | // H T |
| 448 | // [o o . o o o o o ] |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 449 | let len = self.wrap_sub(self.head, target_cap); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 450 | unsafe { |
| 451 | self.copy_nonoverlapping(0, target_cap, len); |
| 452 | } |
| 453 | self.head = len; |
| 454 | debug_assert!(self.head < self.tail); |
| 455 | } else if self.tail >= target_cap { |
| 456 | // H T |
| 457 | // [o o o o o . . . . . . . . . o o ] |
| 458 | // H T |
| 459 | // [o o o o o . o o ] |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 460 | debug_assert!(self.wrap_sub(self.head, 1) < target_cap); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 461 | let len = self.cap - self.tail; |
| 462 | let new_tail = target_cap - len; |
| 463 | unsafe { |
| 464 | self.copy_nonoverlapping(new_tail, self.tail, len); |
| 465 | } |
| 466 | self.tail = new_tail; |
| 467 | debug_assert!(self.head < self.tail); |
| 468 | } |
| 469 | |
| 470 | if mem::size_of::<T>() != 0 { |
| 471 | let old = self.cap * mem::size_of::<T>(); |
| 472 | let new_size = target_cap * mem::size_of::<T>(); |
| 473 | unsafe { |
Niko Matsakis | 8dbdcdb | 2015-02-12 15:38:45 | [diff] [blame] | 474 | let ptr = heap::reallocate(*self.ptr as *mut u8, |
| 475 | old, |
| 476 | new_size, |
| 477 | mem::min_align_of::<T>()) as *mut T; |
| 478 | if ptr.is_null() { ::alloc::oom() } |
| 479 | self.ptr = Unique::new(ptr); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | self.cap = target_cap; |
| 483 | debug_assert!(self.head < self.cap); |
| 484 | debug_assert!(self.tail < self.cap); |
| 485 | debug_assert!(self.cap.count_ones() == 1); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /// Shorten a ringbuf, dropping excess elements from the back. |
| 490 | /// |
| 491 | /// If `len` is greater than the ringbuf's current length, this has no |
| 492 | /// effect. |
| 493 | /// |
| 494 | /// # Examples |
| 495 | /// |
| 496 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 497 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 498 | /// use std::collections::VecDeque; |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 499 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 500 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 501 | /// buf.push_back(5); |
| 502 | /// buf.push_back(10); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 503 | /// buf.push_back(15); |
| 504 | /// buf.truncate(1); |
| 505 | /// assert_eq!(buf.len(), 1); |
| 506 | /// assert_eq!(Some(&5), buf.get(0)); |
| 507 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 508 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 509 | reason = "matches collection reform specification; waiting on panic semantics")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 510 | pub fn truncate(&mut self, len: usize) { |
Jorge Aparicio | efc97a5 | 2015-01-26 21:05:07 | [diff] [blame] | 511 | for _ in len..self.len() { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 512 | self.pop_back(); |
| 513 | } |
| 514 | } |
| 515 | |
P1start | f2aa88c | 2014-08-04 10:48:39 | [diff] [blame] | 516 | /// Returns a front-to-back iterator. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 517 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 518 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 519 | /// |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 520 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 521 | /// # #![feature(core)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 522 | /// use std::collections::VecDeque; |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 523 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 524 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 525 | /// buf.push_back(5); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 526 | /// buf.push_back(3); |
| 527 | /// buf.push_back(4); |
Nick Cameron | 52ef462 | 2014-08-06 09:59:40 | [diff] [blame] | 528 | /// let b: &[_] = &[&5, &3, &4]; |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 529 | /// assert_eq!(buf.iter().collect::<Vec<&i32>>().as_slice(), b); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 530 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 531 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 532 | pub fn iter(&self) -> Iter<T> { |
| 533 | Iter { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 534 | tail: self.tail, |
| 535 | head: self.head, |
| 536 | ring: unsafe { self.buffer_as_slice() } |
| 537 | } |
blake2-ppc | 3385e79 | 2013-07-15 23:13:26 | [diff] [blame] | 538 | } |
| 539 | |
Andrew Wagner | 8fcc832 | 2014-12-15 09:22:49 | [diff] [blame] | 540 | /// Returns a front-to-back iterator that returns mutable references. |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 541 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 542 | /// # Examples |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 543 | /// |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 544 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 545 | /// # #![feature(core)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 546 | /// use std::collections::VecDeque; |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 547 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 548 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 549 | /// buf.push_back(5); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 550 | /// buf.push_back(3); |
| 551 | /// buf.push_back(4); |
Aaron Turon | fc525ee | 2014-09-15 03:27:36 | [diff] [blame] | 552 | /// for num in buf.iter_mut() { |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 553 | /// *num = *num - 2; |
| 554 | /// } |
Nick Cameron | 52ef462 | 2014-08-06 09:59:40 | [diff] [blame] | 555 | /// let b: &[_] = &[&mut 3, &mut 1, &mut 2]; |
Alex Crichton | 77de3ee | 2015-03-26 16:57:58 | [diff] [blame] | 556 | /// assert_eq!(&buf.iter_mut().collect::<Vec<&mut i32>>()[..], b); |
nham | ebe8097 | 2014-07-17 23:19:51 | [diff] [blame] | 557 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 558 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 559 | pub fn iter_mut(&mut self) -> IterMut<T> { |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 560 | IterMut { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 561 | tail: self.tail, |
| 562 | head: self.head, |
Edward Wang | 101498c | 2015-02-25 10:11:23 | [diff] [blame] | 563 | ring: unsafe { self.buffer_as_mut_slice() }, |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 564 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 565 | } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 566 | |
Andrew Paseltiner | 88edf97 | 2015-03-23 12:51:29 | [diff] [blame] | 567 | /// Consumes the list into a front-to-back iterator yielding elements by value. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 568 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 569 | pub fn into_iter(self) -> IntoIter<T> { |
| 570 | IntoIter { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 571 | inner: self, |
| 572 | } |
| 573 | } |
| 574 | |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 575 | /// Returns a pair of slices which contain, in order, the contents of the |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 576 | /// `VecDeque`. |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 577 | #[inline] |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 578 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 579 | reason = "matches collection reform specification, waiting for dust to settle")] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 580 | pub fn as_slices(&self) -> (&[T], &[T]) { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 581 | unsafe { |
| 582 | let contiguous = self.is_contiguous(); |
| 583 | let buf = self.buffer_as_slice(); |
| 584 | if contiguous { |
| 585 | let (empty, buf) = buf.split_at(0); |
Jorge Aparicio | 517f1cc | 2015-01-07 16:58:31 | [diff] [blame] | 586 | (&buf[self.tail..self.head], empty) |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 587 | } else { |
| 588 | let (mid, right) = buf.split_at(self.tail); |
| 589 | let (left, _) = mid.split_at(self.head); |
| 590 | (right, left) |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /// Returns a pair of slices which contain, in order, the contents of the |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 596 | /// `VecDeque`. |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 597 | #[inline] |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 598 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 599 | reason = "matches collection reform specification, waiting for dust to settle")] |
Alexis | 1420ceb | 2015-02-05 18:48:20 | [diff] [blame] | 600 | pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) { |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 601 | unsafe { |
| 602 | let contiguous = self.is_contiguous(); |
| 603 | let head = self.head; |
| 604 | let tail = self.tail; |
| 605 | let buf = self.buffer_as_mut_slice(); |
| 606 | |
| 607 | if contiguous { |
| 608 | let (empty, buf) = buf.split_at_mut(0); |
Aaron Turon | a506d4c | 2015-01-18 00:15:52 | [diff] [blame] | 609 | (&mut buf[tail .. head], empty) |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 610 | } else { |
| 611 | let (mid, right) = buf.split_at_mut(tail); |
| 612 | let (left, _) = mid.split_at_mut(head); |
| 613 | |
| 614 | (right, left) |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 619 | /// Returns the number of elements in the `VecDeque`. |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 620 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 621 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 622 | /// |
| 623 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 624 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 625 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 626 | /// let mut v = VecDeque::new(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 627 | /// assert_eq!(v.len(), 0); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 628 | /// v.push_back(1); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 629 | /// assert_eq!(v.len(), 1); |
| 630 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 631 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 632 | pub fn len(&self) -> usize { count(self.tail, self.head, self.cap) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 633 | |
| 634 | /// Returns true if the buffer contains no elements |
| 635 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 636 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 637 | /// |
| 638 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 639 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 640 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 641 | /// let mut v = VecDeque::new(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 642 | /// assert!(v.is_empty()); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 643 | /// v.push_front(1); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 644 | /// assert!(!v.is_empty()); |
| 645 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 646 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 647 | pub fn is_empty(&self) -> bool { self.len() == 0 } |
| 648 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 649 | /// Creates a draining iterator that clears the `VecDeque` and iterates over |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 650 | /// the removed items from start to end. |
| 651 | /// |
| 652 | /// # Examples |
| 653 | /// |
| 654 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 655 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 656 | /// use std::collections::VecDeque; |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 657 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 658 | /// let mut v = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 659 | /// v.push_back(1); |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 660 | /// assert_eq!(v.drain().next(), Some(1)); |
| 661 | /// assert!(v.is_empty()); |
| 662 | /// ``` |
| 663 | #[inline] |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 664 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 665 | reason = "matches collection reform specification, waiting for dust to settle")] |
Alexis Beingessner | 8dbaa71 | 2014-12-31 00:07:53 | [diff] [blame] | 666 | pub fn drain(&mut self) -> Drain<T> { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 667 | Drain { |
| 668 | inner: self, |
| 669 | } |
| 670 | } |
| 671 | |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 672 | /// Clears the buffer, removing all values. |
| 673 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 674 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 675 | /// |
| 676 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 677 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 678 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 679 | /// let mut v = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 680 | /// v.push_back(1); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 681 | /// v.clear(); |
| 682 | /// assert!(v.is_empty()); |
| 683 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 684 | #[stable(feature = "rust1", since = "1.0.0")] |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 685 | #[inline] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 686 | pub fn clear(&mut self) { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 687 | self.drain(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /// Provides a reference to the front element, or `None` if the sequence is |
| 691 | /// empty. |
| 692 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 693 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 694 | /// |
| 695 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 696 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 697 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 698 | /// let mut d = VecDeque::new(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 699 | /// assert_eq!(d.front(), None); |
| 700 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 701 | /// d.push_back(1); |
| 702 | /// d.push_back(2); |
| 703 | /// assert_eq!(d.front(), Some(&1)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 704 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 705 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 706 | pub fn front(&self) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 707 | if !self.is_empty() { Some(&self[0]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /// Provides a mutable reference to the front element, or `None` if the |
| 711 | /// sequence is empty. |
| 712 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 713 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 714 | /// |
| 715 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 716 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 717 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 718 | /// let mut d = VecDeque::new(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 719 | /// assert_eq!(d.front_mut(), None); |
| 720 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 721 | /// d.push_back(1); |
| 722 | /// d.push_back(2); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 723 | /// match d.front_mut() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 724 | /// Some(x) => *x = 9, |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 725 | /// None => (), |
| 726 | /// } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 727 | /// assert_eq!(d.front(), Some(&9)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 728 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 729 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 730 | pub fn front_mut(&mut self) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 731 | if !self.is_empty() { Some(&mut self[0]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /// Provides a reference to the back element, or `None` if the sequence is |
| 735 | /// empty. |
| 736 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 737 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 738 | /// |
| 739 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 740 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 741 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 742 | /// let mut d = VecDeque::new(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 743 | /// assert_eq!(d.back(), None); |
| 744 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 745 | /// d.push_back(1); |
| 746 | /// d.push_back(2); |
| 747 | /// assert_eq!(d.back(), Some(&2)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 748 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 749 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 750 | pub fn back(&self) -> Option<&T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 751 | if !self.is_empty() { Some(&self[self.len() - 1]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /// Provides a mutable reference to the back element, or `None` if the |
| 755 | /// sequence is empty. |
| 756 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 757 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 758 | /// |
| 759 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 760 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 761 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 762 | /// let mut d = VecDeque::new(); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 763 | /// assert_eq!(d.back(), None); |
| 764 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 765 | /// d.push_back(1); |
| 766 | /// d.push_back(2); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 767 | /// match d.back_mut() { |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 768 | /// Some(x) => *x = 9, |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 769 | /// None => (), |
| 770 | /// } |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 771 | /// assert_eq!(d.back(), Some(&9)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 772 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 773 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 774 | pub fn back_mut(&mut self) -> Option<&mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 775 | let len = self.len(); |
| 776 | if !self.is_empty() { Some(&mut self[len - 1]) } else { None } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /// Removes the first element and returns it, or `None` if the sequence is |
| 780 | /// empty. |
| 781 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 782 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 783 | /// |
| 784 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 785 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 786 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 787 | /// let mut d = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 788 | /// d.push_back(1); |
| 789 | /// d.push_back(2); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 790 | /// |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 791 | /// assert_eq!(d.pop_front(), Some(1)); |
| 792 | /// assert_eq!(d.pop_front(), Some(2)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 793 | /// assert_eq!(d.pop_front(), None); |
| 794 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 795 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 796 | pub fn pop_front(&mut self) -> Option<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 797 | if self.is_empty() { |
| 798 | None |
| 799 | } else { |
| 800 | let tail = self.tail; |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 801 | self.tail = self.wrap_add(self.tail, 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 802 | unsafe { Some(self.buffer_read(tail)) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 803 | } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /// Inserts an element first in the sequence. |
| 807 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 808 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 809 | /// |
| 810 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 811 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 812 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 813 | /// let mut d = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 814 | /// d.push_front(1); |
| 815 | /// d.push_front(2); |
| 816 | /// assert_eq!(d.front(), Some(&2)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 817 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 818 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 819 | pub fn push_front(&mut self, t: T) { |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 820 | if self.is_full() { |
| 821 | self.reserve(1); |
| 822 | debug_assert!(!self.is_full()); |
| 823 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 824 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 825 | self.tail = self.wrap_sub(self.tail, 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 826 | let tail = self.tail; |
| 827 | unsafe { self.buffer_write(tail, t); } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /// Appends an element to the back of a buffer |
| 831 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 832 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 833 | /// |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 834 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 835 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 836 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 837 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 838 | /// buf.push_back(1); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 839 | /// buf.push_back(3); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 840 | /// assert_eq!(3, *buf.back().unwrap()); |
| 841 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 842 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 843 | pub fn push_back(&mut self, t: T) { |
Colin Sherratt | 4cae9ad | 2014-11-11 02:16:29 | [diff] [blame] | 844 | if self.is_full() { |
| 845 | self.reserve(1); |
| 846 | debug_assert!(!self.is_full()); |
| 847 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 848 | |
| 849 | let head = self.head; |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 850 | self.head = self.wrap_add(self.head, 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 851 | unsafe { self.buffer_write(head, t) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /// Removes the last element from a buffer and returns it, or `None` if |
| 855 | /// it is empty. |
| 856 | /// |
jbranchaud | c09defa | 2014-12-09 05:28:07 | [diff] [blame] | 857 | /// # Examples |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 858 | /// |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 859 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 860 | /// use std::collections::VecDeque; |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 861 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 862 | /// let mut buf = VecDeque::new(); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 863 | /// assert_eq!(buf.pop_back(), None); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 864 | /// buf.push_back(1); |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 865 | /// buf.push_back(3); |
| 866 | /// assert_eq!(buf.pop_back(), Some(3)); |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 867 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 868 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 869 | pub fn pop_back(&mut self) -> Option<T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 870 | if self.is_empty() { |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 871 | None |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 872 | } else { |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 873 | self.head = self.wrap_sub(self.head, 1); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 874 | let head = self.head; |
| 875 | unsafe { Some(self.buffer_read(head)) } |
Alex Crichton | 21ac985 | 2014-10-30 20:43:24 | [diff] [blame] | 876 | } |
| 877 | } |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 878 | |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 879 | #[inline] |
| 880 | fn is_contiguous(&self) -> bool { |
| 881 | self.tail <= self.head |
| 882 | } |
| 883 | |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 884 | /// Removes an element from anywhere in the ringbuf and returns it, replacing it with the last |
| 885 | /// element. |
| 886 | /// |
| 887 | /// This does not preserve ordering, but is O(1). |
| 888 | /// |
| 889 | /// Returns `None` if `index` is out of bounds. |
| 890 | /// |
| 891 | /// # Examples |
| 892 | /// |
| 893 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 894 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 895 | /// use std::collections::VecDeque; |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 896 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 897 | /// let mut buf = VecDeque::new(); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 898 | /// assert_eq!(buf.swap_back_remove(0), None); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 899 | /// buf.push_back(5); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 900 | /// buf.push_back(99); |
| 901 | /// buf.push_back(15); |
| 902 | /// buf.push_back(20); |
| 903 | /// buf.push_back(10); |
| 904 | /// assert_eq!(buf.swap_back_remove(1), Some(99)); |
| 905 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 906 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 907 | reason = "the naming of this function may be altered")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 908 | pub fn swap_back_remove(&mut self, index: usize) -> Option<T> { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 909 | let length = self.len(); |
| 910 | if length > 0 && index < length - 1 { |
| 911 | self.swap(index, length - 1); |
| 912 | } else if index >= length { |
| 913 | return None; |
| 914 | } |
| 915 | self.pop_back() |
| 916 | } |
| 917 | |
| 918 | /// Removes an element from anywhere in the ringbuf and returns it, replacing it with the first |
| 919 | /// element. |
| 920 | /// |
| 921 | /// This does not preserve ordering, but is O(1). |
| 922 | /// |
| 923 | /// Returns `None` if `index` is out of bounds. |
| 924 | /// |
| 925 | /// # Examples |
| 926 | /// |
| 927 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 928 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 929 | /// use std::collections::VecDeque; |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 930 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 931 | /// let mut buf = VecDeque::new(); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 932 | /// assert_eq!(buf.swap_front_remove(0), None); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 933 | /// buf.push_back(15); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 934 | /// buf.push_back(5); |
| 935 | /// buf.push_back(10); |
| 936 | /// buf.push_back(99); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 937 | /// buf.push_back(20); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 938 | /// assert_eq!(buf.swap_front_remove(3), Some(99)); |
| 939 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 940 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 941 | reason = "the naming of this function may be altered")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 942 | pub fn swap_front_remove(&mut self, index: usize) -> Option<T> { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 943 | let length = self.len(); |
| 944 | if length > 0 && index < length && index != 0 { |
| 945 | self.swap(index, 0); |
| 946 | } else if index >= length { |
| 947 | return None; |
| 948 | } |
| 949 | self.pop_front() |
| 950 | } |
| 951 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 952 | /// Inserts an element at position `i` within the ringbuf. Whichever |
| 953 | /// end is closer to the insertion point will be moved to make room, |
| 954 | /// and all the affected elements will be moved to new positions. |
| 955 | /// |
| 956 | /// # Panics |
| 957 | /// |
| 958 | /// Panics if `i` is greater than ringbuf's length |
| 959 | /// |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 960 | /// # Examples |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 961 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 962 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 963 | /// use std::collections::VecDeque; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 964 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 965 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 966 | /// buf.push_back(10); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 967 | /// buf.push_back(12); |
| 968 | /// buf.insert(1,11); |
| 969 | /// assert_eq!(Some(&11), buf.get(1)); |
| 970 | /// ``` |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 971 | pub fn insert(&mut self, i: usize, t: T) { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 972 | assert!(i <= self.len(), "index out of bounds"); |
| 973 | if self.is_full() { |
| 974 | self.reserve(1); |
| 975 | debug_assert!(!self.is_full()); |
| 976 | } |
| 977 | |
| 978 | // Move the least number of elements in the ring buffer and insert |
| 979 | // the given object |
| 980 | // |
| 981 | // At most len/2 - 1 elements will be moved. O(min(n, n-i)) |
| 982 | // |
| 983 | // There are three main cases: |
| 984 | // Elements are contiguous |
| 985 | // - special case when tail is 0 |
| 986 | // Elements are discontiguous and the insert is in the tail section |
| 987 | // Elements are discontiguous and the insert is in the head section |
| 988 | // |
| 989 | // For each of those there are two more cases: |
| 990 | // Insert is closer to tail |
| 991 | // Insert is closer to head |
| 992 | // |
| 993 | // Key: H - self.head |
| 994 | // T - self.tail |
| 995 | // o - Valid element |
| 996 | // I - Insertion element |
| 997 | // A - The element that should be after the insertion point |
| 998 | // M - Indicates element was moved |
| 999 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1000 | let idx = self.wrap_add(self.tail, i); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1001 | |
| 1002 | let distance_to_tail = i; |
| 1003 | let distance_to_head = self.len() - i; |
| 1004 | |
Clark Gaebel | 525f65e | 2014-12-16 04:01:58 | [diff] [blame] | 1005 | let contiguous = self.is_contiguous(); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1006 | |
| 1007 | match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { |
| 1008 | (true, true, _) if i == 0 => { |
| 1009 | // push_front |
| 1010 | // |
| 1011 | // T |
| 1012 | // I H |
| 1013 | // [A o o o o o o . . . . . . . . .] |
| 1014 | // |
| 1015 | // H T |
| 1016 | // [A o o o o o o o . . . . . I] |
| 1017 | // |
| 1018 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1019 | self.tail = self.wrap_sub(self.tail, 1); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1020 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1021 | (true, true, _) => unsafe { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1022 | // contiguous, insert closer to tail: |
| 1023 | // |
| 1024 | // T I H |
| 1025 | // [. . . o o A o o o o . . . . . .] |
| 1026 | // |
| 1027 | // T H |
| 1028 | // [. . o o I A o o o o . . . . . .] |
| 1029 | // M M |
| 1030 | // |
| 1031 | // contiguous, insert closer to tail and tail is 0: |
| 1032 | // |
| 1033 | // |
| 1034 | // T I H |
| 1035 | // [o o A o o o o . . . . . . . . .] |
| 1036 | // |
| 1037 | // H T |
| 1038 | // [o I A o o o o o . . . . . . . o] |
| 1039 | // M M |
| 1040 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1041 | let new_tail = self.wrap_sub(self.tail, 1); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1042 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1043 | self.copy(new_tail, self.tail, 1); |
| 1044 | // Already moved the tail, so we only copy `i - 1` elements. |
| 1045 | self.copy(self.tail, self.tail + 1, i - 1); |
| 1046 | |
| 1047 | self.tail = new_tail; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1048 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1049 | (true, false, _) => unsafe { |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1050 | // contiguous, insert closer to head: |
| 1051 | // |
| 1052 | // T I H |
| 1053 | // [. . . o o o o A o o . . . . . .] |
| 1054 | // |
| 1055 | // T H |
| 1056 | // [. . . o o o o I A o o . . . . .] |
| 1057 | // M M M |
| 1058 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1059 | self.copy(idx + 1, idx, self.head - idx); |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1060 | self.head = self.wrap_add(self.head, 1); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1061 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1062 | (false, true, true) => unsafe { |
| 1063 | // discontiguous, insert closer to tail, tail section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1064 | // |
| 1065 | // H T I |
| 1066 | // [o o o o o o . . . . . o o A o o] |
| 1067 | // |
| 1068 | // H T |
| 1069 | // [o o o o o o . . . . o o I A o o] |
| 1070 | // M M |
| 1071 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1072 | self.copy(self.tail - 1, self.tail, i); |
| 1073 | self.tail -= 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1074 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1075 | (false, false, true) => unsafe { |
| 1076 | // discontiguous, insert closer to head, tail section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1077 | // |
| 1078 | // H T I |
| 1079 | // [o o . . . . . . . o o o o o A o] |
| 1080 | // |
| 1081 | // H T |
| 1082 | // [o o o . . . . . . o o o o o I A] |
| 1083 | // M M M M |
| 1084 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1085 | // copy elements up to new head |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1086 | self.copy(1, 0, self.head); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1087 | |
| 1088 | // copy last element into empty spot at bottom of buffer |
| 1089 | self.copy(0, self.cap - 1, 1); |
| 1090 | |
| 1091 | // move elements from idx to end forward not including ^ element |
| 1092 | self.copy(idx + 1, idx, self.cap - 1 - idx); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1093 | |
| 1094 | self.head += 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1095 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1096 | (false, true, false) if idx == 0 => unsafe { |
| 1097 | // discontiguous, insert is closer to tail, head section, |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1098 | // and is at index zero in the internal buffer: |
| 1099 | // |
| 1100 | // I H T |
| 1101 | // [A o o o o o o o o o . . . o o o] |
| 1102 | // |
| 1103 | // H T |
| 1104 | // [A o o o o o o o o o . . o o o I] |
| 1105 | // M M M |
| 1106 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1107 | // copy elements up to new tail |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1108 | self.copy(self.tail - 1, self.tail, self.cap - self.tail); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1109 | |
| 1110 | // copy last element into empty spot at bottom of buffer |
| 1111 | self.copy(self.cap - 1, 0, 1); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1112 | |
| 1113 | self.tail -= 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1114 | }, |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1115 | (false, true, false) => unsafe { |
| 1116 | // discontiguous, insert closer to tail, head section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1117 | // |
| 1118 | // I H T |
| 1119 | // [o o o A o o o o o o . . . o o o] |
| 1120 | // |
| 1121 | // H T |
| 1122 | // [o o I A o o o o o o . . o o o o] |
| 1123 | // M M M M M M |
| 1124 | |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1125 | // copy elements up to new tail |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1126 | self.copy(self.tail - 1, self.tail, self.cap - self.tail); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1127 | |
| 1128 | // copy last element into empty spot at bottom of buffer |
| 1129 | self.copy(self.cap - 1, 0, 1); |
| 1130 | |
| 1131 | // move elements from idx-1 to end forward not including ^ element |
| 1132 | self.copy(0, 1, idx - 1); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1133 | |
| 1134 | self.tail -= 1; |
| 1135 | }, |
| 1136 | (false, false, false) => unsafe { |
| 1137 | // discontiguous, insert closer to head, head section: |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1138 | // |
| 1139 | // I H T |
| 1140 | // [o o o o A o o . . . . . . o o o] |
| 1141 | // |
| 1142 | // H T |
| 1143 | // [o o o o I A o o . . . . . o o o] |
| 1144 | // M M M |
| 1145 | |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1146 | self.copy(idx + 1, idx, self.head - idx); |
| 1147 | self.head += 1; |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | // tail might've been changed so we need to recalculate |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1152 | let new_idx = self.wrap_add(self.tail, i); |
Matt Murphy | 40f28c7 | 2014-12-03 17:12:30 | [diff] [blame] | 1153 | unsafe { |
| 1154 | self.buffer_write(new_idx, t); |
| 1155 | } |
| 1156 | } |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1157 | |
| 1158 | /// Removes and returns the element at position `i` from the ringbuf. |
| 1159 | /// Whichever end is closer to the removal point will be moved to make |
| 1160 | /// room, and all the affected elements will be moved to new positions. |
| 1161 | /// Returns `None` if `i` is out of bounds. |
| 1162 | /// |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1163 | /// # Examples |
Joseph Crail | fcf3f32 | 2015-03-13 02:42:38 | [diff] [blame] | 1164 | /// ``` |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1165 | /// use std::collections::VecDeque; |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1166 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1167 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1168 | /// buf.push_back(5); |
| 1169 | /// buf.push_back(10); |
| 1170 | /// buf.push_back(12); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1171 | /// buf.push_back(15); |
| 1172 | /// buf.remove(2); |
| 1173 | /// assert_eq!(Some(&15), buf.get(2)); |
| 1174 | /// ``` |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1175 | #[stable(feature = "rust1", since = "1.0.0")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1176 | pub fn remove(&mut self, i: usize) -> Option<T> { |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1177 | if self.is_empty() || self.len() <= i { |
| 1178 | return None; |
| 1179 | } |
| 1180 | |
| 1181 | // There are three main cases: |
| 1182 | // Elements are contiguous |
| 1183 | // Elements are discontiguous and the removal is in the tail section |
| 1184 | // Elements are discontiguous and the removal is in the head section |
| 1185 | // - special case when elements are technically contiguous, |
| 1186 | // but self.head = 0 |
| 1187 | // |
| 1188 | // For each of those there are two more cases: |
| 1189 | // Insert is closer to tail |
| 1190 | // Insert is closer to head |
| 1191 | // |
| 1192 | // Key: H - self.head |
| 1193 | // T - self.tail |
| 1194 | // o - Valid element |
| 1195 | // x - Element marked for removal |
| 1196 | // R - Indicates element that is being removed |
| 1197 | // M - Indicates element was moved |
| 1198 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1199 | let idx = self.wrap_add(self.tail, i); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1200 | |
| 1201 | let elem = unsafe { |
| 1202 | Some(self.buffer_read(idx)) |
| 1203 | }; |
| 1204 | |
| 1205 | let distance_to_tail = i; |
| 1206 | let distance_to_head = self.len() - i; |
| 1207 | |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1208 | let contiguous = self.is_contiguous(); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1209 | |
| 1210 | match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { |
| 1211 | (true, true, _) => unsafe { |
| 1212 | // contiguous, remove closer to tail: |
| 1213 | // |
| 1214 | // T R H |
| 1215 | // [. . . o o x o o o o . . . . . .] |
| 1216 | // |
| 1217 | // T H |
| 1218 | // [. . . . o o o o o o . . . . . .] |
| 1219 | // M M |
| 1220 | |
| 1221 | self.copy(self.tail + 1, self.tail, i); |
| 1222 | self.tail += 1; |
| 1223 | }, |
| 1224 | (true, false, _) => unsafe { |
| 1225 | // contiguous, remove closer to head: |
| 1226 | // |
| 1227 | // T R H |
| 1228 | // [. . . o o o o x o o . . . . . .] |
| 1229 | // |
| 1230 | // T H |
| 1231 | // [. . . o o o o o o . . . . . . .] |
| 1232 | // M M |
| 1233 | |
| 1234 | self.copy(idx, idx + 1, self.head - idx - 1); |
| 1235 | self.head -= 1; |
| 1236 | }, |
| 1237 | (false, true, true) => unsafe { |
| 1238 | // discontiguous, remove closer to tail, tail section: |
| 1239 | // |
| 1240 | // H T R |
| 1241 | // [o o o o o o . . . . . o o x o o] |
| 1242 | // |
| 1243 | // H T |
| 1244 | // [o o o o o o . . . . . . o o o o] |
| 1245 | // M M |
| 1246 | |
| 1247 | self.copy(self.tail + 1, self.tail, i); |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1248 | self.tail = self.wrap_add(self.tail, 1); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1249 | }, |
| 1250 | (false, false, false) => unsafe { |
| 1251 | // discontiguous, remove closer to head, head section: |
| 1252 | // |
| 1253 | // R H T |
| 1254 | // [o o o o x o o . . . . . . o o o] |
| 1255 | // |
| 1256 | // H T |
| 1257 | // [o o o o o o . . . . . . . o o o] |
| 1258 | // M M |
| 1259 | |
| 1260 | self.copy(idx, idx + 1, self.head - idx - 1); |
| 1261 | self.head -= 1; |
| 1262 | }, |
| 1263 | (false, false, true) => unsafe { |
| 1264 | // discontiguous, remove closer to head, tail section: |
| 1265 | // |
| 1266 | // H T R |
| 1267 | // [o o o . . . . . . o o o o o x o] |
| 1268 | // |
| 1269 | // H T |
| 1270 | // [o o . . . . . . . o o o o o o o] |
| 1271 | // M M M M |
| 1272 | // |
| 1273 | // or quasi-discontiguous, remove next to head, tail section: |
| 1274 | // |
| 1275 | // H T R |
| 1276 | // [. . . . . . . . . o o o o o x o] |
| 1277 | // |
| 1278 | // T H |
| 1279 | // [. . . . . . . . . o o o o o o .] |
| 1280 | // M |
| 1281 | |
| 1282 | // draw in elements in the tail section |
| 1283 | self.copy(idx, idx + 1, self.cap - idx - 1); |
| 1284 | |
| 1285 | // Prevents underflow. |
| 1286 | if self.head != 0 { |
| 1287 | // copy first element into empty spot |
| 1288 | self.copy(self.cap - 1, 0, 1); |
| 1289 | |
| 1290 | // move elements in the head section backwards |
| 1291 | self.copy(0, 1, self.head - 1); |
| 1292 | } |
| 1293 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1294 | self.head = self.wrap_sub(self.head, 1); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1295 | }, |
| 1296 | (false, true, false) => unsafe { |
| 1297 | // discontiguous, remove closer to tail, head section: |
| 1298 | // |
| 1299 | // R H T |
| 1300 | // [o o x o o o o o o o . . . o o o] |
| 1301 | // |
| 1302 | // H T |
| 1303 | // [o o o o o o o o o o . . . . o o] |
| 1304 | // M M M M M |
| 1305 | |
| 1306 | // draw in elements up to idx |
| 1307 | self.copy(1, 0, idx); |
| 1308 | |
| 1309 | // copy last element into empty spot |
| 1310 | self.copy(0, self.cap - 1, 1); |
| 1311 | |
| 1312 | // move elements from tail to end forward, excluding the last one |
| 1313 | self.copy(self.tail + 1, self.tail, self.cap - self.tail - 1); |
| 1314 | |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1315 | self.tail = self.wrap_add(self.tail, 1); |
Piotr Czarnecki | 59d4153 | 2014-12-16 23:37:55 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | return elem; |
| 1320 | } |
Alexis | dc930b1 | 2015-02-07 16:46:16 | [diff] [blame] | 1321 | |
| 1322 | /// Splits the collection into two at the given index. |
| 1323 | /// |
| 1324 | /// Returns a newly allocated `Self`. `self` contains elements `[0, at)`, |
| 1325 | /// and the returned `Self` contains elements `[at, len)`. |
| 1326 | /// |
| 1327 | /// Note that the capacity of `self` does not change. |
| 1328 | /// |
| 1329 | /// # Panics |
| 1330 | /// |
| 1331 | /// Panics if `at > len` |
| 1332 | /// |
| 1333 | /// # Examples |
Alexis | 3c18bc4 | 2015-02-07 17:13:32 | [diff] [blame] | 1334 | /// |
| 1335 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 1336 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1337 | /// use std::collections::VecDeque; |
Alexis | 3c18bc4 | 2015-02-07 17:13:32 | [diff] [blame] | 1338 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1339 | /// let mut buf: VecDeque<_> = vec![1,2,3].into_iter().collect(); |
Alexis | dc930b1 | 2015-02-07 16:46:16 | [diff] [blame] | 1340 | /// let buf2 = buf.split_off(1); |
| 1341 | /// // buf = [1], buf2 = [2, 3] |
| 1342 | /// assert_eq!(buf.len(), 1); |
| 1343 | /// assert_eq!(buf2.len(), 2); |
| 1344 | /// ``` |
| 1345 | #[inline] |
| 1346 | #[unstable(feature = "collections", |
| 1347 | reason = "new API, waiting for dust to settle")] |
| 1348 | pub fn split_off(&mut self, at: usize) -> Self { |
| 1349 | let len = self.len(); |
| 1350 | assert!(at <= len, "`at` out of bounds"); |
| 1351 | |
| 1352 | let other_len = len - at; |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1353 | let mut other = VecDeque::with_capacity(other_len); |
Alexis | dc930b1 | 2015-02-07 16:46:16 | [diff] [blame] | 1354 | |
| 1355 | unsafe { |
| 1356 | let (first_half, second_half) = self.as_slices(); |
| 1357 | |
| 1358 | let first_len = first_half.len(); |
| 1359 | let second_len = second_half.len(); |
| 1360 | if at < first_len { |
| 1361 | // `at` lies in the first half. |
| 1362 | let amount_in_first = first_len - at; |
| 1363 | |
Alex Crichton | acd48a2 | 2015-03-27 18:12:28 | [diff] [blame^] | 1364 | ptr::copy_nonoverlapping(first_half.as_ptr().offset(at as isize), |
| 1365 | *other.ptr, |
Alex Crichton | ab45694 | 2015-02-23 19:39:16 | [diff] [blame] | 1366 | amount_in_first); |
Alexis | dc930b1 | 2015-02-07 16:46:16 | [diff] [blame] | 1367 | |
| 1368 | // just take all of the second half. |
Alex Crichton | acd48a2 | 2015-03-27 18:12:28 | [diff] [blame^] | 1369 | ptr::copy_nonoverlapping(second_half.as_ptr(), |
| 1370 | other.ptr.offset(amount_in_first as isize), |
Alex Crichton | ab45694 | 2015-02-23 19:39:16 | [diff] [blame] | 1371 | second_len); |
Alexis | dc930b1 | 2015-02-07 16:46:16 | [diff] [blame] | 1372 | } else { |
| 1373 | // `at` lies in the second half, need to factor in the elements we skipped |
| 1374 | // in the first half. |
| 1375 | let offset = at - first_len; |
| 1376 | let amount_in_second = second_len - offset; |
Alex Crichton | acd48a2 | 2015-03-27 18:12:28 | [diff] [blame^] | 1377 | ptr::copy_nonoverlapping(second_half.as_ptr().offset(offset as isize), |
| 1378 | *other.ptr, |
Alex Crichton | ab45694 | 2015-02-23 19:39:16 | [diff] [blame] | 1379 | amount_in_second); |
Alexis | dc930b1 | 2015-02-07 16:46:16 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | // Cleanup where the ends of the buffers are |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1384 | self.head = self.wrap_sub(self.head, other_len); |
Alexis | dc930b1 | 2015-02-07 16:46:16 | [diff] [blame] | 1385 | other.head = other.wrap_index(other_len); |
| 1386 | |
| 1387 | other |
| 1388 | } |
Alexis | 3c18bc4 | 2015-02-07 17:13:32 | [diff] [blame] | 1389 | |
| 1390 | /// Moves all the elements of `other` into `Self`, leaving `other` empty. |
| 1391 | /// |
| 1392 | /// # Panics |
| 1393 | /// |
| 1394 | /// Panics if the new number of elements in self overflows a `usize`. |
| 1395 | /// |
| 1396 | /// # Examples |
| 1397 | /// |
| 1398 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 1399 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1400 | /// use std::collections::VecDeque; |
Alexis | 3c18bc4 | 2015-02-07 17:13:32 | [diff] [blame] | 1401 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1402 | /// let mut buf: VecDeque<_> = vec![1, 2, 3].into_iter().collect(); |
| 1403 | /// let mut buf2: VecDeque<_> = vec![4, 5, 6].into_iter().collect(); |
Alexis | 3c18bc4 | 2015-02-07 17:13:32 | [diff] [blame] | 1404 | /// buf.append(&mut buf2); |
| 1405 | /// assert_eq!(buf.len(), 6); |
| 1406 | /// assert_eq!(buf2.len(), 0); |
| 1407 | /// ``` |
| 1408 | #[inline] |
| 1409 | #[unstable(feature = "collections", |
| 1410 | reason = "new API, waiting for dust to settle")] |
| 1411 | pub fn append(&mut self, other: &mut Self) { |
| 1412 | // naive impl |
| 1413 | self.extend(other.drain()); |
| 1414 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 1415 | } |
| 1416 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1417 | impl<T: Clone> VecDeque<T> { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1418 | /// Modifies the ringbuf in-place so that `len()` is equal to new_len, |
| 1419 | /// either by removing excess elements or by appending copies of a value to the back. |
| 1420 | /// |
| 1421 | /// # Examples |
| 1422 | /// |
| 1423 | /// ``` |
Brian Anderson | e901910 | 2015-03-13 22:28:35 | [diff] [blame] | 1424 | /// # #![feature(collections)] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1425 | /// use std::collections::VecDeque; |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1426 | /// |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1427 | /// let mut buf = VecDeque::new(); |
Tobias Bucher | 7f64fe4 | 2015-01-25 21:05:03 | [diff] [blame] | 1428 | /// buf.push_back(5); |
| 1429 | /// buf.push_back(10); |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1430 | /// buf.push_back(15); |
| 1431 | /// buf.resize(2, 0); |
| 1432 | /// buf.resize(6, 20); |
| 1433 | /// for (a, b) in [5, 10, 20, 20, 20, 20].iter().zip(buf.iter()) { |
| 1434 | /// assert_eq!(a, b); |
| 1435 | /// } |
| 1436 | /// ``` |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 1437 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 1438 | reason = "matches collection reform specification; waiting on panic semantics")] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1439 | pub fn resize(&mut self, new_len: usize, value: T) { |
Piotr Czarnecki | 156a1c3 | 2015-01-05 14:48:58 | [diff] [blame] | 1440 | let len = self.len(); |
| 1441 | |
| 1442 | if new_len > len { |
| 1443 | self.extend(repeat(value).take(new_len - len)) |
| 1444 | } else { |
| 1445 | self.truncate(new_len); |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1450 | /// Returns the index in the underlying buffer for a given logical element index. |
| 1451 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1452 | fn wrap_index(index: usize, size: usize) -> usize { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1453 | // size is always a power of 2 |
Colin Sherratt | 4019118 | 2014-11-12 01:22:07 | [diff] [blame] | 1454 | index & (size - 1) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /// Calculate the number of elements left to be read in the buffer |
| 1458 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1459 | fn count(tail: usize, head: usize, size: usize) -> usize { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1460 | // size is always a power of 2 |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1461 | (head.wrapping_sub(tail)) & (size - 1) |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1462 | } |
| 1463 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1464 | /// `VecDeque` iterator. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1465 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 1466 | pub struct Iter<'a, T:'a> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1467 | ring: &'a [T], |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1468 | tail: usize, |
| 1469 | head: usize |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 1470 | } |
| 1471 | |
Jorge Aparicio | 351409a | 2015-01-04 03:54:18 | [diff] [blame] | 1472 | // FIXME(#19839) Remove in favor of `#[derive(Clone)]` |
Huon Wilson | b7832ed | 2014-12-30 10:01:36 | [diff] [blame] | 1473 | impl<'a, T> Clone for Iter<'a, T> { |
| 1474 | fn clone(&self) -> Iter<'a, T> { |
| 1475 | Iter { |
| 1476 | ring: self.ring, |
| 1477 | tail: self.tail, |
| 1478 | head: self.head |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1483 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1484 | impl<'a, T> Iterator for Iter<'a, T> { |
| 1485 | type Item = &'a T; |
| 1486 | |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1487 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 1488 | fn next(&mut self) -> Option<&'a T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1489 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1490 | return None; |
| 1491 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1492 | let tail = self.tail; |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1493 | self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len()); |
Aaron Turon | 6abfac0 | 2014-12-30 18:51:18 | [diff] [blame] | 1494 | unsafe { Some(self.ring.get_unchecked(tail)) } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1498 | fn size_hint(&self) -> (usize, Option<usize>) { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1499 | let len = count(self.tail, self.head, self.ring.len()); |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1500 | (len, Some(len)) |
| 1501 | } |
| 1502 | } |
| 1503 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1504 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1505 | impl<'a, T> DoubleEndedIterator for Iter<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1506 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 1507 | fn next_back(&mut self) -> Option<&'a T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1508 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1509 | return None; |
| 1510 | } |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1511 | self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len()); |
Aaron Turon | 6abfac0 | 2014-12-30 18:51:18 | [diff] [blame] | 1512 | unsafe { Some(self.ring.get_unchecked(self.head)) } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1513 | } |
| 1514 | } |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 1515 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1516 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1517 | impl<'a, T> ExactSizeIterator for Iter<'a, T> {} |
blake2-ppc | 7c369ee7 | 2013-09-01 16:20:24 | [diff] [blame] | 1518 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1519 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1520 | impl<'a, T> RandomAccessIterator for Iter<'a, T> { |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1521 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1522 | fn indexable(&self) -> usize { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1523 | let (len, _) = self.size_hint(); |
| 1524 | len |
| 1525 | } |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1526 | |
| 1527 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1528 | fn idx(&mut self, j: usize) -> Option<&'a T> { |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1529 | if j >= self.indexable() { |
| 1530 | None |
| 1531 | } else { |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1532 | let idx = wrap_index(self.tail.wrapping_add(j), self.ring.len()); |
Aaron Turon | 6abfac0 | 2014-12-30 18:51:18 | [diff] [blame] | 1533 | unsafe { Some(self.ring.get_unchecked(idx)) } |
blake2-ppc | f686213 | 2013-07-29 18:16:26 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1538 | /// `VecDeque` mutable iterator. |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1539 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 1540 | pub struct IterMut<'a, T:'a> { |
Edward Wang | 101498c | 2015-02-25 10:11:23 | [diff] [blame] | 1541 | ring: &'a mut [T], |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1542 | tail: usize, |
| 1543 | head: usize, |
Niko Matsakis | 1b487a8 | 2014-08-28 01:46:52 | [diff] [blame] | 1544 | } |
| 1545 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1546 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1547 | impl<'a, T> Iterator for IterMut<'a, T> { |
| 1548 | type Item = &'a mut T; |
| 1549 | |
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(&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 | } |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1555 | let tail = self.tail; |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1556 | self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len()); |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1557 | |
| 1558 | unsafe { |
Edward Wang | 101498c | 2015-02-25 10:11:23 | [diff] [blame] | 1559 | let elem = self.ring.get_unchecked_mut(tail); |
| 1560 | Some(&mut *(elem as *mut _)) |
Alex Crichton | 9d5d97b | 2014-10-15 06:05:01 | [diff] [blame] | 1561 | } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1565 | fn size_hint(&self) -> (usize, Option<usize>) { |
Edward Wang | 101498c | 2015-02-25 10:11:23 | [diff] [blame] | 1566 | let len = count(self.tail, self.head, self.ring.len()); |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1567 | (len, Some(len)) |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1571 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1572 | impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1573 | #[inline] |
Erik Price | 5731ca3 | 2013-12-10 07:16:18 | [diff] [blame] | 1574 | fn next_back(&mut self) -> Option<&'a mut T> { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1575 | if self.tail == self.head { |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1576 | return None; |
| 1577 | } |
Felix S. Klock II | e7c9861 | 2015-02-19 07:33:32 | [diff] [blame] | 1578 | self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len()); |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1579 | |
| 1580 | unsafe { |
Edward Wang | 101498c | 2015-02-25 10:11:23 | [diff] [blame] | 1581 | let elem = self.ring.get_unchecked_mut(self.head); |
| 1582 | Some(&mut *(elem as *mut _)) |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1583 | } |
Niko Matsakis | bc4164d | 2013-11-16 22:29:39 | [diff] [blame] | 1584 | } |
| 1585 | } |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 1586 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1587 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1588 | impl<'a, T> ExactSizeIterator for IterMut<'a, T> {} |
blake2-ppc | 7c369ee7 | 2013-09-01 16:20:24 | [diff] [blame] | 1589 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1590 | /// A by-value VecDeque iterator |
Andrew Paseltiner | 64532f7 | 2015-03-23 12:50:47 | [diff] [blame] | 1591 | #[derive(Clone)] |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1592 | #[stable(feature = "rust1", since = "1.0.0")] |
Florian Wilkens | f8cfd24 | 2014-12-19 20:52:10 | [diff] [blame] | 1593 | pub struct IntoIter<T> { |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1594 | inner: VecDeque<T>, |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 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> Iterator for IntoIter<T> { |
| 1599 | type Item = T; |
| 1600 | |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1601 | #[inline] |
| 1602 | fn next(&mut self) -> Option<T> { |
| 1603 | self.inner.pop_front() |
| 1604 | } |
| 1605 | |
| 1606 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1607 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1608 | let len = self.inner.len(); |
| 1609 | (len, Some(len)) |
| 1610 | } |
| 1611 | } |
| 1612 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1613 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1614 | impl<T> DoubleEndedIterator for IntoIter<T> { |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1615 | #[inline] |
| 1616 | fn next_back(&mut self) -> Option<T> { |
| 1617 | self.inner.pop_back() |
| 1618 | } |
| 1619 | } |
| 1620 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1621 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1622 | impl<T> ExactSizeIterator for IntoIter<T> {} |
Alexis Beingessner | 865c2db | 2014-11-23 02:34:11 | [diff] [blame] | 1623 | |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1624 | /// A draining VecDeque iterator |
Brian Anderson | cd6d9ea | 2015-01-23 02:22:03 | [diff] [blame] | 1625 | #[unstable(feature = "collections", |
Brian Anderson | 94ca8a3 | 2015-01-13 02:40:19 | [diff] [blame] | 1626 | reason = "matches collection reform specification, waiting for dust to settle")] |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1627 | pub struct Drain<'a, T: 'a> { |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1628 | inner: &'a mut VecDeque<T>, |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | #[unsafe_destructor] |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1632 | #[stable(feature = "rust1", since = "1.0.0")] |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1633 | impl<'a, T: 'a> Drop for Drain<'a, T> { |
| 1634 | fn drop(&mut self) { |
Jorge Aparicio | f9865ea | 2015-01-11 02:50:07 | [diff] [blame] | 1635 | for _ in self.by_ref() {} |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1636 | self.inner.head = 0; |
| 1637 | self.inner.tail = 0; |
| 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> Iterator for Drain<'a, T> { |
| 1643 | type Item = T; |
| 1644 | |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1645 | #[inline] |
| 1646 | fn next(&mut self) -> Option<T> { |
| 1647 | self.inner.pop_front() |
| 1648 | } |
| 1649 | |
| 1650 | #[inline] |
Alexis | e250fe3 | 2015-02-05 02:17:19 | [diff] [blame] | 1651 | fn size_hint(&self) -> (usize, Option<usize>) { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1652 | let len = self.inner.len(); |
| 1653 | (len, Some(len)) |
| 1654 | } |
| 1655 | } |
| 1656 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1657 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1658 | impl<'a, T: 'a> DoubleEndedIterator for Drain<'a, T> { |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1659 | #[inline] |
| 1660 | fn next_back(&mut self) -> Option<T> { |
| 1661 | self.inner.pop_back() |
| 1662 | } |
| 1663 | } |
| 1664 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1665 | #[stable(feature = "rust1", since = "1.0.0")] |
Jorge Aparicio | 6b116be | 2015-01-02 04:15:35 | [diff] [blame] | 1666 | impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {} |
Clark Gaebel | d57f259 | 2014-12-16 22:45:03 | [diff] [blame] | 1667 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1668 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1669 | impl<A: PartialEq> PartialEq for VecDeque<A> { |
| 1670 | fn eq(&self, other: &VecDeque<A>) -> bool { |
Colin Sherratt | 7a666df | 2014-10-19 20:19:07 | [diff] [blame] | 1671 | self.len() == other.len() && |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1672 | self.iter().zip(other.iter()).all(|(a, b)| a.eq(b)) |
| 1673 | } |
blake2-ppc | 10c7698 | 2013-07-06 13:27:32 | [diff] [blame] | 1674 | } |
| 1675 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1676 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1677 | impl<A: Eq> Eq for VecDeque<A> {} |
nham | 25acfde | 2014-08-01 20:05:03 | [diff] [blame] | 1678 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1679 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1680 | impl<A: PartialOrd> PartialOrd for VecDeque<A> { |
| 1681 | fn partial_cmp(&self, other: &VecDeque<A>) -> Option<Ordering> { |
nham | 6361577 | 2014-07-27 03:18:56 | [diff] [blame] | 1682 | iter::order::partial_cmp(self.iter(), other.iter()) |
| 1683 | } |
| 1684 | } |
| 1685 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1686 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1687 | impl<A: Ord> Ord for VecDeque<A> { |
nham | 3737c53 | 2014-08-01 20:22:48 | [diff] [blame] | 1688 | #[inline] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1689 | fn cmp(&self, other: &VecDeque<A>) -> Ordering { |
nham | 3737c53 | 2014-08-01 20:22:48 | [diff] [blame] | 1690 | iter::order::cmp(self.iter(), other.iter()) |
| 1691 | } |
| 1692 | } |
| 1693 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1694 | #[stable(feature = "rust1", since = "1.0.0")] |
Alex Crichton | 5a32b4a | 2015-02-18 22:34:08 | [diff] [blame] | 1695 | impl<A: Hash> Hash for VecDeque<A> { |
Alex Crichton | f83e23a | 2015-02-18 04:48:07 | [diff] [blame] | 1696 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 1697 | self.len().hash(state); |
| 1698 | for elt in self { |
| 1699 | elt.hash(state); |
| 1700 | } |
| 1701 | } |
| 1702 | } |
nham | 1cfa656 | 2014-07-27 02:33:47 | [diff] [blame] | 1703 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1704 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1705 | impl<A> Index<usize> for VecDeque<A> { |
Jorge Aparicio | 32dd592 | 2015-01-03 15:40:10 | [diff] [blame] | 1706 | type Output = A; |
| 1707 | |
Niko Matsakis | b4d4daf | 2015-03-21 23:33:27 | [diff] [blame] | 1708 | #[inline] |
| 1709 | fn index(&self, i: usize) -> &A { |
| 1710 | self.get(i).expect("Out of bounds access") |
| 1711 | } |
Jorge Aparicio | 32dd592 | 2015-01-03 15:40:10 | [diff] [blame] | 1712 | } |
| 1713 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1714 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1715 | impl<A> IndexMut<usize> for VecDeque<A> { |
Niko Matsakis | b4d4daf | 2015-03-21 23:33:27 | [diff] [blame] | 1716 | #[inline] |
| 1717 | fn index_mut(&mut self, i: usize) -> &mut A { |
| 1718 | self.get_mut(i).expect("Out of bounds access") |
| 1719 | } |
Jorge Aparicio | 32dd592 | 2015-01-03 15:40:10 | [diff] [blame] | 1720 | } |
| 1721 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1722 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1723 | impl<A> FromIterator<A> for VecDeque<A> { |
Alexis | 66613e2 | 2015-02-18 18:06:21 | [diff] [blame] | 1724 | fn from_iter<T: IntoIterator<Item=A>>(iterable: T) -> VecDeque<A> { |
| 1725 | let iterator = iterable.into_iter(); |
blake2-ppc | f8ae526 | 2013-07-30 00:06:49 | [diff] [blame] | 1726 | let (lower, _) = iterator.size_hint(); |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1727 | let mut deq = VecDeque::with_capacity(lower); |
blake2-ppc | f8ae526 | 2013-07-30 00:06:49 | [diff] [blame] | 1728 | deq.extend(iterator); |
blake2-ppc | 08dc72f | 2013-07-06 03:42:45 | [diff] [blame] | 1729 | deq |
| 1730 | } |
| 1731 | } |
| 1732 | |
Alex Crichton | cc68786 | 2015-02-17 18:06:24 | [diff] [blame] | 1733 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1734 | impl<T> IntoIterator for VecDeque<T> { |
Jorge Aparicio | e727378 | 2015-02-13 22:55:10 | [diff] [blame] | 1735 | type Item = T; |
| 1736 | type IntoIter = IntoIter<T>; |
| 1737 | |
| 1738 | fn into_iter(self) -> IntoIter<T> { |
| 1739 | self.into_iter() |
| 1740 | } |
| 1741 | } |
| 1742 | |
Alex Crichton | cc68786 | 2015-02-17 18:06:24 | [diff] [blame] | 1743 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1744 | impl<'a, T> IntoIterator for &'a VecDeque<T> { |
Jorge Aparicio | e727378 | 2015-02-13 22:55:10 | [diff] [blame] | 1745 | type Item = &'a T; |
| 1746 | type IntoIter = Iter<'a, T>; |
| 1747 | |
| 1748 | fn into_iter(self) -> Iter<'a, T> { |
| 1749 | self.iter() |
| 1750 | } |
| 1751 | } |
| 1752 | |
Alex Crichton | cc68786 | 2015-02-17 18:06:24 | [diff] [blame] | 1753 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1754 | impl<'a, T> IntoIterator for &'a mut VecDeque<T> { |
Jorge Aparicio | e727378 | 2015-02-13 22:55:10 | [diff] [blame] | 1755 | type Item = &'a mut T; |
| 1756 | type IntoIter = IterMut<'a, T>; |
| 1757 | |
| 1758 | fn into_iter(mut self) -> IterMut<'a, T> { |
| 1759 | self.iter_mut() |
| 1760 | } |
| 1761 | } |
| 1762 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1763 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1764 | impl<A> Extend<A> for VecDeque<A> { |
Alexis | 4a9d190 | 2015-02-18 15:04:30 | [diff] [blame] | 1765 | fn extend<T: IntoIterator<Item=A>>(&mut self, iter: T) { |
| 1766 | for elt in iter { |
Alexis Beingessner | cf3b2e4 | 2014-11-06 17:24:47 | [diff] [blame] | 1767 | self.push_back(elt); |
blake2-ppc | f8ae526 | 2013-07-30 00:06:49 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
Brian Anderson | b44ee37 | 2015-01-24 05:48:20 | [diff] [blame] | 1772 | #[stable(feature = "rust1", since = "1.0.0")] |
Aaron Turon | 5fa9de1 | 2015-02-18 07:44:55 | [diff] [blame] | 1773 | impl<T: fmt::Debug> fmt::Debug for VecDeque<T> { |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1774 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Tobias Bucher | 408f7b5 | 2015-02-10 21:12:13 | [diff] [blame] | 1775 | try!(write!(f, "[")); |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1776 | |
| 1777 | for (i, e) in self.iter().enumerate() { |
| 1778 | if i != 0 { try!(write!(f, ", ")); } |
Sean McArthur | 44440e5 | 2014-12-20 08:09:35 | [diff] [blame] | 1779 | try!(write!(f, "{:?}", *e)); |
Adolfo OchagavÃa | 8e4e3ab | 2014-06-04 14:15:04 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | write!(f, "]") |
| 1783 | } |
| 1784 | } |
Jorge Aparicio | cb5e429 | 2015-03-12 00:44:02 | [diff] [blame] | 1785 | |
| 1786 | #[cfg(test)] |
| 1787 | mod test { |
Steven Fackler | d502f42 | 2015-03-12 05:41:24 | [diff] [blame] | 1788 | use core::iter::{Iterator, self}; |
Jorge Aparicio | cb5e429 | 2015-03-12 00:44:02 | [diff] [blame] | 1789 | use core::option::Option::Some; |
| 1790 | |
| 1791 | use test; |
| 1792 | |
| 1793 | use super::VecDeque; |
| 1794 | |
| 1795 | #[bench] |
| 1796 | fn bench_push_back_100(b: &mut test::Bencher) { |
| 1797 | let mut deq = VecDeque::with_capacity(101); |
| 1798 | b.iter(|| { |
| 1799 | for i in 0..100 { |
| 1800 | deq.push_back(i); |
| 1801 | } |
| 1802 | deq.head = 0; |
| 1803 | deq.tail = 0; |
| 1804 | }) |
| 1805 | } |
| 1806 | |
| 1807 | #[bench] |
| 1808 | fn bench_push_front_100(b: &mut test::Bencher) { |
| 1809 | let mut deq = VecDeque::with_capacity(101); |
| 1810 | b.iter(|| { |
| 1811 | for i in 0..100 { |
| 1812 | deq.push_front(i); |
| 1813 | } |
| 1814 | deq.head = 0; |
| 1815 | deq.tail = 0; |
| 1816 | }) |
| 1817 | } |
| 1818 | |
| 1819 | #[bench] |
| 1820 | fn bench_pop_back_100(b: &mut test::Bencher) { |
| 1821 | let mut deq= VecDeque::<i32>::with_capacity(101); |
| 1822 | |
| 1823 | b.iter(|| { |
| 1824 | deq.head = 100; |
| 1825 | deq.tail = 0; |
| 1826 | while !deq.is_empty() { |
| 1827 | test::black_box(deq.pop_back()); |
| 1828 | } |
| 1829 | }) |
| 1830 | } |
| 1831 | |
| 1832 | #[bench] |
| 1833 | fn bench_pop_front_100(b: &mut test::Bencher) { |
| 1834 | let mut deq = VecDeque::<i32>::with_capacity(101); |
| 1835 | |
| 1836 | b.iter(|| { |
| 1837 | deq.head = 100; |
| 1838 | deq.tail = 0; |
| 1839 | while !deq.is_empty() { |
| 1840 | test::black_box(deq.pop_front()); |
| 1841 | } |
| 1842 | }) |
| 1843 | } |
| 1844 | |
| 1845 | #[test] |
| 1846 | fn test_swap_front_back_remove() { |
| 1847 | fn test(back: bool) { |
| 1848 | // This test checks that every single combination of tail position and length is tested. |
| 1849 | // Capacity 15 should be large enough to cover every case. |
| 1850 | let mut tester = VecDeque::with_capacity(15); |
| 1851 | let usable_cap = tester.capacity(); |
| 1852 | let final_len = usable_cap / 2; |
| 1853 | |
| 1854 | for len in 0..final_len { |
| 1855 | let expected = if back { |
| 1856 | (0..len).collect() |
| 1857 | } else { |
| 1858 | (0..len).rev().collect() |
| 1859 | }; |
| 1860 | for tail_pos in 0..usable_cap { |
| 1861 | tester.tail = tail_pos; |
| 1862 | tester.head = tail_pos; |
| 1863 | if back { |
| 1864 | for i in 0..len * 2 { |
| 1865 | tester.push_front(i); |
| 1866 | } |
| 1867 | for i in 0..len { |
| 1868 | assert_eq!(tester.swap_back_remove(i), Some(len * 2 - 1 - i)); |
| 1869 | } |
| 1870 | } else { |
| 1871 | for i in 0..len * 2 { |
| 1872 | tester.push_back(i); |
| 1873 | } |
| 1874 | for i in 0..len { |
| 1875 | let idx = tester.len() - 1 - i; |
| 1876 | assert_eq!(tester.swap_front_remove(idx), Some(len * 2 - 1 - i)); |
| 1877 | } |
| 1878 | } |
| 1879 | assert!(tester.tail < tester.cap); |
| 1880 | assert!(tester.head < tester.cap); |
| 1881 | assert_eq!(tester, expected); |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | test(true); |
| 1886 | test(false); |
| 1887 | } |
| 1888 | |
| 1889 | #[test] |
| 1890 | fn test_insert() { |
| 1891 | // This test checks that every single combination of tail position, length, and |
| 1892 | // insertion position is tested. Capacity 15 should be large enough to cover every case. |
| 1893 | |
| 1894 | let mut tester = VecDeque::with_capacity(15); |
| 1895 | // can't guarantee we got 15, so have to get what we got. |
| 1896 | // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else |
| 1897 | // this test isn't covering what it wants to |
| 1898 | let cap = tester.capacity(); |
| 1899 | |
| 1900 | |
| 1901 | // len is the length *after* insertion |
| 1902 | for len in 1..cap { |
| 1903 | // 0, 1, 2, .., len - 1 |
| 1904 | let expected = iter::count(0, 1).take(len).collect(); |
| 1905 | for tail_pos in 0..cap { |
| 1906 | for to_insert in 0..len { |
| 1907 | tester.tail = tail_pos; |
| 1908 | tester.head = tail_pos; |
| 1909 | for i in 0..len { |
| 1910 | if i != to_insert { |
| 1911 | tester.push_back(i); |
| 1912 | } |
| 1913 | } |
| 1914 | tester.insert(to_insert, to_insert); |
| 1915 | assert!(tester.tail < tester.cap); |
| 1916 | assert!(tester.head < tester.cap); |
| 1917 | assert_eq!(tester, expected); |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | #[test] |
| 1924 | fn test_remove() { |
| 1925 | // This test checks that every single combination of tail position, length, and |
| 1926 | // removal position is tested. Capacity 15 should be large enough to cover every case. |
| 1927 | |
| 1928 | let mut tester = VecDeque::with_capacity(15); |
| 1929 | // can't guarantee we got 15, so have to get what we got. |
| 1930 | // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else |
| 1931 | // this test isn't covering what it wants to |
| 1932 | let cap = tester.capacity(); |
| 1933 | |
| 1934 | // len is the length *after* removal |
| 1935 | for len in 0..cap - 1 { |
| 1936 | // 0, 1, 2, .., len - 1 |
| 1937 | let expected = iter::count(0, 1).take(len).collect(); |
| 1938 | for tail_pos in 0..cap { |
| 1939 | for to_remove in 0..len + 1 { |
| 1940 | tester.tail = tail_pos; |
| 1941 | tester.head = tail_pos; |
| 1942 | for i in 0..len { |
| 1943 | if i == to_remove { |
| 1944 | tester.push_back(1234); |
| 1945 | } |
| 1946 | tester.push_back(i); |
| 1947 | } |
| 1948 | if to_remove == len { |
| 1949 | tester.push_back(1234); |
| 1950 | } |
| 1951 | tester.remove(to_remove); |
| 1952 | assert!(tester.tail < tester.cap); |
| 1953 | assert!(tester.head < tester.cap); |
| 1954 | assert_eq!(tester, expected); |
| 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | #[test] |
| 1961 | fn test_shrink_to_fit() { |
| 1962 | // This test checks that every single combination of head and tail position, |
| 1963 | // is tested. Capacity 15 should be large enough to cover every case. |
| 1964 | |
| 1965 | let mut tester = VecDeque::with_capacity(15); |
| 1966 | // can't guarantee we got 15, so have to get what we got. |
| 1967 | // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else |
| 1968 | // this test isn't covering what it wants to |
| 1969 | let cap = tester.capacity(); |
| 1970 | tester.reserve(63); |
| 1971 | let max_cap = tester.capacity(); |
| 1972 | |
| 1973 | for len in 0..cap + 1 { |
| 1974 | // 0, 1, 2, .., len - 1 |
| 1975 | let expected = iter::count(0, 1).take(len).collect(); |
| 1976 | for tail_pos in 0..max_cap + 1 { |
| 1977 | tester.tail = tail_pos; |
| 1978 | tester.head = tail_pos; |
| 1979 | tester.reserve(63); |
| 1980 | for i in 0..len { |
| 1981 | tester.push_back(i); |
| 1982 | } |
| 1983 | tester.shrink_to_fit(); |
| 1984 | assert!(tester.capacity() <= cap); |
| 1985 | assert!(tester.tail < tester.cap); |
| 1986 | assert!(tester.head < tester.cap); |
| 1987 | assert_eq!(tester, expected); |
| 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | #[test] |
| 1993 | fn test_split_off() { |
| 1994 | // This test checks that every single combination of tail position, length, and |
| 1995 | // split position is tested. Capacity 15 should be large enough to cover every case. |
| 1996 | |
| 1997 | let mut tester = VecDeque::with_capacity(15); |
| 1998 | // can't guarantee we got 15, so have to get what we got. |
| 1999 | // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else |
| 2000 | // this test isn't covering what it wants to |
| 2001 | let cap = tester.capacity(); |
| 2002 | |
| 2003 | // len is the length *before* splitting |
| 2004 | for len in 0..cap { |
| 2005 | // index to split at |
| 2006 | for at in 0..len + 1 { |
| 2007 | // 0, 1, 2, .., at - 1 (may be empty) |
| 2008 | let expected_self = iter::count(0, 1).take(at).collect(); |
| 2009 | // at, at + 1, .., len - 1 (may be empty) |
| 2010 | let expected_other = iter::count(at, 1).take(len - at).collect(); |
| 2011 | |
| 2012 | for tail_pos in 0..cap { |
| 2013 | tester.tail = tail_pos; |
| 2014 | tester.head = tail_pos; |
| 2015 | for i in 0..len { |
| 2016 | tester.push_back(i); |
| 2017 | } |
| 2018 | let result = tester.split_off(at); |
| 2019 | assert!(tester.tail < tester.cap); |
| 2020 | assert!(tester.head < tester.cap); |
| 2021 | assert!(result.tail < result.cap); |
| 2022 | assert!(result.head < result.cap); |
| 2023 | assert_eq!(tester, expected_self); |
| 2024 | assert_eq!(result, expected_other); |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | } |