2012-07-04 16:53:12 -05:00
|
|
|
//! A deque. Untested as of yet. Likely buggy
|
2012-10-04 21:58:31 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-09-13 00:09:55 -05:00
|
|
|
#[forbid(non_camel_case_types)];
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use option::{Some, None};
|
|
|
|
use dvec::DVec;
|
2012-09-13 00:09:55 -05:00
|
|
|
use core::cmp::{Eq};
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2012-09-28 19:08:22 -05:00
|
|
|
pub trait Deque<T> {
|
2011-10-24 17:25:41 -05:00
|
|
|
fn size() -> uint;
|
2012-10-03 14:21:48 -05:00
|
|
|
fn add_front(v: T);
|
|
|
|
fn add_back(v: T);
|
2011-10-24 17:25:41 -05:00
|
|
|
fn pop_front() -> T;
|
|
|
|
fn pop_back() -> T;
|
|
|
|
fn peek_front() -> T;
|
|
|
|
fn peek_back() -> T;
|
|
|
|
fn get(int) -> T;
|
2012-01-11 05:49:33 -06:00
|
|
|
}
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2343) eventually, a proper datatype plus an exported impl would
|
|
|
|
// be preferrable.
|
2012-09-28 19:08:22 -05:00
|
|
|
pub fn create<T: Copy>() -> Deque<T> {
|
2012-08-11 09:08:42 -05:00
|
|
|
type Cell<T> = Option<T>;
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let initial_capacity: uint = 32u; // 2^5
|
2011-06-15 13:19:50 -05:00
|
|
|
/**
|
|
|
|
* Grow is only called on full elts, so nelts is also len(elts), unlike
|
|
|
|
* elsewhere.
|
|
|
|
*/
|
2012-10-03 14:21:48 -05:00
|
|
|
fn grow<T: Copy>(nelts: uint, lo: uint, elts: ~[Cell<T>])
|
2012-09-21 20:43:30 -05:00
|
|
|
-> ~[Cell<T>] {
|
|
|
|
let mut elts = move elts;
|
2011-08-15 18:38:23 -05:00
|
|
|
assert (nelts == vec::len(elts));
|
2012-09-21 20:43:30 -05:00
|
|
|
let mut rv = ~[];
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
let nalloc = uint::next_power_of_two(nelts + 1u);
|
|
|
|
while i < nalloc {
|
|
|
|
if i < nelts {
|
2012-09-26 19:33:34 -05:00
|
|
|
rv.push(elts[(lo + i) % nelts]);
|
|
|
|
} else { rv.push(None); }
|
2011-07-12 16:20:15 -05:00
|
|
|
i += 1u;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-07-12 16:20:15 -05:00
|
|
|
|
2012-09-10 19:50:48 -05:00
|
|
|
move rv
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2012-09-13 00:09:55 -05:00
|
|
|
fn get<T: Copy>(elts: &DVec<Cell<T>>, i: uint) -> T {
|
2012-09-28 02:22:18 -05:00
|
|
|
match (*elts).get_elt(i) { Some(move t) => t, _ => fail }
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2012-01-11 05:49:33 -06:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
type Repr<T> = {mut nelts: uint,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut lo: uint,
|
|
|
|
mut hi: uint,
|
2012-08-11 09:08:42 -05:00
|
|
|
elts: DVec<Cell<T>>};
|
2012-01-11 05:49:33 -06:00
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
impl <T: Copy> Repr<T>: Deque<T> {
|
2012-08-01 19:30:05 -05:00
|
|
|
fn size() -> uint { return self.nelts; }
|
2012-10-03 14:21:48 -05:00
|
|
|
fn add_front(t: T) {
|
2012-01-11 05:49:33 -06:00
|
|
|
let oldlo: uint = self.lo;
|
|
|
|
if self.lo == 0u {
|
2012-06-03 09:36:01 -05:00
|
|
|
self.lo = self.elts.len() - 1u;
|
2012-01-11 05:49:33 -06:00
|
|
|
} else { self.lo -= 1u; }
|
|
|
|
if self.lo == self.hi {
|
2012-09-21 20:43:30 -05:00
|
|
|
self.elts.swap(|v| grow(self.nelts, oldlo, move v));
|
2012-06-03 09:36:01 -05:00
|
|
|
self.lo = self.elts.len() - 1u;
|
2012-01-11 05:49:33 -06:00
|
|
|
self.hi = self.nelts;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
self.elts.set_elt(self.lo, Some(t));
|
2012-01-11 05:49:33 -06:00
|
|
|
self.nelts += 1u;
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2012-10-03 14:21:48 -05:00
|
|
|
fn add_back(t: T) {
|
2012-01-11 05:49:33 -06:00
|
|
|
if self.lo == self.hi && self.nelts != 0u {
|
2012-09-21 20:43:30 -05:00
|
|
|
self.elts.swap(|v| grow(self.nelts, self.lo, move v));
|
2012-01-11 05:49:33 -06:00
|
|
|
self.lo = 0u;
|
|
|
|
self.hi = self.nelts;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
self.elts.set_elt(self.hi, Some(t));
|
2012-06-03 09:36:01 -05:00
|
|
|
self.hi = (self.hi + 1u) % self.elts.len();
|
2012-01-11 05:49:33 -06:00
|
|
|
self.nelts += 1u;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
/**
|
|
|
|
* We actually release (turn to none()) the T we're popping so
|
|
|
|
* that we don't keep anyone's refcount up unexpectedly.
|
|
|
|
*/
|
|
|
|
fn pop_front() -> T {
|
2012-09-13 00:09:55 -05:00
|
|
|
let t: T = get(&self.elts, self.lo);
|
2012-08-20 14:23:37 -05:00
|
|
|
self.elts.set_elt(self.lo, None);
|
2012-06-03 09:36:01 -05:00
|
|
|
self.lo = (self.lo + 1u) % self.elts.len();
|
2012-01-11 05:49:33 -06:00
|
|
|
self.nelts -= 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return t;
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
|
|
|
fn pop_back() -> T {
|
2012-01-11 05:49:33 -06:00
|
|
|
if self.hi == 0u {
|
2012-06-03 09:36:01 -05:00
|
|
|
self.hi = self.elts.len() - 1u;
|
2012-01-11 05:49:33 -06:00
|
|
|
} else { self.hi -= 1u; }
|
2012-09-13 00:09:55 -05:00
|
|
|
let t: T = get(&self.elts, self.hi);
|
2012-08-20 14:23:37 -05:00
|
|
|
self.elts.set_elt(self.hi, None);
|
2012-01-11 05:49:33 -06:00
|
|
|
self.nelts -= 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return t;
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2012-09-13 00:09:55 -05:00
|
|
|
fn peek_front() -> T { return get(&self.elts, self.lo); }
|
|
|
|
fn peek_back() -> T { return get(&self.elts, self.hi - 1u); }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn get(i: int) -> T {
|
2012-06-03 09:36:01 -05:00
|
|
|
let idx = (self.lo + (i as uint)) % self.elts.len();
|
2012-09-13 00:09:55 -05:00
|
|
|
return get(&self.elts, idx);
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-11 05:49:33 -06:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
let repr: Repr<T> = {
|
2012-03-26 20:35:18 -05:00
|
|
|
mut nelts: 0u,
|
|
|
|
mut lo: 0u,
|
|
|
|
mut hi: 0u,
|
2012-06-03 09:36:01 -05:00
|
|
|
elts:
|
|
|
|
dvec::from_vec(
|
2012-09-12 12:38:17 -05:00
|
|
|
vec::from_elem(initial_capacity, None))
|
2012-01-11 05:49:33 -06:00
|
|
|
};
|
2012-09-11 19:17:54 -05:00
|
|
|
(move repr) as Deque::<T>
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let d: deque::Deque<int> = deque::create::<int>();
|
2012-01-17 21:05:07 -06:00
|
|
|
assert (d.size() == 0u);
|
|
|
|
d.add_front(17);
|
|
|
|
d.add_front(42);
|
|
|
|
d.add_back(137);
|
|
|
|
assert (d.size() == 3u);
|
|
|
|
d.add_back(137);
|
|
|
|
assert (d.size() == 4u);
|
|
|
|
log(debug, d.peek_front());
|
|
|
|
assert (d.peek_front() == 42);
|
|
|
|
log(debug, d.peek_back());
|
|
|
|
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);
|
|
|
|
assert (i == 42);
|
|
|
|
i = d.pop_back();
|
|
|
|
log(debug, i);
|
|
|
|
assert (i == 137);
|
|
|
|
i = d.pop_back();
|
|
|
|
log(debug, i);
|
|
|
|
assert (i == 137);
|
|
|
|
i = d.pop_back();
|
|
|
|
log(debug, i);
|
|
|
|
assert (i == 17);
|
|
|
|
assert (d.size() == 0u);
|
|
|
|
d.add_back(3);
|
|
|
|
assert (d.size() == 1u);
|
|
|
|
d.add_front(2);
|
|
|
|
assert (d.size() == 2u);
|
|
|
|
d.add_back(4);
|
|
|
|
assert (d.size() == 3u);
|
|
|
|
d.add_front(1);
|
|
|
|
assert (d.size() == 4u);
|
|
|
|
log(debug, d.get(0));
|
|
|
|
log(debug, d.get(1));
|
|
|
|
log(debug, d.get(2));
|
|
|
|
log(debug, d.get(3));
|
|
|
|
assert (d.get(0) == 1);
|
|
|
|
assert (d.get(1) == 2);
|
|
|
|
assert (d.get(2) == 3);
|
|
|
|
assert (d.get(3) == 4);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
let deq: deque::Deque<@int> = deque::create::<@int>();
|
2012-01-17 21:05:07 -06:00
|
|
|
assert (deq.size() == 0u);
|
|
|
|
deq.add_front(a);
|
|
|
|
deq.add_front(b);
|
|
|
|
deq.add_back(c);
|
|
|
|
assert (deq.size() == 3u);
|
|
|
|
deq.add_back(d);
|
|
|
|
assert (deq.size() == 4u);
|
|
|
|
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.size() == 0u);
|
|
|
|
deq.add_back(c);
|
|
|
|
assert (deq.size() == 1u);
|
|
|
|
deq.add_front(b);
|
|
|
|
assert (deq.size() == 2u);
|
|
|
|
deq.add_back(d);
|
|
|
|
assert (deq.size() == 3u);
|
|
|
|
deq.add_front(a);
|
|
|
|
assert (deq.size() == 4u);
|
|
|
|
assert (deq.get(0) == a);
|
|
|
|
assert (deq.get(1) == b);
|
|
|
|
assert (deq.get(2) == c);
|
|
|
|
assert (deq.get(3) == d);
|
|
|
|
}
|
|
|
|
|
2012-10-04 21:58:31 -05:00
|
|
|
fn test_parameterized<T: Copy Eq Owned>(a: T, b: T, c: T, d: T) {
|
2012-08-11 09:08:42 -05:00
|
|
|
let deq: deque::Deque<T> = deque::create::<T>();
|
2012-01-17 21:05:07 -06:00
|
|
|
assert (deq.size() == 0u);
|
|
|
|
deq.add_front(a);
|
|
|
|
deq.add_front(b);
|
|
|
|
deq.add_back(c);
|
|
|
|
assert (deq.size() == 3u);
|
|
|
|
deq.add_back(d);
|
|
|
|
assert (deq.size() == 4u);
|
2012-09-13 00:09:55 -05:00
|
|
|
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;
|
2012-01-17 21:05:07 -06:00
|
|
|
assert (deq.size() == 0u);
|
|
|
|
deq.add_back(c);
|
|
|
|
assert (deq.size() == 1u);
|
|
|
|
deq.add_front(b);
|
|
|
|
assert (deq.size() == 2u);
|
|
|
|
deq.add_back(d);
|
|
|
|
assert (deq.size() == 3u);
|
|
|
|
deq.add_front(a);
|
|
|
|
assert (deq.size() == 4u);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
type RecCy = {x: int, y: int, t: Taggy};
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl Taggy : Eq {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &Taggy) -> bool {
|
|
|
|
match (*self) {
|
|
|
|
One(a1) => match (*other) {
|
|
|
|
One(b1) => return a1 == b1,
|
|
|
|
_ => return false
|
|
|
|
},
|
|
|
|
Two(a1, a2) => match (*other) {
|
|
|
|
Two(b1, b2) => return a1 == b1 && a2 == b2,
|
|
|
|
_ => return false
|
|
|
|
},
|
|
|
|
Three(a1, a2, a3) => match (*other) {
|
|
|
|
Three(b1, b2, b3) => return a1 == b1 && a2 == b2 && a3 == b3,
|
|
|
|
_ => return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(&self, other: &Taggy) -> bool { !(*self).eq(other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-09-13 00:09:55 -05:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl Taggypar<int> : Eq {
|
|
|
|
//let eq4: EqFn<Taggypar<int>> = |x,y| taggypareq::<int>(x, y);
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &Taggypar<int>) -> bool {
|
|
|
|
match (*self) {
|
|
|
|
Onepar::<int>(a1) => match (*other) {
|
|
|
|
Onepar::<int>(b1) => return a1 == b1,
|
|
|
|
_ => return false
|
|
|
|
},
|
|
|
|
Twopar::<int>(a1, a2) => match (*other) {
|
|
|
|
Twopar::<int>(b1, b2) => return a1 == b1 && a2 == b2,
|
|
|
|
_ => return false
|
|
|
|
},
|
|
|
|
Threepar::<int>(a1, a2, a3) => match (*other) {
|
|
|
|
Threepar::<int>(b1, b2, b3) => {
|
|
|
|
return a1 == b1 && a2 == b2 && a3 == b3
|
|
|
|
}
|
|
|
|
_ => return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(&self, other: &Taggypar<int>) -> bool {
|
|
|
|
!(*self).eq(other)
|
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-09-13 00:09:55 -05:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl RecCy : Eq {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &RecCy) -> bool {
|
|
|
|
return (*self).x == (*other).x && (*self).y == (*other).y &&
|
|
|
|
(*self).t == (*other).t;
|
|
|
|
}
|
|
|
|
pure fn ne(&self, other: &RecCy) -> bool { !(*self).eq(other) }
|
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() {
|
2012-08-11 09:08:42 -05: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
|
|
|
}
|