add a base Container trait
This commit is contained in:
parent
d635a6e506
commit
6f4d86ed90
4 changed files with 46 additions and 36 deletions
|
@ -13,7 +13,15 @@
|
|||
#[forbid(deprecated_mode)];
|
||||
#[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.
|
||||
fn clear(&mut self);
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ use to_bytes::IterBytes;
|
|||
/// Open addressing with linear probing.
|
||||
pub mod linear {
|
||||
use iter::BaseIter;
|
||||
use container::{Mutable, Map, Set};
|
||||
use container::{Container, Mutable, Map, Set};
|
||||
use cmp::Eq;
|
||||
use cmp;
|
||||
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 {
|
||||
fn clear(&mut self) {
|
||||
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> {
|
||||
match self.bucket_for_key(self.buckets, k) {
|
||||
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 {
|
||||
fn clear(&mut self) { self.map.clear() }
|
||||
}
|
||||
|
@ -486,12 +488,6 @@ pub mod linear {
|
|||
impl <T: Hash IterBytes Eq> LinearSet<T> {
|
||||
/// Create an empty LinearSet
|
||||
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() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
//! A priority queue implemented with a binary heap
|
||||
|
||||
use core::container::Mutable;
|
||||
use core::container::{Container, Mutable};
|
||||
use core::cmp::Ord;
|
||||
use core::prelude::*;
|
||||
use core::ptr::addr_of;
|
||||
|
@ -25,6 +25,14 @@ pub struct PriorityQueue <T: Ord>{
|
|||
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 {
|
||||
/// Drop all items from the queue
|
||||
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()) }
|
||||
}
|
||||
|
||||
/// 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
|
||||
pure fn is_not_empty(&self) -> bool { self.data.is_not_empty() }
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
#[forbid(deprecated_mode)];
|
||||
|
||||
use core::container::{Mutable, Map, Set};
|
||||
use core::container::{Container, Mutable, Map, 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>: 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 {
|
||||
/// Clear the map, removing all key-value pairs.
|
||||
fn clear(&mut self) {
|
||||
|
@ -112,12 +120,6 @@ impl <K: Ord, V> TreeMap<K, V> {
|
|||
/// Create an empty TreeMap
|
||||
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
|
||||
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 }
|
||||
}
|
||||
|
||||
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 {
|
||||
/// Clear the set, removing all values.
|
||||
fn clear(&mut self) { self.map.clear() }
|
||||
|
@ -230,12 +240,6 @@ impl <T: Ord> TreeSet<T> {
|
|||
/// Create an empty TreeSet
|
||||
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
|
||||
pure fn is_not_empty(&self) -> bool { self.map.is_not_empty() }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue