Add an overloaded [] function to the map interface. Closes #2730.

This commit is contained in:
Michael Sullivan 2012-06-27 16:34:53 -07:00
parent 0c42a3ffee
commit df9b43e27c
2 changed files with 8 additions and 0 deletions

View file

@ -45,6 +45,9 @@ iface map<K, V: copy> {
"] "]
fn get(K) -> V; fn get(K) -> V;
#[doc = "Like get, but as an operator."]
fn [](K) -> V;
#[doc = " #[doc = "
Get the value for the specified key. If the key does not exist in Get the value for the specified key. If the key does not exist in
the map then returns none. the map then returns none.
@ -232,6 +235,10 @@ mod chained {
option::get(self.find(k)) option::get(self.find(k))
} }
fn [](k: K) -> V {
option::get(self.find(k))
}
fn remove(k: K) -> option<V> { fn remove(k: K) -> option<V> {
alt self.search_tbl(k, self.hasher(k)) { alt self.search_tbl(k, self.hasher(k)) {
not_found {none} not_found {none}

View file

@ -79,6 +79,7 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
contains_key(self, key) contains_key(self, key)
} }
fn get(&&key: uint) -> V { get(self, key) } fn get(&&key: uint) -> V { get(self, key) }
fn [](&&key: uint) -> V { get(self, key) }
fn find(&&key: uint) -> option<V> { find(self, key) } fn find(&&key: uint) -> option<V> { find(self, key) }
fn rehash() { fail } fn rehash() { fail }
fn each(it: fn(&&uint, V) -> bool) { fn each(it: fn(&&uint, V) -> bool) {