2010-07-20 20:03:09 -05:00
|
|
|
|
|
|
|
|
2010-07-28 16:00:44 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
/**
|
|
|
|
* A deque, for fun. Untested as of yet. Likely buggy.
|
|
|
|
*/
|
|
|
|
type t[T] =
|
|
|
|
obj {
|
|
|
|
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 ;
|
|
|
|
};
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn create[@T]() -> t[T] {
|
2011-05-12 10:24:54 -05:00
|
|
|
type cell[T] = option::t[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.
|
|
|
|
*/
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-04 19:24:54 -05:00
|
|
|
fn grow[@T](nelts: uint, lo: uint, elts: &[mutable cell[T]]) ->
|
|
|
|
[mutable cell[T]] {
|
2011-07-12 16:20:15 -05:00
|
|
|
assert (nelts == ivec::len(elts));
|
2011-08-04 19:24:54 -05:00
|
|
|
let rv = ~[mutable];
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
|
|
|
let nalloc = uint::next_power_of_two(nelts + 1u);
|
|
|
|
while i < nalloc {
|
|
|
|
if i < nelts {
|
2011-07-12 16:20:15 -05:00
|
|
|
rv += ~[mutable elts.((lo + i) % nelts)];
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { rv += ~[mutable option::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
|
|
|
|
|
|
|
ret rv;
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2011-08-04 19:24:54 -05:00
|
|
|
fn get[@T](elts: &[mutable cell[T]], i: uint) -> T {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret alt elts.(i) { option::some(t) { t } _ { fail } };
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2011-07-29 14:58:52 -05:00
|
|
|
obj deque[@T](mutable nelts: uint,
|
|
|
|
mutable lo: uint,
|
|
|
|
mutable hi: uint,
|
2011-08-04 19:24:54 -05:00
|
|
|
mutable elts: [mutable cell[T]]) {
|
2011-06-15 13:19:50 -05:00
|
|
|
fn size() -> uint { ret nelts; }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn add_front(t: &T) {
|
|
|
|
let oldlo: uint = lo;
|
|
|
|
if lo == 0u {
|
2011-07-12 16:20:15 -05:00
|
|
|
lo = ivec::len[cell[T]](elts) - 1u;
|
2011-06-15 13:19:50 -05:00
|
|
|
} else { lo -= 1u; }
|
2011-07-27 07:19:39 -05:00
|
|
|
if lo == hi {
|
2011-06-15 13:19:50 -05:00
|
|
|
elts = grow[T](nelts, oldlo, elts);
|
2011-07-12 16:20:15 -05:00
|
|
|
lo = ivec::len[cell[T]](elts) - 1u;
|
2011-06-15 13:19:50 -05:00
|
|
|
hi = nelts;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
elts.(lo) = option::some[T](t);
|
|
|
|
nelts += 1u;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn add_back(t: &T) {
|
|
|
|
if lo == hi && nelts != 0u {
|
2011-06-15 13:19:50 -05:00
|
|
|
elts = grow[T](nelts, lo, elts);
|
|
|
|
lo = 0u;
|
|
|
|
hi = nelts;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
elts.(hi) = option::some[T](t);
|
2011-07-12 16:20:15 -05:00
|
|
|
hi = (hi + 1u) % ivec::len[cell[T]](elts);
|
2011-06-15 13:19:50 -05:00
|
|
|
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 {
|
2011-07-27 07:19:39 -05:00
|
|
|
let t: T = get[T](elts, lo);
|
2011-06-15 13:19:50 -05:00
|
|
|
elts.(lo) = option::none[T];
|
2011-07-12 16:20:15 -05:00
|
|
|
lo = (lo + 1u) % ivec::len[cell[T]](elts);
|
2011-06-15 13:19:50 -05:00
|
|
|
nelts -= 1u;
|
|
|
|
ret t;
|
|
|
|
}
|
|
|
|
fn pop_back() -> T {
|
2011-07-27 07:19:39 -05:00
|
|
|
if hi == 0u {
|
2011-07-12 16:20:15 -05:00
|
|
|
hi = ivec::len[cell[T]](elts) - 1u;
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { hi -= 1u; }
|
|
|
|
let t: T = get[T](elts, hi);
|
2011-06-15 13:19:50 -05:00
|
|
|
elts.(hi) = option::none[T];
|
|
|
|
nelts -= 1u;
|
|
|
|
ret t;
|
|
|
|
}
|
|
|
|
fn peek_front() -> T { ret get[T](elts, lo); }
|
|
|
|
fn peek_back() -> T { ret get[T](elts, hi - 1u); }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn get(i: int) -> T {
|
|
|
|
let idx: uint = (lo + (i as uint)) % ivec::len[cell[T]](elts);
|
2011-06-15 13:19:50 -05:00
|
|
|
ret get[T](elts, idx);
|
|
|
|
}
|
|
|
|
}
|
2011-08-04 19:24:54 -05:00
|
|
|
let v: [mutable cell[T]] =
|
2011-07-12 16:20:15 -05:00
|
|
|
ivec::init_elt_mut(option::none, initial_capacity);
|
2010-09-22 17:44:13 -05:00
|
|
|
ret deque[T](0u, 0u, 0u, v);
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2010-09-22 17:44:13 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-09-22 17:44:13 -05:00
|
|
|
// End:
|