about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMichael Sullivan <sully@msully.net>2011-07-21 18:14:39 -0700
committerMichael Sullivan <sully@msully.net>2011-07-21 18:14:39 -0700
commit4b59ae0aa9051e6a5fbec43d0c9bb8a12c0711e0 (patch)
tree9b5960aaa6d03643d2052bfebff73d56ec203251 /src
parentcb024253765c9537d1fa1f789caa4d3f9afe03e8 (diff)
Add some support for using a map like a set.
Diffstat (limited to 'src')
-rw-r--r--src/comp/middle/ast_map.rs5
-rw-r--r--src/lib/map.rs17
2 files changed, 20 insertions, 2 deletions
diff --git a/src/comp/middle/ast_map.rs b/src/comp/middle/ast_map.rs
index aa71242f9c8..78f99f75f18 100644
--- a/src/comp/middle/ast_map.rs
+++ b/src/comp/middle/ast_map.rs
@@ -105,6 +105,11 @@ fn new_smallintmap_adapter[K, V](fn(&K) -> uint key_idx,
                 idx += 1u;
             }
         }
+        iter keys() -> K {
+            for each (@tup(K, V) p in self.items()) {
+                put p._0;
+            }
+        }
     }
 
     auto map = smallintmap::mk[V]();
diff --git a/src/lib/map.rs b/src/lib/map.rs
index 75b4899b3e1..61572308427 100644
--- a/src/lib/map.rs
+++ b/src/lib/map.rs
@@ -1,6 +1,5 @@
 /**
- * At the moment, this is a partial hashmap implementation, not yet fit for
- * use, but useful as a stress test for rustboot.
+ * Hashmap implementation.
  */
 type hashfn[K] = fn(&K) -> uint ;
 
@@ -16,7 +15,13 @@ type hashmap[K, V] =
         fn remove(&K) -> option::t[V] ;
         fn rehash() ;
         iter items() -> @tup(K, V) ;
+        iter keys() -> K ;
     };
+type hashset[K] = hashmap[K, ()];
+
+fn set_add[K](hashset[K] set, &K key) -> bool {
+    ret set.insert(key, ());
+}
 
 fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
     let uint initial_capacity = 32u; // 2^5
@@ -188,6 +193,14 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
                 }
             }
         }
+        iter keys() -> K {
+            for (bucket[K, V] b in bkts) {
+                alt (b) {
+                    case (some(?k, _)) { put k; }
+                    case (_) { }
+                }
+            }
+        }
     }
     auto bkts = make_buckets[K, V](initial_capacity);
     ret hashmap[K, V](hasher, eqer, bkts, initial_capacity, 0u, load_factor);