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-02-16 16:55:55 -06:00
|
|
|
use core::container::{Container, Mutable};
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::cmp::Eq;
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::uint;
|
|
|
|
use core::vec;
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
const initial_capacity: uint = 32u; // 2^5
|
|
|
|
|
|
|
|
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> {
|
|
|
|
pure fn len(&self) -> uint { self.nelts }
|
|
|
|
pure fn is_empty(&self) -> bool { self.len() == 0 }
|
|
|
|
}
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2013-02-16 17:55:25 -06:00
|
|
|
impl<T> Mutable for Deque<T> {
|
|
|
|
fn clear(&mut self) {
|
|
|
|
for vec::each_mut(self.elts) |x| { *x = None }
|
|
|
|
self.nelts = 0;
|
|
|
|
self.lo = 0;
|
|
|
|
self.hi = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 18:00:51 -06:00
|
|
|
impl<T> Deque<T> {
|
2013-02-16 16:55:55 -06:00
|
|
|
static pure fn new() -> Deque<T> {
|
|
|
|
Deque{nelts: 0, lo: 0, hi: 0,
|
|
|
|
elts: vec::from_fn(initial_capacity, |_| None)}
|
|
|
|
}
|
2013-02-16 18:10:10 -06:00
|
|
|
|
|
|
|
fn peek_front(&self) -> &self/T { get(self.elts, self.lo) }
|
|
|
|
fn peek_back(&self) -> &self/T { get(self.elts, self.hi - 1u) }
|
2013-02-16 18:43:29 -06:00
|
|
|
|
|
|
|
fn pop_front(&mut self) -> T {
|
|
|
|
let mut result = self.elts[self.lo].swap_unwrap();
|
|
|
|
self.lo = (self.lo + 1u) % self.elts.len();
|
|
|
|
self.nelts -= 1u;
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pop_back(&mut self) -> T {
|
|
|
|
if self.hi == 0u {
|
|
|
|
self.hi = self.elts.len() - 1u;
|
|
|
|
} else { self.hi -= 1u; }
|
|
|
|
let mut result = self.elts[self.hi].swap_unwrap();
|
|
|
|
self.elts[self.hi] = None;
|
|
|
|
self.nelts -= 1u;
|
|
|
|
result
|
|
|
|
}
|
2013-02-16 18:00:51 -06:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2013-02-16 18:00:51 -06:00
|
|
|
impl<T: Copy> Deque<T> {
|
2013-02-16 16:55:55 -06:00
|
|
|
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-02-16 16:55:55 -06:00
|
|
|
fn add_back(&mut self, t: T) {
|
|
|
|
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-02-16 16:55:55 -06:00
|
|
|
|
|
|
|
fn get(&self, i: int) -> T {
|
|
|
|
let idx = (self.lo + (i as uint)) % self.elts.len();
|
2013-02-16 18:03:27 -06:00
|
|
|
*get(self.elts, idx)
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2013-02-16 16:55:55 -06:00
|
|
|
}
|
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.
|
|
|
|
fn grow<T: Copy>(nelts: uint, lo: uint, elts: &[Option<T>]) -> ~[Option<T>] {
|
|
|
|
assert nelts == elts.len();
|
|
|
|
let mut rv = ~[];
|
|
|
|
|
|
|
|
let mut i = 0u;
|
|
|
|
let nalloc = uint::next_power_of_two(nelts + 1u);
|
|
|
|
while i < nalloc {
|
|
|
|
if i < nelts {
|
|
|
|
rv.push(elts[(lo + i) % nelts]);
|
|
|
|
} else { rv.push(None); }
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv
|
|
|
|
}
|
2013-01-22 10:44:24 -06:00
|
|
|
|
2013-02-16 18:03:27 -06:00
|
|
|
fn get<T>(elts: &r/[Option<T>], i: uint) -> &r/T {
|
|
|
|
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::*;
|
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-02-16 18:10:10 -06:00
|
|
|
assert d.len() == 0u;
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_front(17);
|
|
|
|
d.add_front(42);
|
|
|
|
d.add_back(137);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert d.len() == 3u;
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_back(137);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert d.len() == 4u;
|
2012-01-17 21:05:07 -06:00
|
|
|
log(debug, d.peek_front());
|
2013-02-16 18:10:10 -06:00
|
|
|
assert *d.peek_front() == 42;
|
2012-01-17 21:05:07 -06:00
|
|
|
log(debug, d.peek_back());
|
2013-02-16 18:10:10 -06:00
|
|
|
assert *d.peek_back() == 137;
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i: int = d.pop_front();
|
2012-01-17 21:05:07 -06:00
|
|
|
log(debug, i);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert i == 42;
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
|
|
|
log(debug, i);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert i == 137;
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
|
|
|
log(debug, i);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert i == 137;
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
|
|
|
log(debug, i);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert i == 17;
|
|
|
|
assert d.len() == 0u;
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_back(3);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert d.len() == 1u;
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_front(2);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert d.len() == 2u;
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_back(4);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert d.len() == 3u;
|
2012-01-17 21:05:07 -06:00
|
|
|
d.add_front(1);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert d.len() == 4u;
|
2012-01-17 21:05:07 -06:00
|
|
|
log(debug, d.get(0));
|
|
|
|
log(debug, d.get(1));
|
|
|
|
log(debug, d.get(2));
|
|
|
|
log(debug, d.get(3));
|
2013-02-16 18:10:10 -06:00
|
|
|
assert d.get(0) == 1;
|
|
|
|
assert d.get(1) == 2;
|
|
|
|
assert d.get(2) == 3;
|
|
|
|
assert 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-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 0;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(a);
|
|
|
|
deq.add_front(b);
|
|
|
|
deq.add_back(c);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 3;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(d);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 4;
|
|
|
|
assert *deq.peek_front() == b;
|
|
|
|
assert *deq.peek_back() == d;
|
|
|
|
assert deq.pop_front() == b;
|
|
|
|
assert deq.pop_back() == d;
|
|
|
|
assert deq.pop_back() == c;
|
|
|
|
assert deq.pop_back() == a;
|
|
|
|
assert deq.len() == 0;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(c);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 1;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(b);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 2;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(d);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 3;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(a);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 4;
|
|
|
|
assert deq.get(0) == a;
|
|
|
|
assert deq.get(1) == b;
|
|
|
|
assert deq.get(2) == c;
|
|
|
|
assert deq.get(3) == d;
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-12-11 13:59:45 -06:00
|
|
|
fn test_parameterized<T: Copy Eq Durable>(a: T, b: T, c: T, d: T) {
|
2013-02-16 16:55:55 -06:00
|
|
|
let mut deq = Deque::new();
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 0;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(a);
|
|
|
|
deq.add_front(b);
|
|
|
|
deq.add_back(c);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 3;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(d);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 4;
|
|
|
|
assert *deq.peek_front() == b;
|
|
|
|
assert *deq.peek_back() == d;
|
2012-09-13 00:09:55 -05:00
|
|
|
assert deq.pop_front() == b;
|
|
|
|
assert deq.pop_back() == d;
|
|
|
|
assert deq.pop_back() == c;
|
|
|
|
assert deq.pop_back() == a;
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 0;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(c);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 1;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(b);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 2;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_back(d);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 3;
|
2012-01-17 21:05:07 -06:00
|
|
|
deq.add_front(a);
|
2013-02-16 18:10:10 -06:00
|
|
|
assert deq.len() == 4;
|
2012-09-13 00:09:55 -05:00
|
|
|
assert deq.get(0) == a;
|
|
|
|
assert deq.get(1) == b;
|
|
|
|
assert deq.get(2) == c;
|
|
|
|
assert deq.get(3) == d;
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-01-25 18:57:39 -06: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-01-25 18:57:39 -06: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-01-25 18:57:39 -06: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() {
|
|
|
|
test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3),
|
2012-08-11 09:08:42 -05:00
|
|
|
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
|
|
|
}
|
2012-07-03 12:52:32 -05:00
|
|
|
}
|