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