rust/src/libstd/deque.rs

304 lines
8.7 KiB
Rust
Raw Normal View History

// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// 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
use core::container::{Container, Mutable};
use core::prelude::*;
use core::vec;
static 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
}
impl<T> Container for Deque<T> {
2013-03-15 12:41:02 -05:00
/// Return the number of elements in the deque
fn len(&const self) -> uint { self.nelts }
2013-03-15 12:41:02 -05:00
/// Return true if the deque contains no elements
fn is_empty(&const self) -> bool { self.len() == 0 }
}
impl<T> Mutable for Deque<T> {
2013-03-15 12:41:02 -05:00
/// Clear the deque, removing all values.
fn clear(&mut self) {
2013-03-15 12:41:02 -05:00
for self.elts.each_mut |x| { *x = None }
self.nelts = 0;
self.lo = 0;
self.hi = 0;
}
}
pub impl<T> Deque<T> {
2013-03-15 12:41:02 -05:00
/// Create an empty Deque
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
2013-03-15 12:41:02 -05:00
/// Return a reference to the first element in the deque
///
/// Fails if the deque is empty
fn peek_front(&self) -> &'self T { get(self.elts, self.lo) }
2013-03-15 12:41:02 -05:00
/// Return a reference to the last element in the deque
///
/// Fails if the deque is empty
fn peek_back(&self) -> &'self T { get(self.elts, self.hi - 1u) }
2013-02-16 18:43:29 -06:00
2013-03-15 12:41:02 -05:00
/// Retrieve an element in the deque by index
///
/// Fails if there is no element with the given index
fn get(&self, i: int) -> &'self T {
2013-02-16 19:50:14 -06:00
let idx = (self.lo + (i as uint)) % self.elts.len();
get(self.elts, idx)
}
2013-03-15 12:41:02 -05:00
/// Remove and return the first element in the deque
///
/// Fails if the deque is empty
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
}
2013-03-15 12:41:02 -05:00
/// Remove and return the last element in the deque
///
/// Fails if the deque is empty
2013-02-16 18:43:29 -06:00
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-03-15 12:41:02 -05:00
/// Prepend an element to the deque
fn add_front(&mut self, t: T) {
2013-02-16 18:10:10 -06:00
let oldlo = self.lo;
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;
}
self.elts[self.lo] = Some(t);
self.nelts += 1u;
}
2013-03-15 12:41:02 -05:00
/// Append an element to the deque
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;
}
}
2012-01-11 05:49:33 -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>] {
fail_unless!(nelts == elts.len());
let mut rv = ~[];
2013-02-17 13:08:04 -06:00
do rv.grow_fn(nelts + 1) |i| {
2013-02-16 20:34:09 -06:00
let mut element = None;
element <-> elts[(lo + i) % nelts];
element
}
rv
}
2013-01-22 10:44:24 -06:00
fn get<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!() }
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
use super::*;
use core::cmp::Eq;
use core::kinds::{Durable, Copy};
2012-01-17 21:05:07 -06:00
#[test]
fn test_simple() {
let mut d = Deque::new();
fail_unless!(d.len() == 0u);
2012-01-17 21:05:07 -06:00
d.add_front(17);
d.add_front(42);
d.add_back(137);
fail_unless!(d.len() == 3u);
2012-01-17 21:05:07 -06:00
d.add_back(137);
fail_unless!(d.len() == 4u);
2013-03-08 14:39:42 -06:00
debug!(d.peek_front());
fail_unless!(*d.peek_front() == 42);
2013-03-08 14:39:42 -06:00
debug!(d.peek_back());
fail_unless!(*d.peek_back() == 137);
let mut i: int = d.pop_front();
2013-03-08 14:39:42 -06:00
debug!(i);
fail_unless!(i == 42);
2012-01-17 21:05:07 -06:00
i = d.pop_back();
2013-03-08 14:39:42 -06:00
debug!(i);
fail_unless!(i == 137);
2012-01-17 21:05:07 -06:00
i = d.pop_back();
2013-03-08 14:39:42 -06:00
debug!(i);
fail_unless!(i == 137);
2012-01-17 21:05:07 -06:00
i = d.pop_back();
2013-03-08 14:39:42 -06:00
debug!(i);
fail_unless!(i == 17);
fail_unless!(d.len() == 0u);
2012-01-17 21:05:07 -06:00
d.add_back(3);
fail_unless!(d.len() == 1u);
2012-01-17 21:05:07 -06:00
d.add_front(2);
fail_unless!(d.len() == 2u);
2012-01-17 21:05:07 -06:00
d.add_back(4);
fail_unless!(d.len() == 3u);
2012-01-17 21:05:07 -06:00
d.add_front(1);
fail_unless!(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));
fail_unless!(*d.get(0) == 1);
fail_unless!(*d.get(1) == 2);
fail_unless!(*d.get(2) == 3);
fail_unless!(*d.get(3) == 4);
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_boxes() {
let a: @int = @5;
let b: @int = @72;
let c: @int = @64;
let d: @int = @175;
let mut deq = Deque::new();
fail_unless!(deq.len() == 0);
2012-01-17 21:05:07 -06:00
deq.add_front(a);
deq.add_front(b);
deq.add_back(c);
fail_unless!(deq.len() == 3);
2012-01-17 21:05:07 -06:00
deq.add_back(d);
fail_unless!(deq.len() == 4);
fail_unless!(*deq.peek_front() == b);
fail_unless!(*deq.peek_back() == d);
fail_unless!(deq.pop_front() == b);
fail_unless!(deq.pop_back() == d);
fail_unless!(deq.pop_back() == c);
fail_unless!(deq.pop_back() == a);
fail_unless!(deq.len() == 0);
2012-01-17 21:05:07 -06:00
deq.add_back(c);
fail_unless!(deq.len() == 1);
2012-01-17 21:05:07 -06:00
deq.add_front(b);
fail_unless!(deq.len() == 2);
2012-01-17 21:05:07 -06:00
deq.add_back(d);
fail_unless!(deq.len() == 3);
2012-01-17 21:05:07 -06:00
deq.add_front(a);
fail_unless!(deq.len() == 4);
fail_unless!(*deq.get(0) == a);
fail_unless!(*deq.get(1) == b);
fail_unless!(*deq.get(2) == c);
fail_unless!(*deq.get(3) == d);
2012-01-17 21:05:07 -06:00
}
fn test_parameterized<T:Copy + Eq + Durable>(a: T, b: T, c: T, d: T) {
let mut deq = Deque::new();
fail_unless!(deq.len() == 0);
2012-01-17 21:05:07 -06:00
deq.add_front(a);
deq.add_front(b);
deq.add_back(c);
fail_unless!(deq.len() == 3);
2012-01-17 21:05:07 -06:00
deq.add_back(d);
fail_unless!(deq.len() == 4);
fail_unless!(*deq.peek_front() == b);
fail_unless!(*deq.peek_back() == d);
fail_unless!(deq.pop_front() == b);
fail_unless!(deq.pop_back() == d);
fail_unless!(deq.pop_back() == c);
fail_unless!(deq.pop_back() == a);
fail_unless!(deq.len() == 0);
2012-01-17 21:05:07 -06:00
deq.add_back(c);
fail_unless!(deq.len() == 1);
2012-01-17 21:05:07 -06:00
deq.add_front(b);
fail_unless!(deq.len() == 2);
2012-01-17 21:05:07 -06:00
deq.add_back(d);
fail_unless!(deq.len() == 3);
2012-01-17 21:05:07 -06:00
deq.add_front(a);
fail_unless!(deq.len() == 4);
fail_unless!(*deq.get(0) == a);
fail_unless!(*deq.get(1) == b);
fail_unless!(*deq.get(2) == c);
fail_unless!(*deq.get(3) == d);
2012-01-17 21:05:07 -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
#[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
}
#[deriving(Eq)]
2013-01-22 10:44:24 -06:00
struct RecCy {
x: int,
y: int,
t: Taggy
}
#[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-01-17 21:05:07 -06: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-01-17 21:05:07 -06: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) };
test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
2012-01-17 21:05:07 -06:00
}
2012-07-03 12:52:32 -05:00
}