2013-02-16 16:55:55 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-03-21 18:14:02 -05:00
|
|
|
//! A double-ended queue implemented as a circular buffer
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::uint;
|
2013-05-05 23:42:54 -05:00
|
|
|
use core::util::replace;
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::vec;
|
2013-06-26 10:38:29 -05:00
|
|
|
use core::cast::transmute;
|
2013-05-05 23:42:54 -05:00
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
static initial_capacity: uint = 32u; // 2^5
|
2013-02-16 16:55:55 -06:00
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)]
|
2013-02-16 16:55:55 -06:00
|
|
|
pub struct Deque<T> {
|
|
|
|
priv nelts: uint,
|
|
|
|
priv lo: uint,
|
|
|
|
priv hi: uint,
|
|
|
|
priv elts: ~[Option<T>]
|
2012-01-11 05:49:33 -06:00
|
|
|
}
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
impl<T> Container for Deque<T> {
|
2013-03-15 12:41:02 -05:00
|
|
|
/// Return the number of elements in the deque
|
2013-03-21 23:34:30 -05:00
|
|
|
fn len(&const self) -> uint { self.nelts }
|
2013-03-15 12:41:02 -05:00
|
|
|
|
|
|
|
/// Return true if the deque contains no elements
|
2013-03-21 23:34:30 -05:00
|
|
|
fn is_empty(&const self) -> bool { self.len() == 0 }
|
2013-02-16 16:55:55 -06:00
|
|
|
}
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2013-02-16 17:55:25 -06:00
|
|
|
impl<T> Mutable for Deque<T> {
|
2013-03-15 12:41:02 -05:00
|
|
|
/// Clear the deque, removing all values.
|
2013-02-16 17:55:25 -06:00
|
|
|
fn clear(&mut self) {
|
2013-06-12 20:56:16 -05:00
|
|
|
for self.elts.mut_iter().advance |x| { *x = None }
|
2013-02-16 17:55:25 -06:00
|
|
|
self.nelts = 0;
|
|
|
|
self.lo = 0;
|
|
|
|
self.hi = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<T> Deque<T> {
|
2013-04-10 15:14:06 -05:00
|
|
|
/// Create an empty Deque
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn new() -> Deque<T> {
|
2013-04-10 15:14:06 -05:00
|
|
|
Deque{nelts: 0, lo: 0, hi: 0,
|
|
|
|
elts: vec::from_fn(initial_capacity, |_| None)}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a reference to the first element in the deque
|
|
|
|
///
|
|
|
|
/// Fails if the deque is empty
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn peek_front<'a>(&'a self) -> &'a T { get(self.elts, self.lo) }
|
2013-04-10 15:14:06 -05:00
|
|
|
|
|
|
|
/// Return a reference to the last element in the deque
|
|
|
|
///
|
|
|
|
/// Fails if the deque is empty
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn peek_back<'a>(&'a self) -> &'a T { get(self.elts, self.hi - 1u) }
|
2013-04-10 15:14:06 -05:00
|
|
|
|
|
|
|
/// Retrieve an element in the deque by index
|
|
|
|
///
|
|
|
|
/// Fails if there is no element with the given index
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get<'a>(&'a self, i: int) -> &'a T {
|
2013-04-10 15:14:06 -05:00
|
|
|
let idx = (self.lo + (i as uint)) % self.elts.len();
|
|
|
|
get(self.elts, idx)
|
|
|
|
}
|
|
|
|
|
2013-03-29 20:02:44 -05:00
|
|
|
/// Iterate over the elements in the deque
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn each(&self, f: &fn(&T) -> bool) -> bool {
|
2013-05-02 17:33:27 -05:00
|
|
|
self.eachi(|_i, e| f(e))
|
|
|
|
}
|
2013-03-29 20:02:44 -05:00
|
|
|
|
|
|
|
/// Iterate over the elements in the deque by index
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn eachi(&self, f: &fn(uint, &T) -> bool) -> bool {
|
2013-05-02 17:33:27 -05:00
|
|
|
uint::range(0, self.nelts, |i| f(i, self.get(i as int)))
|
2013-03-29 20:02:44 -05:00
|
|
|
}
|
|
|
|
|
2013-03-15 12:41:02 -05:00
|
|
|
/// Remove and return the first element in the deque
|
|
|
|
///
|
|
|
|
/// Fails if the deque is empty
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn pop_front(&mut self) -> T {
|
2013-04-12 00:10:12 -05:00
|
|
|
let result = self.elts[self.lo].swap_unwrap();
|
2013-02-16 18:43:29 -06:00
|
|
|
self.lo = (self.lo + 1u) % self.elts.len();
|
|
|
|
self.nelts -= 1u;
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2013-03-15 12:41:02 -05:00
|
|
|
/// Remove and return the last element in the deque
|
|
|
|
///
|
|
|
|
/// Fails if the deque is empty
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn pop_back(&mut self) -> T {
|
2013-02-16 18:43:29 -06:00
|
|
|
if self.hi == 0u {
|
|
|
|
self.hi = self.elts.len() - 1u;
|
|
|
|
} else { self.hi -= 1u; }
|
2013-04-12 00:10:12 -05:00
|
|
|
let result = self.elts[self.hi].swap_unwrap();
|
2013-02-16 18:43:29 -06:00
|
|
|
self.elts[self.hi] = None;
|
|
|
|
self.nelts -= 1u;
|
|
|
|
result
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2013-03-15 12:41:02 -05:00
|
|
|
/// Prepend an element to the deque
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn add_front(&mut self, t: T) {
|
2013-02-16 18:10:10 -06:00
|
|
|
let oldlo = self.lo;
|
2013-02-16 16:55:55 -06:00
|
|
|
if self.lo == 0u {
|
|
|
|
self.lo = self.elts.len() - 1u;
|
|
|
|
} else { self.lo -= 1u; }
|
|
|
|
if self.lo == self.hi {
|
|
|
|
self.elts = grow(self.nelts, oldlo, self.elts);
|
|
|
|
self.lo = self.elts.len() - 1u;
|
|
|
|
self.hi = self.nelts;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2013-02-16 16:55:55 -06:00
|
|
|
self.elts[self.lo] = Some(t);
|
|
|
|
self.nelts += 1u;
|
|
|
|
}
|
2011-07-12 16:20:15 -05:00
|
|
|
|
2013-03-15 12:41:02 -05:00
|
|
|
/// Append an element to the deque
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn add_back(&mut self, t: T) {
|
2013-02-16 16:55:55 -06:00
|
|
|
if self.lo == self.hi && self.nelts != 0u {
|
|
|
|
self.elts = grow(self.nelts, self.lo, self.elts);
|
|
|
|
self.lo = 0u;
|
|
|
|
self.hi = self.nelts;
|
|
|
|
}
|
|
|
|
self.elts[self.hi] = Some(t);
|
|
|
|
self.hi = (self.hi + 1u) % self.elts.len();
|
|
|
|
self.nelts += 1u;
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2013-05-27 13:47:38 -05:00
|
|
|
|
|
|
|
/// Reserve capacity for exactly `n` elements in the given deque,
|
|
|
|
/// doing nothing if `self`'s capacity is already equal to or greater
|
|
|
|
/// than the requested capacity
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * n - The number of elements to reserve space for
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn reserve(&mut self, n: uint) {
|
2013-05-27 13:47:38 -05:00
|
|
|
vec::reserve(&mut self.elts, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserve capacity for at least `n` elements in the given deque,
|
|
|
|
/// over-allocating in case the caller needs to reserve additional
|
|
|
|
/// space.
|
|
|
|
///
|
|
|
|
/// Do nothing if `self`'s capacity is already equal to or greater
|
|
|
|
/// than the requested capacity.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * n - The number of elements to reserve space for
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn reserve_at_least(&mut self, n: uint) {
|
2013-05-27 13:47:38 -05:00
|
|
|
vec::reserve_at_least(&mut self.elts, n);
|
|
|
|
}
|
2013-06-25 14:08:47 -05:00
|
|
|
|
|
|
|
/// Front-to-back iterator.
|
|
|
|
pub fn iter<'a>(&'a self) -> DequeIterator<'a, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
DequeIterator { idx: self.lo, nelts: self.nelts, used: 0, vec: self.elts }
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
|
2013-06-25 14:08:47 -05:00
|
|
|
/// Front-to-back iterator which returns mutable values.
|
|
|
|
pub fn mut_iter<'a>(&'a mut self) -> DequeMutIterator<'a, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
DequeMutIterator { idx: self.lo, nelts: self.nelts, used: 0, vec: self.elts }
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Back-to-front iterator.
|
|
|
|
pub fn rev_iter<'a>(&'a self) -> DequeRevIterator<'a, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
DequeRevIterator { idx: self.hi - 1u, nelts: self.nelts, used: 0, vec: self.elts }
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Back-to-front iterator which returns mutable values.
|
|
|
|
pub fn mut_rev_iter<'a>(&'a mut self) -> DequeMutRevIterator<'a, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
DequeMutRevIterator { idx: self.hi - 1u, nelts: self.nelts, used: 0, vec: self.elts }
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 10:38:29 -05:00
|
|
|
macro_rules! iterator {
|
|
|
|
(impl $name:ident -> $elem:ty, $step:expr) => {
|
|
|
|
impl<'self, T> Iterator<$elem> for $name<'self, T> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<$elem> {
|
|
|
|
if self.used >= self.nelts {
|
|
|
|
return None;
|
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
let ret = unsafe {
|
2013-06-26 10:38:29 -05:00
|
|
|
match self.vec[self.idx % self.vec.len()] {
|
|
|
|
Some(ref e) => Some(transmute(e)),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.idx += $step;
|
|
|
|
self.used += 1;
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 14:08:47 -05:00
|
|
|
/// Deque iterator
|
|
|
|
pub struct DequeIterator<'self, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
priv idx: uint,
|
|
|
|
priv nelts: uint,
|
|
|
|
priv used: uint,
|
|
|
|
priv vec: &'self [Option<T>]
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-06-26 10:38:29 -05:00
|
|
|
iterator!{impl DequeIterator -> &'self T, 1}
|
2013-06-25 14:08:47 -05:00
|
|
|
|
|
|
|
/// Deque reverse iterator
|
|
|
|
pub struct DequeRevIterator<'self, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
priv idx: uint,
|
|
|
|
priv nelts: uint,
|
|
|
|
priv used: uint,
|
|
|
|
priv vec: &'self [Option<T>]
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-06-26 10:38:29 -05:00
|
|
|
iterator!{impl DequeRevIterator -> &'self T, -1}
|
|
|
|
|
2013-06-25 14:08:47 -05:00
|
|
|
/// Deque mutable iterator
|
|
|
|
pub struct DequeMutIterator<'self, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
priv idx: uint,
|
|
|
|
priv nelts: uint,
|
|
|
|
priv used: uint,
|
|
|
|
priv vec: &'self mut [Option<T>]
|
|
|
|
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-06-26 10:38:29 -05:00
|
|
|
iterator!{impl DequeMutIterator -> &'self mut T, 1}
|
2013-06-25 14:08:47 -05:00
|
|
|
|
|
|
|
/// Deque mutable reverse iterator
|
|
|
|
pub struct DequeMutRevIterator<'self, T> {
|
2013-06-25 21:33:02 -05:00
|
|
|
priv idx: uint,
|
|
|
|
priv nelts: uint,
|
|
|
|
priv used: uint,
|
|
|
|
priv vec: &'self mut [Option<T>]
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-06-26 10:38:29 -05:00
|
|
|
iterator!{impl DequeMutRevIterator -> &'self mut T, -1}
|
2012-01-11 05:49:33 -06:00
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
/// Grow is only called on full elts, so nelts is also len(elts), unlike
|
|
|
|
/// elsewhere.
|
2013-02-16 20:34:09 -06:00
|
|
|
fn grow<T>(nelts: uint, lo: uint, elts: &mut [Option<T>]) -> ~[Option<T>] {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(nelts, elts.len());
|
2013-02-16 16:55:55 -06:00
|
|
|
let mut rv = ~[];
|
|
|
|
|
2013-02-17 13:08:04 -06:00
|
|
|
do rv.grow_fn(nelts + 1) |i| {
|
2013-05-05 23:42:54 -05:00
|
|
|
replace(&mut elts[(lo + i) % nelts], None)
|
2013-02-16 16:55:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
rv
|
|
|
|
}
|
2013-01-22 10:44:24 -06:00
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
fn get<'r, T>(elts: &'r [Option<T>], i: uint) -> &'r T {
|
2013-02-16 18:03:27 -06:00
|
|
|
match elts[i] { Some(ref t) => t, _ => fail!() }
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-02-16 16:55:55 -06:00
|
|
|
use super::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use core::cmp::Eq;
|
2013-05-07 16:54:42 -05:00
|
|
|
use core::kinds::Copy;
|
2013-05-27 13:47:38 -05:00
|
|
|
use core::vec::capacity;
|
2013-06-26 19:02:34 -05:00
|
|
|
use core;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
2013-02-16 16:55:55 -06:00
|
|
|
let mut d = Deque::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 0u);
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_front(17);
|
|
|
|
d.add_front(42);
|
|
|
|
d.add_back(137);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 3u);
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_back(137);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 4u);
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(d.peek_front());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(*d.peek_front(), 42);
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(d.peek_back());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(*d.peek_back(), 137);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i: int = d.pop_front();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 42);
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 137);
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 137);
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 17);
|
|
|
|
assert_eq!(d.len(), 0u);
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_back(3);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 1u);
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_front(2);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 2u);
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_back(4);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 3u);
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_front(1);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 4u);
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(d.get(0));
|
|
|
|
debug!(d.get(1));
|
|
|
|
debug!(d.get(2));
|
|
|
|
debug!(d.get(3));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(*d.get(0), 1);
|
|
|
|
assert_eq!(*d.get(1), 2);
|
|
|
|
assert_eq!(*d.get(2), 3);
|
|
|
|
assert_eq!(*d.get(3), 4);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-09-13 00:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_boxes() {
|
|
|
|
let a: @int = @5;
|
|
|
|
let b: @int = @72;
|
|
|
|
let c: @int = @64;
|
|
|
|
let d: @int = @175;
|
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
let mut deq = Deque::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 0);
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(a);
|
|
|
|
deq.add_front(b);
|
|
|
|
deq.add_back(c);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(d);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
|
|
|
assert_eq!(*deq.peek_front(), b);
|
|
|
|
assert_eq!(*deq.peek_back(), d);
|
|
|
|
assert_eq!(deq.pop_front(), b);
|
|
|
|
assert_eq!(deq.pop_back(), d);
|
|
|
|
assert_eq!(deq.pop_back(), c);
|
|
|
|
assert_eq!(deq.pop_back(), a);
|
|
|
|
assert_eq!(deq.len(), 0);
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(c);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 1);
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(b);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 2);
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(d);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(a);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
|
|
|
assert_eq!(*deq.get(0), a);
|
|
|
|
assert_eq!(*deq.get(1), b);
|
|
|
|
assert_eq!(*deq.get(2), c);
|
|
|
|
assert_eq!(*deq.get(3), d);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-05-01 18:32:37 -05:00
|
|
|
#[cfg(test)]
|
2013-05-07 16:54:42 -05:00
|
|
|
fn test_parameterized<T:Copy + Eq>(a: T, b: T, c: T, d: T) {
|
2013-02-16 16:55:55 -06:00
|
|
|
let mut deq = Deque::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 0);
|
2013-06-15 19:26:59 -05:00
|
|
|
deq.add_front(copy a);
|
|
|
|
deq.add_front(copy b);
|
|
|
|
deq.add_back(copy c);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2013-06-15 19:26:59 -05:00
|
|
|
deq.add_back(copy d);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
2013-06-15 19:26:59 -05:00
|
|
|
assert_eq!(copy *deq.peek_front(), copy b);
|
|
|
|
assert_eq!(copy *deq.peek_back(), copy d);
|
|
|
|
assert_eq!(deq.pop_front(), copy b);
|
|
|
|
assert_eq!(deq.pop_back(), copy d);
|
|
|
|
assert_eq!(deq.pop_back(), copy c);
|
|
|
|
assert_eq!(deq.pop_back(), copy a);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 0);
|
2013-06-15 19:26:59 -05:00
|
|
|
deq.add_back(copy c);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 1);
|
2013-06-15 19:26:59 -05:00
|
|
|
deq.add_front(copy b);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 2);
|
2013-06-15 19:26:59 -05:00
|
|
|
deq.add_back(copy d);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2013-06-15 19:26:59 -05:00
|
|
|
deq.add_front(copy a);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
2013-06-15 19:26:59 -05:00
|
|
|
assert_eq!(copy *deq.get(0), copy a);
|
|
|
|
assert_eq!(copy *deq.get(1), copy b);
|
|
|
|
assert_eq!(copy *deq.get(2), copy c);
|
|
|
|
assert_eq!(copy *deq.get(3), copy d);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-20 10:36:16 -05:00
|
|
|
#[deriving(Eq)]
|
2012-08-11 09:08:42 -05:00
|
|
|
enum Taggy { One(int), Two(int, int), Three(int, int, int), }
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-03-20 10:36:16 -05:00
|
|
|
#[deriving(Eq)]
|
2012-08-11 09:08:42 -05:00
|
|
|
enum Taggypar<T> {
|
|
|
|
Onepar(int), Twopar(int, int), Threepar(int, int, int),
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-20 10:36:16 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-22 10:44:24 -06:00
|
|
|
struct RecCy {
|
|
|
|
x: int,
|
|
|
|
y: int,
|
2013-01-25 18:57:39 -06:00
|
|
|
t: Taggy
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-09-13 00:09:55 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_param_int() {
|
|
|
|
test_parameterized::<int>(5, 72, 64, 175);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_param_at_int() {
|
|
|
|
test_parameterized::<@int>(@5, @72, @64, @175);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_param_taggy() {
|
2013-06-26 17:14:35 -05:00
|
|
|
test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42));
|
2012-09-13 00:09:55 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-09-13 00:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_param_taggypar() {
|
|
|
|
test_parameterized::<Taggypar<int>>(Onepar::<int>(1),
|
2012-08-11 09:08:42 -05:00
|
|
|
Twopar::<int>(1, 2),
|
|
|
|
Threepar::<int>(1, 2, 3),
|
|
|
|
Twopar::<int>(17, 42));
|
2012-09-13 00:09:55 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-09-13 00:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_param_reccy() {
|
2013-01-22 10:44:24 -06:00
|
|
|
let reccy1 = RecCy { x: 1, y: 2, t: One(1) };
|
|
|
|
let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) };
|
|
|
|
let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) };
|
|
|
|
let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
|
2012-09-13 00:09:55 -05:00
|
|
|
test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2013-03-29 20:02:44 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eachi() {
|
|
|
|
let mut deq = Deque::new();
|
|
|
|
deq.add_back(1);
|
|
|
|
deq.add_back(2);
|
|
|
|
deq.add_back(3);
|
|
|
|
|
|
|
|
for deq.eachi |i, e| {
|
|
|
|
assert_eq!(*e, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
deq.pop_front();
|
|
|
|
|
|
|
|
for deq.eachi |i, e| {
|
|
|
|
assert_eq!(*e, i + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-05-27 13:47:38 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reserve() {
|
|
|
|
let mut d = Deque::new();
|
|
|
|
d.add_back(0u64);
|
|
|
|
d.reserve(50);
|
|
|
|
assert_eq!(capacity(&mut d.elts), 50);
|
|
|
|
let mut d = Deque::new();
|
|
|
|
d.add_back(0u32);
|
|
|
|
d.reserve(50);
|
|
|
|
assert_eq!(capacity(&mut d.elts), 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reserve_at_least() {
|
|
|
|
let mut d = Deque::new();
|
|
|
|
d.add_back(0u64);
|
|
|
|
d.reserve_at_least(50);
|
|
|
|
assert_eq!(capacity(&mut d.elts), 64);
|
|
|
|
let mut d = Deque::new();
|
|
|
|
d.add_back(0u32);
|
|
|
|
d.reserve_at_least(50);
|
|
|
|
assert_eq!(capacity(&mut d.elts), 64);
|
|
|
|
}
|
|
|
|
|
2013-06-26 09:04:44 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iter() {
|
|
|
|
let mut d = Deque::new();
|
2013-06-26 19:02:34 -05:00
|
|
|
for core::int::range(0,5) |i| {
|
2013-06-26 09:04:44 -05:00
|
|
|
d.add_back(i);
|
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.iter().collect::<~[&int]>(), ~[&0,&1,&2,&3,&4]);
|
|
|
|
|
2013-06-26 19:02:34 -05:00
|
|
|
for core::int::range(6,9) |i| {
|
2013-06-26 09:04:44 -05:00
|
|
|
d.add_front(i);
|
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.iter().collect::<~[&int]>(), ~[&8,&7,&6,&0,&1,&2,&3,&4]);
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rev_iter() {
|
|
|
|
let mut d = Deque::new();
|
2013-06-26 19:02:34 -05:00
|
|
|
for core::int::range(0,5) |i| {
|
2013-06-26 09:04:44 -05:00
|
|
|
d.add_back(i);
|
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0]);
|
|
|
|
|
2013-06-26 19:02:34 -05:00
|
|
|
for core::int::range(6,9) |i| {
|
2013-06-26 09:04:44 -05:00
|
|
|
d.add_front(i);
|
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0,&6,&7,&8]);
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
2012-07-03 12:52:32 -05:00
|
|
|
}
|