1
Fork 0

add a base Container trait

This commit is contained in:
Daniel Micay 2013-01-21 21:59:19 -05:00
parent d635a6e506
commit 6f4d86ed90
4 changed files with 46 additions and 36 deletions

View file

@ -13,7 +13,15 @@
#[forbid(deprecated_mode)]; #[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)]; #[forbid(deprecated_pattern)];
pub trait Mutable { pub trait Container {
/// Return the number of elements in the container
pure fn len(&self) -> uint;
/// Return true if the container contains no elements
pure fn is_empty(&self) -> bool;
}
pub trait Mutable: Container {
/// Clear the container, removing all values. /// Clear the container, removing all values.
fn clear(&mut self); fn clear(&mut self);
} }

View file

@ -26,7 +26,7 @@ use to_bytes::IterBytes;
/// Open addressing with linear probing. /// Open addressing with linear probing.
pub mod linear { pub mod linear {
use iter::BaseIter; use iter::BaseIter;
use container::{Mutable, Map, Set}; use container::{Container, Mutable, Map, Set};
use cmp::Eq; use cmp::Eq;
use cmp; use cmp;
use hash::Hash; use hash::Hash;
@ -258,6 +258,11 @@ pub mod linear {
} }
} }
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Container {
pure fn len(&self) -> uint { self.size }
pure fn is_empty(&self) -> bool { self.len() == 0 }
}
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Mutable { impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Mutable {
fn clear(&mut self) { fn clear(&mut self) {
for uint::range(0, self.buckets.len()) |idx| { for uint::range(0, self.buckets.len()) |idx| {
@ -364,14 +369,6 @@ pub mod linear {
} }
} }
pure fn len(&const self) -> uint {
self.size
}
pure fn is_empty(&const self) -> bool {
self.len() == 0
}
pure fn find_ref(&self, k: &K) -> Option<&self/V> { pure fn find_ref(&self, k: &K) -> Option<&self/V> {
match self.bucket_for_key(self.buckets, k) { match self.bucket_for_key(self.buckets, k) {
FoundEntry(idx) => { FoundEntry(idx) => {
@ -464,6 +461,11 @@ pub mod linear {
} }
} }
impl <T: Hash IterBytes Eq> LinearSet<T>: Container {
pure fn len(&self) -> uint { self.map.len() }
pure fn is_empty(&self) -> bool { self.map.is_empty() }
}
impl <T: Hash IterBytes Eq> LinearSet<T>: Mutable { impl <T: Hash IterBytes Eq> LinearSet<T>: Mutable {
fn clear(&mut self) { self.map.clear() } fn clear(&mut self) { self.map.clear() }
} }
@ -486,12 +488,6 @@ pub mod linear {
impl <T: Hash IterBytes Eq> LinearSet<T> { impl <T: Hash IterBytes Eq> LinearSet<T> {
/// Create an empty LinearSet /// Create an empty LinearSet
static fn new() -> LinearSet<T> { LinearSet{map: LinearMap()} } static fn new() -> LinearSet<T> { LinearSet{map: LinearMap()} }
/// Return the number of elements in the set
pure fn len(&self) -> uint { self.map.len() }
/// Return true if the set contains no elements
pure fn is_empty(&self) -> bool { self.map.is_empty() }
} }
} }

View file

@ -10,7 +10,7 @@
//! A priority queue implemented with a binary heap //! A priority queue implemented with a binary heap
use core::container::Mutable; use core::container::{Container, Mutable};
use core::cmp::Ord; use core::cmp::Ord;
use core::prelude::*; use core::prelude::*;
use core::ptr::addr_of; use core::ptr::addr_of;
@ -25,6 +25,14 @@ pub struct PriorityQueue <T: Ord>{
priv data: ~[T], priv data: ~[T],
} }
impl <T: Ord> PriorityQueue<T>: Container {
/// Returns the length of the queue
pure fn len(&self) -> uint { self.data.len() }
/// Returns true if a queue contains no elements
pure fn is_empty(&self) -> bool { self.data.is_empty() }
}
impl <T: Ord> PriorityQueue<T>: Mutable { impl <T: Ord> PriorityQueue<T>: Mutable {
/// Drop all items from the queue /// Drop all items from the queue
fn clear(&mut self) { self.data.truncate(0) } fn clear(&mut self) { self.data.truncate(0) }
@ -39,12 +47,6 @@ impl <T: Ord> PriorityQueue<T> {
if self.is_empty() { None } else { Some(self.top()) } if self.is_empty() { None } else { Some(self.top()) }
} }
/// Returns the length of the queue
pure fn len(&self) -> uint { self.data.len() }
/// Returns true if a queue contains no elements
pure fn is_empty(&self) -> bool { self.data.is_empty() }
/// Returns true if a queue contains some elements /// Returns true if a queue contains some elements
pure fn is_not_empty(&self) -> bool { self.data.is_not_empty() } pure fn is_not_empty(&self) -> bool { self.data.is_not_empty() }

View file

@ -14,7 +14,7 @@
#[forbid(deprecated_mode)]; #[forbid(deprecated_mode)];
use core::container::{Mutable, Map, Set}; use core::container::{Container, Mutable, Map, Set};
use core::cmp::{Eq, Ord}; use core::cmp::{Eq, Ord};
use core::option::{Option, Some, None}; use core::option::{Option, Some, None};
use core::prelude::*; 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) } pure fn ne(&self, other: &TreeMap<K, V>) -> bool { !self.eq(other) }
} }
impl <K: Ord, V> TreeMap<K, V>: Container {
/// Return the number of elements in the map
pure fn len(&self) -> uint { self.length }
/// Return true if the map contains no elements
pure fn is_empty(&self) -> bool { self.root.is_none() }
}
impl <K: Ord, V> TreeMap<K, V>: Mutable { impl <K: Ord, V> TreeMap<K, V>: Mutable {
/// Clear the map, removing all key-value pairs. /// Clear the map, removing all key-value pairs.
fn clear(&mut self) { fn clear(&mut self) {
@ -112,12 +120,6 @@ impl <K: Ord, V> TreeMap<K, V> {
/// Create an empty TreeMap /// Create an empty TreeMap
static pure fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} } static pure fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
/// Return the number of elements in the map
pure fn len(&self) -> uint { self.length }
/// Return true if the map contains no elements
pure fn is_empty(&self) -> bool { self.root.is_none() }
/// Return true if the map contains some elements /// Return true if the map contains some elements
pure fn is_not_empty(&self) -> bool { self.root.is_some() } pure fn is_not_empty(&self) -> bool { self.root.is_some() }
@ -206,6 +208,14 @@ impl <T: Eq Ord> TreeSet<T>: Eq {
pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map } pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map }
} }
impl <T: Ord> TreeSet<T>: Container {
/// Return the number of elements in the map
pure fn len(&self) -> uint { self.map.len() }
/// Return true if the map contains no elements
pure fn is_empty(&self) -> bool { self.map.is_empty() }
}
impl <T: Ord> TreeSet<T>: Mutable { impl <T: Ord> TreeSet<T>: Mutable {
/// Clear the set, removing all values. /// Clear the set, removing all values.
fn clear(&mut self) { self.map.clear() } fn clear(&mut self) { self.map.clear() }
@ -230,12 +240,6 @@ impl <T: Ord> TreeSet<T> {
/// Create an empty TreeSet /// Create an empty TreeSet
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} } static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
/// Return the number of elements in the set
pure fn len(&self) -> uint { self.map.len() }
/// Return true if the set contains no elements
pure fn is_empty(&self) -> bool { self.map.is_empty() }
/// Return true if the set contains some elements /// Return true if the set contains some elements
pure fn is_not_empty(&self) -> bool { self.map.is_not_empty() } pure fn is_not_empty(&self) -> bool { self.map.is_not_empty() }