]> gitweb.factorcode.org Git - factor.git/commitdiff
hashtables: reuse (rehash).
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 8 Mar 2013 06:01:50 +0000 (22:01 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 8 Mar 2013 06:01:50 +0000 (22:01 -0800)
core/hashtables/hashtables.factor

index d2c5765df8a63bd9b7f45359873b96dbf371b6af..d6f0be87d198914638d421a4b38b4bfdc343f759 100644 (file)
@@ -87,8 +87,11 @@ TUPLE: hashtable
     2 fixnum+fast [ set-slot ] 2keep
     1 fixnum+fast set-slot ; inline
 
-: (rehash) ( hash alist -- )
-    swap [ swapd dupd new-key@ set-nth-pair ] curry assoc-each ; inline
+: (set-at) ( value key hash -- )
+    dupd new-key@ set-nth-pair ; inline
+
+: (rehash) ( alist hash -- )
+    [ swapd (set-at) ] curry assoc-each ; inline
 
 : hash-large? ( hash -- ? )
     [ count>> 3 fixnum*fast 1 fixnum+fast ]
@@ -99,7 +102,7 @@ TUPLE: hashtable
         [ >alist { array } declare ]
         [ assoc-size 1 + ]
         [ reset-hash ] tri
-    ] keep swap (rehash) ;
+    ] keep (rehash) ;
 
 : ?grow-hash ( hash -- )
     dup hash-large? [ grow-hash ] [ drop ] if ; inline
@@ -127,10 +130,10 @@ M: hashtable assoc-size ( hash -- n )
     [ count>> ] [ deleted>> ] bi - ; inline
 
 : rehash ( hash -- )
-    dup >alist [ dup clear-assoc ] dip (rehash) ;
+    [ >alist ] [ clear-assoc ] [ (rehash) ] tri ;
 
 M: hashtable set-at ( value key hash -- )
-    dup ?grow-hash dupd new-key@ set-nth-pair ;
+    dup ?grow-hash (set-at) ;
 
 : associate ( value key -- hash )
     1 <hashtable> [ set-at ] keep ; inline
@@ -168,7 +171,7 @@ M: assoc new-assoc drop <hashtable> ; inline
 M: f new-assoc drop <hashtable> ; inline
 
 : >hashtable ( assoc -- hashtable )
-    H{ } assoc-clone-like ;
+    [ >alist ] [ assoc-size <hashtable> ] bi [ (rehash) ] keep ;
 
 M: hashtable assoc-like
     drop dup hashtable? [ >hashtable ] unless ; inline