blob: d2cfb510bc058ee0da306fff53f7f50934bfab0f [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);
246 /// assert_eq!(buf.iter().collect::<Vec<&int>>().as_slice(), &[&5, &3, &4]);
247 /// ```
Palmer Cox3fd8c8b2014-01-15 03:32:24248 pub fn iter<'a>(&'a self) -> Items<'a, T> {
Huon Wilson4b9a7a22014-04-05 05:45:42249 Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()}
blake2-ppc3385e792013-07-15 23:13:26250 }
251
P1startf2aa88c2014-08-04 10:48:39252 /// Returns a front-to-back iterator which returns mutable references.
nhamebe80972014-07-17 23:19:51253 ///
254 /// # Example
255 ///
256 /// ```rust
257 /// use std::collections::RingBuf;
258 ///
259 /// let mut buf = RingBuf::new();
260 /// buf.push(5i);
261 /// buf.push(3);
262 /// buf.push(4);
263 /// for num in buf.mut_iter() {
264 /// *num = *num - 2;
265 /// }
266 /// assert_eq!(buf.mut_iter().collect::<Vec<&mut int>>().as_slice(), &[&mut 3, &mut 1, &mut 2]);
267 /// ```
Palmer Cox3fd8c8b2014-01-15 03:32:24268 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39269 let start_index = raw_index(self.lo, self.elts.len(), 0);
270 let end_index = raw_index(self.lo, self.elts.len(), self.nelts);
271
272 // Divide up the array
273 if end_index <= start_index {
274 // Items to iterate goes from:
275 // start_index to self.elts.len()
276 // and then
277 // 0 to end_index
Guillaume Pinot25bb1a42013-12-01 17:19:39278 let (temp, remaining1) = self.elts.mut_split_at(start_index);
279 let (remaining2, _) = temp.mut_split_at(end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24280 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39281 remaining2: remaining2,
282 nelts: self.nelts }
283 } else {
284 // Items to iterate goes from start_index to end_index:
Guillaume Pinot25bb1a42013-12-01 17:19:39285 let (empty, elts) = self.elts.mut_split_at(0);
Niko Matsakisbc4164d2013-11-16 22:29:39286 let remaining1 = elts.mut_slice(start_index, end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24287 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39288 remaining2: empty,
289 nelts: self.nelts }
290 }
Jed Estep4f7a7422013-06-25 19:08:47291 }
Jed Estep4f7a7422013-06-25 19:08:47292}
293
P1startf2aa88c2014-08-04 10:48:39294/// `RingBuf` iterator.
Palmer Cox3fd8c8b2014-01-15 03:32:24295pub struct Items<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04296 lo: uint,
297 index: uint,
298 rindex: uint,
299 elts: &'a [Option<T>],
Jed Estep4f7a7422013-06-25 19:08:47300}
Niko Matsakisbc4164d2013-11-16 22:29:39301
Palmer Cox3fd8c8b2014-01-15 03:32:24302impl<'a, T> Iterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39303 #[inline]
Erik Price5731ca32013-12-10 07:16:18304 fn next(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39305 if self.index == self.rindex {
306 return None;
307 }
308 let raw_index = raw_index(self.lo, self.elts.len(), self.index);
309 self.index += 1;
310 Some(self.elts[raw_index].get_ref())
311 }
312
313 #[inline]
314 fn size_hint(&self) -> (uint, Option<uint>) {
315 let len = self.rindex - self.index;
316 (len, Some(len))
317 }
318}
319
Palmer Cox3fd8c8b2014-01-15 03:32:24320impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39321 #[inline]
Erik Price5731ca32013-12-10 07:16:18322 fn next_back(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39323 if self.index == self.rindex {
324 return None;
325 }
326 self.rindex -= 1;
327 let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
328 Some(self.elts[raw_index].get_ref())
329 }
330}
Jed Estep35314c92013-06-26 15:38:29331
Palmer Cox3fd8c8b2014-01-15 03:32:24332impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24333
Palmer Cox3fd8c8b2014-01-15 03:32:24334impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> {
blake2-ppcf6862132013-07-29 18:16:26335 #[inline]
336 fn indexable(&self) -> uint { self.rindex - self.index }
337
338 #[inline]
Alex Crichtonf4083a22014-04-22 05:15:42339 fn idx(&mut self, j: uint) -> Option<&'a T> {
blake2-ppcf6862132013-07-29 18:16:26340 if j >= self.indexable() {
341 None
342 } else {
343 let raw_index = raw_index(self.lo, self.elts.len(), self.index + j);
344 Some(self.elts[raw_index].get_ref())
345 }
346 }
347}
348
P1startf2aa88c2014-08-04 10:48:39349/// `RingBuf` mutable iterator.
Palmer Cox3fd8c8b2014-01-15 03:32:24350pub struct MutItems<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04351 remaining1: &'a mut [Option<T>],
352 remaining2: &'a mut [Option<T>],
353 nelts: uint,
Jed Estep4f7a7422013-06-25 19:08:47354}
Niko Matsakisbc4164d2013-11-16 22:29:39355
Palmer Cox3fd8c8b2014-01-15 03:32:24356impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39357 #[inline]
Brian Anderson4e1024f2014-08-07 06:59:12358 #[allow(deprecated)] // mut_shift_ref
Erik Price5731ca32013-12-10 07:16:18359 fn next(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39360 if self.nelts == 0 {
361 return None;
362 }
363 let r = if self.remaining1.len() > 0 {
364 &mut self.remaining1
365 } else {
366 assert!(self.remaining2.len() > 0);
367 &mut self.remaining2
368 };
369 self.nelts -= 1;
Nathaniel Hermand451c152014-01-25 17:00:46370 Some(r.mut_shift_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39371 }
372
373 #[inline]
374 fn size_hint(&self) -> (uint, Option<uint>) {
375 (self.nelts, Some(self.nelts))
376 }
377}
378
Palmer Cox3fd8c8b2014-01-15 03:32:24379impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39380 #[inline]
Brian Anderson4e1024f2014-08-07 06:59:12381 #[allow(deprecated)] // mut_shift_ref
Erik Price5731ca32013-12-10 07:16:18382 fn next_back(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39383 if self.nelts == 0 {
384 return None;
385 }
386 let r = if self.remaining2.len() > 0 {
387 &mut self.remaining2
388 } else {
389 assert!(self.remaining1.len() > 0);
390 &mut self.remaining1
391 };
392 self.nelts -= 1;
Nathaniel Herman33960342014-01-25 20:33:31393 Some(r.mut_pop_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39394 }
395}
Daniel Micayb47e1e92013-02-16 22:55:55396
Palmer Cox3fd8c8b2014-01-15 03:32:24397impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24398
Daniel Micayb47e1e92013-02-16 22:55:55399/// Grow is only called on full elts, so nelts is also len(elts), unlike
400/// elsewhere.
Huon Wilson4b9a7a22014-04-05 05:45:42401fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut Vec<Option<T>>) {
Corey Richardsoncc57ca02013-05-19 02:02:45402 assert_eq!(nelts, elts.len());
blake2-ppc8a326762013-07-06 03:42:45403 let lo = *loptr;
Kevin Butler64896d62014-08-07 01:11:13404 elts.reserve(nelts * 2);
405 let newlen = elts.capacity();
Daniel Micayb47e1e92013-02-16 22:55:55406
blake2-ppc40ce0b72013-07-06 03:42:45407 /* fill with None */
Kevin Butler64896d62014-08-07 01:11:13408 for _ in range(elts.len(), newlen) {
blake2-ppc40ce0b72013-07-06 03:42:45409 elts.push(None);
Daniel Micayb47e1e92013-02-16 22:55:55410 }
blake2-ppc8a326762013-07-06 03:42:45411
412 /*
413 Move the shortest half into the newly reserved area.
414 lo ---->|
415 nelts ----------->|
416 [o o o|o o o o o]
417 A [. . .|o o o o o o o o|. . . . .]
418 B [o o o|. . . . . . . .|o o o o o]
419 */
420
421 assert!(newlen - nelts/2 >= nelts);
422 if lo <= (nelts - lo) { // A
Daniel Micay100894552013-08-03 16:45:23423 for i in range(0u, lo) {
Huon Wilson4b9a7a22014-04-05 05:45:42424 elts.as_mut_slice().swap(i, nelts + i);
blake2-ppc8a326762013-07-06 03:42:45425 }
426 } else { // B
Daniel Micay100894552013-08-03 16:45:23427 for i in range(lo, nelts) {
Huon Wilson4b9a7a22014-04-05 05:45:42428 elts.as_mut_slice().swap(i, newlen - nelts + i);
blake2-ppc8a326762013-07-06 03:42:45429 }
430 *loptr += newlen - nelts;
blake2-ppc40ce0b72013-07-06 03:42:45431 }
Daniel Micayb47e1e92013-02-16 22:55:55432}
433
P1startf2aa88c2014-08-04 10:48:39434/// Returns the index in the underlying `Vec` for a given logical element index.
blake2-ppc75015c32013-07-06 03:42:45435fn raw_index(lo: uint, len: uint, index: uint) -> uint {
436 if lo >= len - index {
437 lo + index - len
438 } else {
439 lo + index
440 }
441}
442
Alex Crichton748bc3c2014-05-30 00:45:07443impl<A: PartialEq> PartialEq for RingBuf<A> {
blake2-ppc70523712013-07-10 13:27:14444 fn eq(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32445 self.nelts == other.nelts &&
446 self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
447 }
blake2-ppc70523712013-07-10 13:27:14448 fn ne(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32449 !self.eq(other)
450 }
451}
452
nham25acfde2014-08-01 20:05:03453impl<A: Eq> Eq for RingBuf<A> {}
454
nham63615772014-07-27 03:18:56455impl<A: PartialOrd> PartialOrd for RingBuf<A> {
456 fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
457 iter::order::partial_cmp(self.iter(), other.iter())
458 }
459}
460
nham3737c532014-08-01 20:22:48461impl<A: Ord> Ord for RingBuf<A> {
462 #[inline]
463 fn cmp(&self, other: &RingBuf<A>) -> Ordering {
464 iter::order::cmp(self.iter(), other.iter())
465 }
466}
467
nham1cfa6562014-07-27 02:33:47468impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
469 fn hash(&self, state: &mut S) {
nham9fa44242014-07-27 16:37:32470 self.len().hash(state);
nham1cfa6562014-07-27 02:33:47471 for elt in self.iter() {
472 elt.hash(state);
473 }
474 }
475}
476
P1startfd10d202014-08-02 06:39:39477impl<A> Index<uint, A> for RingBuf<A> {
478 #[inline]
Aaron Turonf77cabe2014-08-11 23:47:46479 #[allow(deprecated)]
P1startfd10d202014-08-02 06:39:39480 fn index<'a>(&'a self, i: &uint) -> &'a A {
481 self.get(*i)
482 }
483}
484
485// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
486/*impl<A> IndexMut<uint, A> for RingBuf<A> {
487 #[inline]
488 fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut A {
489 self.get_mut(*index)
490 }
491}*/
492
Huon Wilson53487a02013-08-13 13:08:14493impl<A> FromIterator<A> for RingBuf<A> {
Brian Andersonee052192014-03-31 04:45:55494 fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
blake2-ppcf8ae5262013-07-30 00:06:49495 let (lower, _) = iterator.size_hint();
496 let mut deq = RingBuf::with_capacity(lower);
497 deq.extend(iterator);
blake2-ppc08dc72f2013-07-06 03:42:45498 deq
499 }
500}
501
Huon Wilson53487a02013-08-13 13:08:14502impl<A> Extendable<A> for RingBuf<A> {
Marvin Löbel6200e762014-03-20 13:12:56503 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
504 for elt in iterator {
Aaron Turonf77cabe2014-08-11 23:47:46505 self.push(elt);
blake2-ppcf8ae5262013-07-30 00:06:49506 }
507 }
508}
509
Alex Crichton6a585372014-05-30 01:50:12510impl<T: fmt::Show> fmt::Show for RingBuf<T> {
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:04511 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
512 try!(write!(f, "["));
513
514 for (i, e) in self.iter().enumerate() {
515 if i != 0 { try!(write!(f, ", ")); }
516 try!(write!(f, "{}", *e));
517 }
518
519 write!(f, "]")
520 }
521}
522
Brian Anderson6e27b272012-01-18 03:05:07523#[cfg(test)]
524mod tests {
Alex Crichton02882fb2014-02-28 09:23:06525 use std::fmt::Show;
Alex Crichton760b93a2014-05-30 02:03:06526 use std::prelude::*;
Alex Crichtonade807c2014-06-12 02:33:52527 use std::gc::{GC, Gc};
nham1cfa6562014-07-27 02:33:47528 use std::hash;
Alex Crichton760b93a2014-05-30 02:03:06529 use test::Bencher;
530 use test;
531
Brian Anderson054b1ff2014-07-14 21:45:10532 use {Deque, Mutable, MutableSeq};
Alex Crichtonf47e4b22014-01-07 06:33:50533 use super::RingBuf;
Alex Crichton760b93a2014-05-30 02:03:06534 use vec::Vec;
Patrick Waltonfa5ee932012-12-28 02:24:18535
Brian Anderson6e27b272012-01-18 03:05:07536 #[test]
537 fn test_simple() {
blake2-ppc70523712013-07-10 13:27:14538 let mut d = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45539 assert_eq!(d.len(), 0u);
Niko Matsakis9e3d0b02014-04-21 21:58:52540 d.push_front(17i);
541 d.push_front(42i);
blake2-ppc70523712013-07-10 13:27:14542 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45543 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14544 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45545 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31546 debug!("{:?}", d.front());
blake2-ppc70523712013-07-10 13:27:14547 assert_eq!(*d.front().unwrap(), 42);
Alex Crichtondaf5f5a2013-10-21 20:08:31548 debug!("{:?}", d.back());
blake2-ppc70523712013-07-10 13:27:14549 assert_eq!(*d.back().unwrap(), 137);
550 let mut i = d.pop_front();
Alex Crichtondaf5f5a2013-10-21 20:08:31551 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14552 assert_eq!(i, Some(42));
Brian Anderson6e27b272012-01-18 03:05:07553 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31554 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14555 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07556 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31557 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14558 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07559 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31560 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14561 assert_eq!(i, Some(17));
Corey Richardsoncc57ca02013-05-19 02:02:45562 assert_eq!(d.len(), 0u);
blake2-ppc70523712013-07-10 13:27:14563 d.push_back(3);
Corey Richardsoncc57ca02013-05-19 02:02:45564 assert_eq!(d.len(), 1u);
blake2-ppc70523712013-07-10 13:27:14565 d.push_front(2);
Corey Richardsoncc57ca02013-05-19 02:02:45566 assert_eq!(d.len(), 2u);
blake2-ppc70523712013-07-10 13:27:14567 d.push_back(4);
Corey Richardsoncc57ca02013-05-19 02:02:45568 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14569 d.push_front(1);
Corey Richardsoncc57ca02013-05-19 02:02:45570 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31571 debug!("{:?}", d.get(0));
572 debug!("{:?}", d.get(1));
573 debug!("{:?}", d.get(2));
574 debug!("{:?}", d.get(3));
Corey Richardsoncc57ca02013-05-19 02:02:45575 assert_eq!(*d.get(0), 1);
576 assert_eq!(*d.get(1), 2);
577 assert_eq!(*d.get(2), 3);
578 assert_eq!(*d.get(3), 4);
Brian Anderson6e27b272012-01-18 03:05:07579 }
580
Kevin Cantuc43426e2012-09-13 05:09:55581 #[test]
582 fn test_boxes() {
Alex Crichtonade807c2014-06-12 02:33:52583 let a: Gc<int> = box(GC) 5;
584 let b: Gc<int> = box(GC) 72;
585 let c: Gc<int> = box(GC) 64;
586 let d: Gc<int> = box(GC) 175;
Kevin Cantuc43426e2012-09-13 05:09:55587
blake2-ppc70523712013-07-10 13:27:14588 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45589 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14590 deq.push_front(a);
591 deq.push_front(b);
592 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45593 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14594 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45595 assert_eq!(deq.len(), 4);
blake2-ppc70523712013-07-10 13:27:14596 assert_eq!(deq.front(), Some(&b));
597 assert_eq!(deq.back(), Some(&d));
598 assert_eq!(deq.pop_front(), Some(b));
599 assert_eq!(deq.pop_back(), Some(d));
600 assert_eq!(deq.pop_back(), Some(c));
601 assert_eq!(deq.pop_back(), Some(a));
Corey Richardsoncc57ca02013-05-19 02:02:45602 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14603 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45604 assert_eq!(deq.len(), 1);
blake2-ppc70523712013-07-10 13:27:14605 deq.push_front(b);
Corey Richardsoncc57ca02013-05-19 02:02:45606 assert_eq!(deq.len(), 2);
blake2-ppc70523712013-07-10 13:27:14607 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45608 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14609 deq.push_front(a);
Corey Richardsoncc57ca02013-05-19 02:02:45610 assert_eq!(deq.len(), 4);
611 assert_eq!(*deq.get(0), a);
612 assert_eq!(*deq.get(1), b);
613 assert_eq!(*deq.get(2), c);
614 assert_eq!(*deq.get(3), d);
Daniel Micay373c0722013-02-17 00:10:10615 }
616
Felix S. Klock IIa636f512013-05-01 23:32:37617 #[cfg(test)]
Alex Crichton748bc3c2014-05-30 00:45:07618 fn test_parameterized<T:Clone + PartialEq + Show>(a: T, b: T, c: T, d: T) {
Patrick Waltondc4bf172013-07-13 04:05:59619 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45620 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59621 deq.push_front(a.clone());
622 deq.push_front(b.clone());
623 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45624 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59625 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45626 assert_eq!(deq.len(), 4);
Marvin Löbel0ac7a212013-08-03 23:59:24627 assert_eq!((*deq.front().unwrap()).clone(), b.clone());
628 assert_eq!((*deq.back().unwrap()).clone(), d.clone());
629 assert_eq!(deq.pop_front().unwrap(), b.clone());
630 assert_eq!(deq.pop_back().unwrap(), d.clone());
631 assert_eq!(deq.pop_back().unwrap(), c.clone());
632 assert_eq!(deq.pop_back().unwrap(), a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45633 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59634 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45635 assert_eq!(deq.len(), 1);
Patrick Waltondc4bf172013-07-13 04:05:59636 deq.push_front(b.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45637 assert_eq!(deq.len(), 2);
Patrick Waltondc4bf172013-07-13 04:05:59638 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45639 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59640 deq.push_front(a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45641 assert_eq!(deq.len(), 4);
Patrick Walton99b33f72013-07-02 19:47:32642 assert_eq!((*deq.get(0)).clone(), a.clone());
643 assert_eq!((*deq.get(1)).clone(), b.clone());
644 assert_eq!((*deq.get(2)).clone(), c.clone());
645 assert_eq!((*deq.get(3)).clone(), d.clone());
Brian Anderson6e27b272012-01-18 03:05:07646 }
647
blake2-ppc81933ed2013-07-06 03:42:45648 #[test]
blake2-ppc70523712013-07-10 13:27:14649 fn test_push_front_grow() {
650 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23651 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14652 deq.push_front(i);
blake2-ppc81933ed2013-07-06 03:42:45653 }
654 assert_eq!(deq.len(), 66);
655
Daniel Micay100894552013-08-03 16:45:23656 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45657 assert_eq!(*deq.get(i), 65 - i);
658 }
659
blake2-ppc70523712013-07-10 13:27:14660 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23661 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14662 deq.push_back(i);
blake2-ppc81933ed2013-07-06 03:42:45663 }
664
Daniel Micay100894552013-08-03 16:45:23665 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45666 assert_eq!(*deq.get(i), i);
667 }
668 }
669
P1startfd10d202014-08-02 06:39:39670 #[test]
671 fn test_index() {
672 let mut deq = RingBuf::new();
673 for i in range(1u, 4) {
674 deq.push_front(i);
675 }
676 assert_eq!(deq[1], 2);
677 }
678
679 #[test]
680 #[should_fail]
681 fn test_index_out_of_bounds() {
682 let mut deq = RingBuf::new();
683 for i in range(1u, 4) {
684 deq.push_front(i);
685 }
686 deq[3];
687 }
688
blake2-ppc81933ed2013-07-06 03:42:45689 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35690 fn bench_new(b: &mut test::Bencher) {
Patrick Walton38efa172013-11-22 03:20:48691 b.iter(|| {
Patrick Walton86939432013-08-08 18:38:10692 let _: RingBuf<u64> = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48693 })
blake2-ppc81933ed2013-07-06 03:42:45694 }
695
696 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35697 fn bench_push_back(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14698 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48699 b.iter(|| {
Patrick Waltona5bb0a32014-06-27 19:30:25700 deq.push_back(0i);
Patrick Walton38efa172013-11-22 03:20:48701 })
blake2-ppc81933ed2013-07-06 03:42:45702 }
703
704 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35705 fn bench_push_front(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14706 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48707 b.iter(|| {
Patrick Waltona5bb0a32014-06-27 19:30:25708 deq.push_front(0i);
Patrick Walton38efa172013-11-22 03:20:48709 })
blake2-ppc81933ed2013-07-06 03:42:45710 }
711
712 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35713 fn bench_grow(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14714 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48715 b.iter(|| {
Niko Matsakis9e3d0b02014-04-21 21:58:52716 for _ in range(0i, 65) {
Patrick Waltona5bb0a32014-06-27 19:30:25717 deq.push_front(1i);
Brendan Zabarauskas729060d2014-01-30 00:20:34718 }
Patrick Walton38efa172013-11-22 03:20:48719 })
blake2-ppc81933ed2013-07-06 03:42:45720 }
721
Alex Crichton748bc3c2014-05-30 00:45:07722 #[deriving(Clone, PartialEq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32723 enum Taggy {
724 One(int),
725 Two(int, int),
726 Three(int, int, int),
Brian Anderson6e27b272012-01-18 03:05:07727 }
728
Alex Crichton748bc3c2014-05-30 00:45:07729 #[deriving(Clone, PartialEq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32730 enum Taggypar<T> {
731 Onepar(int),
732 Twopar(int, int),
733 Threepar(int, int, int),
734 }
735
Alex Crichton748bc3c2014-05-30 00:45:07736 #[deriving(Clone, PartialEq, Show)]
Erick Tryzelaare84576b2013-01-22 16:44:24737 struct RecCy {
738 x: int,
739 y: int,
Patrick Waltoneb4d39e2013-01-26 00:57:39740 t: Taggy
Patrick Walton9117dcb2012-09-20 01:00:26741 }
Kevin Cantuc43426e2012-09-13 05:09:55742
743 #[test]
744 fn test_param_int() {
745 test_parameterized::<int>(5, 72, 64, 175);
746 }
747
748 #[test]
749 fn test_param_at_int() {
Alex Crichtonade807c2014-06-12 02:33:52750 test_parameterized::<Gc<int>>(box(GC) 5, box(GC) 72,
751 box(GC) 64, box(GC) 175);
Kevin Cantuc43426e2012-09-13 05:09:55752 }
753
754 #[test]
755 fn test_param_taggy() {
Corey Richardsonf8ae9b02013-06-26 22:14:35756 test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55757 }
758
759 #[test]
760 fn test_param_taggypar() {
761 test_parameterized::<Taggypar<int>>(Onepar::<int>(1),
Ben Striegela605fd02012-08-11 14:08:42762 Twopar::<int>(1, 2),
763 Threepar::<int>(1, 2, 3),
764 Twopar::<int>(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55765 }
Brian Anderson6e27b272012-01-18 03:05:07766
Kevin Cantuc43426e2012-09-13 05:09:55767 #[test]
768 fn test_param_reccy() {
Erick Tryzelaare84576b2013-01-22 16:44:24769 let reccy1 = RecCy { x: 1, y: 2, t: One(1) };
770 let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) };
771 let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) };
772 let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
Kevin Cantuc43426e2012-09-13 05:09:55773 test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
Brian Anderson6e27b272012-01-18 03:05:07774 }
Erick Tryzelaar909d8f02013-03-30 01:02:44775
776 #[test]
blake2-ppc0ff5c172013-07-06 03:42:45777 fn test_with_capacity() {
blake2-ppc70523712013-07-10 13:27:14778 let mut d = RingBuf::with_capacity(0);
Patrick Waltona5bb0a32014-06-27 19:30:25779 d.push_back(1i);
blake2-ppc0ff5c172013-07-06 03:42:45780 assert_eq!(d.len(), 1);
blake2-ppc70523712013-07-10 13:27:14781 let mut d = RingBuf::with_capacity(50);
Patrick Waltona5bb0a32014-06-27 19:30:25782 d.push_back(1i);
blake2-ppc0ff5c172013-07-06 03:42:45783 assert_eq!(d.len(), 1);
784 }
785
786 #[test]
Kevin Butler64896d62014-08-07 01:11:13787 fn test_with_capacity_non_power_two() {
788 let mut d3 = RingBuf::with_capacity(3);
789 d3.push(1i);
790
791 // X = None, | = lo
792 // [|1, X, X]
793 assert_eq!(d3.pop_front(), Some(1));
794 // [X, |X, X]
795 assert_eq!(d3.front(), None);
796
797 // [X, |3, X]
798 d3.push(3);
799 // [X, |3, 6]
800 d3.push(6);
801 // [X, X, |6]
802 assert_eq!(d3.pop_front(), Some(3));
803
804 // Pushing the lo past half way point to trigger
805 // the 'B' scenario for growth
806 // [9, X, |6]
807 d3.push(9);
808 // [9, 12, |6]
809 d3.push(12);
810
811 d3.push(15);
812 // There used to be a bug here about how the
813 // RingBuf made growth assumptions about the
814 // underlying Vec which didn't hold and lead
815 // to corruption.
816 // (Vec grows to next power of two)
817 //good- [9, 12, 15, X, X, X, X, |6]
818 //bug- [15, 12, X, X, X, |6, X, X]
819 assert_eq!(d3.pop_front(), Some(6));
820
821 // Which leads us to the following state which
822 // would be a failure case.
823 //bug- [15, 12, X, X, X, X, |X, X]
824 assert_eq!(d3.front(), Some(&9));
825 }
826
827 #[test]
David Manescu65f35782014-01-31 13:03:20828 fn test_reserve_exact() {
blake2-ppc70523712013-07-10 13:27:14829 let mut d = RingBuf::new();
830 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20831 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47832 assert_eq!(d.elts.capacity(), 50);
blake2-ppc70523712013-07-10 13:27:14833 let mut d = RingBuf::new();
834 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20835 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47836 assert_eq!(d.elts.capacity(), 50);
Tim Chevalier77de84b2013-05-27 18:47:38837 }
838
839 #[test]
David Manescu65f35782014-01-31 13:03:20840 fn test_reserve() {
blake2-ppc70523712013-07-10 13:27:14841 let mut d = RingBuf::new();
842 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20843 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47844 assert_eq!(d.elts.capacity(), 64);
blake2-ppc70523712013-07-10 13:27:14845 let mut d = RingBuf::new();
846 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20847 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47848 assert_eq!(d.elts.capacity(), 64);
Tim Chevalier77de84b2013-05-27 18:47:38849 }
850
Jed Estep096fb792013-06-26 14:04:44851 #[test]
blake2-ppc57757a82013-09-26 07:19:26852 fn test_swap() {
Niko Matsakis9e3d0b02014-04-21 21:58:52853 let mut d: RingBuf<int> = range(0i, 5).collect();
blake2-ppc57757a82013-09-26 07:19:26854 d.pop_front();
855 d.swap(0, 3);
Huon Wilson4b9a7a22014-04-05 05:45:42856 assert_eq!(d.iter().map(|&x|x).collect::<Vec<int>>(), vec!(4, 2, 3, 1));
blake2-ppc57757a82013-09-26 07:19:26857 }
858
859 #[test]
Jed Estep096fb792013-06-26 14:04:44860 fn test_iter() {
blake2-ppc70523712013-07-10 13:27:14861 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45862 assert_eq!(d.iter().next(), None);
blake2-ppc9ccf4432013-07-14 20:30:22863 assert_eq!(d.iter().size_hint(), (0, Some(0)));
blake2-ppcf88d5322013-07-06 03:42:45864
Niko Matsakis9e3d0b02014-04-21 21:58:52865 for i in range(0i, 5) {
blake2-ppc70523712013-07-10 13:27:14866 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44867 }
Nick Cameron37a94b82014-08-04 12:19:02868 {
869 let b: &[_] = &[&0,&1,&2,&3,&4];
870 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b);
871 }
Corey Richardsonf8ae9b02013-06-26 22:14:35872
Niko Matsakis9e3d0b02014-04-21 21:58:52873 for i in range(6i, 9) {
blake2-ppc70523712013-07-10 13:27:14874 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44875 }
Nick Cameron37a94b82014-08-04 12:19:02876 {
877 let b: &[_] = &[&8,&7,&6,&0,&1,&2,&3,&4];
878 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b);
879 }
blake2-ppc9ccf4432013-07-14 20:30:22880
881 let mut it = d.iter();
882 let mut len = d.len();
883 loop {
884 match it.next() {
885 None => break,
886 _ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) }
887 }
888 }
Jed Estep096fb792013-06-26 14:04:44889 }
890
891 #[test]
892 fn test_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14893 let mut d = RingBuf::new();
Jonathan S03609e52014-04-21 04:59:12894 assert_eq!(d.iter().rev().next(), None);
blake2-ppcf88d5322013-07-06 03:42:45895
Niko Matsakis9e3d0b02014-04-21 21:58:52896 for i in range(0i, 5) {
blake2-ppc70523712013-07-10 13:27:14897 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44898 }
Nick Cameron37a94b82014-08-04 12:19:02899 {
900 let b: &[_] = &[&4,&3,&2,&1,&0];
901 assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b);
902 }
Corey Richardsonf8ae9b02013-06-26 22:14:35903
Niko Matsakis9e3d0b02014-04-21 21:58:52904 for i in range(6i, 9) {
blake2-ppc70523712013-07-10 13:27:14905 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44906 }
Nick Cameron37a94b82014-08-04 12:19:02907 let b: &[_] = &[&4,&3,&2,&1,&0,&6,&7,&8];
908 assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b);
Jed Estep096fb792013-06-26 14:04:44909 }
blake2-ppc08dc72f2013-07-06 03:42:45910
911 #[test]
Niko Matsakisbc4164d2013-11-16 22:29:39912 fn test_mut_rev_iter_wrap() {
913 let mut d = RingBuf::with_capacity(3);
Jonathan S03609e52014-04-21 04:59:12914 assert!(d.mut_iter().rev().next().is_none());
Niko Matsakisbc4164d2013-11-16 22:29:39915
Niko Matsakis9e3d0b02014-04-21 21:58:52916 d.push_back(1i);
Niko Matsakisbc4164d2013-11-16 22:29:39917 d.push_back(2);
918 d.push_back(3);
919 assert_eq!(d.pop_front(), Some(1));
920 d.push_back(4);
921
Jonathan S03609e52014-04-21 04:59:12922 assert_eq!(d.mut_iter().rev().map(|x| *x).collect::<Vec<int>>(),
Huon Wilson4b9a7a22014-04-05 05:45:42923 vec!(4, 3, 2));
Niko Matsakisbc4164d2013-11-16 22:29:39924 }
925
926 #[test]
blake2-ppcf88d5322013-07-06 03:42:45927 fn test_mut_iter() {
blake2-ppc70523712013-07-10 13:27:14928 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45929 assert!(d.mut_iter().next().is_none());
930
Daniel Micay100894552013-08-03 16:45:23931 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14932 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45933 }
934
Daniel Micay100894552013-08-03 16:45:23935 for (i, elt) in d.mut_iter().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45936 assert_eq!(*elt, 2 - i);
937 *elt = i;
938 }
939
940 {
941 let mut it = d.mut_iter();
942 assert_eq!(*it.next().unwrap(), 0);
943 assert_eq!(*it.next().unwrap(), 1);
944 assert_eq!(*it.next().unwrap(), 2);
945 assert!(it.next().is_none());
946 }
947 }
948
949 #[test]
950 fn test_mut_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14951 let mut d = RingBuf::new();
Jonathan S03609e52014-04-21 04:59:12952 assert!(d.mut_iter().rev().next().is_none());
blake2-ppcf88d5322013-07-06 03:42:45953
Daniel Micay100894552013-08-03 16:45:23954 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14955 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45956 }
957
Jonathan S03609e52014-04-21 04:59:12958 for (i, elt) in d.mut_iter().rev().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45959 assert_eq!(*elt, i);
960 *elt = i;
961 }
962
963 {
Jonathan S03609e52014-04-21 04:59:12964 let mut it = d.mut_iter().rev();
blake2-ppcf88d5322013-07-06 03:42:45965 assert_eq!(*it.next().unwrap(), 0);
966 assert_eq!(*it.next().unwrap(), 1);
967 assert_eq!(*it.next().unwrap(), 2);
968 assert!(it.next().is_none());
969 }
970 }
971
972 #[test]
Brian Andersonee052192014-03-31 04:45:55973 fn test_from_iter() {
Daniel Micay6919cf52013-09-08 15:01:16974 use std::iter;
Niko Matsakis9e3d0b02014-04-21 21:58:52975 let v = vec!(1i,2,3,4,5,6,7);
Erick Tryzelaar68f40d22013-08-10 03:09:47976 let deq: RingBuf<int> = v.iter().map(|&x| x).collect();
Huon Wilson4b9a7a22014-04-05 05:45:42977 let u: Vec<int> = deq.iter().map(|&x| x).collect();
blake2-ppc08dc72f2013-07-06 03:42:45978 assert_eq!(u, v);
979
Daniel Micay6919cf52013-09-08 15:01:16980 let mut seq = iter::count(0u, 2).take(256);
blake2-ppc70523712013-07-10 13:27:14981 let deq: RingBuf<uint> = seq.collect();
Daniel Micay100894552013-08-03 16:45:23982 for (i, &x) in deq.iter().enumerate() {
blake2-ppc08dc72f2013-07-06 03:42:45983 assert_eq!(2*i, x);
984 }
985 assert_eq!(deq.len(), 256);
986 }
blake2-ppc10c76982013-07-06 13:27:32987
988 #[test]
989 fn test_clone() {
blake2-ppc70523712013-07-10 13:27:14990 let mut d = RingBuf::new();
Niko Matsakis9e3d0b02014-04-21 21:58:52991 d.push_front(17i);
blake2-ppc70523712013-07-10 13:27:14992 d.push_front(42);
993 d.push_back(137);
994 d.push_back(137);
blake2-ppc10c76982013-07-06 13:27:32995 assert_eq!(d.len(), 4u);
996 let mut e = d.clone();
997 assert_eq!(e.len(), 4u);
998 while !d.is_empty() {
999 assert_eq!(d.pop_back(), e.pop_back());
1000 }
1001 assert_eq!(d.len(), 0u);
1002 assert_eq!(e.len(), 0u);
1003 }
1004
1005 #[test]
1006 fn test_eq() {
blake2-ppc70523712013-07-10 13:27:141007 let mut d = RingBuf::new();
Alex Crichton02882fb2014-02-28 09:23:061008 assert!(d == RingBuf::with_capacity(0));
Niko Matsakis9e3d0b02014-04-21 21:58:521009 d.push_front(137i);
blake2-ppc70523712013-07-10 13:27:141010 d.push_front(17);
1011 d.push_front(42);
1012 d.push_back(137);
1013 let mut e = RingBuf::with_capacity(0);
1014 e.push_back(42);
1015 e.push_back(17);
1016 e.push_back(137);
1017 e.push_back(137);
Alex Crichton02882fb2014-02-28 09:23:061018 assert!(&e == &d);
blake2-ppc10c76982013-07-06 13:27:321019 e.pop_back();
blake2-ppc70523712013-07-10 13:27:141020 e.push_back(0);
blake2-ppc10c76982013-07-06 13:27:321021 assert!(e != d);
1022 e.clear();
Alex Crichton02882fb2014-02-28 09:23:061023 assert!(e == RingBuf::new());
blake2-ppc10c76982013-07-06 13:27:321024 }
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041025
1026 #[test]
nham1cfa6562014-07-27 02:33:471027 fn test_hash() {
1028 let mut x = RingBuf::new();
1029 let mut y = RingBuf::new();
1030
1031 x.push(1i);
1032 x.push(2);
1033 x.push(3);
1034
1035 y.push(0i);
1036 y.push(1i);
1037 y.pop_front();
1038 y.push(2);
1039 y.push(3);
1040
1041 assert!(hash::hash(&x) == hash::hash(&y));
1042 }
1043
1044 #[test]
nham63615772014-07-27 03:18:561045 fn test_ord() {
1046 let x = RingBuf::new();
1047 let mut y = RingBuf::new();
1048 y.push(1i);
1049 y.push(2);
1050 y.push(3);
1051 assert!(x < y);
1052 assert!(y > x);
1053 assert!(x <= x);
1054 assert!(x >= x);
1055 }
1056
1057 #[test]
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041058 fn test_show() {
Niko Matsakis9e3d0b02014-04-21 21:58:521059 let ringbuf: RingBuf<int> = range(0i, 10).collect();
Adolfo Ochagavía8e4e3ab2014-06-04 14:15:041060 assert!(format!("{}", ringbuf).as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1061
1062 let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
1063 .map(|&s| s)
1064 .collect();
1065 assert!(format!("{}", ringbuf).as_slice() == "[just, one, test, more]");
1066 }
Michael Sullivanc854d6e2012-07-03 17:52:321067}