rust/src/libstd/deque.rs

311 lines
9.9 KiB
Rust
Raw Normal View History

//! A deque. Untested as of yet. Likely buggy
2012-08-20 14:23:37 -05:00
import option::{Some, None};
2012-08-27 16:22:25 -05:00
import dvec::DVec;
trait t<T> {
fn size() -> uint;
fn add_front(T);
fn add_back(T);
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
}
// FIXME (#2343) eventually, a proper datatype plus an exported impl would
// be preferrable.
fn create<T: copy>() -> t<T> {
2012-08-20 14:23:37 -05:00
type cell<T> = Option<T>;
2011-07-27 07:19:39 -05:00
let initial_capacity: uint = 32u; // 2^5
/**
* Grow is only called on full elts, so nelts is also len(elts), unlike
* elsewhere.
*/
fn grow<T: copy>(nelts: uint, lo: uint, -elts: ~[mut cell<T>]) ->
~[mut cell<T>] {
2011-08-15 18:38:23 -05:00
assert (nelts == vec::len(elts));
let mut rv = ~[mut];
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 {
vec::push(rv, elts[(lo + i) % nelts]);
2012-08-20 14:23:37 -05:00
} else { vec::push(rv, None); }
i += 1u;
}
2012-08-01 19:30:05 -05:00
return rv;
}
2012-08-14 18:54:13 -05:00
fn get<T: copy>(elts: DVec<cell<T>>, i: uint) -> T {
2012-08-20 14:23:37 -05:00
match elts.get_elt(i) { Some(t) => t, _ => fail }
}
2012-01-11 05:49:33 -06:00
2012-03-26 20:35:18 -05:00
type repr<T> = {mut nelts: uint,
mut lo: uint,
mut hi: uint,
2012-08-14 18:54:13 -05:00
elts: DVec<cell<T>>};
2012-01-11 05:49:33 -06:00
2012-08-07 20:10:06 -05:00
impl <T: copy> repr<T>: t<T> {
2012-08-01 19:30:05 -05:00
fn size() -> uint { return self.nelts; }
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-06-30 18:19:07 -05:00
self.elts.swap(|v| grow(self.nelts, oldlo, 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;
}
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;
}
fn add_back(t: T) {
2012-01-11 05:49:33 -06:00
if self.lo == self.hi && self.nelts != 0u {
2012-06-30 18:19:07 -05:00
self.elts.swap(|v| grow(self.nelts, self.lo, v));
2012-01-11 05:49:33 -06:00
self.lo = 0u;
self.hi = self.nelts;
}
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;
}
/**
* 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-01-11 05:49:33 -06: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;
}
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; }
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;
}
2012-08-01 19:30:05 -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-08-01 19:30:05 -05:00
return get(self.elts, idx);
}
}
2012-01-11 05:49:33 -06: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(
vec::to_mut(
2012-08-20 14:23:37 -05:00
vec::from_elem(initial_capacity, None)))
2012-01-11 05:49:33 -06:00
};
repr as t::<T>
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
#[test]
fn test_simple() {
let d: deque::t<int> = deque::create::<int>();
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);
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);
}
fn test_boxes(a: @int, b: @int, c: @int, d: @int) {
let deq: deque::t<@int> = deque::create::<@int>();
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);
}
type eqfn<T> = fn@(T, T) -> bool;
fn test_parameterized<T: copy owned>(
e: eqfn<T>, a: T, b: T, c: T, d: T) {
2012-01-17 21:05:07 -06:00
let deq: deque::t<T> = deque::create::<T>();
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 (e(deq.peek_front(), b));
assert (e(deq.peek_back(), d));
assert (e(deq.pop_front(), b));
assert (e(deq.pop_back(), d));
assert (e(deq.pop_back(), c));
assert (e(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 (e(deq.get(0), a));
assert (e(deq.get(1), b));
assert (e(deq.get(2), c));
assert (e(deq.get(3), d));
}
2012-01-19 19:55:34 -06:00
enum taggy { one(int), two(int, int), three(int, int, int), }
2012-01-17 21:05:07 -06:00
2012-01-19 17:20:57 -06:00
enum taggypar<T> {
2012-01-19 19:55:34 -06:00
onepar(int), twopar(int, int), threepar(int, int, int),
2012-01-17 21:05:07 -06:00
}
type reccy = {x: int, y: int, t: taggy};
#[test]
fn test() {
2012-08-01 19:30:05 -05:00
fn inteq(&&a: int, &&b: int) -> bool { return a == b; }
fn intboxeq(&&a: @int, &&b: @int) -> bool { return a == b; }
2012-01-17 21:05:07 -06:00
fn taggyeq(a: taggy, b: taggy) -> bool {
2012-08-06 14:34:08 -05:00
match a {
one(a1) => match b {
2012-08-03 21:59:04 -05:00
one(b1) => return a1 == b1,
_ => return false
},
2012-08-06 14:34:08 -05:00
two(a1, a2) => match b {
2012-08-03 21:59:04 -05:00
two(b1, b2) => return a1 == b1 && a2 == b2,
_ => return false
},
2012-08-06 14:34:08 -05:00
three(a1, a2, a3) => match b {
2012-08-03 21:59:04 -05:00
three(b1, b2, b3) => return a1 == b1 && a2 == b2 && a3 == b3,
_ => return false
2012-01-17 21:05:07 -06:00
}
}
}
fn taggypareq<T>(a: taggypar<T>, b: taggypar<T>) -> bool {
2012-08-06 14:34:08 -05:00
match a {
onepar::<T>(a1) => match b {
2012-08-03 21:59:04 -05:00
onepar::<T>(b1) => return a1 == b1,
_ => return false
},
2012-08-06 14:34:08 -05:00
twopar::<T>(a1, a2) => match b {
2012-08-03 21:59:04 -05:00
twopar::<T>(b1, b2) => return a1 == b1 && a2 == b2,
_ => return false
},
2012-08-06 14:34:08 -05:00
threepar::<T>(a1, a2, a3) => match b {
2012-08-03 21:59:04 -05:00
threepar::<T>(b1, b2, b3) => {
return a1 == b1 && a2 == b2 && a3 == b3
2012-01-17 21:05:07 -06:00
}
2012-08-03 21:59:04 -05:00
_ => return false
2012-01-17 21:05:07 -06:00
}
}
}
fn reccyeq(a: reccy, b: reccy) -> bool {
2012-08-01 19:30:05 -05:00
return a.x == b.x && a.y == b.y && taggyeq(a.t, b.t);
2012-01-17 21:05:07 -06:00
}
2012-08-22 19:24:52 -05:00
debug!("*** test boxes");
2012-01-17 21:05:07 -06:00
test_boxes(@5, @72, @64, @175);
2012-08-22 19:24:52 -05:00
debug!("*** end test boxes");
debug!("test parameterized: int");
2012-01-17 21:05:07 -06:00
let eq1: eqfn<int> = inteq;
test_parameterized::<int>(eq1, 5, 72, 64, 175);
2012-08-22 19:24:52 -05:00
debug!("*** test parameterized: @int");
2012-01-17 21:05:07 -06:00
let eq2: eqfn<@int> = intboxeq;
test_parameterized::<@int>(eq2, @5, @72, @64, @175);
2012-08-22 19:24:52 -05:00
debug!("*** end test parameterized @int");
debug!("test parameterized: taggy");
2012-01-17 21:05:07 -06:00
let eq3: eqfn<taggy> = taggyeq;
test_parameterized::<taggy>(eq3, one(1), two(1, 2), three(1, 2, 3),
two(17, 42));
2012-08-22 19:24:52 -05:00
debug!("*** test parameterized: taggypar<int>");
2012-06-30 18:19:07 -05:00
let eq4: eqfn<taggypar<int>> = |x,y| taggypareq::<int>(x, y);
2012-01-17 21:05:07 -06:00
test_parameterized::<taggypar<int>>(eq4, onepar::<int>(1),
twopar::<int>(1, 2),
threepar::<int>(1, 2, 3),
twopar::<int>(17, 42));
2012-08-22 19:24:52 -05:00
debug!("*** end test parameterized: taggypar::<int>");
2012-01-17 21:05:07 -06:00
2012-08-22 19:24:52 -05:00
debug!("*** test parameterized: reccy");
2012-01-17 21:05:07 -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)};
let eq5: eqfn<reccy> = reccyeq;
test_parameterized::<reccy>(eq5, reccy1, reccy2, reccy3, reccy4);
2012-08-22 19:24:52 -05:00
debug!("*** end test parameterized: reccy");
debug!("*** done");
2012-01-17 21:05:07 -06:00
}
2012-07-03 12:52:32 -05:00
}