]> gitweb.factorcode.org Git - factor.git/commitdiff
hashtables: make sure capacity and growth use same load factor.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 15 Jul 2015 01:32:40 +0000 (18:32 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 15 Jul 2015 01:32:40 +0000 (18:32 -0700)
core/hashtables/hashtables-tests.factor
core/hashtables/hashtables.factor

index f188b8610e652402318b152a0001785d17a371c2..bf512296a15aed566b31e8bbd86693484b2296ae 100644 (file)
@@ -1,5 +1,5 @@
-USING: accessors assocs continuations hashtables io kernel make
-math namespaces prettyprint sequences sequences.private
+USING: accessors assocs continuations fry hashtables io kernel
+make math namespaces prettyprint sequences sequences.private
 tools.test vectors ;
 IN: hashtables.tests
 
@@ -124,8 +124,8 @@ H{ } clone "counting" set
 H{ } "x" set
 100 [ drop "x" get clear-assoc ] each-integer
 
-! Crash discovered by erg
-{ t } [ 0.75 <hashtable> dup clone = ] unit-test
+! non-integer capacity not allowed
+[ 0.75 <hashtable> ] must-fail
 
 ! Another crash discovered by erg
 { } [
@@ -141,6 +141,14 @@ H{ } "x" set
     [ [ neg ] dip sq ] assoc-map
 ] unit-test
 
+! make sure growth and capacity use same load-factor
+{ t } [
+    100 iota
+    [ [ <hashtable> ] map ]
+    [ [ H{ } clone [ '[ dup _ set-at ] each-integer ] keep ] map ] bi
+    [ [ array>> length ] bi@ = ] 2all?
+] unit-test
+
 ! Bug discovered by littledan
 { { 5 5 5 5 } } [
     [
index 13b65680bb257c4077a22f26faab71ec91bce8b4..0f6ad5919cd749b9a648a84910b2d23162e63dc8 100644 (file)
@@ -36,7 +36,7 @@ TUPLE: hashtable
     [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
 
 : <hash-array> ( n -- array )
-    1 + next-power-of-2 4 * ((empty)) <array> ; inline
+    3 * 1 + 2/ next-power-of-2 2 * ((empty)) <array> ; inline
 
 : init-hash ( hash -- )
     0 >>count 0 >>deleted drop ; inline
@@ -83,7 +83,7 @@ TUPLE: hashtable
     [ array>> 2dup hash@ 0 f (new-key@) ] keep swap
     [ over [ hash-deleted- ] [ hash-count+ ] if swap or ] [ 2drop ] if ; inline
 
-: set-nth-pair ( value key seq n -- )
+: set-nth-pair ( value key array n -- )
     2 fixnum+fast [ set-slot ] 2keep
     1 fixnum+fast set-slot ; inline
 
@@ -94,7 +94,7 @@ TUPLE: hashtable
     [ swapd (set-at) ] curry assoc-each ; inline
 
 : hash-large? ( hash -- ? )
-    [ count>> 3 fixnum*fast ]
+    [ count>> 1 fixnum+fast 3 fixnum*fast ]
     [ array>> length>> ] bi fixnum>= ; inline
 
 : each-pair ( ... array quot: ( ... key value -- ... ) -- ... )
@@ -119,6 +119,7 @@ TUPLE: hashtable
 PRIVATE>
 
 : <hashtable> ( n -- hash )
+    integer>fixnum-strict
     [ 0 0 ] dip <hash-array> hashtable boa ; inline
 
 M: hashtable at*