blob: 2f0fbfadb17f3c0b9a3243156e8958b216db43c3 [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
P1startf2aa88c2014-08-04 10:48:3911//! A double-ended queue implemented as a circular buffer.
blake2-ppc0f9b9a52013-07-11 14:17:5112//!
P1startf2aa88c2014-08-04 10:48:3913//! `RingBuf` implements the trait `Deque`. It should be imported with
14//! `use collections::Deque`.
Patrick Waltonf3723cf2013-05-17 22:28:4415
Alex Crichton6a585372014-05-30 01:50:1216use core::prelude::*;
17
18use core::cmp;
Tom Jakubowskid6a39412014-06-09 07:30:0419use core::default::Default;
Alex Crichton6a585372014-05-30 01:50:1220use core::fmt;
nham63615772014-07-27 03:18:5621use core::iter;
nham1cfa6562014-07-27 02:33:4722use std::hash::{Writer, Hash};
Alex Crichton998fece2013-05-06 04:42:5423
Patrick Walton7f928d12014-08-13 03:31:3024use {Deque, Mutable, MutableSeq};
Alex Crichton6a585372014-05-30 01:50:1225use vec::Vec;
blake2-ppc70523712013-07-10 13:27:1426
blake2-ppc0ff5c172013-07-06 03:42:4527static INITIAL_CAPACITY: uint = 8u; // 2^3
28static MINIMUM_CAPACITY: uint = 2u;
Daniel Micayb47e1e92013-02-16 22:55:5529
P1startf2aa88c2014-08-04 10:48:3930/// `RingBuf` is a circular buffer that implements `Deque`.
blake2-ppc10c76982013-07-06 13:27:3231#[deriving(Clone)]
blake2-ppc70523712013-07-10 13:27:1432pub struct RingBuf<T> {
Alex Crichton8ad7e542014-03-27 22:10:0433 nelts: uint,
34 lo: uint,
Huon Wilson4b9a7a22014-04-05 05:45:4235 elts: Vec<Option<T>>
Marijn Haverbeke26610db2012-01-11 11:49:3336}
Roy Frostig9c818892010-07-21 01:03:0937
Brian Anderson50942c72014-05-19 18:32:0938impl<T> Collection for RingBuf<T> {
P1startf2aa88c2014-08-04 10:48:3939 /// Returns the number of elements in the `RingBuf`.
Alex Crichtonb29c3682013-06-24 03:44:1140 fn len(&self) -> uint { self.nelts }
Daniel Micayb47e1e92013-02-16 22:55:5541}
Roy Frostig9c818892010-07-21 01:03:0942
blake2-ppc70523712013-07-10 13:27:1443impl<T> Mutable for RingBuf<T> {
P1startf2aa88c2014-08-04 10:48:3944 /// Clears the `RingBuf`, removing all values.
Daniel Micayed7c9c42013-02-16 23:55:2545 fn clear(&mut self) {
Daniel Micay100894552013-08-03 16:45:2346 for x in self.elts.mut_iter() { *x = None }
Daniel Micayed7c9c42013-02-16 23:55:2547 self.nelts = 0;
48 self.lo = 0;
Daniel Micayed7c9c42013-02-16 23:55:2549 }
50}
51
blake2-ppc70523712013-07-10 13:27:1452impl<T> Deque<T> for RingBuf<T> {
P1startf2aa88c2014-08-04 10:48:3953 /// Returns a reference to the first element in the `RingBuf`.
blake2-ppc70523712013-07-10 13:27:1454 fn front<'a>(&'a self) -> Option<&'a T> {
Aaron Turonf77cabe2014-08-11 23:47:4655 if self.nelts > 0 { Some(&self[0]) } else { None }
blake2-ppc0ff5c172013-07-06 03:42:4556 }
57
P1startf2aa88c2014-08-04 10:48:3958 /// Returns a mutable reference to the first element in the `RingBuf`.
blake2-ppc70523712013-07-10 13:27:1459 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
60 if self.nelts > 0 { Some(self.get_mut(0)) } else { None }
Niko Matsakis03396472013-04-10 20:14:0661 }
62
P1startf2aa88c2014-08-04 10:48:3963 /// Returns a reference to the last element in the `RingBuf`.
blake2-ppc70523712013-07-10 13:27:1464 fn back<'a>(&'a self) -> Option<&'a T> {
Aaron Turonf77cabe2014-08-11 23:47:4665 if self.nelts > 0 { Some(&self[self.nelts - 1]) } else { None }
blake2-ppc70523712013-07-10 13:27:1466 }
Niko Matsakis03396472013-04-10 20:14:0667
P1startf2aa88c2014-08-04 10:48:3968 /// Returns a mutable reference to the last element in the `RingBuf`.
blake2-ppc70523712013-07-10 13:27:1469 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
Cameron Zwarich159e27a2014-06-14 03:48:0970 let nelts = self.nelts;
71 if nelts > 0 { Some(self.get_mut(nelts - 1)) } else { None }
blake2-ppc70523712013-07-10 13:27:1472 }
73
P1startf2aa88c2014-08-04 10:48:3974 /// Removes and returns the first element in the `RingBuf`, or `None` if it
75 /// is empty.
blake2-ppc70523712013-07-10 13:27:1476 fn pop_front(&mut self) -> Option<T> {
Huon Wilson4b9a7a22014-04-05 05:45:4277 let result = self.elts.get_mut(self.lo).take();
blake2-ppc70523712013-07-10 13:27:1478 if result.is_some() {
79 self.lo = (self.lo + 1u) % self.elts.len();
80 self.nelts -= 1u;
blake2-ppc5d72f3f2013-07-06 03:42:4581 }
Daniel Micay61906612013-02-17 00:43:2982 result
83 }
84
P1startf2aa88c2014-08-04 10:48:3985 /// Prepends an element to the `RingBuf`.
blake2-ppc70523712013-07-10 13:27:1486 fn push_front(&mut self, t: T) {
blake2-ppc40ce0b72013-07-06 03:42:4587 if self.nelts == self.elts.len() {
blake2-ppc8a326762013-07-06 03:42:4588 grow(self.nelts, &mut self.lo, &mut self.elts);
blake2-ppc40ce0b72013-07-06 03:42:4589 }
Daniel Micayb47e1e92013-02-16 22:55:5590 if self.lo == 0u {
91 self.lo = self.elts.len() - 1u;
92 } else { self.lo -= 1u; }
Huon Wilson4b9a7a22014-04-05 05:45:4293 *self.elts.get_mut(self.lo) = Some(t);
Daniel Micayb47e1e92013-02-16 22:55:5594 self.nelts += 1u;
Erick Tryzelaare84576b2013-01-22 16:44:2495 }
Brian Anderson94e42c22014-07-12 01:08:4696}
Marijn Haverbeke26610db2012-01-11 11:49:3397
Brian Anderson94e42c22014-07-12 01:08:4698impl<T> MutableSeq<T> for RingBuf<T> {
99 fn push(&mut self, t: T) {
blake2-ppc5d72f3f2013-07-06 03:42:45100 if self.nelts == self.elts.len() {
blake2-ppc8a326762013-07-06 03:42:45101 grow(self.nelts, &mut self.lo, &mut self.elts);
Graydon Hoare2880ecd2010-09-22 22:44:13102 }
blake2-ppc5d72f3f2013-07-06 03:42:45103 let hi = self.raw_index(self.nelts);
Huon Wilson4b9a7a22014-04-05 05:45:42104 *self.elts.get_mut(hi) = Some(t);
Daniel Micayb47e1e92013-02-16 22:55:55105 self.nelts += 1u;
Graydon Hoarece729932011-06-15 18:19:50106 }
Brian Anderson94e42c22014-07-12 01:08:46107 fn pop(&mut self) -> Option<T> {
108 if self.nelts > 0 {
109 self.nelts -= 1;
110 let hi = self.raw_index(self.nelts);
111 self.elts.get_mut(hi).take()
112 } else {
113 None
114 }
115 }
Brian Andersond36a8f32014-07-11 17:12:38116}
117
Tom Jakubowskid6a39412014-06-09 07:30:04118impl<T> Default for RingBuf<T> {
119 #[inline]
120 fn default() -> RingBuf<T> { RingBuf::new() }
121}
122
blake2-ppc70523712013-07-10 13:27:14123impl<T> RingBuf<T> {
P1startf2aa88c2014-08-04 10:48:39124 /// Creates an empty `RingBuf`.
blake2-ppc70523712013-07-10 13:27:14125 pub fn new() -> RingBuf<T> {
126 RingBuf::with_capacity(INITIAL_CAPACITY)
127 }
128
P1startf2aa88c2014-08-04 10:48:39129 /// Creates an empty `RingBuf` with space for at least `n` elements.
blake2-ppc70523712013-07-10 13:27:14130 pub fn with_capacity(n: uint) -> RingBuf<T> {
131 RingBuf{nelts: 0, lo: 0,
Huon Wilson4b9a7a22014-04-05 05:45:42132 elts: Vec::from_fn(cmp::max(MINIMUM_CAPACITY, n), |_| None)}
blake2-ppc70523712013-07-10 13:27:14133 }
134
P1startf2aa88c2014-08-04 10:48:39135 /// Retrieva an element in the `RingBuf` by index.
blake2-ppc70523712013-07-10 13:27:14136 ///
P1startf2aa88c2014-08-04 10:48:39137 /// Fails if there is no element with the given index.
nhamebe80972014-07-17 23:19:51138 ///
139 /// # Example
140 ///
141 /// ```rust
P1startfd10d202014-08-02 06:39:39142 /// #![allow(deprecated)]
143 ///
nhamebe80972014-07-17 23:19:51144 /// use std::collections::RingBuf;
145 ///
146 /// let mut buf = RingBuf::new();
147 /// buf.push(3i);
148 /// buf.push(4);
149 /// buf.push(5);
150 /// assert_eq!(buf.get(1), &4);
151 /// ```
P1startfd10d202014-08-02 06:39:39152 #[deprecated = "prefer using indexing, e.g., ringbuf[0]"]
blake2-ppc70523712013-07-10 13:27:14153 pub fn get<'a>(&'a self, i: uint) -> &'a T {
154 let idx = self.raw_index(i);
Aaron Turonf77cabe2014-08-11 23:47:46155 match self.elts[idx] {
Alex Crichtondaf5f5a2013-10-21 20:08:31156 None => fail!(),
blake2-ppc70523712013-07-10 13:27:14157 Some(ref v) => v
158 }
159 }
160
P1startf2aa88c2014-08-04 10:48:39161 /// Retrieves an element in the `RingBuf` by index.
blake2-ppc70523712013-07-10 13:27:14162 ///
P1startf2aa88c2014-08-04 10:48:39163 /// Fails if there is no element with the given index.
nhamebe80972014-07-17 23:19:51164 ///
165 /// # Example
166 ///
167 /// ```rust
168 /// use std::collections::RingBuf;
169 ///
170 /// let mut buf = RingBuf::new();
171 /// buf.push(3i);
172 /// buf.push(4);
173 /// buf.push(5);
174 /// *buf.get_mut(1) = 7;
P1startfd10d202014-08-02 06:39:39175 /// assert_eq!(buf[1], 7);
nhamebe80972014-07-17 23:19:51176 /// ```
blake2-ppc70523712013-07-10 13:27:14177 pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
178 let idx = self.raw_index(i);
Huon Wilson4b9a7a22014-04-05 05:45:42179 match *self.elts.get_mut(idx) {
Alex Crichtondaf5f5a2013-10-21 20:08:31180 None => fail!(),
blake2-ppc70523712013-07-10 13:27:14181 Some(ref mut v) => v
182 }
183 }
184
P1startf2aa88c2014-08-04 10:48:39185 /// Swaps elements at indices `i` and `j`.
blake2-ppc57757a82013-09-26 07:19:26186 ///
187 /// `i` and `j` may be equal.
188 ///
P1startf2aa88c2014-08-04 10:48:39189 /// Fails if there is no element with either index.
nhamebe80972014-07-17 23:19:51190 ///
191 /// # Example
192 ///
193 /// ```rust
194 /// use std::collections::RingBuf;
195 ///
196 /// let mut buf = RingBuf::new();
197 /// buf.push(3i);
198 /// buf.push(4);
199 /// buf.push(5);
200 /// buf.swap(0, 2);
P1startfd10d202014-08-02 06:39:39201 /// assert_eq!(buf[0], 5);
202 /// assert_eq!(buf[2], 3);
nhamebe80972014-07-17 23:19:51203 /// ```
blake2-ppc57757a82013-09-26 07:19:26204 pub fn swap(&mut self, i: uint, j: uint) {
205 assert!(i < self.len());
206 assert!(j < self.len());
207 let ri = self.raw_index(i);
208 let rj = self.raw_index(j);
Huon Wilson4b9a7a22014-04-05 05:45:42209 self.elts.as_mut_slice().swap(ri, rj);
blake2-ppc57757a82013-09-26 07:19:26210 }
211
P1startf2aa88c2014-08-04 10:48:39212 /// Returns the index in the underlying `Vec` for a given logical element
213 /// index.
blake2-ppc70523712013-07-10 13:27:14214 fn raw_index(&self, idx: uint) -> uint {
215 raw_index(self.lo, self.elts.len(), idx)
216 }
217
P1startf2aa88c2014-08-04 10:48:39218 /// Reserves capacity for exactly `n` elements in the given `RingBuf`,
Tim Chevalier77de84b2013-05-27 18:47:38219 /// doing nothing if `self`'s capacity is already equal to or greater
P1startf2aa88c2014-08-04 10:48:39220 /// than the requested capacity.
David Manescu65f35782014-01-31 13:03:20221 pub fn reserve_exact(&mut self, n: uint) {
222 self.elts.reserve_exact(n);
Tim Chevalier77de84b2013-05-27 18:47:38223 }
224
P1startf2aa88c2014-08-04 10:48:39225 /// Reserves capacity for at least `n` elements in the given `RingBuf`,
Tim Chevalier77de84b2013-05-27 18:47:38226 /// over-allocating in case the caller needs to reserve additional
227 /// space.
228 ///
229 /// Do nothing if `self`'s capacity is already equal to or greater
230 /// than the requested capacity.
David Manescu65f35782014-01-31 13:03:20231 pub fn reserve(&mut self, n: uint) {
232 self.elts.reserve(n);
Tim Chevalier77de84b2013-05-27 18:47:38233 }
Jed Estep4f7a7422013-06-25 19:08:47234
P1startf2aa88c2014-08-04 10:48:39235 /// Returns a front-to-back iterator.
nhamebe80972014-07-17 23:19:51236 ///
237 /// # Example
238 ///
239 /// ```rust
240 /// use std::collections::RingBuf;
241 ///
242 /// let mut buf = RingBuf::new();
243 /// buf.push(5i);
244 /// buf.push(3);
245 /// buf.push(4);
Nick Cameron52ef4622014-08-06 09:59:40246 /// let b: &[_] = &[&5, &3, &4];
247 /// assert_eq!(buf.iter().collect::<Vec<&int>>().as_slice(), b);
nhamebe80972014-07-17 23:19:51248 /// ```
Palmer Cox3fd8c8b2014-01-15 03:32:24249 pub fn iter<'a>(&'a self) -> Items<'a, T> {
Huon Wilson4b9a7a22014-04-05 05:45:42250 Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()}
blake2-ppc3385e792013-07-15 23:13:26251 }
252
P1startf2aa88c2014-08-04 10:48:39253 /// Returns a front-to-back iterator which returns mutable references.
nhamebe80972014-07-17 23:19:51254 ///
255 /// # Example
256 ///
257 /// ```rust
258 /// use std::collections::RingBuf;
259 ///
260 /// let mut buf = RingBuf::new();
261 /// buf.push(5i);
262 /// buf.push(3);
263 /// buf.push(4);
264 /// for num in buf.mut_iter() {
265 /// *num = *num - 2;
266 /// }
Nick Cameron52ef4622014-08-06 09:59:40267 /// let b: &[_] = &[&mut 3, &mut 1, &mut 2];
268 /// assert_eq!(buf.mut_iter().collect::<Vec<&mut int>>().as_slice(), b);
nhamebe80972014-07-17 23:19:51269 /// ```
Palmer Cox3fd8c8b2014-01-15 03:32:24270 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39271 let start_index = raw_index(self.lo, self.elts.len(), 0);
272 let end_index = raw_index(self.lo, self.elts.len(), self.nelts);
273
274 // Divide up the array
275 if end_index <= start_index {
276 // Items to iterate goes from:
277 // start_index to self.elts.len()
278 // and then
279 // 0 to end_index
Guillaume Pinot25bb1a42013-12-01 17:19:39280 let (temp, remaining1) = self.elts.mut_split_at(start_index);
281 let (remaining2, _) = temp.mut_split_at(end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24282 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39283 remaining2: remaining2,
284 nelts: self.nelts }
285 } else {
286 // Items to iterate goes from start_index to end_index:
Guillaume Pinot25bb1a42013-12-01 17:19:39287 let (empty, elts) = self.elts.mut_split_at(0);
Niko Matsakisbc4164d2013-11-16 22:29:39288 let remaining1 = elts.mut_slice(start_index, end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24289 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39290 remaining2: empty,
291 nelts: self.nelts }
292 }
Jed Estep4f7a7422013-06-25 19:08:47293 }
Jed Estep4f7a7422013-06-25 19:08:47294}
295
P1startf2aa88c2014-08-04 10:48:39296/// `RingBuf` iterator.
Palmer Cox3fd8c8b2014-01-15 03:32:24297pub struct Items<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04298 lo: uint,
299 index: uint,
300 rindex: uint,
301 elts: &'a [Option<T>],
Jed Estep4f7a7422013-06-25 19:08:47302}
Niko Matsakisbc4164d2013-11-16 22:29:39303
Palmer Cox3fd8c8b2014-01-15 03:32:24304impl<'a, T> Iterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39305 #[inline]
Erik Price5731ca32013-12-10 07:16:18306 fn next(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39307 if self.index == self.rindex {
308 return None;
309 }
310 let raw_index = raw_index(self.lo, self.elts.len(), self.index);
311 self.index += 1;
312 Some(self.elts[raw_index].get_ref())
313 }
314
315 #[inline]
316 fn size_hint(&self) -> (uint, Option<uint>) {
317 let len = self.rindex - self.index;
318 (len, Some(len))
319 }
320}
321
Palmer Cox3fd8c8b2014-01-15 03:32:24322impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39323 #[inline]
Erik Price5731ca32013-12-10 07:16:18324 fn next_back(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39325 if self.index == self.rindex {
326 return None;
327 }
328 self.rindex -= 1;
329 let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
330 Some(self.elts[raw_index].get_ref())
331 }
332}
Jed Estep35314c92013-06-26 15:38:29333
Palmer Cox3fd8c8b2014-01-15 03:32:24334impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24335
Palmer Cox3fd8c8b2014-01-15 03:32:24336impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> {
blake2-ppcf6862132013-07-29 18:16:26337 #[inline]
338 fn indexable(&self) -> uint { self.rindex - self.index }
339
340 #[inline]
Alex Crichtonf4083a22014-04-22 05:15:42341 fn idx(&mut self, j: uint) -> Option<&'a T> {
blake2-ppcf6862132013-07-29 18:16:26342 if j >= self.indexable() {
343 None
344 } else {
345 let raw_index = raw_index(self.lo, self.elts.len(), self.index + j);
346 Some(self.elts[raw_index].get_ref())
347 }
348 }
349}
350
P1startf2aa88c2014-08-04 10:48:39351/// `RingBuf` mutable iterator.
Palmer Cox3fd8c8b2014-01-15 03:32:24352pub struct MutItems<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04353 remaining1: &'a mut [Option<T>],
354 remaining2: &'a mut [Option<T>],
355 nelts: uint,
Jed Estep4f7a7422013-06-25 19:08:47356}
Niko Matsakisbc4164d2013-11-16 22:29:39357
Palmer Cox3fd8c8b2014-01-15 03:32:24358impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39359 #[inline]
Brian Anderson4e1024f2014-08-07 06:59:12360 #[allow(deprecated)] // mut_shift_ref
Erik Price5731ca32013-12-10 07:16:18361 fn next(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39362 if self.nelts == 0 {
363 return None;
364 }
365 let r = if self.remaining1.len() > 0 {
366 &mut self.remaining1
367 } else {
368 assert!(self.remaining2.len() > 0);
369 &mut self.remaining2
370 };
371 self.nelts -= 1;
Nathaniel Hermand451c152014-01-25 17:00:46372 Some(r.mut_shift_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39373 }
374
375 #[inline]
376 fn size_hint(&self) -> (uint, Option<uint>) {
377 (self.nelts, Some(self.nelts))
378 }
379}
380
Palmer Cox3fd8c8b2014-01-15 03:32:24381impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39382 #[inline]
Brian Anderson4e1024f2014-08-07 06:59:12383 #[allow(deprecated)] // mut_shift_ref
Erik Price5731ca32013-12-10 07:16:18384 fn next_back(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39385 if self.nelts == 0 {
386 return None;
387 }
388 let r = if self.remaining2.len() > 0 {
389 &mut self.remaining2
390 } else {
391 assert!(self.remaining1.len() > 0);
392 &mut self.remaining1
393 };
394 self.nelts -= 1;
Nathaniel Herman33960342014-01-25 20:33:31395 Some(r.mut_pop_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39396 }
397}
Daniel Micayb47e1e92013-02-16 22:55:55398
Palmer Cox3fd8c8b2014-01-15 03:32:24399impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24400
Daniel Micayb47e1e92013-02-16 22:55:55401/// Grow is only called on full elts, so nelts is also len(elts), unlike
402/// elsewhere.
Huon Wilson4b9a7a22014-04-05 05:45:42403fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut Vec<Option<T>>) {
Corey Richardsoncc57ca02013-05-19 02:02:45404 assert_eq!(nelts, elts.len());
blake2-ppc8a326762013-07-06 03:42:45405 let lo = *loptr;
Kevin Butler64896d62014-08-07 01:11:13406 elts.reserve(nelts * 2);
407 let newlen = elts.capacity();
Daniel Micayb47e1e92013-02-16 22:55:55408
blake2-ppc40ce0b72013-07-06 03:42:45409 /* fill with None */
Kevin Butler64896d62014-08-07 01:11:13410 for _ in range(elts.len(), newlen) {
blake2-ppc40ce0b72013-07-06 03:42:45411 elts.push(None);
Daniel Micayb47e1e92013-02-16 22:55:55412 }
blake2-ppc8a326762013-07-06 03:42:45413
414 /*
415 Move the shortest half into the newly reserved area.
416 lo ---->|
417 nelts ----------->|
418 [o o o|o o o o o]
419 A [. . .|o o o o o o o o|. . . . .]
420 B [o o o|. . . . . . . .|o o o o o]
421 */
422
423 assert!(newlen - nelts/2 >= nelts);
424 if lo <= (nelts - lo) { // A
Daniel Micay100894552013-08-03 16:45:23425 for i in range(0u, lo) {
Huon Wilson4b9a7a22014-04-05 05:45:42426 elts.as_mut_slice().swap(i, nelts + i);
blake2-ppc8a326762013-07-06 03:42:45427 }
428 } else { // B
Daniel Micay100894552013-08-03 16:45:23429 for i in range(lo, nelts) {
Huon Wilson4b9a7a22014-04-05 05:45:42430 elts.as_mut_slice().swap(i, newlen - nelts + i);
blake2-ppc8a326762013-07-06 03:42:45431 }
432 *loptr += newlen - nelts;
blake2-ppc40ce0b72013-07-06 03:42:45433 }
Daniel Micayb47e1e92013-02-16 22:55:55434}
435
P1startf2aa88c2014-08-04 10:48:39436/// Returns the index in the underlying `Vec` for a given logical element index.
blake2-ppc75015c32013-07-06 03:42:45437fn raw_index(lo: uint, len: uint, index: uint) -> uint {
438 if lo >= len - index {
439 lo + index - len
440 } else {
441 lo + index
442 }
443}
444
Alex Crichton748bc3c2014-05-30 00:45:07445impl<A: PartialEq> PartialEq for RingBuf<A> {
blake2-ppc70523712013-07-10 13:27:14446 fn eq(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32447 self.nelts == other.nelts &&
448 self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
449 }
blake2-ppc70523712013-07-10 13:27:14450 fn ne(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32451 !self.eq(other)
452 }
453}
454
nham25acfde2014-08-01 20:05:03455impl<A: Eq> Eq for RingBuf<A> {}
456
nham63615772014-07-27 03:18:56457impl<A: PartialOrd> PartialOrd for RingBuf<A> {
458 fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
459 iter::order::partial_cmp(self.iter(), other.iter())
460 }
461}
462
nham3737c532014-08-01 20:22:48463impl<A: Ord> Ord for RingBuf<A> {
464 #[inline]
465 fn cmp(&self, other: &RingBuf<A>) -> Ordering {
466 iter::order::cmp(self.iter(), other.iter())
467 }
468}
469
nham1cfa6562014-07-27 02:33:47470impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
471 fn hash(&self, state: &mut S) {
nham9fa44242014-07-27 16:37:32472 self.len().hash(state);
nham1cfa6562014-07-27 02:33:47473 for elt in self.iter() {
474 elt.hash(state);
475 }
476 }
477}
478
P1startfd10d202014-08-02 06:39:39479impl<A> Index<uint, A> for RingBuf<A> {
480 #[inline]
Aaron Turonf77cabe2014-08-11 23:47:46481 #[allow(deprecated)]
P1startfd10d202014-08-02 06:39:39482 fn index<'a>(&'a self, i: &uint) -> &'a A {
483 self.get(*i)
484 }
485}
486
487// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
488/*impl<A> IndexMut<uint, A> for RingBuf<A> {
489 #[inline]
490 fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut A {
491 self.get_mut(*index)
492 }
493}*/
494
Huon Wilson53487a02013-08-13 13:08:14495impl<A> FromIterator<A> for RingBuf<A> {
Brian Andersonee052192014-03-31 04:45:55496 fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
blake2-ppcf8ae5262013-07-30 00:06:49497 let (lower, _) = iterator.size_hint();
498 let mut deq = RingBuf::with_capacity(lower);
499 deq.extend(iterator);
blake2-ppc08dc72f2013-07-06 03:42:45500 deq
501 }
502}
503
Huon Wilson53487a02013-08-13 13:08:14504impl<A> Extendable<A> for RingBuf<A> {
Marvin Löbel6200e762014-03-20 13:12:56505 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
506 for elt in iterator {
Aaron Turonf77cabe2014-08-11 23:47:46507 self.push(elt);
blake2-ppcf8ae5262013-07-30 00:06:49508 }
509 }
510}
511
Alex Crichton6a585372014-05-30 01:50:12512impl<T: fmt::Show> fmt::Show for RingBuf<T> {
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:04513 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
514 try!(write!(f, "["));
515
516 for (i, e) in self.iter().enumerate() {
517 if i != 0 { try!(write!(f, ", ")); }
518 try!(write!(f, "{}", *e));
519 }
520
521 write!(f, "]")
522 }
523}
524
Brian Anderson6e27b272012-01-18 03:05:07525#[cfg(test)]
526mod tests {
Alex Crichton02882fb2014-02-28 09:23:06527 use std::fmt::Show;
Alex Crichton760b93a2014-05-30 02:03:06528 use std::prelude::*;
Alex Crichtonade807c2014-06-12 02:33:52529 use std::gc::{GC, Gc};
nham1cfa6562014-07-27 02:33:47530 use std::hash;
Alex Crichton760b93a2014-05-30 02:03:06531 use test::Bencher;
532 use test;
533
Brian Anderson054b1ff2014-07-14 21:45:10534 use {Deque, Mutable, MutableSeq};
Alex Crichtonf47e4b22014-01-07 06:33:50535 use super::RingBuf;
Alex Crichton760b93a2014-05-30 02:03:06536 use vec::Vec;
Patrick Waltonfa5ee932012-12-28 02:24:18537
Brian Anderson6e27b272012-01-18 03:05:07538 #[test]
539 fn test_simple() {
blake2-ppc70523712013-07-10 13:27:14540 let mut d = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45541 assert_eq!(d.len(), 0u);
Niko Matsakis9e3d0b02014-04-21 21:58:52542 d.push_front(17i);
543 d.push_front(42i);
blake2-ppc70523712013-07-10 13:27:14544 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45545 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14546 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45547 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31548 debug!("{:?}", d.front());
blake2-ppc70523712013-07-10 13:27:14549 assert_eq!(*d.front().unwrap(), 42);
Alex Crichtondaf5f5a2013-10-21 20:08:31550 debug!("{:?}", d.back());
blake2-ppc70523712013-07-10 13:27:14551 assert_eq!(*d.back().unwrap(), 137);
552 let mut i = d.pop_front();
Alex Crichtondaf5f5a2013-10-21 20:08:31553 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14554 assert_eq!(i, Some(42));
Brian Anderson6e27b272012-01-18 03:05:07555 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31556 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14557 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07558 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31559 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14560 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07561 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31562 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14563 assert_eq!(i, Some(17));
Corey Richardsoncc57ca02013-05-19 02:02:45564 assert_eq!(d.len(), 0u);
blake2-ppc70523712013-07-10 13:27:14565 d.push_back(3);
Corey Richardsoncc57ca02013-05-19 02:02:45566 assert_eq!(d.len(), 1u);
blake2-ppc70523712013-07-10 13:27:14567 d.push_front(2);
Corey Richardsoncc57ca02013-05-19 02:02:45568 assert_eq!(d.len(), 2u);
blake2-ppc70523712013-07-10 13:27:14569 d.push_back(4);
Corey Richardsoncc57ca02013-05-19 02:02:45570 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14571 d.push_front(1);
Corey Richardsoncc57ca02013-05-19 02:02:45572 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31573 debug!("{:?}", d.get(0));
574 debug!("{:?}", d.get(1));
575 debug!("{:?}", d.get(2));
576 debug!("{:?}", d.get(3));
Corey Richardsoncc57ca02013-05-19 02:02:45577 assert_eq!(*d.get(0), 1);
578 assert_eq!(*d.get(1), 2);
579 assert_eq!(*d.get(2), 3);
580 assert_eq!(*d.get(3), 4);
Brian Anderson6e27b272012-01-18 03:05:07581 }
582
Kevin Cantuc43426e2012-09-13 05:09:55583 #[test]
584 fn test_boxes() {
Alex Crichtonade807c2014-06-12 02:33:52585 let a: Gc<int> = box(GC) 5;
586 let b: Gc<int> = box(GC) 72;
587 let c: Gc<int> = box(GC) 64;
588 let d: Gc<int> = box(GC) 175;
Kevin Cantuc43426e2012-09-13 05:09:55589
blake2-ppc70523712013-07-10 13:27:14590 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45591 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14592 deq.push_front(a);
593 deq.push_front(b);
594 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45595 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14596 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45597 assert_eq!(deq.len(), 4);
blake2-ppc70523712013-07-10 13:27:14598 assert_eq!(deq.front(), Some(&b));
599 assert_eq!(deq.back(), Some(&d));
600 assert_eq!(deq.pop_front(), Some(b));
601 assert_eq!(deq.pop_back(), Some(d));
602 assert_eq!(deq.pop_back(), Some(c));
603 assert_eq!(deq.pop_back(), Some(a));
Corey Richardsoncc57ca02013-05-19 02:02:45604 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14605 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45606 assert_eq!(deq.len(), 1);
blake2-ppc70523712013-07-10 13:27:14607 deq.push_front(b);
Corey Richardsoncc57ca02013-05-19 02:02:45608 assert_eq!(deq.len(), 2);
blake2-ppc70523712013-07-10 13:27:14609 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45610 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14611 deq.push_front(a);
Corey Richardsoncc57ca02013-05-19 02:02:45612 assert_eq!(deq.len(), 4);
613 assert_eq!(*deq.get(0), a);
614 assert_eq!(*deq.get(1), b);
615 assert_eq!(*deq.get(2), c);
616 assert_eq!(*deq.get(3), d);
Daniel Micay373c0722013-02-17 00:10:10617 }
618
Felix S. Klock IIa636f512013-05-01 23:32:37619 #[cfg(test)]
Alex Crichton748bc3c2014-05-30 00:45:07620 fn test_parameterized<T:Clone + PartialEq + Show>(a: T, b: T, c: T, d: T) {
Patrick Waltondc4bf172013-07-13 04:05:59621 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45622 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59623 deq.push_front(a.clone());
624 deq.push_front(b.clone());
625 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45626 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59627 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45628 assert_eq!(deq.len(), 4);
Marvin Löbel0ac7a212013-08-03 23:59:24629 assert_eq!((*deq.front().unwrap()).clone(), b.clone());
630 assert_eq!((*deq.back().unwrap()).clone(), d.clone());
631 assert_eq!(deq.pop_front().unwrap(), b.clone());
632 assert_eq!(deq.pop_back().unwrap(), d.clone());
633 assert_eq!(deq.pop_back().unwrap(), c.clone());
634 assert_eq!(deq.pop_back().unwrap(), a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45635 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59636 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45637 assert_eq!(deq.len(), 1);
Patrick Waltondc4bf172013-07-13 04:05:59638 deq.push_front(b.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45639 assert_eq!(deq.len(), 2);
Patrick Waltondc4bf172013-07-13 04:05:59640 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45641 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59642 deq.push_front(a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45643 assert_eq!(deq.len(), 4);
Patrick Walton99b33f72013-07-02 19:47:32644 assert_eq!((*deq.get(0)).clone(), a.clone());
645 assert_eq!((*deq.get(1)).clone(), b.clone());
646 assert_eq!((*deq.get(2)).clone(), c.clone());
647 assert_eq!((*deq.get(3)).clone(), d.clone());
Brian Anderson6e27b272012-01-18 03:05:07648 }
649
blake2-ppc81933ed2013-07-06 03:42:45650 #[test]
blake2-ppc70523712013-07-10 13:27:14651 fn test_push_front_grow() {
652 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23653 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14654 deq.push_front(i);
blake2-ppc81933ed2013-07-06 03:42:45655 }
656 assert_eq!(deq.len(), 66);
657
Daniel Micay100894552013-08-03 16:45:23658 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45659 assert_eq!(*deq.get(i), 65 - i);
660 }
661
blake2-ppc70523712013-07-10 13:27:14662 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23663 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14664 deq.push_back(i);
blake2-ppc81933ed2013-07-06 03:42:45665 }
666
Daniel Micay100894552013-08-03 16:45:23667 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45668 assert_eq!(*deq.get(i), i);
669 }
670 }
671
P1startfd10d202014-08-02 06:39:39672 #[test]
673 fn test_index() {
674 let mut deq = RingBuf::new();
675 for i in range(1u, 4) {
676 deq.push_front(i);
677 }
678 assert_eq!(deq[1], 2);
679 }
680
681 #[test]
682 #[should_fail]
683 fn test_index_out_of_bounds() {
684 let mut deq = RingBuf::new();
685 for i in range(1u, 4) {
686 deq.push_front(i);
687 }
688 deq[3];
689 }
690
blake2-ppc81933ed2013-07-06 03:42:45691 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35692 fn bench_new(b: &mut test::Bencher) {
Patrick Walton38efa172013-11-22 03:20:48693 b.iter(|| {
Patrick Walton86939432013-08-08 18:38:10694 let _: RingBuf<u64> = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48695 })
blake2-ppc81933ed2013-07-06 03:42:45696 }
697
698 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35699 fn bench_push_back(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14700 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48701 b.iter(|| {
Patrick Waltona5bb0a32014-06-27 19:30:25702 deq.push_back(0i);
Patrick Walton38efa172013-11-22 03:20:48703 })
blake2-ppc81933ed2013-07-06 03:42:45704 }
705
706 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35707 fn bench_push_front(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14708 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48709 b.iter(|| {
Patrick Waltona5bb0a32014-06-27 19:30:25710 deq.push_front(0i);
Patrick Walton38efa172013-11-22 03:20:48711 })
blake2-ppc81933ed2013-07-06 03:42:45712 }
713
714 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35715 fn bench_grow(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14716 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48717 b.iter(|| {
Niko Matsakis9e3d0b02014-04-21 21:58:52718 for _ in range(0i, 65) {
Patrick Waltona5bb0a32014-06-27 19:30:25719 deq.push_front(1i);
Brendan Zabarauskas729060d2014-01-30 00:20:34720 }
Patrick Walton38efa172013-11-22 03:20:48721 })
blake2-ppc81933ed2013-07-06 03:42:45722 }
723
Alex Crichton748bc3c2014-05-30 00:45:07724 #[deriving(Clone, PartialEq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32725 enum Taggy {
726 One(int),
727 Two(int, int),
728 Three(int, int, int),
Brian Anderson6e27b272012-01-18 03:05:07729 }
730
Alex Crichton748bc3c2014-05-30 00:45:07731 #[deriving(Clone, PartialEq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32732 enum Taggypar<T> {
733 Onepar(int),
734 Twopar(int, int),
735 Threepar(int, int, int),
736 }
737
Alex Crichton748bc3c2014-05-30 00:45:07738 #[deriving(Clone, PartialEq, Show)]
Erick Tryzelaare84576b2013-01-22 16:44:24739 struct RecCy {
740 x: int,
741 y: int,
Patrick Waltoneb4d39e2013-01-26 00:57:39742 t: Taggy
Patrick Walton9117dcb2012-09-20 01:00:26743 }
Kevin Cantuc43426e2012-09-13 05:09:55744
745 #[test]
746 fn test_param_int() {
747 test_parameterized::<int>(5, 72, 64, 175);
748 }
749
750 #[test]
751 fn test_param_at_int() {
Alex Crichtonade807c2014-06-12 02:33:52752 test_parameterized::<Gc<int>>(box(GC) 5, box(GC) 72,
753 box(GC) 64, box(GC) 175);
Kevin Cantuc43426e2012-09-13 05:09:55754 }
755
756 #[test]
757 fn test_param_taggy() {
Corey Richardsonf8ae9b02013-06-26 22:14:35758 test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55759 }
760
761 #[test]
762 fn test_param_taggypar() {
763 test_parameterized::<Taggypar<int>>(Onepar::<int>(1),
Ben Striegela605fd02012-08-11 14:08:42764 Twopar::<int>(1, 2),
765 Threepar::<int>(1, 2, 3),
766 Twopar::<int>(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55767 }
Brian Anderson6e27b272012-01-18 03:05:07768
Kevin Cantuc43426e2012-09-13 05:09:55769 #[test]
770 fn test_param_reccy() {
Erick Tryzelaare84576b2013-01-22 16:44:24771 let reccy1 = RecCy { x: 1, y: 2, t: One(1) };
772 let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) };
773 let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) };
774 let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
Kevin Cantuc43426e2012-09-13 05:09:55775 test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
Brian Anderson6e27b272012-01-18 03:05:07776 }
Erick Tryzelaar909d8f02013-03-30 01:02:44777
778 #[test]
blake2-ppc0ff5c172013-07-06 03:42:45779 fn test_with_capacity() {
blake2-ppc70523712013-07-10 13:27:14780 let mut d = RingBuf::with_capacity(0);
Patrick Waltona5bb0a32014-06-27 19:30:25781 d.push_back(1i);
blake2-ppc0ff5c172013-07-06 03:42:45782 assert_eq!(d.len(), 1);
blake2-ppc70523712013-07-10 13:27:14783 let mut d = RingBuf::with_capacity(50);
Patrick Waltona5bb0a32014-06-27 19:30:25784 d.push_back(1i);
blake2-ppc0ff5c172013-07-06 03:42:45785 assert_eq!(d.len(), 1);
786 }
787
788 #[test]
Kevin Butler64896d62014-08-07 01:11:13789 fn test_with_capacity_non_power_two() {
790 let mut d3 = RingBuf::with_capacity(3);
791 d3.push(1i);
792
793 // X = None, | = lo
794 // [|1, X, X]
795 assert_eq!(d3.pop_front(), Some(1));
796 // [X, |X, X]
797 assert_eq!(d3.front(), None);
798
799 // [X, |3, X]
800 d3.push(3);
801 // [X, |3, 6]
802 d3.push(6);
803 // [X, X, |6]
804 assert_eq!(d3.pop_front(), Some(3));
805
806 // Pushing the lo past half way point to trigger
807 // the 'B' scenario for growth
808 // [9, X, |6]
809 d3.push(9);
810 // [9, 12, |6]
811 d3.push(12);
812
813 d3.push(15);
814 // There used to be a bug here about how the
815 // RingBuf made growth assumptions about the
816 // underlying Vec which didn't hold and lead
817 // to corruption.
818 // (Vec grows to next power of two)
819 //good- [9, 12, 15, X, X, X, X, |6]
820 //bug- [15, 12, X, X, X, |6, X, X]
821 assert_eq!(d3.pop_front(), Some(6));
822
823 // Which leads us to the following state which
824 // would be a failure case.
825 //bug- [15, 12, X, X, X, X, |X, X]
826 assert_eq!(d3.front(), Some(&9));
827 }
828
829 #[test]
David Manescu65f35782014-01-31 13:03:20830 fn test_reserve_exact() {
blake2-ppc70523712013-07-10 13:27:14831 let mut d = RingBuf::new();
832 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20833 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47834 assert_eq!(d.elts.capacity(), 50);
blake2-ppc70523712013-07-10 13:27:14835 let mut d = RingBuf::new();
836 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20837 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47838 assert_eq!(d.elts.capacity(), 50);
Tim Chevalier77de84b2013-05-27 18:47:38839 }
840
841 #[test]
David Manescu65f35782014-01-31 13:03:20842 fn test_reserve() {
blake2-ppc70523712013-07-10 13:27:14843 let mut d = RingBuf::new();
844 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20845 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47846 assert_eq!(d.elts.capacity(), 64);
blake2-ppc70523712013-07-10 13:27:14847 let mut d = RingBuf::new();
848 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20849 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47850 assert_eq!(d.elts.capacity(), 64);
Tim Chevalier77de84b2013-05-27 18:47:38851 }
852
Jed Estep096fb792013-06-26 14:04:44853 #[test]
blake2-ppc57757a82013-09-26 07:19:26854 fn test_swap() {
Niko Matsakis9e3d0b02014-04-21 21:58:52855 let mut d: RingBuf<int> = range(0i, 5).collect();
blake2-ppc57757a82013-09-26 07:19:26856 d.pop_front();
857 d.swap(0, 3);
Huon Wilson4b9a7a22014-04-05 05:45:42858 assert_eq!(d.iter().map(|&x|x).collect::<Vec<int>>(), vec!(4, 2, 3, 1));
blake2-ppc57757a82013-09-26 07:19:26859 }
860
861 #[test]
Jed Estep096fb792013-06-26 14:04:44862 fn test_iter() {
blake2-ppc70523712013-07-10 13:27:14863 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45864 assert_eq!(d.iter().next(), None);
blake2-ppc9ccf4432013-07-14 20:30:22865 assert_eq!(d.iter().size_hint(), (0, Some(0)));
blake2-ppcf88d5322013-07-06 03:42:45866
Niko Matsakis9e3d0b02014-04-21 21:58:52867 for i in range(0i, 5) {
blake2-ppc70523712013-07-10 13:27:14868 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44869 }
Nick Cameron37a94b82014-08-04 12:19:02870 {
871 let b: &[_] = &[&0,&1,&2,&3,&4];
872 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b);
873 }
Corey Richardsonf8ae9b02013-06-26 22:14:35874
Niko Matsakis9e3d0b02014-04-21 21:58:52875 for i in range(6i, 9) {
blake2-ppc70523712013-07-10 13:27:14876 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44877 }
Nick Cameron37a94b82014-08-04 12:19:02878 {
879 let b: &[_] = &[&8,&7,&6,&0,&1,&2,&3,&4];
880 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b);
881 }
blake2-ppc9ccf4432013-07-14 20:30:22882
883 let mut it = d.iter();
884 let mut len = d.len();
885 loop {
886 match it.next() {
887 None => break,
888 _ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) }
889 }
890 }
Jed Estep096fb792013-06-26 14:04:44891 }
892
893 #[test]
894 fn test_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14895 let mut d = RingBuf::new();
Jonathan S03609e52014-04-21 04:59:12896 assert_eq!(d.iter().rev().next(), None);
blake2-ppcf88d5322013-07-06 03:42:45897
Niko Matsakis9e3d0b02014-04-21 21:58:52898 for i in range(0i, 5) {
blake2-ppc70523712013-07-10 13:27:14899 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44900 }
Nick Cameron37a94b82014-08-04 12:19:02901 {
902 let b: &[_] = &[&4,&3,&2,&1,&0];
903 assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b);
904 }
Corey Richardsonf8ae9b02013-06-26 22:14:35905
Niko Matsakis9e3d0b02014-04-21 21:58:52906 for i in range(6i, 9) {
blake2-ppc70523712013-07-10 13:27:14907 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44908 }
Nick Cameron37a94b82014-08-04 12:19:02909 let b: &[_] = &[&4,&3,&2,&1,&0,&6,&7,&8];
910 assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b);
Jed Estep096fb792013-06-26 14:04:44911 }
blake2-ppc08dc72f2013-07-06 03:42:45912
913 #[test]
Niko Matsakisbc4164d2013-11-16 22:29:39914 fn test_mut_rev_iter_wrap() {
915 let mut d = RingBuf::with_capacity(3);
Jonathan S03609e52014-04-21 04:59:12916 assert!(d.mut_iter().rev().next().is_none());
Niko Matsakisbc4164d2013-11-16 22:29:39917
Niko Matsakis9e3d0b02014-04-21 21:58:52918 d.push_back(1i);
Niko Matsakisbc4164d2013-11-16 22:29:39919 d.push_back(2);
920 d.push_back(3);
921 assert_eq!(d.pop_front(), Some(1));
922 d.push_back(4);
923
Jonathan S03609e52014-04-21 04:59:12924 assert_eq!(d.mut_iter().rev().map(|x| *x).collect::<Vec<int>>(),
Huon Wilson4b9a7a22014-04-05 05:45:42925 vec!(4, 3, 2));
Niko Matsakisbc4164d2013-11-16 22:29:39926 }
927
928 #[test]
blake2-ppcf88d5322013-07-06 03:42:45929 fn test_mut_iter() {
blake2-ppc70523712013-07-10 13:27:14930 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45931 assert!(d.mut_iter().next().is_none());
932
Daniel Micay100894552013-08-03 16:45:23933 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14934 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45935 }
936
Daniel Micay100894552013-08-03 16:45:23937 for (i, elt) in d.mut_iter().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45938 assert_eq!(*elt, 2 - i);
939 *elt = i;
940 }
941
942 {
943 let mut it = d.mut_iter();
944 assert_eq!(*it.next().unwrap(), 0);
945 assert_eq!(*it.next().unwrap(), 1);
946 assert_eq!(*it.next().unwrap(), 2);
947 assert!(it.next().is_none());
948 }
949 }
950
951 #[test]
952 fn test_mut_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14953 let mut d = RingBuf::new();
Jonathan S03609e52014-04-21 04:59:12954 assert!(d.mut_iter().rev().next().is_none());
blake2-ppcf88d5322013-07-06 03:42:45955
Daniel Micay100894552013-08-03 16:45:23956 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14957 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45958 }
959
Jonathan S03609e52014-04-21 04:59:12960 for (i, elt) in d.mut_iter().rev().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45961 assert_eq!(*elt, i);
962 *elt = i;
963 }
964
965 {
Jonathan S03609e52014-04-21 04:59:12966 let mut it = d.mut_iter().rev();
blake2-ppcf88d5322013-07-06 03:42:45967 assert_eq!(*it.next().unwrap(), 0);
968 assert_eq!(*it.next().unwrap(), 1);
969 assert_eq!(*it.next().unwrap(), 2);
970 assert!(it.next().is_none());
971 }
972 }
973
974 #[test]
Brian Andersonee052192014-03-31 04:45:55975 fn test_from_iter() {
Daniel Micay6919cf52013-09-08 15:01:16976 use std::iter;
Niko Matsakis9e3d0b02014-04-21 21:58:52977 let v = vec!(1i,2,3,4,5,6,7);
Erick Tryzelaar68f40d22013-08-10 03:09:47978 let deq: RingBuf<int> = v.iter().map(|&x| x).collect();
Huon Wilson4b9a7a22014-04-05 05:45:42979 let u: Vec<int> = deq.iter().map(|&x| x).collect();
blake2-ppc08dc72f2013-07-06 03:42:45980 assert_eq!(u, v);
981
Daniel Micay6919cf52013-09-08 15:01:16982 let mut seq = iter::count(0u, 2).take(256);
blake2-ppc70523712013-07-10 13:27:14983 let deq: RingBuf<uint> = seq.collect();
Daniel Micay100894552013-08-03 16:45:23984 for (i, &x) in deq.iter().enumerate() {
blake2-ppc08dc72f2013-07-06 03:42:45985 assert_eq!(2*i, x);
986 }
987 assert_eq!(deq.len(), 256);
988 }
blake2-ppc10c76982013-07-06 13:27:32989
990 #[test]
991 fn test_clone() {
blake2-ppc70523712013-07-10 13:27:14992 let mut d = RingBuf::new();
Niko Matsakis9e3d0b02014-04-21 21:58:52993 d.push_front(17i);
blake2-ppc70523712013-07-10 13:27:14994 d.push_front(42);
995 d.push_back(137);
996 d.push_back(137);
blake2-ppc10c76982013-07-06 13:27:32997 assert_eq!(d.len(), 4u);
998 let mut e = d.clone();
999 assert_eq!(e.len(), 4u);
1000 while !d.is_empty() {
1001 assert_eq!(d.pop_back(), e.pop_back());
1002 }
1003 assert_eq!(d.len(), 0u);
1004 assert_eq!(e.len(), 0u);
1005 }
1006
1007 #[test]
1008 fn test_eq() {
blake2-ppc70523712013-07-10 13:27:141009 let mut d = RingBuf::new();
Alex Crichton02882fb2014-02-28 09:23:061010 assert!(d == RingBuf::with_capacity(0));
Niko Matsakis9e3d0b02014-04-21 21:58:521011 d.push_front(137i);
blake2-ppc70523712013-07-10 13:27:141012 d.push_front(17);
1013 d.push_front(42);
1014 d.push_back(137);
1015 let mut e = RingBuf::with_capacity(0);
1016 e.push_back(42);
1017 e.push_back(17);
1018 e.push_back(137);
1019 e.push_back(137);
Alex Crichton02882fb2014-02-28 09:23:061020 assert!(&e == &d);
blake2-ppc10c76982013-07-06 13:27:321021 e.pop_back();
blake2-ppc70523712013-07-10 13:27:141022 e.push_back(0);
blake2-ppc10c76982013-07-06 13:27:321023 assert!(e != d);
1024 e.clear();
Alex Crichton02882fb2014-02-28 09:23:061025 assert!(e == RingBuf::new());
blake2-ppc10c76982013-07-06 13:27:321026 }
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041027
1028 #[test]
nham1cfa6562014-07-27 02:33:471029 fn test_hash() {
1030 let mut x = RingBuf::new();
1031 let mut y = RingBuf::new();
1032
1033 x.push(1i);
1034 x.push(2);
1035 x.push(3);
1036
1037 y.push(0i);
1038 y.push(1i);
1039 y.pop_front();
1040 y.push(2);
1041 y.push(3);
1042
1043 assert!(hash::hash(&x) == hash::hash(&y));
1044 }
1045
1046 #[test]
nham63615772014-07-27 03:18:561047 fn test_ord() {
1048 let x = RingBuf::new();
1049 let mut y = RingBuf::new();
1050 y.push(1i);
1051 y.push(2);
1052 y.push(3);
1053 assert!(x < y);
1054 assert!(y > x);
1055 assert!(x <= x);
1056 assert!(x >= x);
1057 }
1058
1059 #[test]
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041060 fn test_show() {
Niko Matsakis9e3d0b02014-04-21 21:58:521061 let ringbuf: RingBuf<int> = range(0i, 10).collect();
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041062 assert!(format!("{}", ringbuf).as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1063
1064 let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
1065 .map(|&s| s)
1066 .collect();
1067 assert!(format!("{}", ringbuf).as_slice() == "[just, one, test, more]");
1068 }
Michael Sullivanc854d6e2012-07-03 17:52:321069}