Skip to content

Commit d635a6e

Browse files
committed
add a container::Map trait
1 parent ffb9049 commit d635a6e

File tree

3 files changed

+91
-82
lines changed

3 files changed

+91
-82
lines changed

src/libcore/container.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,37 @@
1010

1111
//! Container traits
1212
13+
#[forbid(deprecated_mode)];
14+
#[forbid(deprecated_pattern)];
15+
1316
pub trait Mutable {
1417
/// Clear the container, removing all values.
1518
fn clear(&mut self);
1619
}
1720

21+
pub trait Map<K, V>: Mutable {
22+
/// Return true if the map contains a value for the specified key
23+
pure fn contains_key(&self, key: &K) -> bool;
24+
25+
/// Visit all key-value pairs
26+
pure fn each(&self, f: fn(&K, &V) -> bool);
27+
28+
/// Visit all keys
29+
pure fn each_key(&self, f: fn(&K) -> bool);
30+
31+
/// Visit all values
32+
pure fn each_value(&self, f: fn(&V) -> bool);
33+
34+
/// Insert a key-value pair into the map. An existing value for a
35+
/// key is replaced by the new value. Return true if the key did
36+
/// not already exist in the map.
37+
fn insert(&mut self, key: K, value: V) -> bool;
38+
39+
/// Remove a key-value pair from the map. Return true if the key
40+
/// was present in the map, otherwise false.
41+
fn remove(&mut self, key: &K) -> bool;
42+
}
43+
1844
pub trait Set<T>: Mutable {
1945
/// Return true if the set contains a value
2046
pure fn contains(&self, value: &T) -> bool;

src/libcore/send_map.rs

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,10 @@ use hash::Hash;
2323
use prelude::*;
2424
use to_bytes::IterBytes;
2525

26-
pub trait SendMap<K:Eq Hash, V: Copy> {
27-
// FIXME(#3148) ^^^^ once find_ref() works, we can drop V:copy
28-
29-
fn insert(&mut self, k: K, +v: V) -> bool;
30-
fn remove(&mut self, k: &K) -> bool;
31-
fn pop(&mut self, k: &K) -> Option<V>;
32-
fn swap(&mut self, k: K, +v: V) -> Option<V>;
33-
fn consume(&mut self, f: fn(K, V));
34-
pure fn len(&const self) -> uint;
35-
pure fn is_empty(&const self) -> bool;
36-
pure fn contains_key(&const self, k: &K) -> bool;
37-
pure fn each(&self, blk: fn(k: &K, v: &V) -> bool);
38-
pure fn each_key_ref(&self, blk: fn(k: &K) -> bool);
39-
pure fn each_value_ref(&self, blk: fn(v: &V) -> bool);
40-
pure fn find(&const self, k: &K) -> Option<V>;
41-
pure fn get(&const self, k: &K) -> V;
42-
pure fn find_ref(&self, k: &K) -> Option<&self/V>;
43-
pure fn get_ref(&self, k: &K) -> &self/V;
44-
}
45-
4626
/// Open addressing with linear probing.
4727
pub mod linear {
4828
use iter::BaseIter;
49-
use container::{Mutable, Set};
29+
use container::{Mutable, Map, Set};
5030
use cmp::Eq;
5131
use cmp;
5232
use hash::Hash;
@@ -287,7 +267,34 @@ pub mod linear {
287267
}
288268
}
289269

290-
impl<K:Hash IterBytes Eq,V> LinearMap<K,V> {
270+
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Map<K, V> {
271+
pure fn contains_key(&self, k: &K) -> bool {
272+
match self.bucket_for_key(self.buckets, k) {
273+
FoundEntry(_) => {true}
274+
TableFull | FoundHole(_) => {false}
275+
}
276+
}
277+
278+
pure fn each(&self, blk: fn(k: &K, v: &V) -> bool) {
279+
for vec::each(self.buckets) |slot| {
280+
let mut broke = false;
281+
do slot.iter |bucket| {
282+
if !blk(&bucket.key, &bucket.value) {
283+
broke = true; // FIXME(#3064) just write "break;"
284+
}
285+
}
286+
if broke { break; }
287+
}
288+
}
289+
290+
pure fn each_key(&self, blk: fn(k: &K) -> bool) {
291+
self.each(|k, _v| blk(k))
292+
}
293+
294+
pure fn each_value(&self, blk: fn(v: &V) -> bool) {
295+
self.each(|_k, v| blk(v))
296+
}
297+
291298
fn insert(&mut self, k: K, v: V) -> bool {
292299
if self.size >= self.resize_at {
293300
// n.b.: We could also do this after searching, so
@@ -309,7 +316,9 @@ pub mod linear {
309316
None => false,
310317
}
311318
}
319+
}
312320

321+
impl<K:Hash IterBytes Eq,V> LinearMap<K,V> {
313322
fn pop(&mut self, k: &K) -> Option<V> {
314323
let hash = k.hash_keyed(self.k0, self.k1) as uint;
315324
self.pop_internal(hash, k)
@@ -363,14 +372,6 @@ pub mod linear {
363372
self.len() == 0
364373
}
365374

366-
pure fn contains_key(&const self,
367-
k: &K) -> bool {
368-
match self.bucket_for_key(self.buckets, k) {
369-
FoundEntry(_) => {true}
370-
TableFull | FoundHole(_) => {false}
371-
}
372-
}
373-
374375
pure fn find_ref(&self, k: &K) -> Option<&self/V> {
375376
match self.bucket_for_key(self.buckets, k) {
376377
FoundEntry(idx) => {
@@ -397,26 +398,6 @@ pub mod linear {
397398
None => fail fmt!("No entry found for key: %?", k),
398399
}
399400
}
400-
401-
pure fn each(&self, blk: fn(k: &K, v: &V) -> bool) {
402-
for vec::each(self.buckets) |slot| {
403-
let mut broke = false;
404-
do slot.iter |bucket| {
405-
if !blk(&bucket.key, &bucket.value) {
406-
broke = true; // FIXME(#3064) just write "break;"
407-
}
408-
}
409-
if broke { break; }
410-
}
411-
}
412-
413-
pure fn each_key(&self, blk: fn(k: &K) -> bool) {
414-
self.each(|k, _v| blk(k))
415-
}
416-
417-
pure fn each_value(&self, blk: fn(v: &V) -> bool) {
418-
self.each(|_k, v| blk(v))
419-
}
420401
}
421402

422403
impl<K:Hash IterBytes Eq, V: Copy> LinearMap<K,V> {

src/libstd/treemap.rs

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

17-
use core::container::{Mutable, Set};
17+
use core::container::{Mutable, Map, Set};
1818
use core::cmp::{Eq, Ord};
1919
use core::option::{Option, Some, None};
2020
use core::prelude::*;
@@ -75,6 +75,39 @@ impl <K: Ord, V> TreeMap<K, V>: Mutable {
7575
}
7676
}
7777

78+
impl <K: Ord, V> TreeMap<K, V>: Map<K, V> {
79+
/// Return true if the map contains a value for the specified key
80+
pure fn contains_key(&self, key: &K) -> bool {
81+
self.find(key).is_some()
82+
}
83+
84+
/// Visit all key-value pairs in order
85+
pure fn each(&self, f: fn(&K, &V) -> bool) { each(&self.root, f) }
86+
87+
/// Visit all keys in order
88+
pure fn each_key(&self, f: fn(&K) -> bool) { self.each(|k, _| f(k)) }
89+
90+
/// Visit all values in order
91+
pure fn each_value(&self, f: fn(&V) -> bool) { self.each(|_, v| f(v)) }
92+
93+
/// Insert a key-value pair into the map. An existing value for a
94+
/// key is replaced by the new value. Return true if the key did
95+
/// not already exist in the map.
96+
fn insert(&mut self, key: K, value: V) -> bool {
97+
let ret = insert(&mut self.root, key, value);
98+
if ret { self.length += 1 }
99+
ret
100+
}
101+
102+
/// Remove a key-value pair from the map. Return true if the key
103+
/// was present in the map, otherwise false.
104+
fn remove(&mut self, key: &K) -> bool {
105+
let ret = remove(&mut self.root, key);
106+
if ret { self.length -= 1 }
107+
ret
108+
}
109+
}
110+
78111
impl <K: Ord, V> TreeMap<K, V> {
79112
/// Create an empty TreeMap
80113
static pure fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
@@ -88,15 +121,6 @@ impl <K: Ord, V> TreeMap<K, V> {
88121
/// Return true if the map contains some elements
89122
pure fn is_not_empty(&self) -> bool { self.root.is_some() }
90123

91-
/// Visit all key-value pairs in order
92-
pure fn each(&self, f: fn(&K, &V) -> bool) { each(&self.root, f) }
93-
94-
/// Visit all keys in order
95-
pure fn each_key(&self, f: fn(&K) -> bool) { self.each(|k, _| f(k)) }
96-
97-
/// Visit all values in order
98-
pure fn each_value(&self, f: fn(&V) -> bool) { self.each(|_, v| f(v)) }
99-
100124
/// Visit all key-value pairs in reverse order
101125
pure fn each_reverse(&self, f: fn(&K, &V) -> bool) {
102126
each_reverse(&self.root, f);
@@ -112,11 +136,6 @@ impl <K: Ord, V> TreeMap<K, V> {
112136
self.each_reverse(|_, v| f(v))
113137
}
114138

115-
/// Return true if the map contains a value for the specified key
116-
pure fn contains_key(&self, key: &K) -> bool {
117-
self.find(key).is_some()
118-
}
119-
120139
/// Return the value corresponding to the key in the map
121140
pure fn find(&self, key: &K) -> Option<&self/V> {
122141
let mut current: &self/Option<~TreeNode<K, V>> = &self.root;
@@ -137,23 +156,6 @@ impl <K: Ord, V> TreeMap<K, V> {
137156
}
138157
}
139158

140-
/// Insert a key-value pair into the map. An existing value for a
141-
/// key is replaced by the new value. Return true if the key did
142-
/// not already exist in the map.
143-
fn insert(&mut self, key: K, value: V) -> bool {
144-
let ret = insert(&mut self.root, key, value);
145-
if ret { self.length += 1 }
146-
ret
147-
}
148-
149-
/// Remove a key-value pair from the map. Return true if the key
150-
/// was present in the map, otherwise false.
151-
fn remove(&mut self, key: &K) -> bool {
152-
let ret = remove(&mut self.root, key);
153-
if ret { self.length -= 1 }
154-
ret
155-
}
156-
157159
/// Get a lazy iterator over the key-value pairs in the map.
158160
/// Requires that it be frozen (immutable).
159161
pure fn iter(&self) -> TreeMapIterator/&self<K, V> {

0 commit comments

Comments
 (0)