add a Mutable container trait with clear
This commit is contained in:
parent
66e50892c1
commit
ffb9049274
4 changed files with 68 additions and 14 deletions
|
@ -10,7 +10,12 @@
|
|||
|
||||
//! Container traits
|
||||
|
||||
pub trait Set<T> {
|
||||
pub trait Mutable {
|
||||
/// Clear the container, removing all values.
|
||||
fn clear(&mut self);
|
||||
}
|
||||
|
||||
pub trait Set<T>: Mutable {
|
||||
/// Return true if the set contains a value
|
||||
pure fn contains(&self, value: &T) -> bool;
|
||||
|
||||
|
|
|
@ -31,7 +31,6 @@ pub trait SendMap<K:Eq Hash, V: Copy> {
|
|||
fn pop(&mut self, k: &K) -> Option<V>;
|
||||
fn swap(&mut self, k: K, +v: V) -> Option<V>;
|
||||
fn consume(&mut self, f: fn(K, V));
|
||||
fn clear(&mut self);
|
||||
pure fn len(&const self) -> uint;
|
||||
pure fn is_empty(&const self) -> bool;
|
||||
pure fn contains_key(&const self, k: &K) -> bool;
|
||||
|
@ -47,7 +46,7 @@ pub trait SendMap<K:Eq Hash, V: Copy> {
|
|||
/// Open addressing with linear probing.
|
||||
pub mod linear {
|
||||
use iter::BaseIter;
|
||||
use container::Set;
|
||||
use container::{Mutable, Set};
|
||||
use cmp::Eq;
|
||||
use cmp;
|
||||
use hash::Hash;
|
||||
|
@ -279,6 +278,15 @@ pub mod linear {
|
|||
}
|
||||
}
|
||||
|
||||
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Mutable {
|
||||
fn clear(&mut self) {
|
||||
for uint::range(0, self.buckets.len()) |idx| {
|
||||
self.buckets[idx] = None;
|
||||
}
|
||||
self.size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl<K:Hash IterBytes Eq,V> LinearMap<K,V> {
|
||||
fn insert(&mut self, k: K, v: V) -> bool {
|
||||
if self.size >= self.resize_at {
|
||||
|
@ -347,13 +355,6 @@ pub mod linear {
|
|||
}
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
for uint::range(0, self.buckets.len()) |idx| {
|
||||
self.buckets[idx] = None;
|
||||
}
|
||||
self.size = 0;
|
||||
}
|
||||
|
||||
pure fn len(&const self) -> uint {
|
||||
self.size
|
||||
}
|
||||
|
@ -482,6 +483,10 @@ pub mod linear {
|
|||
}
|
||||
}
|
||||
|
||||
impl <T: Hash IterBytes Eq> LinearSet<T>: Mutable {
|
||||
fn clear(&mut self) { self.map.clear() }
|
||||
}
|
||||
|
||||
impl <T: Hash IterBytes Eq> LinearSet<T>: Set<T> {
|
||||
/// Return true if the set contains a value
|
||||
pure fn contains(&self, value: &T) -> bool {
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
//! A priority queue implemented with a binary heap
|
||||
|
||||
use core::container::Mutable;
|
||||
use core::cmp::Ord;
|
||||
use core::prelude::*;
|
||||
use core::ptr::addr_of;
|
||||
|
@ -24,6 +25,11 @@ pub struct PriorityQueue <T: Ord>{
|
|||
priv data: ~[T],
|
||||
}
|
||||
|
||||
impl <T: Ord> PriorityQueue<T>: Mutable {
|
||||
/// Drop all items from the queue
|
||||
fn clear(&mut self) { self.data.truncate(0) }
|
||||
}
|
||||
|
||||
impl <T: Ord> PriorityQueue<T> {
|
||||
/// Returns the greatest item in the queue - fails if empty
|
||||
pure fn top(&self) -> &self/T { &self.data[0] }
|
||||
|
@ -51,9 +57,6 @@ impl <T: Ord> PriorityQueue<T> {
|
|||
vec::reserve_at_least(&mut self.data, n)
|
||||
}
|
||||
|
||||
/// Drop all items from the queue
|
||||
fn clear(&mut self) { self.data.truncate(0) }
|
||||
|
||||
/// Pop the greatest item from the queue - fails if empty
|
||||
fn pop(&mut self) -> T {
|
||||
let mut item = self.data.pop();
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
#[forbid(deprecated_mode)];
|
||||
|
||||
use core::container::Set;
|
||||
use core::container::{Mutable, Set};
|
||||
use core::cmp::{Eq, Ord};
|
||||
use core::option::{Option, Some, None};
|
||||
use core::prelude::*;
|
||||
|
@ -67,6 +67,14 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
|
|||
pure fn ne(&self, other: &TreeMap<K, V>) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
impl <K: Ord, V> TreeMap<K, V>: Mutable {
|
||||
/// Clear the map, removing all key-value pairs.
|
||||
fn clear(&mut self) {
|
||||
self.root = None;
|
||||
self.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
impl <K: Ord, V> TreeMap<K, V> {
|
||||
/// Create an empty TreeMap
|
||||
static pure fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
|
||||
|
@ -196,6 +204,11 @@ impl <T: Eq Ord> TreeSet<T>: Eq {
|
|||
pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map }
|
||||
}
|
||||
|
||||
impl <T: Ord> TreeSet<T>: Mutable {
|
||||
/// Clear the set, removing all values.
|
||||
fn clear(&mut self) { self.map.clear() }
|
||||
}
|
||||
|
||||
impl <T: Ord> TreeSet<T>: Set<T> {
|
||||
/// Return true if the set contains a value
|
||||
pure fn contains(&self, value: &T) -> bool {
|
||||
|
@ -624,6 +637,20 @@ mod test_treemap {
|
|||
assert m.find(&2).unwrap() == &11;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clear() {
|
||||
let mut m = TreeMap::new();
|
||||
m.clear();
|
||||
assert m.insert(5, 11);
|
||||
assert m.insert(12, -3);
|
||||
assert m.insert(19, 2);
|
||||
m.clear();
|
||||
assert m.find(&5).is_none();
|
||||
assert m.find(&12).is_none();
|
||||
assert m.find(&19).is_none();
|
||||
assert m.is_empty();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u8_map() {
|
||||
let mut m = TreeMap::new();
|
||||
|
@ -844,6 +871,20 @@ mod test_treemap {
|
|||
mod test_set {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_clear() {
|
||||
let mut s = TreeSet::new();
|
||||
s.clear();
|
||||
assert s.insert(5);
|
||||
assert s.insert(12);
|
||||
assert s.insert(19);
|
||||
s.clear();
|
||||
assert !s.contains(&5);
|
||||
assert !s.contains(&12);
|
||||
assert !s.contains(&19);
|
||||
assert s.is_empty();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disjoint() {
|
||||
let mut xs = TreeSet::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue