Skip to content

Commit 6f4d86e

Browse files
committed
add a base Container trait
1 parent d635a6e commit 6f4d86e

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

src/libcore/container.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
#[forbid(deprecated_mode)];
1414
#[forbid(deprecated_pattern)];
1515

16-
pub trait Mutable {
16+
pub trait Container {
17+
/// Return the number of elements in the container
18+
pure fn len(&self) -> uint;
19+
20+
/// Return true if the container contains no elements
21+
pure fn is_empty(&self) -> bool;
22+
}
23+
24+
pub trait Mutable: Container {
1725
/// Clear the container, removing all values.
1826
fn clear(&mut self);
1927
}

src/libcore/send_map.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use to_bytes::IterBytes;
2626
/// Open addressing with linear probing.
2727
pub mod linear {
2828
use iter::BaseIter;
29-
use container::{Mutable, Map, Set};
29+
use container::{Container, Mutable, Map, Set};
3030
use cmp::Eq;
3131
use cmp;
3232
use hash::Hash;
@@ -258,6 +258,11 @@ pub mod linear {
258258
}
259259
}
260260

261+
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Container {
262+
pure fn len(&self) -> uint { self.size }
263+
pure fn is_empty(&self) -> bool { self.len() == 0 }
264+
}
265+
261266
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Mutable {
262267
fn clear(&mut self) {
263268
for uint::range(0, self.buckets.len()) |idx| {
@@ -364,14 +369,6 @@ pub mod linear {
364369
}
365370
}
366371

367-
pure fn len(&const self) -> uint {
368-
self.size
369-
}
370-
371-
pure fn is_empty(&const self) -> bool {
372-
self.len() == 0
373-
}
374-
375372
pure fn find_ref(&self, k: &K) -> Option<&self/V> {
376373
match self.bucket_for_key(self.buckets, k) {
377374
FoundEntry(idx) => {
@@ -464,6 +461,11 @@ pub mod linear {
464461
}
465462
}
466463

464+
impl <T: Hash IterBytes Eq> LinearSet<T>: Container {
465+
pure fn len(&self) -> uint { self.map.len() }
466+
pure fn is_empty(&self) -> bool { self.map.is_empty() }
467+
}
468+
467469
impl <T: Hash IterBytes Eq> LinearSet<T>: Mutable {
468470
fn clear(&mut self) { self.map.clear() }
469471
}
@@ -486,12 +488,6 @@ pub mod linear {
486488
impl <T: Hash IterBytes Eq> LinearSet<T> {
487489
/// Create an empty LinearSet
488490
static fn new() -> LinearSet<T> { LinearSet{map: LinearMap()} }
489-
490-
/// Return the number of elements in the set
491-
pure fn len(&self) -> uint { self.map.len() }
492-
493-
/// Return true if the set contains no elements
494-
pure fn is_empty(&self) -> bool { self.map.is_empty() }
495491
}
496492
}
497493

src/libstd/priority_queue.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! A priority queue implemented with a binary heap
1212
13-
use core::container::Mutable;
13+
use core::container::{Container, Mutable};
1414
use core::cmp::Ord;
1515
use core::prelude::*;
1616
use core::ptr::addr_of;
@@ -25,6 +25,14 @@ pub struct PriorityQueue <T: Ord>{
2525
priv data: ~[T],
2626
}
2727

28+
impl <T: Ord> PriorityQueue<T>: Container {
29+
/// Returns the length of the queue
30+
pure fn len(&self) -> uint { self.data.len() }
31+
32+
/// Returns true if a queue contains no elements
33+
pure fn is_empty(&self) -> bool { self.data.is_empty() }
34+
}
35+
2836
impl <T: Ord> PriorityQueue<T>: Mutable {
2937
/// Drop all items from the queue
3038
fn clear(&mut self) { self.data.truncate(0) }
@@ -39,12 +47,6 @@ impl <T: Ord> PriorityQueue<T> {
3947
if self.is_empty() { None } else { Some(self.top()) }
4048
}
4149

42-
/// Returns the length of the queue
43-
pure fn len(&self) -> uint { self.data.len() }
44-
45-
/// Returns true if a queue contains no elements
46-
pure fn is_empty(&self) -> bool { self.data.is_empty() }
47-
4850
/// Returns true if a queue contains some elements
4951
pure fn is_not_empty(&self) -> bool { self.data.is_not_empty() }
5052

src/libstd/treemap.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
#[forbid(deprecated_mode)];
1616

17-
use core::container::{Mutable, Map, Set};
17+
use core::container::{Container, Mutable, Map, Set};
1818
use core::cmp::{Eq, Ord};
1919
use core::option::{Option, Some, None};
2020
use core::prelude::*;
@@ -67,6 +67,14 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
6767
pure fn ne(&self, other: &TreeMap<K, V>) -> bool { !self.eq(other) }
6868
}
6969

70+
impl <K: Ord, V> TreeMap<K, V>: Container {
71+
/// Return the number of elements in the map
72+
pure fn len(&self) -> uint { self.length }
73+
74+
/// Return true if the map contains no elements
75+
pure fn is_empty(&self) -> bool { self.root.is_none() }
76+
}
77+
7078
impl <K: Ord, V> TreeMap<K, V>: Mutable {
7179
/// Clear the map, removing all key-value pairs.
7280
fn clear(&mut self) {
@@ -112,12 +120,6 @@ impl <K: Ord, V> TreeMap<K, V> {
112120
/// Create an empty TreeMap
113121
static pure fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
114122

115-
/// Return the number of elements in the map
116-
pure fn len(&self) -> uint { self.length }
117-
118-
/// Return true if the map contains no elements
119-
pure fn is_empty(&self) -> bool { self.root.is_none() }
120-
121123
/// Return true if the map contains some elements
122124
pure fn is_not_empty(&self) -> bool { self.root.is_some() }
123125

@@ -206,6 +208,14 @@ impl <T: Eq Ord> TreeSet<T>: Eq {
206208
pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map }
207209
}
208210

211+
impl <T: Ord> TreeSet<T>: Container {
212+
/// Return the number of elements in the map
213+
pure fn len(&self) -> uint { self.map.len() }
214+
215+
/// Return true if the map contains no elements
216+
pure fn is_empty(&self) -> bool { self.map.is_empty() }
217+
}
218+
209219
impl <T: Ord> TreeSet<T>: Mutable {
210220
/// Clear the set, removing all values.
211221
fn clear(&mut self) { self.map.clear() }
@@ -230,12 +240,6 @@ impl <T: Ord> TreeSet<T> {
230240
/// Create an empty TreeSet
231241
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
232242

233-
/// Return the number of elements in the set
234-
pure fn len(&self) -> uint { self.map.len() }
235-
236-
/// Return true if the set contains no elements
237-
pure fn is_empty(&self) -> bool { self.map.is_empty() }
238-
239243
/// Return true if the set contains some elements
240244
pure fn is_not_empty(&self) -> bool { self.map.is_not_empty() }
241245

0 commit comments

Comments
 (0)