Make std::map require const keys.

This commit is contained in:
Eric Holk 2012-05-31 10:26:05 -07:00
parent f394933641
commit 3acc3c4d85
2 changed files with 10 additions and 8 deletions

View file

@ -282,7 +282,7 @@ Parameters:
hasher - The hash function for key type K
eqer - The equality function for key type K
*/
fn hashmap<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>)
fn hashmap<K: const copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>)
-> hashmap<K, V> {
chained::mk(hasher, eqer)
}
@ -316,7 +316,9 @@ fn uint_hash<V: copy>() -> hashmap<uint, V> {
#[doc = "
Convenience function for adding keys to a hashmap with nil type keys
"]
fn set_add<K: copy>(set: set<K>, key: K) -> bool { ret set.insert(key, ()); }
fn set_add<K: const copy>(set: set<K>, key: K) -> bool {
ret set.insert(key, ());
}
#[doc = "
Convert a set into a vector.
@ -331,7 +333,7 @@ fn vec_from_set<T: copy>(s: set<T>) -> [T] {
}
#[doc = "Construct a hashmap from a vector"]
fn hash_from_vec<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>,
fn hash_from_vec<K: const copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>,
items: [(K, V)]) -> hashmap<K, V> {
let map = hashmap(hasher, eqer);
vec::iter(items) { |item|

View file

@ -5,18 +5,18 @@ import std::map;
import std::map::{hashmap, hashfn, eqfn};
import dvec::{dvec, extensions};
type interner<T> =
type interner<T: const> =
{map: hashmap<T, uint>,
vect: dvec<T>,
hasher: hashfn<T>,
eqer: eqfn<T>};
fn mk<T: copy>(hasher: hashfn<T>, eqer: eqfn<T>) -> interner<T> {
fn mk<T: const copy>(hasher: hashfn<T>, eqer: eqfn<T>) -> interner<T> {
let m = map::hashmap::<T, uint>(hasher, eqer);
ret {map: m, vect: dvec(), hasher: hasher, eqer: eqer};
}
fn intern<T: copy>(itr: interner<T>, val: T) -> uint {
fn intern<T: const copy>(itr: interner<T>, val: T) -> uint {
alt itr.map.find(val) {
some(idx) { ret idx; }
none {
@ -31,10 +31,10 @@ fn intern<T: copy>(itr: interner<T>, val: T) -> uint {
// |get| isn't "pure" in the traditional sense, because it can go from
// failing to returning a value as items are interned. But for typestate,
// where we first check a pred and then rely on it, ceasing to fail is ok.
pure fn get<T: copy>(itr: interner<T>, idx: uint) -> T {
pure fn get<T: const copy>(itr: interner<T>, idx: uint) -> T {
unchecked {
itr.vect.get_elt(idx)
}
}
fn len<T>(itr: interner<T>) -> uint { ret itr.vect.len(); }
fn len<T: const>(itr: interner<T>) -> uint { ret itr.vect.len(); }