From 6f4d86ed904ed7ae04edc4226a0a1c2d39067d57 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 21 Jan 2013 21:59:19 -0500 Subject: [PATCH] add a base Container trait --- src/libcore/container.rs | 10 +++++++++- src/libcore/send_map.rs | 26 +++++++++++--------------- src/libstd/priority_queue.rs | 16 +++++++++------- src/libstd/treemap.rs | 30 +++++++++++++++++------------- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/libcore/container.rs b/src/libcore/container.rs index dd556aec45f..062416838cc 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -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); } diff --git a/src/libcore/send_map.rs b/src/libcore/send_map.rs index 812b138097a..dc4e24c4f8a 100644 --- a/src/libcore/send_map.rs +++ b/src/libcore/send_map.rs @@ -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 LinearMap: Container { + pure fn len(&self) -> uint { self.size } + pure fn is_empty(&self) -> bool { self.len() == 0 } + } + impl LinearMap: 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 LinearSet: Container { + pure fn len(&self) -> uint { self.map.len() } + pure fn is_empty(&self) -> bool { self.map.is_empty() } + } + impl LinearSet: Mutable { fn clear(&mut self) { self.map.clear() } } @@ -486,12 +488,6 @@ pub mod linear { impl LinearSet { /// Create an empty LinearSet static fn new() -> LinearSet { 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() } } } diff --git a/src/libstd/priority_queue.rs b/src/libstd/priority_queue.rs index 0ff40b6421a..ee78fafb60b 100644 --- a/src/libstd/priority_queue.rs +++ b/src/libstd/priority_queue.rs @@ -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 { priv data: ~[T], } +impl PriorityQueue: 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 PriorityQueue: Mutable { /// Drop all items from the queue fn clear(&mut self) { self.data.truncate(0) } @@ -39,12 +47,6 @@ impl PriorityQueue { 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() } diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index ff60d7d1c2b..0bb8738773c 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -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 TreeMap: Eq { pure fn ne(&self, other: &TreeMap) -> bool { !self.eq(other) } } +impl TreeMap: 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 TreeMap: Mutable { /// Clear the map, removing all key-value pairs. fn clear(&mut self) { @@ -112,12 +120,6 @@ impl TreeMap { /// Create an empty TreeMap static pure fn new() -> TreeMap { 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 TreeSet: Eq { pure fn ne(&self, other: &TreeSet) -> bool { self.map != other.map } } +impl TreeSet: 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 TreeSet: Mutable { /// Clear the set, removing all values. fn clear(&mut self) { self.map.clear() } @@ -230,12 +240,6 @@ impl TreeSet { /// Create an empty TreeSet static pure fn new() -> TreeSet { 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() }