Skip to content

Commit 274e75c

Browse files
committed
implement container::Container for SmallIntMap
1 parent aee7929 commit 274e75c

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/libstd/smallintmap.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
*/
1515
#[forbid(deprecated_mode)];
1616

17-
use map;
1817
use map::StdMap;
1918

19+
use core::container::{Container, Mutable, Map, Set};
2020
use core::dvec::DVec;
2121
use core::ops;
2222
use core::option::{Some, None};
@@ -80,9 +80,9 @@ pub pure fn contains_key<T: Copy>(self: SmallIntMap<T>, key: uint) -> bool {
8080
return !find(self, key).is_none();
8181
}
8282

83-
/// Implements the map::map interface for smallintmap
84-
impl<V: Copy> SmallIntMap<V>: map::StdMap<uint, V> {
85-
pure fn size() -> uint {
83+
impl<V> SmallIntMap<V>: Container {
84+
/// Return the number of elements in the map
85+
pure fn len(&self) -> uint {
8686
let mut sz = 0u;
8787
for self.v.each |item| {
8888
match *item {
@@ -92,6 +92,14 @@ impl<V: Copy> SmallIntMap<V>: map::StdMap<uint, V> {
9292
}
9393
sz
9494
}
95+
96+
/// Return true if the map contains no elements
97+
pure fn is_empty(&self) -> bool { self.len() == 0 }
98+
}
99+
100+
/// Implements the map::map interface for smallintmap
101+
impl<V: Copy> SmallIntMap<V>: StdMap<uint, V> {
102+
pure fn size() -> uint { self.len() }
95103
#[inline(always)]
96104
fn insert(key: uint, value: V) -> bool {
97105
let exists = contains_key(self, key);
@@ -165,8 +173,8 @@ impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
165173
}
166174

167175
/// Cast the given smallintmap to a map::map
168-
pub fn as_map<V: Copy>(s: SmallIntMap<V>) -> map::StdMap<uint, V> {
169-
s as map::StdMap::<uint, V>
176+
pub fn as_map<V: Copy>(s: SmallIntMap<V>) -> StdMap<uint, V> {
177+
s as StdMap::<uint, V>
170178
}
171179

172180
#[cfg(test)]
@@ -176,6 +184,22 @@ mod tests {
176184
use core::option::None;
177185
use core::option;
178186

187+
#[test]
188+
fn test_len() {
189+
let mut map = mk();
190+
assert map.len() == 0;
191+
assert map.is_empty();
192+
map.insert(5, 20);
193+
assert map.len() == 1;
194+
assert !map.is_empty();
195+
map.insert(11, 12);
196+
assert map.len() == 2;
197+
assert !map.is_empty();
198+
map.insert(14, 22);
199+
assert map.len() == 3;
200+
assert !map.is_empty();
201+
}
202+
179203
#[test]
180204
fn test_insert_with_key() {
181205
let map: SmallIntMap<uint> = mk();

0 commit comments

Comments
 (0)