]> gitweb.factorcode.org Git - factor.git/commitdiff
more efficient hash-each
authorSlava Pestov <slava@factorcode.org>
Mon, 13 Jun 2005 02:06:03 +0000 (02:06 +0000)
committerSlava Pestov <slava@factorcode.org>
Mon, 13 Jun 2005 02:06:03 +0000 (02:06 +0000)
library/collections/hashtables.factor
library/test/hashtables.factor

index 5a929154ca4edd8c5094130ff979f6960009a34e..91db3efe023436e1411c2481844ca93811ccc1f6 100644 (file)
@@ -111,11 +111,9 @@ IN: hashtables
     [ remove-assoc ] set-hash* ;
 
 : hash-clear ( hash -- )
-    #! Remove all entries from a hashtable.
     0 over set-hash-size [ f -rot set-hash-bucket ] each-bucket ;
 
 : buckets>list ( hash -- list )
-    #! Push a list of key/value pairs in a hashtable.
     hash-array >list ;
 
 : alist>hash ( alist -- hash )
@@ -123,16 +121,13 @@ IN: hashtables
     [ unswons pick set-hash ] each ;
 
 : hash-keys ( hash -- list )
-    #! Push a list of keys in a hashtable.
     hash>alist [ car ] map ;
 
 : hash-values ( hash -- alist )
-    #! Push a list of values in a hashtable.
     hash>alist [ cdr ] map ;
 
-: hash-each ( hash code -- )
-    #! Apply the code to each key/value pair of the hashtable.
-    >r hash>alist r> each ; inline
+: hash-each ( hash quot -- )
+    swap hash-array [ swap each ] each-with ; inline
 
 : hash-subset ( hash quot -- hash | quot: [[ k v ]] -- ? )
     >r hash>alist r> subset alist>hash ;
index a61971a18f6c5ae06ad117b3b908d64726c34945..036f00c545dad8efdb32f4f2d874b64c13f73ee4 100644 (file)
@@ -119,3 +119,13 @@ f 100000000000000000000000000 "testhash" get set-hash
 10 [ f f "f-hash-test" get set-hash ] times
 
 [ 1 ] [ "f-hash-test" get hash-size ] unit-test
+
+[ 21 ] [
+    0 {{
+        [[ 1 2 ]]
+        [[ 3 4 ]]
+        [[ 5 6 ]]
+    }} [
+        uncons + +
+    ] hash-each
+] unit-test