blob: 4b9f3980db282fc634592f7112bffcc4da439a9a [file] [log] [blame]
Jonathan S03609e52014-04-21 04:59:121// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
Graydon Hoare00c856c2012-12-04 00:48:012// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
Steve Klabnik84030fd2014-08-30 21:11:2211//! This crate implements a double-ended queue with `O(1)` amortized inserts and removals from both
12//! ends of the container. It also has `O(1)` indexing like a vector. The contained elements are
13//! not required to be copyable, and the queue will be sendable if the contained type is sendable.
Patrick Waltonf3723cf2013-05-17 22:28:4414
Brian Andersonb44ee372015-01-24 05:48:2015#![stable(feature = "rust1", since = "1.0.0")]
Aaron Turoncb765ce2015-01-05 00:35:2016
Alex Crichton6a585372014-05-30 01:50:1217use core::prelude::*;
18
Alex Crichton56290a02014-12-22 17:04:2319use core::cmp::Ordering;
Tom Jakubowskid6a39412014-06-09 07:30:0420use core::default::Default;
Alex Crichton6a585372014-05-30 01:50:1221use core::fmt;
Jorge Aparicioa65d3f52015-01-08 03:01:0522use core::iter::{self, repeat, FromIterator, IntoIterator, RandomAccessIterator};
Nick Cameron9f07d052015-01-06 22:33:4223use core::marker;
Colin Sherratt7a666df2014-10-19 20:19:0724use core::mem;
Colin Sherratt6277e3b2014-11-14 09:21:4425use core::num::{Int, UnsignedInt};
Alex Crichton56290a02014-12-22 17:04:2326use core::ops::{Index, IndexMut};
27use core::ptr;
28use core::raw::Slice as RawSlice;
Alex Crichton998fece2013-05-06 04:42:5429
Alex Crichton511f0b82014-12-09 20:37:2330use std::hash::{Writer, Hash, Hasher};
Colin Sherratt7a666df2014-10-19 20:19:0731use std::cmp;
32
33use alloc::heap;
blake2-ppc70523712013-07-10 13:27:1434
Piotr Czarnecki156a1c32015-01-05 14:48:5835static INITIAL_CAPACITY: uint = 7u; // 2^3 - 1
36static MINIMUM_CAPACITY: uint = 1u; // 2 - 1
Alexis Beingessnercf3b2e42014-11-06 17:24:4737
Tobias Bucher4a46f5e2014-12-09 22:05:1638/// `RingBuf` is a circular buffer, which can be used as a double-ended queue efficiently.
Brian Andersonb44ee372015-01-24 05:48:2039#[stable(feature = "rust1", since = "1.0.0")]
blake2-ppc70523712013-07-10 13:27:1440pub struct RingBuf<T> {
Colin Sherratt7a666df2014-10-19 20:19:0741 // tail and head are pointers into the buffer. Tail always points
42 // to the first element that could be read, Head always points
43 // to where data should be written.
44 // If tail == head the buffer is empty. The length of the ringbuf
45 // is defined as the distance between the two.
46
47 tail: uint,
48 head: uint,
49 cap: uint,
50 ptr: *mut T
51}
52
Brian Andersonb44ee372015-01-24 05:48:2053#[stable(feature = "rust1", since = "1.0.0")]
Rohit Joshi8fb25ab2014-12-30 09:20:2454unsafe impl<T: Send> Send for RingBuf<T> {}
55
Brian Andersonb44ee372015-01-24 05:48:2056#[stable(feature = "rust1", since = "1.0.0")]
Rohit Joshi8fb25ab2014-12-30 09:20:2457unsafe impl<T: Sync> Sync for RingBuf<T> {}
58
Brian Andersonb44ee372015-01-24 05:48:2059#[stable(feature = "rust1", since = "1.0.0")]
Colin Sherratt7a666df2014-10-19 20:19:0760impl<T: Clone> Clone for RingBuf<T> {
61 fn clone(&self) -> RingBuf<T> {
62 self.iter().map(|t| t.clone()).collect()
63 }
64}
65
66#[unsafe_destructor]
Brian Andersonb44ee372015-01-24 05:48:2067#[stable(feature = "rust1", since = "1.0.0")]
Colin Sherratt7a666df2014-10-19 20:19:0768impl<T> Drop for RingBuf<T> {
69 fn drop(&mut self) {
70 self.clear();
71 unsafe {
72 if mem::size_of::<T>() != 0 {
73 heap::deallocate(self.ptr as *mut u8,
74 self.cap * mem::size_of::<T>(),
75 mem::min_align_of::<T>())
76 }
77 }
78 }
Marijn Haverbeke26610db2012-01-11 11:49:3379}
Roy Frostig9c818892010-07-21 01:03:0980
Brian Andersonb44ee372015-01-24 05:48:2081#[stable(feature = "rust1", since = "1.0.0")]
Tom Jakubowskid6a39412014-06-09 07:30:0482impl<T> Default for RingBuf<T> {
83 #[inline]
84 fn default() -> RingBuf<T> { RingBuf::new() }
85}
86
blake2-ppc70523712013-07-10 13:27:1487impl<T> RingBuf<T> {
Colin Sherratt7a666df2014-10-19 20:19:0788 /// Turn ptr into a slice
89 #[inline]
Alexis Beingessner8dbaa712014-12-31 00:07:5390 unsafe fn buffer_as_slice(&self) -> &[T] {
we812ce6c2015-01-17 04:34:1091 mem::transmute(RawSlice { data: self.ptr, len: self.cap })
Clark Gaebel525f65e2014-12-16 04:01:5892 }
93
94 /// Turn ptr into a mut slice
95 #[inline]
Alexis Beingessner8dbaa712014-12-31 00:07:5396 unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] {
we812ce6c2015-01-17 04:34:1097 mem::transmute(RawSlice { data: self.ptr, len: self.cap })
Colin Sherratt7a666df2014-10-19 20:19:0798 }
99
100 /// Moves an element out of the buffer
101 #[inline]
102 unsafe fn buffer_read(&mut self, off: uint) -> T {
we812ce6c2015-01-17 04:34:10103 ptr::read(self.ptr.offset(off as int))
Colin Sherratt7a666df2014-10-19 20:19:07104 }
105
106 /// Writes an element into the buffer, moving it.
107 #[inline]
108 unsafe fn buffer_write(&mut self, off: uint, t: T) {
109 ptr::write(self.ptr.offset(off as int), t);
110 }
111
112 /// Returns true iff the buffer is at capacity
113 #[inline]
114 fn is_full(&self) -> bool { self.cap - self.len() == 1 }
Colin Sherratt40191182014-11-12 01:22:07115
116 /// Returns the index in the underlying buffer for a given logical element index.
117 #[inline]
118 fn wrap_index(&self, idx: uint) -> uint { wrap_index(idx, self.cap) }
Matt Murphy40f28c72014-12-03 17:12:30119
120 /// Copies a contiguous block of memory len long from src to dst
121 #[inline]
Piotr Czarnecki59d41532014-12-16 23:37:55122 unsafe fn copy(&self, dst: uint, src: uint, len: uint) {
123 debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
124 self.cap);
125 debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
126 self.cap);
127 ptr::copy_memory(
128 self.ptr.offset(dst as int),
Piotr Czarnecki156a1c32015-01-05 14:48:58129 self.ptr.offset(src as int),
130 len);
131 }
132
133 /// Copies a contiguous block of memory len long from src to dst
134 #[inline]
135 unsafe fn copy_nonoverlapping(&self, dst: uint, src: uint, len: uint) {
136 debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
137 self.cap);
138 debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
139 self.cap);
140 ptr::copy_nonoverlapping_memory(
141 self.ptr.offset(dst as int),
142 self.ptr.offset(src as int),
Piotr Czarnecki59d41532014-12-16 23:37:55143 len);
Matt Murphy40f28c72014-12-03 17:12:30144 }
Colin Sherratt7a666df2014-10-19 20:19:07145}
146
147impl<T> RingBuf<T> {
P1startf2aa88c2014-08-04 10:48:39148 /// Creates an empty `RingBuf`.
Brian Andersonb44ee372015-01-24 05:48:20149 #[stable(feature = "rust1", since = "1.0.0")]
blake2-ppc70523712013-07-10 13:27:14150 pub fn new() -> RingBuf<T> {
151 RingBuf::with_capacity(INITIAL_CAPACITY)
152 }
153
P1startf2aa88c2014-08-04 10:48:39154 /// Creates an empty `RingBuf` with space for at least `n` elements.
Brian Andersonb44ee372015-01-24 05:48:20155 #[stable(feature = "rust1", since = "1.0.0")]
blake2-ppc70523712013-07-10 13:27:14156 pub fn with_capacity(n: uint) -> RingBuf<T> {
Colin Sherratt7a666df2014-10-19 20:19:07157 // +1 since the ringbuffer always leaves one space empty
Piotr Czarnecki156a1c32015-01-05 14:48:58158 let cap = cmp::max(n + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
159 assert!(cap > n, "capacity overflow");
Colin Sherratt6277e3b2014-11-14 09:21:44160 let size = cap.checked_mul(mem::size_of::<T>())
Colin Sherratt7a666df2014-10-19 20:19:07161 .expect("capacity overflow");
162
Colin Sherrattba24e332014-11-10 03:34:53163 let ptr = if mem::size_of::<T>() != 0 {
164 unsafe {
165 let ptr = heap::allocate(size, mem::min_align_of::<T>()) as *mut T;;
166 if ptr.is_null() { ::alloc::oom() }
167 ptr
168 }
169 } else {
170 heap::EMPTY as *mut T
171 };
172
Colin Sherratt7a666df2014-10-19 20:19:07173 RingBuf {
174 tail: 0,
175 head: 0,
176 cap: cap,
Colin Sherrattba24e332014-11-10 03:34:53177 ptr: ptr
Colin Sherratt7a666df2014-10-19 20:19:07178 }
blake2-ppc70523712013-07-10 13:27:14179 }
180
P1startf2aa88c2014-08-04 10:48:39181 /// Retrieves an element in the `RingBuf` by index.
blake2-ppc70523712013-07-10 13:27:14182 ///
jbranchaudc09defa2014-12-09 05:28:07183 /// # Examples
Alexis Beingessnercf3b2e42014-11-06 17:24:47184 ///
185 /// ```rust
186 /// use std::collections::RingBuf;
187 ///
188 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03189 /// buf.push_back(3);
Alexis Beingessnercf3b2e42014-11-06 17:24:47190 /// buf.push_back(4);
191 /// buf.push_back(5);
192 /// assert_eq!(buf.get(1).unwrap(), &4);
193 /// ```
Brian Andersonb44ee372015-01-24 05:48:20194 #[stable(feature = "rust1", since = "1.0.0")]
Alexis Beingessnercf3b2e42014-11-06 17:24:47195 pub fn get(&self, i: uint) -> Option<&T> {
Colin Sherratt7a666df2014-10-19 20:19:07196 if i < self.len() {
Colin Sherratt40191182014-11-12 01:22:07197 let idx = self.wrap_index(self.tail + i);
Colin Sherratt7a666df2014-10-19 20:19:07198 unsafe { Some(&*self.ptr.offset(idx as int)) }
199 } else {
200 None
Alexis Beingessnercf3b2e42014-11-06 17:24:47201 }
202 }
203
204 /// Retrieves an element in the `RingBuf` mutably by index.
nhamebe80972014-07-17 23:19:51205 ///
jbranchaudc09defa2014-12-09 05:28:07206 /// # Examples
nhamebe80972014-07-17 23:19:51207 ///
208 /// ```rust
209 /// use std::collections::RingBuf;
210 ///
211 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03212 /// buf.push_back(3);
Alexis Beingessnercf3b2e42014-11-06 17:24:47213 /// buf.push_back(4);
214 /// buf.push_back(5);
215 /// match buf.get_mut(1) {
216 /// None => {}
217 /// Some(elem) => {
218 /// *elem = 7;
219 /// }
220 /// }
221 ///
P1startfd10d202014-08-02 06:39:39222 /// assert_eq!(buf[1], 7);
nhamebe80972014-07-17 23:19:51223 /// ```
Brian Andersonb44ee372015-01-24 05:48:20224 #[stable(feature = "rust1", since = "1.0.0")]
Alexis Beingessnercf3b2e42014-11-06 17:24:47225 pub fn get_mut(&mut self, i: uint) -> Option<&mut T> {
Colin Sherratt7a666df2014-10-19 20:19:07226 if i < self.len() {
Colin Sherratt40191182014-11-12 01:22:07227 let idx = self.wrap_index(self.tail + i);
Colin Sherratt7a666df2014-10-19 20:19:07228 unsafe { Some(&mut *self.ptr.offset(idx as int)) }
229 } else {
230 None
Alexis Beingessnercf3b2e42014-11-06 17:24:47231 }
blake2-ppc70523712013-07-10 13:27:14232 }
233
P1startf2aa88c2014-08-04 10:48:39234 /// Swaps elements at indices `i` and `j`.
blake2-ppc57757a82013-09-26 07:19:26235 ///
236 /// `i` and `j` may be equal.
237 ///
P1startf2aa88c2014-08-04 10:48:39238 /// Fails if there is no element with either index.
nhamebe80972014-07-17 23:19:51239 ///
jbranchaudc09defa2014-12-09 05:28:07240 /// # Examples
nhamebe80972014-07-17 23:19:51241 ///
242 /// ```rust
243 /// use std::collections::RingBuf;
244 ///
245 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03246 /// buf.push_back(3);
Alexis Beingessnercf3b2e42014-11-06 17:24:47247 /// buf.push_back(4);
248 /// buf.push_back(5);
nhamebe80972014-07-17 23:19:51249 /// buf.swap(0, 2);
P1startfd10d202014-08-02 06:39:39250 /// assert_eq!(buf[0], 5);
251 /// assert_eq!(buf[2], 3);
nhamebe80972014-07-17 23:19:51252 /// ```
Brian Andersonb44ee372015-01-24 05:48:20253 #[stable(feature = "rust1", since = "1.0.0")]
blake2-ppc57757a82013-09-26 07:19:26254 pub fn swap(&mut self, i: uint, j: uint) {
255 assert!(i < self.len());
256 assert!(j < self.len());
Colin Sherratt40191182014-11-12 01:22:07257 let ri = self.wrap_index(self.tail + i);
258 let rj = self.wrap_index(self.tail + j);
Colin Sherratt7a666df2014-10-19 20:19:07259 unsafe {
260 ptr::swap(self.ptr.offset(ri as int), self.ptr.offset(rj as int))
261 }
blake2-ppc70523712013-07-10 13:27:14262 }
263
Alexis Beingessnercf3b2e42014-11-06 17:24:47264 /// Returns the number of elements the `RingBuf` can hold without
265 /// reallocating.
266 ///
jbranchaudc09defa2014-12-09 05:28:07267 /// # Examples
Alexis Beingessnercf3b2e42014-11-06 17:24:47268 ///
269 /// ```
270 /// use std::collections::RingBuf;
271 ///
Tobias Bucher7f64fe42015-01-25 21:05:03272 /// let buf: RingBuf<i32> = RingBuf::with_capacity(10);
Colin Sherratt7a666df2014-10-19 20:19:07273 /// assert!(buf.capacity() >= 10);
Alexis Beingessnercf3b2e42014-11-06 17:24:47274 /// ```
275 #[inline]
Brian Andersonb44ee372015-01-24 05:48:20276 #[stable(feature = "rust1", since = "1.0.0")]
Colin Sherratt7a666df2014-10-19 20:19:07277 pub fn capacity(&self) -> uint { self.cap - 1 }
Tim Chevalier77de84b2013-05-27 18:47:38278
Alexis Beingessnercf3b2e42014-11-06 17:24:47279 /// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
280 /// given `RingBuf`. Does nothing if the capacity is already sufficient.
Tim Chevalier77de84b2013-05-27 18:47:38281 ///
Alexis Beingessnercf3b2e42014-11-06 17:24:47282 /// Note that the allocator may give the collection more space than it requests. Therefore
283 /// capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future
284 /// insertions are expected.
285 ///
286 /// # Panics
287 ///
288 /// Panics if the new capacity overflows `uint`.
289 ///
jbranchaudc09defa2014-12-09 05:28:07290 /// # Examples
Alexis Beingessnercf3b2e42014-11-06 17:24:47291 ///
292 /// ```
293 /// use std::collections::RingBuf;
294 ///
Tobias Bucher7f64fe42015-01-25 21:05:03295 /// let mut buf: RingBuf<i32> = vec![1].into_iter().collect();
Alexis Beingessnercf3b2e42014-11-06 17:24:47296 /// buf.reserve_exact(10);
297 /// assert!(buf.capacity() >= 11);
298 /// ```
Brian Andersonb44ee372015-01-24 05:48:20299 #[stable(feature = "rust1", since = "1.0.0")]
Alexis Beingessnercf3b2e42014-11-06 17:24:47300 pub fn reserve_exact(&mut self, additional: uint) {
Colin Sherratt7a666df2014-10-19 20:19:07301 self.reserve(additional);
Alexis Beingessnercf3b2e42014-11-06 17:24:47302 }
303
304 /// Reserves capacity for at least `additional` more elements to be inserted in the given
305 /// `Ringbuf`. The collection may reserve more space to avoid frequent reallocations.
306 ///
307 /// # Panics
308 ///
309 /// Panics if the new capacity overflows `uint`.
310 ///
jbranchaudc09defa2014-12-09 05:28:07311 /// # Examples
Alexis Beingessnercf3b2e42014-11-06 17:24:47312 ///
313 /// ```
314 /// use std::collections::RingBuf;
315 ///
Tobias Bucher7f64fe42015-01-25 21:05:03316 /// let mut buf: RingBuf<i32> = vec![1].into_iter().collect();
Alexis Beingessnercf3b2e42014-11-06 17:24:47317 /// buf.reserve(10);
318 /// assert!(buf.capacity() >= 11);
319 /// ```
Brian Andersonb44ee372015-01-24 05:48:20320 #[stable(feature = "rust1", since = "1.0.0")]
Alexis Beingessnercf3b2e42014-11-06 17:24:47321 pub fn reserve(&mut self, additional: uint) {
Colin Sherratt7a666df2014-10-19 20:19:07322 let new_len = self.len() + additional;
323 assert!(new_len + 1 > self.len(), "capacity overflow");
324 if new_len > self.capacity() {
Colin Sherratt6277e3b2014-11-14 09:21:44325 let count = (new_len + 1).next_power_of_two();
Colin Sherratt7a666df2014-10-19 20:19:07326 assert!(count >= new_len + 1);
327
328 if mem::size_of::<T>() != 0 {
329 let old = self.cap * mem::size_of::<T>();
Colin Sherratt6277e3b2014-11-14 09:21:44330 let new = count.checked_mul(mem::size_of::<T>())
Colin Sherratt7a666df2014-10-19 20:19:07331 .expect("capacity overflow");
332 unsafe {
333 self.ptr = heap::reallocate(self.ptr as *mut u8,
334 old,
335 new,
336 mem::min_align_of::<T>()) as *mut T;
Colin Sherrattba24e332014-11-10 03:34:53337 if self.ptr.is_null() { ::alloc::oom() }
Colin Sherratt7a666df2014-10-19 20:19:07338 }
339 }
340
341 // Move the shortest contiguous section of the ring buffer
342 // T H
343 // [o o o o o o o . ]
344 // T H
345 // A [o o o o o o o . . . . . . . . . ]
346 // H T
347 // [o o . o o o o o ]
348 // T H
349 // B [. . . o o o o o o o . . . . . . ]
350 // H T
351 // [o o o o o . o o ]
352 // H T
353 // C [o o o o o . . . . . . . . . o o ]
354
355 let oldcap = self.cap;
356 self.cap = count;
357
358 if self.tail <= self.head { // A
359 // Nop
360 } else if self.head < oldcap - self.tail { // B
361 unsafe {
Piotr Czarnecki156a1c32015-01-05 14:48:58362 self.copy_nonoverlapping(oldcap, 0, self.head);
Colin Sherratt7a666df2014-10-19 20:19:07363 }
364 self.head += oldcap;
Colin Sherratt4cae9ad2014-11-11 02:16:29365 debug_assert!(self.head > self.tail);
Colin Sherratt7a666df2014-10-19 20:19:07366 } else { // C
Piotr Czarnecki156a1c32015-01-05 14:48:58367 let new_tail = count - (oldcap - self.tail);
Colin Sherratt7a666df2014-10-19 20:19:07368 unsafe {
Piotr Czarnecki156a1c32015-01-05 14:48:58369 self.copy_nonoverlapping(new_tail, self.tail, oldcap - self.tail);
Colin Sherratt7a666df2014-10-19 20:19:07370 }
Piotr Czarnecki156a1c32015-01-05 14:48:58371 self.tail = new_tail;
Colin Sherratt4cae9ad2014-11-11 02:16:29372 debug_assert!(self.head < self.tail);
Colin Sherratt7a666df2014-10-19 20:19:07373 }
Colin Sherratt4cae9ad2014-11-11 02:16:29374 debug_assert!(self.head < self.cap);
375 debug_assert!(self.tail < self.cap);
Colin Sherratt40191182014-11-12 01:22:07376 debug_assert!(self.cap.count_ones() == 1);
Colin Sherratt7a666df2014-10-19 20:19:07377 }
Tim Chevalier77de84b2013-05-27 18:47:38378 }
Jed Estep4f7a7422013-06-25 19:08:47379
Piotr Czarnecki156a1c32015-01-05 14:48:58380 /// Shrinks the capacity of the ringbuf as much as possible.
381 ///
382 /// It will drop down as close as possible to the length but the allocator may still inform the
383 /// ringbuf that there is space for a few more elements.
384 ///
385 /// # Examples
386 ///
387 /// ```
388 /// use std::collections::RingBuf;
389 ///
390 /// let mut buf = RingBuf::with_capacity(15);
Jorge Aparicioefc97a52015-01-26 21:05:07391 /// buf.extend(0u..4);
Piotr Czarnecki156a1c32015-01-05 14:48:58392 /// assert_eq!(buf.capacity(), 15);
393 /// buf.shrink_to_fit();
394 /// assert!(buf.capacity() >= 4);
395 /// ```
396 pub fn shrink_to_fit(&mut self) {
397 // +1 since the ringbuffer always leaves one space empty
398 // len + 1 can't overflow for an existing, well-formed ringbuf.
399 let target_cap = cmp::max(self.len() + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
400 if target_cap < self.cap {
401 // There are three cases of interest:
402 // All elements are out of desired bounds
403 // Elements are contiguous, and head is out of desired bounds
404 // Elements are discontiguous, and tail is out of desired bounds
405 //
406 // At all other times, element positions are unaffected.
407 //
408 // Indicates that elements at the head should be moved.
409 let head_outside = self.head == 0 || self.head >= target_cap;
410 // Move elements from out of desired bounds (positions after target_cap)
411 if self.tail >= target_cap && head_outside {
412 // T H
413 // [. . . . . . . . o o o o o o o . ]
414 // T H
415 // [o o o o o o o . ]
416 unsafe {
417 self.copy_nonoverlapping(0, self.tail, self.len());
418 }
419 self.head = self.len();
420 self.tail = 0;
421 } else if self.tail != 0 && self.tail < target_cap && head_outside {
422 // T H
423 // [. . . o o o o o o o . . . . . . ]
424 // H T
425 // [o o . o o o o o ]
426 let len = self.wrap_index(self.head - target_cap);
427 unsafe {
428 self.copy_nonoverlapping(0, target_cap, len);
429 }
430 self.head = len;
431 debug_assert!(self.head < self.tail);
432 } else if self.tail >= target_cap {
433 // H T
434 // [o o o o o . . . . . . . . . o o ]
435 // H T
436 // [o o o o o . o o ]
437 debug_assert!(self.wrap_index(self.head - 1) < target_cap);
438 let len = self.cap - self.tail;
439 let new_tail = target_cap - len;
440 unsafe {
441 self.copy_nonoverlapping(new_tail, self.tail, len);
442 }
443 self.tail = new_tail;
444 debug_assert!(self.head < self.tail);
445 }
446
447 if mem::size_of::<T>() != 0 {
448 let old = self.cap * mem::size_of::<T>();
449 let new_size = target_cap * mem::size_of::<T>();
450 unsafe {
451 self.ptr = heap::reallocate(self.ptr as *mut u8,
452 old,
453 new_size,
454 mem::min_align_of::<T>()) as *mut T;
455 if self.ptr.is_null() { ::alloc::oom() }
456 }
457 }
458 self.cap = target_cap;
459 debug_assert!(self.head < self.cap);
460 debug_assert!(self.tail < self.cap);
461 debug_assert!(self.cap.count_ones() == 1);
462 }
463 }
464
465 /// Shorten a ringbuf, dropping excess elements from the back.
466 ///
467 /// If `len` is greater than the ringbuf's current length, this has no
468 /// effect.
469 ///
470 /// # Examples
471 ///
472 /// ```
473 /// use std::collections::RingBuf;
474 ///
475 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03476 /// buf.push_back(5);
477 /// buf.push_back(10);
Piotr Czarnecki156a1c32015-01-05 14:48:58478 /// buf.push_back(15);
479 /// buf.truncate(1);
480 /// assert_eq!(buf.len(), 1);
481 /// assert_eq!(Some(&5), buf.get(0));
482 /// ```
Brian Andersoncd6d9ea2015-01-23 02:22:03483 #[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:19484 reason = "matches collection reform specification; waiting on panic semantics")]
Piotr Czarnecki156a1c32015-01-05 14:48:58485 pub fn truncate(&mut self, len: uint) {
Jorge Aparicioefc97a52015-01-26 21:05:07486 for _ in len..self.len() {
Piotr Czarnecki156a1c32015-01-05 14:48:58487 self.pop_back();
488 }
489 }
490
P1startf2aa88c2014-08-04 10:48:39491 /// Returns a front-to-back iterator.
nhamebe80972014-07-17 23:19:51492 ///
jbranchaudc09defa2014-12-09 05:28:07493 /// # Examples
nhamebe80972014-07-17 23:19:51494 ///
495 /// ```rust
496 /// use std::collections::RingBuf;
497 ///
498 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03499 /// buf.push_back(5);
Alexis Beingessnercf3b2e42014-11-06 17:24:47500 /// buf.push_back(3);
501 /// buf.push_back(4);
Nick Cameron52ef4622014-08-06 09:59:40502 /// let b: &[_] = &[&5, &3, &4];
Tobias Bucher7f64fe42015-01-25 21:05:03503 /// assert_eq!(buf.iter().collect::<Vec<&i32>>().as_slice(), b);
nhamebe80972014-07-17 23:19:51504 /// ```
Brian Andersonb44ee372015-01-24 05:48:20505 #[stable(feature = "rust1", since = "1.0.0")]
Florian Wilkensf8cfd242014-12-19 20:52:10506 pub fn iter(&self) -> Iter<T> {
507 Iter {
Colin Sherratt7a666df2014-10-19 20:19:07508 tail: self.tail,
509 head: self.head,
510 ring: unsafe { self.buffer_as_slice() }
511 }
blake2-ppc3385e792013-07-15 23:13:26512 }
513
Andrew Wagner8fcc8322014-12-15 09:22:49514 /// Returns a front-to-back iterator that returns mutable references.
nhamebe80972014-07-17 23:19:51515 ///
jbranchaudc09defa2014-12-09 05:28:07516 /// # Examples
nhamebe80972014-07-17 23:19:51517 ///
518 /// ```rust
519 /// use std::collections::RingBuf;
520 ///
521 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03522 /// buf.push_back(5);
Alexis Beingessnercf3b2e42014-11-06 17:24:47523 /// buf.push_back(3);
524 /// buf.push_back(4);
Aaron Turonfc525ee2014-09-15 03:27:36525 /// for num in buf.iter_mut() {
nhamebe80972014-07-17 23:19:51526 /// *num = *num - 2;
527 /// }
Nick Cameron52ef4622014-08-06 09:59:40528 /// let b: &[_] = &[&mut 3, &mut 1, &mut 2];
Tobias Bucher7f64fe42015-01-25 21:05:03529 /// assert_eq!(&buf.iter_mut().collect::<Vec<&mut i32>>()[], b);
nhamebe80972014-07-17 23:19:51530 /// ```
Brian Andersonb44ee372015-01-24 05:48:20531 #[stable(feature = "rust1", since = "1.0.0")]
Florian Wilkensf8cfd242014-12-19 20:52:10532 pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> {
533 IterMut {
Colin Sherratt7a666df2014-10-19 20:19:07534 tail: self.tail,
535 head: self.head,
536 cap: self.cap,
537 ptr: self.ptr,
538 marker: marker::ContravariantLifetime::<'a>,
Niko Matsakisbc4164d2013-11-16 22:29:39539 }
Jed Estep4f7a7422013-06-25 19:08:47540 }
Alex Crichton21ac9852014-10-30 20:43:24541
Alexis Beingessner865c2db2014-11-23 02:34:11542 /// Consumes the list into an iterator yielding elements by value.
Brian Andersonb44ee372015-01-24 05:48:20543 #[stable(feature = "rust1", since = "1.0.0")]
Florian Wilkensf8cfd242014-12-19 20:52:10544 pub fn into_iter(self) -> IntoIter<T> {
545 IntoIter {
Alexis Beingessner865c2db2014-11-23 02:34:11546 inner: self,
547 }
548 }
549
Clark Gaebel525f65e2014-12-16 04:01:58550 /// Returns a pair of slices which contain, in order, the contents of the
551 /// `RingBuf`.
552 #[inline]
Brian Andersoncd6d9ea2015-01-23 02:22:03553 #[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:19554 reason = "matches collection reform specification, waiting for dust to settle")]
Clark Gaebel525f65e2014-12-16 04:01:58555 pub fn as_slices<'a>(&'a self) -> (&'a [T], &'a [T]) {
556 unsafe {
557 let contiguous = self.is_contiguous();
558 let buf = self.buffer_as_slice();
559 if contiguous {
560 let (empty, buf) = buf.split_at(0);
Jorge Aparicio517f1cc2015-01-07 16:58:31561 (&buf[self.tail..self.head], empty)
Clark Gaebel525f65e2014-12-16 04:01:58562 } else {
563 let (mid, right) = buf.split_at(self.tail);
564 let (left, _) = mid.split_at(self.head);
565 (right, left)
566 }
567 }
568 }
569
570 /// Returns a pair of slices which contain, in order, the contents of the
571 /// `RingBuf`.
572 #[inline]
Brian Andersoncd6d9ea2015-01-23 02:22:03573 #[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:19574 reason = "matches collection reform specification, waiting for dust to settle")]
Clark Gaebel525f65e2014-12-16 04:01:58575 pub fn as_mut_slices<'a>(&'a mut self) -> (&'a mut [T], &'a mut [T]) {
576 unsafe {
577 let contiguous = self.is_contiguous();
578 let head = self.head;
579 let tail = self.tail;
580 let buf = self.buffer_as_mut_slice();
581
582 if contiguous {
583 let (empty, buf) = buf.split_at_mut(0);
Aaron Turona506d4c2015-01-18 00:15:52584 (&mut buf[tail .. head], empty)
Clark Gaebel525f65e2014-12-16 04:01:58585 } else {
586 let (mid, right) = buf.split_at_mut(tail);
587 let (left, _) = mid.split_at_mut(head);
588
589 (right, left)
590 }
591 }
592 }
593
Alex Crichton21ac9852014-10-30 20:43:24594 /// Returns the number of elements in the `RingBuf`.
595 ///
jbranchaudc09defa2014-12-09 05:28:07596 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24597 ///
598 /// ```
599 /// use std::collections::RingBuf;
600 ///
601 /// let mut v = RingBuf::new();
602 /// assert_eq!(v.len(), 0);
Tobias Bucher7f64fe42015-01-25 21:05:03603 /// v.push_back(1);
Alex Crichton21ac9852014-10-30 20:43:24604 /// assert_eq!(v.len(), 1);
605 /// ```
Brian Andersonb44ee372015-01-24 05:48:20606 #[stable(feature = "rust1", since = "1.0.0")]
Colin Sherratt7a666df2014-10-19 20:19:07607 pub fn len(&self) -> uint { count(self.tail, self.head, self.cap) }
Alex Crichton21ac9852014-10-30 20:43:24608
609 /// Returns true if the buffer contains no elements
610 ///
jbranchaudc09defa2014-12-09 05:28:07611 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24612 ///
613 /// ```
614 /// use std::collections::RingBuf;
615 ///
616 /// let mut v = RingBuf::new();
617 /// assert!(v.is_empty());
Tobias Bucher7f64fe42015-01-25 21:05:03618 /// v.push_front(1);
Alex Crichton21ac9852014-10-30 20:43:24619 /// assert!(!v.is_empty());
620 /// ```
Brian Andersonb44ee372015-01-24 05:48:20621 #[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton21ac9852014-10-30 20:43:24622 pub fn is_empty(&self) -> bool { self.len() == 0 }
623
Clark Gaebeld57f2592014-12-16 22:45:03624 /// Creates a draining iterator that clears the `RingBuf` and iterates over
625 /// the removed items from start to end.
626 ///
627 /// # Examples
628 ///
629 /// ```
630 /// use std::collections::RingBuf;
631 ///
632 /// let mut v = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03633 /// v.push_back(1);
Clark Gaebeld57f2592014-12-16 22:45:03634 /// assert_eq!(v.drain().next(), Some(1));
635 /// assert!(v.is_empty());
636 /// ```
637 #[inline]
Brian Andersoncd6d9ea2015-01-23 02:22:03638 #[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:19639 reason = "matches collection reform specification, waiting for dust to settle")]
Alexis Beingessner8dbaa712014-12-31 00:07:53640 pub fn drain(&mut self) -> Drain<T> {
Clark Gaebeld57f2592014-12-16 22:45:03641 Drain {
642 inner: self,
643 }
644 }
645
Alex Crichton21ac9852014-10-30 20:43:24646 /// Clears the buffer, removing all values.
647 ///
jbranchaudc09defa2014-12-09 05:28:07648 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24649 ///
650 /// ```
651 /// use std::collections::RingBuf;
652 ///
653 /// let mut v = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03654 /// v.push_back(1);
Alex Crichton21ac9852014-10-30 20:43:24655 /// v.clear();
656 /// assert!(v.is_empty());
657 /// ```
Brian Andersonb44ee372015-01-24 05:48:20658 #[stable(feature = "rust1", since = "1.0.0")]
Clark Gaebeld57f2592014-12-16 22:45:03659 #[inline]
Alex Crichton21ac9852014-10-30 20:43:24660 pub fn clear(&mut self) {
Clark Gaebeld57f2592014-12-16 22:45:03661 self.drain();
Alex Crichton21ac9852014-10-30 20:43:24662 }
663
664 /// Provides a reference to the front element, or `None` if the sequence is
665 /// empty.
666 ///
jbranchaudc09defa2014-12-09 05:28:07667 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24668 ///
669 /// ```
670 /// use std::collections::RingBuf;
671 ///
672 /// let mut d = RingBuf::new();
673 /// assert_eq!(d.front(), None);
674 ///
Tobias Bucher7f64fe42015-01-25 21:05:03675 /// d.push_back(1);
676 /// d.push_back(2);
677 /// assert_eq!(d.front(), Some(&1));
Alex Crichton21ac9852014-10-30 20:43:24678 /// ```
Brian Andersonb44ee372015-01-24 05:48:20679 #[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton21ac9852014-10-30 20:43:24680 pub fn front(&self) -> Option<&T> {
Colin Sherratt7a666df2014-10-19 20:19:07681 if !self.is_empty() { Some(&self[0]) } else { None }
Alex Crichton21ac9852014-10-30 20:43:24682 }
683
684 /// Provides a mutable reference to the front element, or `None` if the
685 /// sequence is empty.
686 ///
jbranchaudc09defa2014-12-09 05:28:07687 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24688 ///
689 /// ```
690 /// use std::collections::RingBuf;
691 ///
692 /// let mut d = RingBuf::new();
693 /// assert_eq!(d.front_mut(), None);
694 ///
Tobias Bucher7f64fe42015-01-25 21:05:03695 /// d.push_back(1);
696 /// d.push_back(2);
Alex Crichton21ac9852014-10-30 20:43:24697 /// match d.front_mut() {
Tobias Bucher7f64fe42015-01-25 21:05:03698 /// Some(x) => *x = 9,
Alex Crichton21ac9852014-10-30 20:43:24699 /// None => (),
700 /// }
Tobias Bucher7f64fe42015-01-25 21:05:03701 /// assert_eq!(d.front(), Some(&9));
Alex Crichton21ac9852014-10-30 20:43:24702 /// ```
Brian Andersonb44ee372015-01-24 05:48:20703 #[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton21ac9852014-10-30 20:43:24704 pub fn front_mut(&mut self) -> Option<&mut T> {
Colin Sherratt7a666df2014-10-19 20:19:07705 if !self.is_empty() { Some(&mut self[0]) } else { None }
Alex Crichton21ac9852014-10-30 20:43:24706 }
707
708 /// Provides a reference to the back element, or `None` if the sequence is
709 /// empty.
710 ///
jbranchaudc09defa2014-12-09 05:28:07711 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24712 ///
713 /// ```
714 /// use std::collections::RingBuf;
715 ///
716 /// let mut d = RingBuf::new();
717 /// assert_eq!(d.back(), None);
718 ///
Tobias Bucher7f64fe42015-01-25 21:05:03719 /// d.push_back(1);
720 /// d.push_back(2);
721 /// assert_eq!(d.back(), Some(&2));
Alex Crichton21ac9852014-10-30 20:43:24722 /// ```
Brian Andersonb44ee372015-01-24 05:48:20723 #[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton21ac9852014-10-30 20:43:24724 pub fn back(&self) -> Option<&T> {
Colin Sherratt7a666df2014-10-19 20:19:07725 if !self.is_empty() { Some(&self[self.len() - 1]) } else { None }
Alex Crichton21ac9852014-10-30 20:43:24726 }
727
728 /// Provides a mutable reference to the back element, or `None` if the
729 /// sequence is empty.
730 ///
jbranchaudc09defa2014-12-09 05:28:07731 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24732 ///
733 /// ```
734 /// use std::collections::RingBuf;
735 ///
736 /// let mut d = RingBuf::new();
737 /// assert_eq!(d.back(), None);
738 ///
Tobias Bucher7f64fe42015-01-25 21:05:03739 /// d.push_back(1);
740 /// d.push_back(2);
Alex Crichton21ac9852014-10-30 20:43:24741 /// match d.back_mut() {
Tobias Bucher7f64fe42015-01-25 21:05:03742 /// Some(x) => *x = 9,
Alex Crichton21ac9852014-10-30 20:43:24743 /// None => (),
744 /// }
Tobias Bucher7f64fe42015-01-25 21:05:03745 /// assert_eq!(d.back(), Some(&9));
Alex Crichton21ac9852014-10-30 20:43:24746 /// ```
Brian Andersonb44ee372015-01-24 05:48:20747 #[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton21ac9852014-10-30 20:43:24748 pub fn back_mut(&mut self) -> Option<&mut T> {
Colin Sherratt7a666df2014-10-19 20:19:07749 let len = self.len();
750 if !self.is_empty() { Some(&mut self[len - 1]) } else { None }
Alex Crichton21ac9852014-10-30 20:43:24751 }
752
753 /// Removes the first element and returns it, or `None` if the sequence is
754 /// empty.
755 ///
jbranchaudc09defa2014-12-09 05:28:07756 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24757 ///
758 /// ```
759 /// use std::collections::RingBuf;
760 ///
761 /// let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03762 /// d.push_back(1);
763 /// d.push_back(2);
Alex Crichton21ac9852014-10-30 20:43:24764 ///
Tobias Bucher7f64fe42015-01-25 21:05:03765 /// assert_eq!(d.pop_front(), Some(1));
766 /// assert_eq!(d.pop_front(), Some(2));
Alex Crichton21ac9852014-10-30 20:43:24767 /// assert_eq!(d.pop_front(), None);
768 /// ```
Brian Andersonb44ee372015-01-24 05:48:20769 #[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton21ac9852014-10-30 20:43:24770 pub fn pop_front(&mut self) -> Option<T> {
Colin Sherratt7a666df2014-10-19 20:19:07771 if self.is_empty() {
772 None
773 } else {
774 let tail = self.tail;
Colin Sherratt40191182014-11-12 01:22:07775 self.tail = self.wrap_index(self.tail + 1);
Colin Sherratt7a666df2014-10-19 20:19:07776 unsafe { Some(self.buffer_read(tail)) }
Alex Crichton21ac9852014-10-30 20:43:24777 }
Alex Crichton21ac9852014-10-30 20:43:24778 }
779
780 /// Inserts an element first in the sequence.
781 ///
jbranchaudc09defa2014-12-09 05:28:07782 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24783 ///
784 /// ```
785 /// use std::collections::RingBuf;
786 ///
787 /// let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03788 /// d.push_front(1);
789 /// d.push_front(2);
790 /// assert_eq!(d.front(), Some(&2));
Alex Crichton21ac9852014-10-30 20:43:24791 /// ```
Brian Andersonb44ee372015-01-24 05:48:20792 #[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton21ac9852014-10-30 20:43:24793 pub fn push_front(&mut self, t: T) {
Colin Sherratt4cae9ad2014-11-11 02:16:29794 if self.is_full() {
795 self.reserve(1);
796 debug_assert!(!self.is_full());
797 }
Colin Sherratt7a666df2014-10-19 20:19:07798
Colin Sherratt40191182014-11-12 01:22:07799 self.tail = self.wrap_index(self.tail - 1);
Colin Sherratt7a666df2014-10-19 20:19:07800 let tail = self.tail;
801 unsafe { self.buffer_write(tail, t); }
Alex Crichton21ac9852014-10-30 20:43:24802 }
803
Alex Crichton21ac9852014-10-30 20:43:24804 /// Appends an element to the back of a buffer
805 ///
jbranchaudc09defa2014-12-09 05:28:07806 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24807 ///
808 /// ```rust
809 /// use std::collections::RingBuf;
810 ///
811 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03812 /// buf.push_back(1);
Alexis Beingessnercf3b2e42014-11-06 17:24:47813 /// buf.push_back(3);
Alex Crichton21ac9852014-10-30 20:43:24814 /// assert_eq!(3, *buf.back().unwrap());
815 /// ```
Brian Andersonb44ee372015-01-24 05:48:20816 #[stable(feature = "rust1", since = "1.0.0")]
Alexis Beingessnercf3b2e42014-11-06 17:24:47817 pub fn push_back(&mut self, t: T) {
Colin Sherratt4cae9ad2014-11-11 02:16:29818 if self.is_full() {
819 self.reserve(1);
820 debug_assert!(!self.is_full());
821 }
Colin Sherratt7a666df2014-10-19 20:19:07822
823 let head = self.head;
Colin Sherratt40191182014-11-12 01:22:07824 self.head = self.wrap_index(self.head + 1);
Colin Sherratt7a666df2014-10-19 20:19:07825 unsafe { self.buffer_write(head, t) }
Alex Crichton21ac9852014-10-30 20:43:24826 }
827
Alex Crichton21ac9852014-10-30 20:43:24828 /// Removes the last element from a buffer and returns it, or `None` if
829 /// it is empty.
830 ///
jbranchaudc09defa2014-12-09 05:28:07831 /// # Examples
Alex Crichton21ac9852014-10-30 20:43:24832 ///
833 /// ```rust
834 /// use std::collections::RingBuf;
835 ///
836 /// let mut buf = RingBuf::new();
Alexis Beingessnercf3b2e42014-11-06 17:24:47837 /// assert_eq!(buf.pop_back(), None);
Tobias Bucher7f64fe42015-01-25 21:05:03838 /// buf.push_back(1);
Alexis Beingessnercf3b2e42014-11-06 17:24:47839 /// buf.push_back(3);
840 /// assert_eq!(buf.pop_back(), Some(3));
Alex Crichton21ac9852014-10-30 20:43:24841 /// ```
Brian Andersonb44ee372015-01-24 05:48:20842 #[stable(feature = "rust1", since = "1.0.0")]
Alexis Beingessnercf3b2e42014-11-06 17:24:47843 pub fn pop_back(&mut self) -> Option<T> {
Colin Sherratt7a666df2014-10-19 20:19:07844 if self.is_empty() {
Alex Crichton21ac9852014-10-30 20:43:24845 None
Colin Sherratt7a666df2014-10-19 20:19:07846 } else {
Colin Sherratt40191182014-11-12 01:22:07847 self.head = self.wrap_index(self.head - 1);
Colin Sherratt7a666df2014-10-19 20:19:07848 let head = self.head;
849 unsafe { Some(self.buffer_read(head)) }
Alex Crichton21ac9852014-10-30 20:43:24850 }
851 }
Matt Murphy40f28c72014-12-03 17:12:30852
Clark Gaebel525f65e2014-12-16 04:01:58853 #[inline]
854 fn is_contiguous(&self) -> bool {
855 self.tail <= self.head
856 }
857
Piotr Czarnecki156a1c32015-01-05 14:48:58858 /// Removes an element from anywhere in the ringbuf and returns it, replacing it with the last
859 /// element.
860 ///
861 /// This does not preserve ordering, but is O(1).
862 ///
863 /// Returns `None` if `index` is out of bounds.
864 ///
865 /// # Examples
866 ///
867 /// ```
868 /// use std::collections::RingBuf;
869 ///
870 /// let mut buf = RingBuf::new();
871 /// assert_eq!(buf.swap_back_remove(0), None);
Tobias Bucher7f64fe42015-01-25 21:05:03872 /// buf.push_back(5);
Piotr Czarnecki156a1c32015-01-05 14:48:58873 /// buf.push_back(99);
874 /// buf.push_back(15);
875 /// buf.push_back(20);
876 /// buf.push_back(10);
877 /// assert_eq!(buf.swap_back_remove(1), Some(99));
878 /// ```
Brian Andersoncd6d9ea2015-01-23 02:22:03879 #[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:19880 reason = "the naming of this function may be altered")]
Piotr Czarnecki156a1c32015-01-05 14:48:58881 pub fn swap_back_remove(&mut self, index: uint) -> Option<T> {
882 let length = self.len();
883 if length > 0 && index < length - 1 {
884 self.swap(index, length - 1);
885 } else if index >= length {
886 return None;
887 }
888 self.pop_back()
889 }
890
891 /// Removes an element from anywhere in the ringbuf and returns it, replacing it with the first
892 /// element.
893 ///
894 /// This does not preserve ordering, but is O(1).
895 ///
896 /// Returns `None` if `index` is out of bounds.
897 ///
898 /// # Examples
899 ///
900 /// ```
901 /// use std::collections::RingBuf;
902 ///
903 /// let mut buf = RingBuf::new();
904 /// assert_eq!(buf.swap_front_remove(0), None);
Tobias Bucher7f64fe42015-01-25 21:05:03905 /// buf.push_back(15);
Piotr Czarnecki156a1c32015-01-05 14:48:58906 /// buf.push_back(5);
907 /// buf.push_back(10);
908 /// buf.push_back(99);
Tobias Bucher7f64fe42015-01-25 21:05:03909 /// buf.push_back(20);
Piotr Czarnecki156a1c32015-01-05 14:48:58910 /// assert_eq!(buf.swap_front_remove(3), Some(99));
911 /// ```
Brian Andersoncd6d9ea2015-01-23 02:22:03912 #[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:19913 reason = "the naming of this function may be altered")]
Piotr Czarnecki156a1c32015-01-05 14:48:58914 pub fn swap_front_remove(&mut self, index: uint) -> Option<T> {
915 let length = self.len();
916 if length > 0 && index < length && index != 0 {
917 self.swap(index, 0);
918 } else if index >= length {
919 return None;
920 }
921 self.pop_front()
922 }
923
Matt Murphy40f28c72014-12-03 17:12:30924 /// Inserts an element at position `i` within the ringbuf. Whichever
925 /// end is closer to the insertion point will be moved to make room,
926 /// and all the affected elements will be moved to new positions.
927 ///
928 /// # Panics
929 ///
930 /// Panics if `i` is greater than ringbuf's length
931 ///
Piotr Czarnecki156a1c32015-01-05 14:48:58932 /// # Examples
Matt Murphy40f28c72014-12-03 17:12:30933 /// ```rust
934 /// use std::collections::RingBuf;
935 ///
936 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:03937 /// buf.push_back(10);
Matt Murphy40f28c72014-12-03 17:12:30938 /// buf.push_back(12);
939 /// buf.insert(1,11);
940 /// assert_eq!(Some(&11), buf.get(1));
941 /// ```
942 pub fn insert(&mut self, i: uint, t: T) {
943 assert!(i <= self.len(), "index out of bounds");
944 if self.is_full() {
945 self.reserve(1);
946 debug_assert!(!self.is_full());
947 }
948
949 // Move the least number of elements in the ring buffer and insert
950 // the given object
951 //
952 // At most len/2 - 1 elements will be moved. O(min(n, n-i))
953 //
954 // There are three main cases:
955 // Elements are contiguous
956 // - special case when tail is 0
957 // Elements are discontiguous and the insert is in the tail section
958 // Elements are discontiguous and the insert is in the head section
959 //
960 // For each of those there are two more cases:
961 // Insert is closer to tail
962 // Insert is closer to head
963 //
964 // Key: H - self.head
965 // T - self.tail
966 // o - Valid element
967 // I - Insertion element
968 // A - The element that should be after the insertion point
969 // M - Indicates element was moved
970
971 let idx = self.wrap_index(self.tail + i);
972
973 let distance_to_tail = i;
974 let distance_to_head = self.len() - i;
975
Clark Gaebel525f65e2014-12-16 04:01:58976 let contiguous = self.is_contiguous();
Matt Murphy40f28c72014-12-03 17:12:30977
978 match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) {
979 (true, true, _) if i == 0 => {
980 // push_front
981 //
982 // T
983 // I H
984 // [A o o o o o o . . . . . . . . .]
985 //
986 // H T
987 // [A o o o o o o o . . . . . I]
988 //
989
990 self.tail = self.wrap_index(self.tail - 1);
991 },
Piotr Czarnecki59d41532014-12-16 23:37:55992 (true, true, _) => unsafe {
Matt Murphy40f28c72014-12-03 17:12:30993 // contiguous, insert closer to tail:
994 //
995 // T I H
996 // [. . . o o A o o o o . . . . . .]
997 //
998 // T H
999 // [. . o o I A o o o o . . . . . .]
1000 // M M
1001 //
1002 // contiguous, insert closer to tail and tail is 0:
1003 //
1004 //
1005 // T I H
1006 // [o o A o o o o . . . . . . . . .]
1007 //
1008 // H T
1009 // [o I A o o o o o . . . . . . . o]
1010 // M M
1011
Piotr Czarnecki59d41532014-12-16 23:37:551012 let new_tail = self.wrap_index(self.tail - 1);
Matt Murphy40f28c72014-12-03 17:12:301013
Piotr Czarnecki59d41532014-12-16 23:37:551014 self.copy(new_tail, self.tail, 1);
1015 // Already moved the tail, so we only copy `i - 1` elements.
1016 self.copy(self.tail, self.tail + 1, i - 1);
1017
1018 self.tail = new_tail;
Matt Murphy40f28c72014-12-03 17:12:301019 },
Piotr Czarnecki59d41532014-12-16 23:37:551020 (true, false, _) => unsafe {
Matt Murphy40f28c72014-12-03 17:12:301021 // contiguous, insert closer to head:
1022 //
1023 // T I H
1024 // [. . . o o o o A o o . . . . . .]
1025 //
1026 // T H
1027 // [. . . o o o o I A o o . . . . .]
1028 // M M M
1029
Piotr Czarnecki59d41532014-12-16 23:37:551030 self.copy(idx + 1, idx, self.head - idx);
Matt Murphy40f28c72014-12-03 17:12:301031 self.head = self.wrap_index(self.head + 1);
Matt Murphy40f28c72014-12-03 17:12:301032 },
Piotr Czarnecki59d41532014-12-16 23:37:551033 (false, true, true) => unsafe {
1034 // discontiguous, insert closer to tail, tail section:
Matt Murphy40f28c72014-12-03 17:12:301035 //
1036 // H T I
1037 // [o o o o o o . . . . . o o A o o]
1038 //
1039 // H T
1040 // [o o o o o o . . . . o o I A o o]
1041 // M M
1042
Piotr Czarnecki59d41532014-12-16 23:37:551043 self.copy(self.tail - 1, self.tail, i);
1044 self.tail -= 1;
Matt Murphy40f28c72014-12-03 17:12:301045 },
Piotr Czarnecki59d41532014-12-16 23:37:551046 (false, false, true) => unsafe {
1047 // discontiguous, insert closer to head, tail section:
Matt Murphy40f28c72014-12-03 17:12:301048 //
1049 // H T I
1050 // [o o . . . . . . . o o o o o A o]
1051 //
1052 // H T
1053 // [o o o . . . . . . o o o o o I A]
1054 // M M M M
1055
Matt Murphy40f28c72014-12-03 17:12:301056 // copy elements up to new head
Piotr Czarnecki59d41532014-12-16 23:37:551057 self.copy(1, 0, self.head);
Matt Murphy40f28c72014-12-03 17:12:301058
1059 // copy last element into empty spot at bottom of buffer
1060 self.copy(0, self.cap - 1, 1);
1061
1062 // move elements from idx to end forward not including ^ element
1063 self.copy(idx + 1, idx, self.cap - 1 - idx);
Piotr Czarnecki59d41532014-12-16 23:37:551064
1065 self.head += 1;
Matt Murphy40f28c72014-12-03 17:12:301066 },
Piotr Czarnecki59d41532014-12-16 23:37:551067 (false, true, false) if idx == 0 => unsafe {
1068 // discontiguous, insert is closer to tail, head section,
Matt Murphy40f28c72014-12-03 17:12:301069 // and is at index zero in the internal buffer:
1070 //
1071 // I H T
1072 // [A o o o o o o o o o . . . o o o]
1073 //
1074 // H T
1075 // [A o o o o o o o o o . . o o o I]
1076 // M M M
1077
Matt Murphy40f28c72014-12-03 17:12:301078 // copy elements up to new tail
Piotr Czarnecki59d41532014-12-16 23:37:551079 self.copy(self.tail - 1, self.tail, self.cap - self.tail);
Matt Murphy40f28c72014-12-03 17:12:301080
1081 // copy last element into empty spot at bottom of buffer
1082 self.copy(self.cap - 1, 0, 1);
Piotr Czarnecki59d41532014-12-16 23:37:551083
1084 self.tail -= 1;
Matt Murphy40f28c72014-12-03 17:12:301085 },
Piotr Czarnecki59d41532014-12-16 23:37:551086 (false, true, false) => unsafe {
1087 // discontiguous, insert closer to tail, head section:
Matt Murphy40f28c72014-12-03 17:12:301088 //
1089 // I H T
1090 // [o o o A o o o o o o . . . o o o]
1091 //
1092 // H T
1093 // [o o I A o o o o o o . . o o o o]
1094 // M M M M M M
1095
Matt Murphy40f28c72014-12-03 17:12:301096 // copy elements up to new tail
Piotr Czarnecki59d41532014-12-16 23:37:551097 self.copy(self.tail - 1, self.tail, self.cap - self.tail);
Matt Murphy40f28c72014-12-03 17:12:301098
1099 // copy last element into empty spot at bottom of buffer
1100 self.copy(self.cap - 1, 0, 1);
1101
1102 // move elements from idx-1 to end forward not including ^ element
1103 self.copy(0, 1, idx - 1);
Piotr Czarnecki59d41532014-12-16 23:37:551104
1105 self.tail -= 1;
1106 },
1107 (false, false, false) => unsafe {
1108 // discontiguous, insert closer to head, head section:
Matt Murphy40f28c72014-12-03 17:12:301109 //
1110 // I H T
1111 // [o o o o A o o . . . . . . o o o]
1112 //
1113 // H T
1114 // [o o o o I A o o . . . . . o o o]
1115 // M M M
1116
Piotr Czarnecki59d41532014-12-16 23:37:551117 self.copy(idx + 1, idx, self.head - idx);
1118 self.head += 1;
Matt Murphy40f28c72014-12-03 17:12:301119 }
1120 }
1121
1122 // tail might've been changed so we need to recalculate
1123 let new_idx = self.wrap_index(self.tail + i);
1124 unsafe {
1125 self.buffer_write(new_idx, t);
1126 }
1127 }
Piotr Czarnecki59d41532014-12-16 23:37:551128
1129 /// Removes and returns the element at position `i` from the ringbuf.
1130 /// Whichever end is closer to the removal point will be moved to make
1131 /// room, and all the affected elements will be moved to new positions.
1132 /// Returns `None` if `i` is out of bounds.
1133 ///
Piotr Czarnecki156a1c32015-01-05 14:48:581134 /// # Examples
Piotr Czarnecki59d41532014-12-16 23:37:551135 /// ```rust
1136 /// use std::collections::RingBuf;
1137 ///
1138 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:031139 /// buf.push_back(5);
1140 /// buf.push_back(10);
1141 /// buf.push_back(12);
Piotr Czarnecki59d41532014-12-16 23:37:551142 /// buf.push_back(15);
1143 /// buf.remove(2);
1144 /// assert_eq!(Some(&15), buf.get(2));
1145 /// ```
Brian Andersonb44ee372015-01-24 05:48:201146 #[stable(feature = "rust1", since = "1.0.0")]
Piotr Czarnecki59d41532014-12-16 23:37:551147 pub fn remove(&mut self, i: uint) -> Option<T> {
1148 if self.is_empty() || self.len() <= i {
1149 return None;
1150 }
1151
1152 // There are three main cases:
1153 // Elements are contiguous
1154 // Elements are discontiguous and the removal is in the tail section
1155 // Elements are discontiguous and the removal is in the head section
1156 // - special case when elements are technically contiguous,
1157 // but self.head = 0
1158 //
1159 // For each of those there are two more cases:
1160 // Insert is closer to tail
1161 // Insert is closer to head
1162 //
1163 // Key: H - self.head
1164 // T - self.tail
1165 // o - Valid element
1166 // x - Element marked for removal
1167 // R - Indicates element that is being removed
1168 // M - Indicates element was moved
1169
1170 let idx = self.wrap_index(self.tail + i);
1171
1172 let elem = unsafe {
1173 Some(self.buffer_read(idx))
1174 };
1175
1176 let distance_to_tail = i;
1177 let distance_to_head = self.len() - i;
1178
Piotr Czarnecki156a1c32015-01-05 14:48:581179 let contiguous = self.is_contiguous();
Piotr Czarnecki59d41532014-12-16 23:37:551180
1181 match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) {
1182 (true, true, _) => unsafe {
1183 // contiguous, remove closer to tail:
1184 //
1185 // T R H
1186 // [. . . o o x o o o o . . . . . .]
1187 //
1188 // T H
1189 // [. . . . o o o o o o . . . . . .]
1190 // M M
1191
1192 self.copy(self.tail + 1, self.tail, i);
1193 self.tail += 1;
1194 },
1195 (true, false, _) => unsafe {
1196 // contiguous, remove closer to head:
1197 //
1198 // T R H
1199 // [. . . o o o o x o o . . . . . .]
1200 //
1201 // T H
1202 // [. . . o o o o o o . . . . . . .]
1203 // M M
1204
1205 self.copy(idx, idx + 1, self.head - idx - 1);
1206 self.head -= 1;
1207 },
1208 (false, true, true) => unsafe {
1209 // discontiguous, remove closer to tail, tail section:
1210 //
1211 // H T R
1212 // [o o o o o o . . . . . o o x o o]
1213 //
1214 // H T
1215 // [o o o o o o . . . . . . o o o o]
1216 // M M
1217
1218 self.copy(self.tail + 1, self.tail, i);
1219 self.tail = self.wrap_index(self.tail + 1);
1220 },
1221 (false, false, false) => unsafe {
1222 // discontiguous, remove closer to head, head section:
1223 //
1224 // R H T
1225 // [o o o o x o o . . . . . . o o o]
1226 //
1227 // H T
1228 // [o o o o o o . . . . . . . o o o]
1229 // M M
1230
1231 self.copy(idx, idx + 1, self.head - idx - 1);
1232 self.head -= 1;
1233 },
1234 (false, false, true) => unsafe {
1235 // discontiguous, remove closer to head, tail section:
1236 //
1237 // H T R
1238 // [o o o . . . . . . o o o o o x o]
1239 //
1240 // H T
1241 // [o o . . . . . . . o o o o o o o]
1242 // M M M M
1243 //
1244 // or quasi-discontiguous, remove next to head, tail section:
1245 //
1246 // H T R
1247 // [. . . . . . . . . o o o o o x o]
1248 //
1249 // T H
1250 // [. . . . . . . . . o o o o o o .]
1251 // M
1252
1253 // draw in elements in the tail section
1254 self.copy(idx, idx + 1, self.cap - idx - 1);
1255
1256 // Prevents underflow.
1257 if self.head != 0 {
1258 // copy first element into empty spot
1259 self.copy(self.cap - 1, 0, 1);
1260
1261 // move elements in the head section backwards
1262 self.copy(0, 1, self.head - 1);
1263 }
1264
1265 self.head = self.wrap_index(self.head - 1);
1266 },
1267 (false, true, false) => unsafe {
1268 // discontiguous, remove closer to tail, head section:
1269 //
1270 // R H T
1271 // [o o x o o o o o o o . . . o o o]
1272 //
1273 // H T
1274 // [o o o o o o o o o o . . . . o o]
1275 // M M M M M
1276
1277 // draw in elements up to idx
1278 self.copy(1, 0, idx);
1279
1280 // copy last element into empty spot
1281 self.copy(0, self.cap - 1, 1);
1282
1283 // move elements from tail to end forward, excluding the last one
1284 self.copy(self.tail + 1, self.tail, self.cap - self.tail - 1);
1285
1286 self.tail = self.wrap_index(self.tail + 1);
1287 }
1288 }
1289
1290 return elem;
1291 }
Jed Estep4f7a7422013-06-25 19:08:471292}
1293
Piotr Czarnecki156a1c32015-01-05 14:48:581294impl<T: Clone> RingBuf<T> {
1295 /// Modifies the ringbuf in-place so that `len()` is equal to new_len,
1296 /// either by removing excess elements or by appending copies of a value to the back.
1297 ///
1298 /// # Examples
1299 ///
1300 /// ```
1301 /// use std::collections::RingBuf;
1302 ///
1303 /// let mut buf = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:031304 /// buf.push_back(5);
1305 /// buf.push_back(10);
Piotr Czarnecki156a1c32015-01-05 14:48:581306 /// buf.push_back(15);
1307 /// buf.resize(2, 0);
1308 /// buf.resize(6, 20);
1309 /// for (a, b) in [5, 10, 20, 20, 20, 20].iter().zip(buf.iter()) {
1310 /// assert_eq!(a, b);
1311 /// }
1312 /// ```
Brian Andersoncd6d9ea2015-01-23 02:22:031313 #[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:191314 reason = "matches collection reform specification; waiting on panic semantics")]
Piotr Czarnecki156a1c32015-01-05 14:48:581315 pub fn resize(&mut self, new_len: uint, value: T) {
1316 let len = self.len();
1317
1318 if new_len > len {
1319 self.extend(repeat(value).take(new_len - len))
1320 } else {
1321 self.truncate(new_len);
1322 }
1323 }
1324}
1325
Colin Sherratt7a666df2014-10-19 20:19:071326/// Returns the index in the underlying buffer for a given logical element index.
1327#[inline]
1328fn wrap_index(index: uint, size: uint) -> uint {
1329 // size is always a power of 2
Colin Sherratt40191182014-11-12 01:22:071330 index & (size - 1)
Colin Sherratt7a666df2014-10-19 20:19:071331}
1332
1333/// Calculate the number of elements left to be read in the buffer
1334#[inline]
1335fn count(tail: uint, head: uint, size: uint) -> uint {
1336 // size is always a power of 2
1337 (head - tail) & (size - 1)
1338}
1339
Niko Matsakis1b487a82014-08-28 01:46:521340/// `RingBuf` iterator.
Brian Andersonb44ee372015-01-24 05:48:201341#[stable(feature = "rust1", since = "1.0.0")]
Florian Wilkensf8cfd242014-12-19 20:52:101342pub struct Iter<'a, T:'a> {
Colin Sherratt7a666df2014-10-19 20:19:071343 ring: &'a [T],
1344 tail: uint,
1345 head: uint
Niko Matsakis1b487a82014-08-28 01:46:521346}
1347
Jorge Aparicio351409a2015-01-04 03:54:181348// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
Huon Wilsonb7832ed2014-12-30 10:01:361349impl<'a, T> Clone for Iter<'a, T> {
1350 fn clone(&self) -> Iter<'a, T> {
1351 Iter {
1352 ring: self.ring,
1353 tail: self.tail,
1354 head: self.head
1355 }
1356 }
1357}
1358
Brian Andersonb44ee372015-01-24 05:48:201359#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351360impl<'a, T> Iterator for Iter<'a, T> {
1361 type Item = &'a T;
1362
Niko Matsakisbc4164d2013-11-16 22:29:391363 #[inline]
Erik Price5731ca32013-12-10 07:16:181364 fn next(&mut self) -> Option<&'a T> {
Colin Sherratt7a666df2014-10-19 20:19:071365 if self.tail == self.head {
Niko Matsakisbc4164d2013-11-16 22:29:391366 return None;
1367 }
Colin Sherratt7a666df2014-10-19 20:19:071368 let tail = self.tail;
1369 self.tail = wrap_index(self.tail + 1, self.ring.len());
Aaron Turon6abfac02014-12-30 18:51:181370 unsafe { Some(self.ring.get_unchecked(tail)) }
Niko Matsakisbc4164d2013-11-16 22:29:391371 }
1372
1373 #[inline]
1374 fn size_hint(&self) -> (uint, Option<uint>) {
Colin Sherratt7a666df2014-10-19 20:19:071375 let len = count(self.tail, self.head, self.ring.len());
Niko Matsakisbc4164d2013-11-16 22:29:391376 (len, Some(len))
1377 }
1378}
1379
Brian Andersonb44ee372015-01-24 05:48:201380#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351381impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:391382 #[inline]
Erik Price5731ca32013-12-10 07:16:181383 fn next_back(&mut self) -> Option<&'a T> {
Colin Sherratt7a666df2014-10-19 20:19:071384 if self.tail == self.head {
Niko Matsakisbc4164d2013-11-16 22:29:391385 return None;
1386 }
Colin Sherratt7a666df2014-10-19 20:19:071387 self.head = wrap_index(self.head - 1, self.ring.len());
Aaron Turon6abfac02014-12-30 18:51:181388 unsafe { Some(self.ring.get_unchecked(self.head)) }
Niko Matsakisbc4164d2013-11-16 22:29:391389 }
1390}
Jed Estep35314c92013-06-26 15:38:291391
Brian Andersonb44ee372015-01-24 05:48:201392#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351393impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:241394
Brian Andersonb44ee372015-01-24 05:48:201395#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351396impl<'a, T> RandomAccessIterator for Iter<'a, T> {
blake2-ppcf6862132013-07-29 18:16:261397 #[inline]
Colin Sherratt7a666df2014-10-19 20:19:071398 fn indexable(&self) -> uint {
1399 let (len, _) = self.size_hint();
1400 len
1401 }
blake2-ppcf6862132013-07-29 18:16:261402
1403 #[inline]
Alex Crichtonf4083a22014-04-22 05:15:421404 fn idx(&mut self, j: uint) -> Option<&'a T> {
blake2-ppcf6862132013-07-29 18:16:261405 if j >= self.indexable() {
1406 None
1407 } else {
Colin Sherratt7a666df2014-10-19 20:19:071408 let idx = wrap_index(self.tail + j, self.ring.len());
Aaron Turon6abfac02014-12-30 18:51:181409 unsafe { Some(self.ring.get_unchecked(idx)) }
blake2-ppcf6862132013-07-29 18:16:261410 }
1411 }
1412}
1413
Florian Wilkensf8cfd242014-12-19 20:52:101414// FIXME This was implemented differently from Iter because of a problem
Colin Sherratt7a666df2014-10-19 20:19:071415// with returning the mutable reference. I couldn't find a way to
1416// make the lifetime checker happy so, but there should be a way.
Niko Matsakis1b487a82014-08-28 01:46:521417/// `RingBuf` mutable iterator.
Brian Andersonb44ee372015-01-24 05:48:201418#[stable(feature = "rust1", since = "1.0.0")]
Florian Wilkensf8cfd242014-12-19 20:52:101419pub struct IterMut<'a, T:'a> {
Colin Sherratt7a666df2014-10-19 20:19:071420 ptr: *mut T,
1421 tail: uint,
1422 head: uint,
1423 cap: uint,
1424 marker: marker::ContravariantLifetime<'a>,
Niko Matsakis1b487a82014-08-28 01:46:521425}
1426
Brian Andersonb44ee372015-01-24 05:48:201427#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351428impl<'a, T> Iterator for IterMut<'a, T> {
1429 type Item = &'a mut T;
1430
Niko Matsakisbc4164d2013-11-16 22:29:391431 #[inline]
Erik Price5731ca32013-12-10 07:16:181432 fn next(&mut self) -> Option<&'a mut T> {
Colin Sherratt7a666df2014-10-19 20:19:071433 if self.tail == self.head {
Niko Matsakisbc4164d2013-11-16 22:29:391434 return None;
1435 }
Colin Sherratt7a666df2014-10-19 20:19:071436 let tail = self.tail;
1437 self.tail = wrap_index(self.tail + 1, self.cap);
Alexis Beingessner865c2db2014-11-23 02:34:111438
1439 unsafe {
1440 Some(&mut *self.ptr.offset(tail as int))
Alex Crichton9d5d97b2014-10-15 06:05:011441 }
Niko Matsakisbc4164d2013-11-16 22:29:391442 }
1443
1444 #[inline]
1445 fn size_hint(&self) -> (uint, Option<uint>) {
Colin Sherratt7a666df2014-10-19 20:19:071446 let len = count(self.tail, self.head, self.cap);
1447 (len, Some(len))
Niko Matsakisbc4164d2013-11-16 22:29:391448 }
1449}
1450
Brian Andersonb44ee372015-01-24 05:48:201451#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351452impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:391453 #[inline]
Erik Price5731ca32013-12-10 07:16:181454 fn next_back(&mut self) -> Option<&'a mut T> {
Colin Sherratt7a666df2014-10-19 20:19:071455 if self.tail == self.head {
Niko Matsakisbc4164d2013-11-16 22:29:391456 return None;
1457 }
Colin Sherratt7a666df2014-10-19 20:19:071458 self.head = wrap_index(self.head - 1, self.cap);
Alexis Beingessner865c2db2014-11-23 02:34:111459
1460 unsafe {
1461 Some(&mut *self.ptr.offset(self.head as int))
1462 }
Niko Matsakisbc4164d2013-11-16 22:29:391463 }
1464}
Daniel Micayb47e1e92013-02-16 22:55:551465
Brian Andersonb44ee372015-01-24 05:48:201466#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351467impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:241468
Alexis Beingessner8dbaa712014-12-31 00:07:531469/// A by-value RingBuf iterator
Brian Andersonb44ee372015-01-24 05:48:201470#[stable(feature = "rust1", since = "1.0.0")]
Florian Wilkensf8cfd242014-12-19 20:52:101471pub struct IntoIter<T> {
Alexis Beingessner865c2db2014-11-23 02:34:111472 inner: RingBuf<T>,
1473}
1474
Brian Andersonb44ee372015-01-24 05:48:201475#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351476impl<T> Iterator for IntoIter<T> {
1477 type Item = T;
1478
Alexis Beingessner865c2db2014-11-23 02:34:111479 #[inline]
1480 fn next(&mut self) -> Option<T> {
1481 self.inner.pop_front()
1482 }
1483
1484 #[inline]
1485 fn size_hint(&self) -> (uint, Option<uint>) {
1486 let len = self.inner.len();
1487 (len, Some(len))
1488 }
1489}
1490
Brian Andersonb44ee372015-01-24 05:48:201491#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351492impl<T> DoubleEndedIterator for IntoIter<T> {
Alexis Beingessner865c2db2014-11-23 02:34:111493 #[inline]
1494 fn next_back(&mut self) -> Option<T> {
1495 self.inner.pop_back()
1496 }
1497}
1498
Brian Andersonb44ee372015-01-24 05:48:201499#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351500impl<T> ExactSizeIterator for IntoIter<T> {}
Alexis Beingessner865c2db2014-11-23 02:34:111501
Clark Gaebeld57f2592014-12-16 22:45:031502/// A draining RingBuf iterator
Brian Andersoncd6d9ea2015-01-23 02:22:031503#[unstable(feature = "collections",
Brian Anderson94ca8a32015-01-13 02:40:191504 reason = "matches collection reform specification, waiting for dust to settle")]
Clark Gaebeld57f2592014-12-16 22:45:031505pub struct Drain<'a, T: 'a> {
1506 inner: &'a mut RingBuf<T>,
1507}
1508
1509#[unsafe_destructor]
Brian Andersonb44ee372015-01-24 05:48:201510#[stable(feature = "rust1", since = "1.0.0")]
Clark Gaebeld57f2592014-12-16 22:45:031511impl<'a, T: 'a> Drop for Drain<'a, T> {
1512 fn drop(&mut self) {
Jorge Apariciof9865ea2015-01-11 02:50:071513 for _ in self.by_ref() {}
Clark Gaebeld57f2592014-12-16 22:45:031514 self.inner.head = 0;
1515 self.inner.tail = 0;
1516 }
1517}
1518
Brian Andersonb44ee372015-01-24 05:48:201519#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351520impl<'a, T: 'a> Iterator for Drain<'a, T> {
1521 type Item = T;
1522
Clark Gaebeld57f2592014-12-16 22:45:031523 #[inline]
1524 fn next(&mut self) -> Option<T> {
1525 self.inner.pop_front()
1526 }
1527
1528 #[inline]
1529 fn size_hint(&self) -> (uint, Option<uint>) {
1530 let len = self.inner.len();
1531 (len, Some(len))
1532 }
1533}
1534
Brian Andersonb44ee372015-01-24 05:48:201535#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351536impl<'a, T: 'a> DoubleEndedIterator for Drain<'a, T> {
Clark Gaebeld57f2592014-12-16 22:45:031537 #[inline]
1538 fn next_back(&mut self) -> Option<T> {
1539 self.inner.pop_back()
1540 }
1541}
1542
Brian Andersonb44ee372015-01-24 05:48:201543#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio6b116be2015-01-02 04:15:351544impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {}
Clark Gaebeld57f2592014-12-16 22:45:031545
Brian Andersonb44ee372015-01-24 05:48:201546#[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton748bc3c2014-05-30 00:45:071547impl<A: PartialEq> PartialEq for RingBuf<A> {
blake2-ppc70523712013-07-10 13:27:141548 fn eq(&self, other: &RingBuf<A>) -> bool {
Colin Sherratt7a666df2014-10-19 20:19:071549 self.len() == other.len() &&
blake2-ppc10c76982013-07-06 13:27:321550 self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
1551 }
blake2-ppc10c76982013-07-06 13:27:321552}
1553
Brian Andersonb44ee372015-01-24 05:48:201554#[stable(feature = "rust1", since = "1.0.0")]
nham25acfde2014-08-01 20:05:031555impl<A: Eq> Eq for RingBuf<A> {}
1556
Brian Andersonb44ee372015-01-24 05:48:201557#[stable(feature = "rust1", since = "1.0.0")]
nham63615772014-07-27 03:18:561558impl<A: PartialOrd> PartialOrd for RingBuf<A> {
1559 fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
1560 iter::order::partial_cmp(self.iter(), other.iter())
1561 }
1562}
1563
Brian Andersonb44ee372015-01-24 05:48:201564#[stable(feature = "rust1", since = "1.0.0")]
nham3737c532014-08-01 20:22:481565impl<A: Ord> Ord for RingBuf<A> {
1566 #[inline]
1567 fn cmp(&self, other: &RingBuf<A>) -> Ordering {
1568 iter::order::cmp(self.iter(), other.iter())
1569 }
1570}
1571
Brian Andersonb44ee372015-01-24 05:48:201572#[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton511f0b82014-12-09 20:37:231573impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for RingBuf<A> {
nham1cfa6562014-07-27 02:33:471574 fn hash(&self, state: &mut S) {
nham9fa44242014-07-27 16:37:321575 self.len().hash(state);
Jorge Apariciod5d7e652015-01-31 17:20:461576 for elt in self {
nham1cfa6562014-07-27 02:33:471577 elt.hash(state);
1578 }
1579 }
1580}
1581
Brian Andersonb44ee372015-01-24 05:48:201582#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio32dd5922015-01-03 15:40:101583impl<A> Index<uint> for RingBuf<A> {
1584 type Output = A;
1585
1586 #[inline]
1587 fn index<'a>(&'a self, i: &uint) -> &'a A {
1588 self.get(*i).expect("Out of bounds access")
1589 }
1590}
1591
Brian Andersonb44ee372015-01-24 05:48:201592#[stable(feature = "rust1", since = "1.0.0")]
Jorge Aparicio32dd5922015-01-03 15:40:101593impl<A> IndexMut<uint> for RingBuf<A> {
1594 type Output = A;
1595
1596 #[inline]
1597 fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut A {
1598 self.get_mut(*i).expect("Out of bounds access")
1599 }
1600}
1601
Brian Andersonb44ee372015-01-24 05:48:201602#[stable(feature = "rust1", since = "1.0.0")]
Huon Wilson53487a02013-08-13 13:08:141603impl<A> FromIterator<A> for RingBuf<A> {
Jorge Aparicio6b116be2015-01-02 04:15:351604 fn from_iter<T: Iterator<Item=A>>(iterator: T) -> RingBuf<A> {
blake2-ppcf8ae5262013-07-30 00:06:491605 let (lower, _) = iterator.size_hint();
1606 let mut deq = RingBuf::with_capacity(lower);
1607 deq.extend(iterator);
blake2-ppc08dc72f2013-07-06 03:42:451608 deq
1609 }
1610}
1611
Jorge Aparicioa65d3f52015-01-08 03:01:051612impl<T> IntoIterator for RingBuf<T> {
1613 type Iter = IntoIter<T>;
1614
1615 fn into_iter(self) -> IntoIter<T> {
1616 self.into_iter()
1617 }
1618}
1619
1620impl<'a, T> IntoIterator for &'a RingBuf<T> {
1621 type Iter = Iter<'a, T>;
1622
1623 fn into_iter(self) -> Iter<'a, T> {
1624 self.iter()
1625 }
1626}
1627
1628impl<'a, T> IntoIterator for &'a mut RingBuf<T> {
1629 type Iter = IterMut<'a, T>;
1630
1631 fn into_iter(mut self) -> IterMut<'a, T> {
1632 self.iter_mut()
1633 }
1634}
1635
Brian Andersonb44ee372015-01-24 05:48:201636#[stable(feature = "rust1", since = "1.0.0")]
gamazeps16c8cd92014-11-08 00:39:391637impl<A> Extend<A> for RingBuf<A> {
Jorge Aparicio6b116be2015-01-02 04:15:351638 fn extend<T: Iterator<Item=A>>(&mut self, mut iterator: T) {
Marvin Löbel6200e762014-03-20 13:12:561639 for elt in iterator {
Alexis Beingessnercf3b2e42014-11-06 17:24:471640 self.push_back(elt);
blake2-ppcf8ae5262013-07-30 00:06:491641 }
1642 }
1643}
1644
Brian Andersonb44ee372015-01-24 05:48:201645#[stable(feature = "rust1", since = "1.0.0")]
Alex Crichton3cb9fa22015-01-20 23:45:071646impl<T: fmt::Debug> fmt::Debug for RingBuf<T> {
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041647 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Sean McArthur44440e52014-12-20 08:09:351648 try!(write!(f, "RingBuf ["));
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041649
1650 for (i, e) in self.iter().enumerate() {
1651 if i != 0 { try!(write!(f, ", ")); }
Sean McArthur44440e52014-12-20 08:09:351652 try!(write!(f, "{:?}", *e));
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041653 }
1654
1655 write!(f, "]")
1656 }
1657}
1658
Brian Anderson6e27b272012-01-18 03:05:071659#[cfg(test)]
1660mod tests {
Steven Fackler3dcd2152014-11-06 08:05:531661 use self::Taggy::*;
1662 use self::Taggypar::*;
Eduard Burtescub45d30d2014-12-19 12:02:221663 use prelude::*;
Eduard Burtescub45d30d2014-12-19 12:02:221664 use core::iter;
Alex Crichton3cb9fa22015-01-20 23:45:071665 use std::fmt::Debug;
Alex Crichton511f0b82014-12-09 20:37:231666 use std::hash::{self, SipHasher};
Alex Crichton760b93a2014-05-30 02:03:061667 use test::Bencher;
1668 use test;
1669
Alex Crichtonf47e4b22014-01-07 06:33:501670 use super::RingBuf;
Patrick Waltonfa5ee932012-12-28 02:24:181671
Brian Anderson6e27b272012-01-18 03:05:071672 #[test]
Victor Berger52ea83d2014-09-22 17:30:061673 #[allow(deprecated)]
Brian Anderson6e27b272012-01-18 03:05:071674 fn test_simple() {
blake2-ppc70523712013-07-10 13:27:141675 let mut d = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:451676 assert_eq!(d.len(), 0u);
Tobias Bucher7f64fe42015-01-25 21:05:031677 d.push_front(17);
1678 d.push_front(42);
Alexis Beingessnercf3b2e42014-11-06 17:24:471679 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:451680 assert_eq!(d.len(), 3u);
Alexis Beingessnercf3b2e42014-11-06 17:24:471681 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:451682 assert_eq!(d.len(), 4u);
blake2-ppc70523712013-07-10 13:27:141683 assert_eq!(*d.front().unwrap(), 42);
blake2-ppc70523712013-07-10 13:27:141684 assert_eq!(*d.back().unwrap(), 137);
1685 let mut i = d.pop_front();
blake2-ppc70523712013-07-10 13:27:141686 assert_eq!(i, Some(42));
Alexis Beingessnercf3b2e42014-11-06 17:24:471687 i = d.pop_back();
blake2-ppc70523712013-07-10 13:27:141688 assert_eq!(i, Some(137));
Alexis Beingessnercf3b2e42014-11-06 17:24:471689 i = d.pop_back();
blake2-ppc70523712013-07-10 13:27:141690 assert_eq!(i, Some(137));
Alexis Beingessnercf3b2e42014-11-06 17:24:471691 i = d.pop_back();
blake2-ppc70523712013-07-10 13:27:141692 assert_eq!(i, Some(17));
Corey Richardsoncc57ca02013-05-19 02:02:451693 assert_eq!(d.len(), 0u);
Alexis Beingessnercf3b2e42014-11-06 17:24:471694 d.push_back(3);
Corey Richardsoncc57ca02013-05-19 02:02:451695 assert_eq!(d.len(), 1u);
blake2-ppc70523712013-07-10 13:27:141696 d.push_front(2);
Corey Richardsoncc57ca02013-05-19 02:02:451697 assert_eq!(d.len(), 2u);
Alexis Beingessnercf3b2e42014-11-06 17:24:471698 d.push_back(4);
Corey Richardsoncc57ca02013-05-19 02:02:451699 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:141700 d.push_front(1);
Corey Richardsoncc57ca02013-05-19 02:02:451701 assert_eq!(d.len(), 4u);
Alex Crichton9d5d97b2014-10-15 06:05:011702 debug!("{}", d[0]);
1703 debug!("{}", d[1]);
1704 debug!("{}", d[2]);
1705 debug!("{}", d[3]);
1706 assert_eq!(d[0], 1);
1707 assert_eq!(d[1], 2);
1708 assert_eq!(d[2], 3);
1709 assert_eq!(d[3], 4);
Brian Anderson6e27b272012-01-18 03:05:071710 }
1711
Felix S. Klock IIa636f512013-05-01 23:32:371712 #[cfg(test)]
Alex Crichton3cb9fa22015-01-20 23:45:071713 fn test_parameterized<T:Clone + PartialEq + Debug>(a: T, b: T, c: T, d: T) {
Patrick Waltondc4bf172013-07-13 04:05:591714 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:451715 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:591716 deq.push_front(a.clone());
1717 deq.push_front(b.clone());
Alexis Beingessnercf3b2e42014-11-06 17:24:471718 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:451719 assert_eq!(deq.len(), 3);
Alexis Beingessnercf3b2e42014-11-06 17:24:471720 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:451721 assert_eq!(deq.len(), 4);
Marvin Löbel0ac7a212013-08-03 23:59:241722 assert_eq!((*deq.front().unwrap()).clone(), b.clone());
1723 assert_eq!((*deq.back().unwrap()).clone(), d.clone());
1724 assert_eq!(deq.pop_front().unwrap(), b.clone());
Alexis Beingessnercf3b2e42014-11-06 17:24:471725 assert_eq!(deq.pop_back().unwrap(), d.clone());
1726 assert_eq!(deq.pop_back().unwrap(), c.clone());
1727 assert_eq!(deq.pop_back().unwrap(), a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:451728 assert_eq!(deq.len(), 0);
Alexis Beingessnercf3b2e42014-11-06 17:24:471729 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:451730 assert_eq!(deq.len(), 1);
Patrick Waltondc4bf172013-07-13 04:05:591731 deq.push_front(b.clone());
Corey Richardsoncc57ca02013-05-19 02:02:451732 assert_eq!(deq.len(), 2);
Alexis Beingessnercf3b2e42014-11-06 17:24:471733 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:451734 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:591735 deq.push_front(a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:451736 assert_eq!(deq.len(), 4);
NODA, Kaif27ad3d2014-10-05 10:11:171737 assert_eq!(deq[0].clone(), a.clone());
1738 assert_eq!(deq[1].clone(), b.clone());
1739 assert_eq!(deq[2].clone(), c.clone());
1740 assert_eq!(deq[3].clone(), d.clone());
Brian Anderson6e27b272012-01-18 03:05:071741 }
1742
blake2-ppc81933ed2013-07-06 03:42:451743 #[test]
blake2-ppc70523712013-07-10 13:27:141744 fn test_push_front_grow() {
1745 let mut deq = RingBuf::new();
Jorge Aparicio7d661af2015-01-26 20:46:121746 for i in 0u..66 {
blake2-ppc70523712013-07-10 13:27:141747 deq.push_front(i);
blake2-ppc81933ed2013-07-06 03:42:451748 }
1749 assert_eq!(deq.len(), 66);
1750
Jorge Aparicio7d661af2015-01-26 20:46:121751 for i in 0u..66 {
NODA, Kaif27ad3d2014-10-05 10:11:171752 assert_eq!(deq[i], 65 - i);
blake2-ppc81933ed2013-07-06 03:42:451753 }
1754
blake2-ppc70523712013-07-10 13:27:141755 let mut deq = RingBuf::new();
Jorge Aparicio7d661af2015-01-26 20:46:121756 for i in 0u..66 {
Alexis Beingessnercf3b2e42014-11-06 17:24:471757 deq.push_back(i);
blake2-ppc81933ed2013-07-06 03:42:451758 }
1759
Jorge Aparicio7d661af2015-01-26 20:46:121760 for i in 0u..66 {
NODA, Kaif27ad3d2014-10-05 10:11:171761 assert_eq!(deq[i], i);
blake2-ppc81933ed2013-07-06 03:42:451762 }
1763 }
1764
P1startfd10d202014-08-02 06:39:391765 #[test]
1766 fn test_index() {
1767 let mut deq = RingBuf::new();
Jorge Aparicio7d661af2015-01-26 20:46:121768 for i in 1u..4 {
P1startfd10d202014-08-02 06:39:391769 deq.push_front(i);
1770 }
1771 assert_eq!(deq[1], 2);
1772 }
1773
1774 #[test]
1775 #[should_fail]
1776 fn test_index_out_of_bounds() {
1777 let mut deq = RingBuf::new();
Jorge Aparicio7d661af2015-01-26 20:46:121778 for i in 1u..4 {
P1startfd10d202014-08-02 06:39:391779 deq.push_front(i);
1780 }
1781 deq[3];
1782 }
1783
blake2-ppc81933ed2013-07-06 03:42:451784 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:351785 fn bench_new(b: &mut test::Bencher) {
Patrick Walton38efa172013-11-22 03:20:481786 b.iter(|| {
Colin Sherratt5e549d82014-11-11 02:57:521787 let ring: RingBuf<u64> = RingBuf::new();
1788 test::black_box(ring);
Patrick Walton38efa172013-11-22 03:20:481789 })
blake2-ppc81933ed2013-07-06 03:42:451790 }
1791
1792 #[bench]
Colin Sherratt7a666df2014-10-19 20:19:071793 fn bench_push_back_100(b: &mut test::Bencher) {
Colin Sherratt5e549d82014-11-11 02:57:521794 let mut deq = RingBuf::with_capacity(101);
Patrick Walton38efa172013-11-22 03:20:481795 b.iter(|| {
Tobias Bucher7f64fe42015-01-25 21:05:031796 for i in 0..100 {
Colin Sherratt7a666df2014-10-19 20:19:071797 deq.push_back(i);
1798 }
Colin Sherratt5e549d82014-11-11 02:57:521799 deq.head = 0;
1800 deq.tail = 0;
Patrick Walton38efa172013-11-22 03:20:481801 })
blake2-ppc81933ed2013-07-06 03:42:451802 }
1803
1804 #[bench]
Colin Sherratt7a666df2014-10-19 20:19:071805 fn bench_push_front_100(b: &mut test::Bencher) {
Colin Sherratt5e549d82014-11-11 02:57:521806 let mut deq = RingBuf::with_capacity(101);
Patrick Walton38efa172013-11-22 03:20:481807 b.iter(|| {
Tobias Bucher7f64fe42015-01-25 21:05:031808 for i in 0..100 {
Colin Sherratt7a666df2014-10-19 20:19:071809 deq.push_front(i);
1810 }
Colin Sherratt5e549d82014-11-11 02:57:521811 deq.head = 0;
1812 deq.tail = 0;
Patrick Walton38efa172013-11-22 03:20:481813 })
blake2-ppc81933ed2013-07-06 03:42:451814 }
1815
1816 #[bench]
Colin Sherratt5e549d82014-11-11 02:57:521817 fn bench_pop_back_100(b: &mut test::Bencher) {
Tobias Bucher7f64fe42015-01-25 21:05:031818 let mut deq: RingBuf<i32> = RingBuf::with_capacity(101);
Colin Sherratt7a666df2014-10-19 20:19:071819
Patrick Walton38efa172013-11-22 03:20:481820 b.iter(|| {
Colin Sherratt5e549d82014-11-11 02:57:521821 deq.head = 100;
1822 deq.tail = 0;
1823 while !deq.is_empty() {
1824 test::black_box(deq.pop_back());
Colin Sherratt7a666df2014-10-19 20:19:071825 }
Colin Sherratt7a666df2014-10-19 20:19:071826 })
1827 }
1828
1829 #[bench]
1830 fn bench_pop_front_100(b: &mut test::Bencher) {
Tobias Bucher7f64fe42015-01-25 21:05:031831 let mut deq: RingBuf<i32> = RingBuf::with_capacity(101);
Colin Sherratt7a666df2014-10-19 20:19:071832
1833 b.iter(|| {
Colin Sherratt5e549d82014-11-11 02:57:521834 deq.head = 100;
1835 deq.tail = 0;
1836 while !deq.is_empty() {
1837 test::black_box(deq.pop_front());
Colin Sherratt7a666df2014-10-19 20:19:071838 }
Colin Sherratt7a666df2014-10-19 20:19:071839 })
1840 }
1841
1842 #[bench]
1843 fn bench_grow_1025(b: &mut test::Bencher) {
1844 b.iter(|| {
1845 let mut deq = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:031846 for i in 0..1025 {
Colin Sherratt7a666df2014-10-19 20:19:071847 deq.push_front(i);
Brendan Zabarauskas729060d2014-01-30 00:20:341848 }
Colin Sherratt5e549d82014-11-11 02:57:521849 test::black_box(deq);
Patrick Walton38efa172013-11-22 03:20:481850 })
blake2-ppc81933ed2013-07-06 03:42:451851 }
1852
Colin Sherratt7a666df2014-10-19 20:19:071853 #[bench]
1854 fn bench_iter_1000(b: &mut test::Bencher) {
Tobias Bucher7f64fe42015-01-25 21:05:031855 let ring: RingBuf<i32> = (0..1000).collect();
Colin Sherratt7a666df2014-10-19 20:19:071856
1857 b.iter(|| {
1858 let mut sum = 0;
Jorge Apariciod5d7e652015-01-31 17:20:461859 for &i in &ring {
Colin Sherratt7a666df2014-10-19 20:19:071860 sum += i;
1861 }
Colin Sherratt5e549d82014-11-11 02:57:521862 test::black_box(sum);
Colin Sherratt7a666df2014-10-19 20:19:071863 })
1864 }
1865
1866 #[bench]
1867 fn bench_mut_iter_1000(b: &mut test::Bencher) {
Tobias Bucher7f64fe42015-01-25 21:05:031868 let mut ring: RingBuf<i32> = (0..1000).collect();
Colin Sherratt7a666df2014-10-19 20:19:071869
1870 b.iter(|| {
Colin Sherratt5e549d82014-11-11 02:57:521871 let mut sum = 0;
Colin Sherratt7a666df2014-10-19 20:19:071872 for i in ring.iter_mut() {
Colin Sherratt5e549d82014-11-11 02:57:521873 sum += *i;
Colin Sherratt7a666df2014-10-19 20:19:071874 }
Colin Sherratt5e549d82014-11-11 02:57:521875 test::black_box(sum);
Colin Sherratt7a666df2014-10-19 20:19:071876 })
1877 }
1878
Jorge Aparicio788181d2015-01-28 13:34:181879 #[derive(Clone, PartialEq, Debug)]
Patrick Walton99b33f72013-07-02 19:47:321880 enum Taggy {
Tobias Bucher7f64fe42015-01-25 21:05:031881 One(i32),
1882 Two(i32, i32),
1883 Three(i32, i32, i32),
Brian Anderson6e27b272012-01-18 03:05:071884 }
1885
Jorge Aparicio788181d2015-01-28 13:34:181886 #[derive(Clone, PartialEq, Debug)]
Patrick Walton99b33f72013-07-02 19:47:321887 enum Taggypar<T> {
Tobias Bucher7f64fe42015-01-25 21:05:031888 Onepar(i32),
1889 Twopar(i32, i32),
1890 Threepar(i32, i32, i32),
Patrick Walton99b33f72013-07-02 19:47:321891 }
1892
Jorge Aparicio788181d2015-01-28 13:34:181893 #[derive(Clone, PartialEq, Debug)]
Erick Tryzelaare84576b2013-01-22 16:44:241894 struct RecCy {
Tobias Bucher7f64fe42015-01-25 21:05:031895 x: i32,
1896 y: i32,
Patrick Waltoneb4d39e2013-01-26 00:57:391897 t: Taggy
Patrick Walton9117dcb2012-09-20 01:00:261898 }
Kevin Cantuc43426e2012-09-13 05:09:551899
1900 #[test]
1901 fn test_param_int() {
Tobias Bucher7f64fe42015-01-25 21:05:031902 test_parameterized::<i32>(5, 72, 64, 175);
Kevin Cantuc43426e2012-09-13 05:09:551903 }
1904
1905 #[test]
Kevin Cantuc43426e2012-09-13 05:09:551906 fn test_param_taggy() {
Corey Richardsonf8ae9b02013-06-26 22:14:351907 test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:551908 }
1909
1910 #[test]
1911 fn test_param_taggypar() {
Tobias Bucher7f64fe42015-01-25 21:05:031912 test_parameterized::<Taggypar<i32>>(Onepar::<i32>(1),
1913 Twopar::<i32>(1, 2),
1914 Threepar::<i32>(1, 2, 3),
1915 Twopar::<i32>(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:551916 }
Brian Anderson6e27b272012-01-18 03:05:071917
Kevin Cantuc43426e2012-09-13 05:09:551918 #[test]
1919 fn test_param_reccy() {
Erick Tryzelaare84576b2013-01-22 16:44:241920 let reccy1 = RecCy { x: 1, y: 2, t: One(1) };
1921 let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) };
1922 let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) };
1923 let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
Kevin Cantuc43426e2012-09-13 05:09:551924 test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
Brian Anderson6e27b272012-01-18 03:05:071925 }
Erick Tryzelaar909d8f02013-03-30 01:02:441926
1927 #[test]
blake2-ppc0ff5c172013-07-06 03:42:451928 fn test_with_capacity() {
blake2-ppc70523712013-07-10 13:27:141929 let mut d = RingBuf::with_capacity(0);
Tobias Bucher7f64fe42015-01-25 21:05:031930 d.push_back(1);
blake2-ppc0ff5c172013-07-06 03:42:451931 assert_eq!(d.len(), 1);
blake2-ppc70523712013-07-10 13:27:141932 let mut d = RingBuf::with_capacity(50);
Tobias Bucher7f64fe42015-01-25 21:05:031933 d.push_back(1);
blake2-ppc0ff5c172013-07-06 03:42:451934 assert_eq!(d.len(), 1);
1935 }
1936
1937 #[test]
Kevin Butler64896d62014-08-07 01:11:131938 fn test_with_capacity_non_power_two() {
1939 let mut d3 = RingBuf::with_capacity(3);
Tobias Bucher7f64fe42015-01-25 21:05:031940 d3.push_back(1);
Kevin Butler64896d62014-08-07 01:11:131941
1942 // X = None, | = lo
1943 // [|1, X, X]
1944 assert_eq!(d3.pop_front(), Some(1));
1945 // [X, |X, X]
1946 assert_eq!(d3.front(), None);
1947
1948 // [X, |3, X]
Alexis Beingessnercf3b2e42014-11-06 17:24:471949 d3.push_back(3);
Kevin Butler64896d62014-08-07 01:11:131950 // [X, |3, 6]
Alexis Beingessnercf3b2e42014-11-06 17:24:471951 d3.push_back(6);
Kevin Butler64896d62014-08-07 01:11:131952 // [X, X, |6]
1953 assert_eq!(d3.pop_front(), Some(3));
1954
1955 // Pushing the lo past half way point to trigger
1956 // the 'B' scenario for growth
1957 // [9, X, |6]
Alexis Beingessnercf3b2e42014-11-06 17:24:471958 d3.push_back(9);
Kevin Butler64896d62014-08-07 01:11:131959 // [9, 12, |6]
Alexis Beingessnercf3b2e42014-11-06 17:24:471960 d3.push_back(12);
Kevin Butler64896d62014-08-07 01:11:131961
Alexis Beingessnercf3b2e42014-11-06 17:24:471962 d3.push_back(15);
Kevin Butler64896d62014-08-07 01:11:131963 // There used to be a bug here about how the
1964 // RingBuf made growth assumptions about the
1965 // underlying Vec which didn't hold and lead
1966 // to corruption.
1967 // (Vec grows to next power of two)
1968 //good- [9, 12, 15, X, X, X, X, |6]
1969 //bug- [15, 12, X, X, X, |6, X, X]
1970 assert_eq!(d3.pop_front(), Some(6));
1971
1972 // Which leads us to the following state which
1973 // would be a failure case.
1974 //bug- [15, 12, X, X, X, X, |X, X]
1975 assert_eq!(d3.front(), Some(&9));
1976 }
1977
1978 #[test]
David Manescu65f35782014-01-31 13:03:201979 fn test_reserve_exact() {
blake2-ppc70523712013-07-10 13:27:141980 let mut d = RingBuf::new();
Alexis Beingessnercf3b2e42014-11-06 17:24:471981 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:201982 d.reserve_exact(50);
Alexis Beingessnercf3b2e42014-11-06 17:24:471983 assert!(d.capacity() >= 51);
blake2-ppc70523712013-07-10 13:27:141984 let mut d = RingBuf::new();
Alexis Beingessnercf3b2e42014-11-06 17:24:471985 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:201986 d.reserve_exact(50);
Alexis Beingessnercf3b2e42014-11-06 17:24:471987 assert!(d.capacity() >= 51);
Tim Chevalier77de84b2013-05-27 18:47:381988 }
1989
1990 #[test]
David Manescu65f35782014-01-31 13:03:201991 fn test_reserve() {
blake2-ppc70523712013-07-10 13:27:141992 let mut d = RingBuf::new();
Alexis Beingessnercf3b2e42014-11-06 17:24:471993 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:201994 d.reserve(50);
Colin Sherratt7a666df2014-10-19 20:19:071995 assert!(d.capacity() >= 51);
blake2-ppc70523712013-07-10 13:27:141996 let mut d = RingBuf::new();
Alexis Beingessnercf3b2e42014-11-06 17:24:471997 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:201998 d.reserve(50);
Colin Sherratt7a666df2014-10-19 20:19:071999 assert!(d.capacity() >= 51);
Tim Chevalier77de84b2013-05-27 18:47:382000 }
2001
Jed Estep096fb792013-06-26 14:04:442002 #[test]
blake2-ppc57757a82013-09-26 07:19:262003 fn test_swap() {
Tobias Bucher7f64fe42015-01-25 21:05:032004 let mut d: RingBuf<i32> = (0..5).collect();
blake2-ppc57757a82013-09-26 07:19:262005 d.pop_front();
2006 d.swap(0, 3);
Tobias Bucher7f64fe42015-01-25 21:05:032007 assert_eq!(d.iter().map(|&x|x).collect::<Vec<i32>>(), vec!(4, 2, 3, 1));
blake2-ppc57757a82013-09-26 07:19:262008 }
2009
2010 #[test]
Jed Estep096fb792013-06-26 14:04:442011 fn test_iter() {
blake2-ppc70523712013-07-10 13:27:142012 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:452013 assert_eq!(d.iter().next(), None);
blake2-ppc9ccf4432013-07-14 20:30:222014 assert_eq!(d.iter().size_hint(), (0, Some(0)));
blake2-ppcf88d5322013-07-06 03:42:452015
Tobias Bucher7f64fe42015-01-25 21:05:032016 for i in 0..5 {
Alexis Beingessnercf3b2e42014-11-06 17:24:472017 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:442018 }
Nick Cameron37a94b82014-08-04 12:19:022019 {
2020 let b: &[_] = &[&0,&1,&2,&3,&4];
Tobias Bucher7f64fe42015-01-25 21:05:032021 assert_eq!(d.iter().collect::<Vec<&i32>>(), b);
Nick Cameron37a94b82014-08-04 12:19:022022 }
Corey Richardsonf8ae9b02013-06-26 22:14:352023
Tobias Bucher7f64fe42015-01-25 21:05:032024 for i in 6..9 {
blake2-ppc70523712013-07-10 13:27:142025 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:442026 }
Nick Cameron37a94b82014-08-04 12:19:022027 {
2028 let b: &[_] = &[&8,&7,&6,&0,&1,&2,&3,&4];
Tobias Bucher7f64fe42015-01-25 21:05:032029 assert_eq!(d.iter().collect::<Vec<&i32>>(), b);
Nick Cameron37a94b82014-08-04 12:19:022030 }
blake2-ppc9ccf4432013-07-14 20:30:222031
2032 let mut it = d.iter();
2033 let mut len = d.len();
2034 loop {
2035 match it.next() {
2036 None => break,
2037 _ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) }
2038 }
2039 }
Jed Estep096fb792013-06-26 14:04:442040 }
2041
2042 #[test]
2043 fn test_rev_iter() {
blake2-ppc70523712013-07-10 13:27:142044 let mut d = RingBuf::new();
Jonathan S03609e52014-04-21 04:59:122045 assert_eq!(d.iter().rev().next(), None);
blake2-ppcf88d5322013-07-06 03:42:452046
Tobias Bucher7f64fe42015-01-25 21:05:032047 for i in 0..5 {
Alexis Beingessnercf3b2e42014-11-06 17:24:472048 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:442049 }
Nick Cameron37a94b82014-08-04 12:19:022050 {
2051 let b: &[_] = &[&4,&3,&2,&1,&0];
Tobias Bucher7f64fe42015-01-25 21:05:032052 assert_eq!(d.iter().rev().collect::<Vec<&i32>>(), b);
Nick Cameron37a94b82014-08-04 12:19:022053 }
Corey Richardsonf8ae9b02013-06-26 22:14:352054
Tobias Bucher7f64fe42015-01-25 21:05:032055 for i in 6..9 {
blake2-ppc70523712013-07-10 13:27:142056 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:442057 }
Nick Cameron37a94b82014-08-04 12:19:022058 let b: &[_] = &[&4,&3,&2,&1,&0,&6,&7,&8];
Tobias Bucher7f64fe42015-01-25 21:05:032059 assert_eq!(d.iter().rev().collect::<Vec<&i32>>(), b);
Jed Estep096fb792013-06-26 14:04:442060 }
blake2-ppc08dc72f2013-07-06 03:42:452061
2062 #[test]
Niko Matsakisbc4164d2013-11-16 22:29:392063 fn test_mut_rev_iter_wrap() {
2064 let mut d = RingBuf::with_capacity(3);
Aaron Turonfc525ee2014-09-15 03:27:362065 assert!(d.iter_mut().rev().next().is_none());
Niko Matsakisbc4164d2013-11-16 22:29:392066
Tobias Bucher7f64fe42015-01-25 21:05:032067 d.push_back(1);
Alexis Beingessnercf3b2e42014-11-06 17:24:472068 d.push_back(2);
2069 d.push_back(3);
Niko Matsakisbc4164d2013-11-16 22:29:392070 assert_eq!(d.pop_front(), Some(1));
Alexis Beingessnercf3b2e42014-11-06 17:24:472071 d.push_back(4);
Niko Matsakisbc4164d2013-11-16 22:29:392072
Tobias Bucher7f64fe42015-01-25 21:05:032073 assert_eq!(d.iter_mut().rev().map(|x| *x).collect::<Vec<i32>>(),
Huon Wilson4b9a7a22014-04-05 05:45:422074 vec!(4, 3, 2));
Niko Matsakisbc4164d2013-11-16 22:29:392075 }
2076
2077 #[test]
blake2-ppcf88d5322013-07-06 03:42:452078 fn test_mut_iter() {
blake2-ppc70523712013-07-10 13:27:142079 let mut d = RingBuf::new();
Aaron Turonfc525ee2014-09-15 03:27:362080 assert!(d.iter_mut().next().is_none());
blake2-ppcf88d5322013-07-06 03:42:452081
Jorge Aparicio7d661af2015-01-26 20:46:122082 for i in 0u..3 {
blake2-ppc70523712013-07-10 13:27:142083 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:452084 }
2085
Aaron Turonfc525ee2014-09-15 03:27:362086 for (i, elt) in d.iter_mut().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:452087 assert_eq!(*elt, 2 - i);
2088 *elt = i;
2089 }
2090
2091 {
Aaron Turonfc525ee2014-09-15 03:27:362092 let mut it = d.iter_mut();
blake2-ppcf88d5322013-07-06 03:42:452093 assert_eq!(*it.next().unwrap(), 0);
2094 assert_eq!(*it.next().unwrap(), 1);
2095 assert_eq!(*it.next().unwrap(), 2);
2096 assert!(it.next().is_none());
2097 }
2098 }
2099
2100 #[test]
2101 fn test_mut_rev_iter() {
blake2-ppc70523712013-07-10 13:27:142102 let mut d = RingBuf::new();
Aaron Turonfc525ee2014-09-15 03:27:362103 assert!(d.iter_mut().rev().next().is_none());
blake2-ppcf88d5322013-07-06 03:42:452104
Jorge Aparicio7d661af2015-01-26 20:46:122105 for i in 0u..3 {
blake2-ppc70523712013-07-10 13:27:142106 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:452107 }
2108
Aaron Turonfc525ee2014-09-15 03:27:362109 for (i, elt) in d.iter_mut().rev().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:452110 assert_eq!(*elt, i);
2111 *elt = i;
2112 }
2113
2114 {
Aaron Turonfc525ee2014-09-15 03:27:362115 let mut it = d.iter_mut().rev();
blake2-ppcf88d5322013-07-06 03:42:452116 assert_eq!(*it.next().unwrap(), 0);
2117 assert_eq!(*it.next().unwrap(), 1);
2118 assert_eq!(*it.next().unwrap(), 2);
2119 assert!(it.next().is_none());
2120 }
2121 }
2122
2123 #[test]
Alexis Beingessner865c2db2014-11-23 02:34:112124 fn test_into_iter() {
2125
2126 // Empty iter
2127 {
Tobias Bucher7f64fe42015-01-25 21:05:032128 let d: RingBuf<i32> = RingBuf::new();
Alexis Beingessner865c2db2014-11-23 02:34:112129 let mut iter = d.into_iter();
2130
2131 assert_eq!(iter.size_hint(), (0, Some(0)));
2132 assert_eq!(iter.next(), None);
2133 assert_eq!(iter.size_hint(), (0, Some(0)));
2134 }
2135
2136 // simple iter
2137 {
2138 let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032139 for i in 0..5 {
Alexis Beingessner865c2db2014-11-23 02:34:112140 d.push_back(i);
2141 }
2142
2143 let b = vec![0,1,2,3,4];
Tobias Bucher7f64fe42015-01-25 21:05:032144 assert_eq!(d.into_iter().collect::<Vec<i32>>(), b);
Alexis Beingessner865c2db2014-11-23 02:34:112145 }
2146
2147 // wrapped iter
2148 {
2149 let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032150 for i in 0..5 {
Alexis Beingessner865c2db2014-11-23 02:34:112151 d.push_back(i);
2152 }
Tobias Bucher7f64fe42015-01-25 21:05:032153 for i in 6..9 {
Alexis Beingessner865c2db2014-11-23 02:34:112154 d.push_front(i);
2155 }
2156
2157 let b = vec![8,7,6,0,1,2,3,4];
Tobias Bucher7f64fe42015-01-25 21:05:032158 assert_eq!(d.into_iter().collect::<Vec<i32>>(), b);
Alexis Beingessner865c2db2014-11-23 02:34:112159 }
2160
2161 // partially used
2162 {
2163 let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032164 for i in 0..5 {
Alexis Beingessner865c2db2014-11-23 02:34:112165 d.push_back(i);
2166 }
Tobias Bucher7f64fe42015-01-25 21:05:032167 for i in 6..9 {
Alexis Beingessner865c2db2014-11-23 02:34:112168 d.push_front(i);
2169 }
2170
2171 let mut it = d.into_iter();
2172 assert_eq!(it.size_hint(), (8, Some(8)));
2173 assert_eq!(it.next(), Some(8));
2174 assert_eq!(it.size_hint(), (7, Some(7)));
2175 assert_eq!(it.next_back(), Some(4));
2176 assert_eq!(it.size_hint(), (6, Some(6)));
2177 assert_eq!(it.next(), Some(7));
2178 assert_eq!(it.size_hint(), (5, Some(5)));
2179 }
2180 }
2181
2182 #[test]
Clark Gaebeld57f2592014-12-16 22:45:032183 fn test_drain() {
2184
2185 // Empty iter
2186 {
Tobias Bucher7f64fe42015-01-25 21:05:032187 let mut d: RingBuf<i32> = RingBuf::new();
Clark Gaebeld57f2592014-12-16 22:45:032188
2189 {
2190 let mut iter = d.drain();
2191
2192 assert_eq!(iter.size_hint(), (0, Some(0)));
2193 assert_eq!(iter.next(), None);
2194 assert_eq!(iter.size_hint(), (0, Some(0)));
2195 }
2196
2197 assert!(d.is_empty());
2198 }
2199
2200 // simple iter
2201 {
2202 let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032203 for i in 0..5 {
Clark Gaebeld57f2592014-12-16 22:45:032204 d.push_back(i);
2205 }
2206
Alex Crichton3a2530d2015-01-30 20:26:442207 assert_eq!(d.drain().collect::<Vec<_>>(), [0, 1, 2, 3, 4]);
Clark Gaebeld57f2592014-12-16 22:45:032208 assert!(d.is_empty());
2209 }
2210
2211 // wrapped iter
2212 {
2213 let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032214 for i in 0..5 {
Clark Gaebeld57f2592014-12-16 22:45:032215 d.push_back(i);
2216 }
Alex Crichton3a2530d2015-01-30 20:26:442217 for i in 6..9 {
Clark Gaebeld57f2592014-12-16 22:45:032218 d.push_front(i);
2219 }
2220
Alex Crichton3a2530d2015-01-30 20:26:442221 assert_eq!(d.drain().collect::<Vec<_>>(), [8,7,6,0,1,2,3,4]);
Clark Gaebeld57f2592014-12-16 22:45:032222 assert!(d.is_empty());
2223 }
2224
2225 // partially used
2226 {
Alex Crichton3a2530d2015-01-30 20:26:442227 let mut d: RingBuf<i32> = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032228 for i in 0..5 {
Clark Gaebeld57f2592014-12-16 22:45:032229 d.push_back(i);
2230 }
Alex Crichton3a2530d2015-01-30 20:26:442231 for i in 6..9 {
Clark Gaebeld57f2592014-12-16 22:45:032232 d.push_front(i);
2233 }
2234
2235 {
2236 let mut it = d.drain();
2237 assert_eq!(it.size_hint(), (8, Some(8)));
2238 assert_eq!(it.next(), Some(8));
2239 assert_eq!(it.size_hint(), (7, Some(7)));
2240 assert_eq!(it.next_back(), Some(4));
2241 assert_eq!(it.size_hint(), (6, Some(6)));
2242 assert_eq!(it.next(), Some(7));
2243 assert_eq!(it.size_hint(), (5, Some(5)));
2244 }
2245 assert!(d.is_empty());
2246 }
2247 }
2248
2249 #[test]
Brian Andersonee052192014-03-31 04:45:552250 fn test_from_iter() {
Eduard Burtescub45d30d2014-12-19 12:02:222251 use core::iter;
Tobias Bucher7f64fe42015-01-25 21:05:032252 let v = vec!(1,2,3,4,5,6,7);
2253 let deq: RingBuf<i32> = v.iter().map(|&x| x).collect();
2254 let u: Vec<i32> = deq.iter().map(|&x| x).collect();
blake2-ppc08dc72f2013-07-06 03:42:452255 assert_eq!(u, v);
2256
Aaron Turonb299c2b2014-11-06 17:32:372257 let seq = iter::count(0u, 2).take(256);
blake2-ppc70523712013-07-10 13:27:142258 let deq: RingBuf<uint> = seq.collect();
Daniel Micay100894552013-08-03 16:45:232259 for (i, &x) in deq.iter().enumerate() {
blake2-ppc08dc72f2013-07-06 03:42:452260 assert_eq!(2*i, x);
2261 }
2262 assert_eq!(deq.len(), 256);
2263 }
blake2-ppc10c76982013-07-06 13:27:322264
2265 #[test]
2266 fn test_clone() {
blake2-ppc70523712013-07-10 13:27:142267 let mut d = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032268 d.push_front(17);
blake2-ppc70523712013-07-10 13:27:142269 d.push_front(42);
Alexis Beingessnercf3b2e42014-11-06 17:24:472270 d.push_back(137);
2271 d.push_back(137);
blake2-ppc10c76982013-07-06 13:27:322272 assert_eq!(d.len(), 4u);
2273 let mut e = d.clone();
2274 assert_eq!(e.len(), 4u);
2275 while !d.is_empty() {
Alexis Beingessnercf3b2e42014-11-06 17:24:472276 assert_eq!(d.pop_back(), e.pop_back());
blake2-ppc10c76982013-07-06 13:27:322277 }
2278 assert_eq!(d.len(), 0u);
2279 assert_eq!(e.len(), 0u);
2280 }
2281
2282 #[test]
2283 fn test_eq() {
blake2-ppc70523712013-07-10 13:27:142284 let mut d = RingBuf::new();
Alex Crichton02882fb2014-02-28 09:23:062285 assert!(d == RingBuf::with_capacity(0));
Tobias Bucher7f64fe42015-01-25 21:05:032286 d.push_front(137);
blake2-ppc70523712013-07-10 13:27:142287 d.push_front(17);
2288 d.push_front(42);
Alexis Beingessnercf3b2e42014-11-06 17:24:472289 d.push_back(137);
blake2-ppc70523712013-07-10 13:27:142290 let mut e = RingBuf::with_capacity(0);
Alexis Beingessnercf3b2e42014-11-06 17:24:472291 e.push_back(42);
2292 e.push_back(17);
2293 e.push_back(137);
2294 e.push_back(137);
Alex Crichton02882fb2014-02-28 09:23:062295 assert!(&e == &d);
Alexis Beingessnercf3b2e42014-11-06 17:24:472296 e.pop_back();
2297 e.push_back(0);
blake2-ppc10c76982013-07-06 13:27:322298 assert!(e != d);
2299 e.clear();
Alex Crichton02882fb2014-02-28 09:23:062300 assert!(e == RingBuf::new());
blake2-ppc10c76982013-07-06 13:27:322301 }
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:042302
2303 #[test]
nham1cfa6562014-07-27 02:33:472304 fn test_hash() {
2305 let mut x = RingBuf::new();
2306 let mut y = RingBuf::new();
2307
Tobias Bucher7f64fe42015-01-25 21:05:032308 x.push_back(1);
Alexis Beingessnercf3b2e42014-11-06 17:24:472309 x.push_back(2);
2310 x.push_back(3);
nham1cfa6562014-07-27 02:33:472311
Tobias Bucher7f64fe42015-01-25 21:05:032312 y.push_back(0);
2313 y.push_back(1);
nham1cfa6562014-07-27 02:33:472314 y.pop_front();
Alexis Beingessnercf3b2e42014-11-06 17:24:472315 y.push_back(2);
2316 y.push_back(3);
nham1cfa6562014-07-27 02:33:472317
Alex Crichton511f0b82014-12-09 20:37:232318 assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
nham1cfa6562014-07-27 02:33:472319 }
2320
2321 #[test]
nham63615772014-07-27 03:18:562322 fn test_ord() {
2323 let x = RingBuf::new();
2324 let mut y = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032325 y.push_back(1);
Alexis Beingessnercf3b2e42014-11-06 17:24:472326 y.push_back(2);
2327 y.push_back(3);
nham63615772014-07-27 03:18:562328 assert!(x < y);
2329 assert!(y > x);
2330 assert!(x <= x);
2331 assert!(x >= x);
2332 }
2333
2334 #[test]
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:042335 fn test_show() {
Tobias Bucher7f64fe42015-01-25 21:05:032336 let ringbuf: RingBuf<i32> = (0..10).collect();
Alex Crichton3cb9fa22015-01-20 23:45:072337 assert_eq!(format!("{:?}", ringbuf), "RingBuf [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:042338
2339 let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
2340 .map(|&s| s)
2341 .collect();
Alex Crichtona6400082015-01-07 00:16:352342 assert_eq!(format!("{:?}", ringbuf), "RingBuf [\"just\", \"one\", \"test\", \"more\"]");
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:042343 }
Colin Sherratt7a666df2014-10-19 20:19:072344
2345 #[test]
2346 fn test_drop() {
2347 static mut drops: uint = 0;
2348 struct Elem;
2349 impl Drop for Elem {
2350 fn drop(&mut self) {
2351 unsafe { drops += 1; }
2352 }
2353 }
2354
2355 let mut ring = RingBuf::new();
2356 ring.push_back(Elem);
2357 ring.push_front(Elem);
2358 ring.push_back(Elem);
2359 ring.push_front(Elem);
2360 drop(ring);
2361
2362 assert_eq!(unsafe {drops}, 4);
2363 }
2364
2365 #[test]
2366 fn test_drop_with_pop() {
2367 static mut drops: uint = 0;
2368 struct Elem;
2369 impl Drop for Elem {
2370 fn drop(&mut self) {
2371 unsafe { drops += 1; }
2372 }
2373 }
2374
2375 let mut ring = RingBuf::new();
2376 ring.push_back(Elem);
2377 ring.push_front(Elem);
2378 ring.push_back(Elem);
2379 ring.push_front(Elem);
2380
2381 drop(ring.pop_back());
2382 drop(ring.pop_front());
2383 assert_eq!(unsafe {drops}, 2);
2384
2385 drop(ring);
2386 assert_eq!(unsafe {drops}, 4);
2387 }
2388
2389 #[test]
2390 fn test_drop_clear() {
2391 static mut drops: uint = 0;
2392 struct Elem;
2393 impl Drop for Elem {
2394 fn drop(&mut self) {
2395 unsafe { drops += 1; }
2396 }
2397 }
2398
2399 let mut ring = RingBuf::new();
2400 ring.push_back(Elem);
2401 ring.push_front(Elem);
2402 ring.push_back(Elem);
2403 ring.push_front(Elem);
2404 ring.clear();
2405 assert_eq!(unsafe {drops}, 4);
2406
2407 drop(ring);
2408 assert_eq!(unsafe {drops}, 4);
2409 }
2410
2411 #[test]
2412 fn test_reserve_grow() {
2413 // test growth path A
2414 // [T o o H] -> [T o o H . . . . ]
2415 let mut ring = RingBuf::with_capacity(4);
Tobias Bucher7f64fe42015-01-25 21:05:032416 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072417 ring.push_back(i);
2418 }
2419 ring.reserve(7);
Tobias Bucher7f64fe42015-01-25 21:05:032420 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072421 assert_eq!(ring.pop_front(), Some(i));
2422 }
2423
2424 // test growth path B
2425 // [H T o o] -> [. T o o H . . . ]
2426 let mut ring = RingBuf::with_capacity(4);
Tobias Bucher7f64fe42015-01-25 21:05:032427 for i in 0..1 {
Colin Sherratt7a666df2014-10-19 20:19:072428 ring.push_back(i);
2429 assert_eq!(ring.pop_front(), Some(i));
2430 }
Tobias Bucher7f64fe42015-01-25 21:05:032431 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072432 ring.push_back(i);
2433 }
2434 ring.reserve(7);
Tobias Bucher7f64fe42015-01-25 21:05:032435 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072436 assert_eq!(ring.pop_front(), Some(i));
2437 }
2438
2439 // test growth path C
2440 // [o o H T] -> [o o H . . . . T ]
2441 let mut ring = RingBuf::with_capacity(4);
Tobias Bucher7f64fe42015-01-25 21:05:032442 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072443 ring.push_back(i);
2444 assert_eq!(ring.pop_front(), Some(i));
2445 }
Tobias Bucher7f64fe42015-01-25 21:05:032446 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072447 ring.push_back(i);
2448 }
2449 ring.reserve(7);
Tobias Bucher7f64fe42015-01-25 21:05:032450 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072451 assert_eq!(ring.pop_front(), Some(i));
2452 }
2453 }
2454
2455 #[test]
2456 fn test_get() {
2457 let mut ring = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032458 ring.push_back(0);
Colin Sherratt7a666df2014-10-19 20:19:072459 assert_eq!(ring.get(0), Some(&0));
2460 assert_eq!(ring.get(1), None);
2461
2462 ring.push_back(1);
2463 assert_eq!(ring.get(0), Some(&0));
2464 assert_eq!(ring.get(1), Some(&1));
2465 assert_eq!(ring.get(2), None);
2466
2467 ring.push_back(2);
2468 assert_eq!(ring.get(0), Some(&0));
2469 assert_eq!(ring.get(1), Some(&1));
2470 assert_eq!(ring.get(2), Some(&2));
2471 assert_eq!(ring.get(3), None);
2472
2473 assert_eq!(ring.pop_front(), Some(0));
2474 assert_eq!(ring.get(0), Some(&1));
2475 assert_eq!(ring.get(1), Some(&2));
2476 assert_eq!(ring.get(2), None);
2477
2478 assert_eq!(ring.pop_front(), Some(1));
2479 assert_eq!(ring.get(0), Some(&2));
2480 assert_eq!(ring.get(1), None);
2481
2482 assert_eq!(ring.pop_front(), Some(2));
2483 assert_eq!(ring.get(0), None);
2484 assert_eq!(ring.get(1), None);
2485 }
2486
2487 #[test]
2488 fn test_get_mut() {
2489 let mut ring = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032490 for i in 0..3 {
Colin Sherratt7a666df2014-10-19 20:19:072491 ring.push_back(i);
2492 }
2493
2494 match ring.get_mut(1) {
2495 Some(x) => *x = -1,
2496 None => ()
2497 };
2498
2499 assert_eq!(ring.get_mut(0), Some(&mut 0));
2500 assert_eq!(ring.get_mut(1), Some(&mut -1));
2501 assert_eq!(ring.get_mut(2), Some(&mut 2));
2502 assert_eq!(ring.get_mut(3), None);
2503
2504 assert_eq!(ring.pop_front(), Some(0));
2505 assert_eq!(ring.get_mut(0), Some(&mut -1));
2506 assert_eq!(ring.get_mut(1), Some(&mut 2));
2507 assert_eq!(ring.get_mut(2), None);
2508 }
Matt Murphy40f28c72014-12-03 17:12:302509
2510 #[test]
Piotr Czarnecki156a1c32015-01-05 14:48:582511 fn test_swap_front_back_remove() {
2512 fn test(back: bool) {
2513 // This test checks that every single combination of tail position and length is tested.
2514 // Capacity 15 should be large enough to cover every case.
2515 let mut tester = RingBuf::with_capacity(15);
2516 let usable_cap = tester.capacity();
2517 let final_len = usable_cap / 2;
2518
Jorge Aparicio7d661af2015-01-26 20:46:122519 for len in 0..final_len {
Piotr Czarnecki156a1c32015-01-05 14:48:582520 let expected = if back {
Jorge Aparicioc300d682015-01-26 20:44:222521 (0..len).collect()
Piotr Czarnecki156a1c32015-01-05 14:48:582522 } else {
Jorge Aparicioc300d682015-01-26 20:44:222523 (0..len).rev().collect()
Piotr Czarnecki156a1c32015-01-05 14:48:582524 };
Jorge Aparicio7d661af2015-01-26 20:46:122525 for tail_pos in 0..usable_cap {
Piotr Czarnecki156a1c32015-01-05 14:48:582526 tester.tail = tail_pos;
2527 tester.head = tail_pos;
2528 if back {
Jorge Aparicio7d661af2015-01-26 20:46:122529 for i in 0..len * 2 {
Piotr Czarnecki156a1c32015-01-05 14:48:582530 tester.push_front(i);
2531 }
Jorge Aparicio7d661af2015-01-26 20:46:122532 for i in 0..len {
Piotr Czarnecki156a1c32015-01-05 14:48:582533 assert_eq!(tester.swap_back_remove(i), Some(len * 2 - 1 - i));
2534 }
2535 } else {
Jorge Aparicio7d661af2015-01-26 20:46:122536 for i in 0..len * 2 {
Piotr Czarnecki156a1c32015-01-05 14:48:582537 tester.push_back(i);
2538 }
Jorge Aparicio7d661af2015-01-26 20:46:122539 for i in 0..len {
Piotr Czarnecki156a1c32015-01-05 14:48:582540 let idx = tester.len() - 1 - i;
2541 assert_eq!(tester.swap_front_remove(idx), Some(len * 2 - 1 - i));
2542 }
2543 }
2544 assert!(tester.tail < tester.cap);
2545 assert!(tester.head < tester.cap);
2546 assert_eq!(tester, expected);
2547 }
2548 }
2549 }
2550 test(true);
2551 test(false);
2552 }
2553
2554 #[test]
Matt Murphy40f28c72014-12-03 17:12:302555 fn test_insert() {
2556 // This test checks that every single combination of tail position, length, and
Piotr Czarnecki59d41532014-12-16 23:37:552557 // insertion position is tested. Capacity 15 should be large enough to cover every case.
Matt Murphy40f28c72014-12-03 17:12:302558
Piotr Czarnecki59d41532014-12-16 23:37:552559 let mut tester = RingBuf::with_capacity(15);
2560 // can't guarantee we got 15, so have to get what we got.
2561 // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else
Matt Murphy40f28c72014-12-03 17:12:302562 // this test isn't covering what it wants to
2563 let cap = tester.capacity();
2564
2565
2566 // len is the length *after* insertion
Jorge Aparicio7d661af2015-01-26 20:46:122567 for len in 1..cap {
Matt Murphy40f28c72014-12-03 17:12:302568 // 0, 1, 2, .., len - 1
2569 let expected = iter::count(0, 1).take(len).collect();
Jorge Aparicio7d661af2015-01-26 20:46:122570 for tail_pos in 0..cap {
2571 for to_insert in 0..len {
Matt Murphy40f28c72014-12-03 17:12:302572 tester.tail = tail_pos;
2573 tester.head = tail_pos;
Jorge Aparicio7d661af2015-01-26 20:46:122574 for i in 0..len {
Matt Murphy40f28c72014-12-03 17:12:302575 if i != to_insert {
2576 tester.push_back(i);
2577 }
2578 }
2579 tester.insert(to_insert, to_insert);
Piotr Czarnecki59d41532014-12-16 23:37:552580 assert!(tester.tail < tester.cap);
2581 assert!(tester.head < tester.cap);
2582 assert_eq!(tester, expected);
2583 }
2584 }
2585 }
2586 }
2587
2588 #[test]
2589 fn test_remove() {
2590 // This test checks that every single combination of tail position, length, and
2591 // removal position is tested. Capacity 15 should be large enough to cover every case.
2592
2593 let mut tester = RingBuf::with_capacity(15);
2594 // can't guarantee we got 15, so have to get what we got.
2595 // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else
2596 // this test isn't covering what it wants to
2597 let cap = tester.capacity();
2598
2599 // len is the length *after* removal
Jorge Aparicio7d661af2015-01-26 20:46:122600 for len in 0..cap - 1 {
Piotr Czarnecki59d41532014-12-16 23:37:552601 // 0, 1, 2, .., len - 1
2602 let expected = iter::count(0, 1).take(len).collect();
Jorge Aparicio7d661af2015-01-26 20:46:122603 for tail_pos in 0..cap {
2604 for to_remove in 0..len + 1 {
Piotr Czarnecki59d41532014-12-16 23:37:552605 tester.tail = tail_pos;
2606 tester.head = tail_pos;
Jorge Aparicio7d661af2015-01-26 20:46:122607 for i in 0..len {
Piotr Czarnecki59d41532014-12-16 23:37:552608 if i == to_remove {
2609 tester.push_back(1234);
2610 }
2611 tester.push_back(i);
2612 }
2613 if to_remove == len {
2614 tester.push_back(1234);
2615 }
2616 tester.remove(to_remove);
2617 assert!(tester.tail < tester.cap);
2618 assert!(tester.head < tester.cap);
Matt Murphy40f28c72014-12-03 17:12:302619 assert_eq!(tester, expected);
2620 }
2621 }
2622 }
2623 }
2624
2625 #[test]
Piotr Czarnecki156a1c32015-01-05 14:48:582626 fn test_shrink_to_fit() {
2627 // This test checks that every single combination of head and tail position,
2628 // is tested. Capacity 15 should be large enough to cover every case.
2629
2630 let mut tester = RingBuf::with_capacity(15);
2631 // can't guarantee we got 15, so have to get what we got.
2632 // 15 would be great, but we will definitely get 2^k - 1, for k >= 4, or else
2633 // this test isn't covering what it wants to
2634 let cap = tester.capacity();
2635 tester.reserve(63);
2636 let max_cap = tester.capacity();
2637
Jorge Aparicio7d661af2015-01-26 20:46:122638 for len in 0..cap + 1 {
Piotr Czarnecki156a1c32015-01-05 14:48:582639 // 0, 1, 2, .., len - 1
2640 let expected = iter::count(0, 1).take(len).collect();
Jorge Aparicio7d661af2015-01-26 20:46:122641 for tail_pos in 0..max_cap + 1 {
Piotr Czarnecki156a1c32015-01-05 14:48:582642 tester.tail = tail_pos;
2643 tester.head = tail_pos;
2644 tester.reserve(63);
Jorge Aparicio7d661af2015-01-26 20:46:122645 for i in 0..len {
Piotr Czarnecki156a1c32015-01-05 14:48:582646 tester.push_back(i);
2647 }
2648 tester.shrink_to_fit();
2649 assert!(tester.capacity() <= cap);
2650 assert!(tester.tail < tester.cap);
2651 assert!(tester.head < tester.cap);
2652 assert_eq!(tester, expected);
2653 }
2654 }
2655 }
2656
2657 #[test]
Matt Murphy40f28c72014-12-03 17:12:302658 fn test_front() {
2659 let mut ring = RingBuf::new();
Tobias Bucher7f64fe42015-01-25 21:05:032660 ring.push_back(10);
2661 ring.push_back(20);
Matt Murphy40f28c72014-12-03 17:12:302662 assert_eq!(ring.front(), Some(&10));
2663 ring.pop_front();
2664 assert_eq!(ring.front(), Some(&20));
2665 ring.pop_front();
2666 assert_eq!(ring.front(), None);
2667 }
Clark Gaebel525f65e2014-12-16 04:01:582668
2669 #[test]
2670 fn test_as_slices() {
Tobias Bucher7f64fe42015-01-25 21:05:032671 let mut ring: RingBuf<i32> = RingBuf::with_capacity(127);
Alex Crichton3a2530d2015-01-30 20:26:442672 let cap = ring.capacity() as i32;
Clark Gaebel525f65e2014-12-16 04:01:582673 let first = cap/2;
2674 let last = cap - first;
Jorge Aparicio7d661af2015-01-26 20:46:122675 for i in 0..first {
Clark Gaebel525f65e2014-12-16 04:01:582676 ring.push_back(i);
2677
2678 let (left, right) = ring.as_slices();
Jorge Aparicioc300d682015-01-26 20:44:222679 let expected: Vec<_> = (0..i+1).collect();
Clark Gaebel525f65e2014-12-16 04:01:582680 assert_eq!(left, expected);
2681 assert_eq!(right, []);
2682 }
2683
Jorge Aparicio7d661af2015-01-26 20:46:122684 for j in -last..0 {
Clark Gaebel525f65e2014-12-16 04:01:582685 ring.push_front(j);
2686 let (left, right) = ring.as_slices();
Jorge Aparicioc300d682015-01-26 20:44:222687 let expected_left: Vec<_> = (-last..j+1).rev().collect();
2688 let expected_right: Vec<_> = (0..first).collect();
Clark Gaebel525f65e2014-12-16 04:01:582689 assert_eq!(left, expected_left);
2690 assert_eq!(right, expected_right);
2691 }
2692
Alex Crichton3a2530d2015-01-30 20:26:442693 assert_eq!(ring.len() as i32, cap);
2694 assert_eq!(ring.capacity() as i32, cap);
Clark Gaebel525f65e2014-12-16 04:01:582695 }
2696
2697 #[test]
2698 fn test_as_mut_slices() {
Tobias Bucher7f64fe42015-01-25 21:05:032699 let mut ring: RingBuf<i32> = RingBuf::with_capacity(127);
Alex Crichton3a2530d2015-01-30 20:26:442700 let cap = ring.capacity() as i32;
Clark Gaebel525f65e2014-12-16 04:01:582701 let first = cap/2;
2702 let last = cap - first;
Jorge Aparicio7d661af2015-01-26 20:46:122703 for i in 0..first {
Clark Gaebel525f65e2014-12-16 04:01:582704 ring.push_back(i);
2705
2706 let (left, right) = ring.as_mut_slices();
Jorge Aparicioc300d682015-01-26 20:44:222707 let expected: Vec<_> = (0..i+1).collect();
Clark Gaebel525f65e2014-12-16 04:01:582708 assert_eq!(left, expected);
2709 assert_eq!(right, []);
2710 }
2711
Jorge Aparicio7d661af2015-01-26 20:46:122712 for j in -last..0 {
Clark Gaebel525f65e2014-12-16 04:01:582713 ring.push_front(j);
2714 let (left, right) = ring.as_mut_slices();
Jorge Aparicioc300d682015-01-26 20:44:222715 let expected_left: Vec<_> = (-last..j+1).rev().collect();
2716 let expected_right: Vec<_> = (0..first).collect();
Clark Gaebel525f65e2014-12-16 04:01:582717 assert_eq!(left, expected_left);
2718 assert_eq!(right, expected_right);
2719 }
2720
Alex Crichton3a2530d2015-01-30 20:26:442721 assert_eq!(ring.len() as i32, cap);
2722 assert_eq!(ring.capacity() as i32, cap);
Clark Gaebel525f65e2014-12-16 04:01:582723 }
Michael Sullivanc854d6e2012-07-03 17:52:322724}