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

index 75c91438642776247d563e082d915807487bea6c..4b2f78aab3f1ce556206376ac719ba9ae7673927 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2010 Daniel Ehrenberg
 ! See http://factorcode.org/license.txt for BSD license.
-USING: sets tools.test kernel sorting prettyprint hash-sets ;
+USING: accessors fry hash-sets kernel math prettyprint
+sequences sets sorting tools.test ;
 IN: hash-sets.tests
 
 { { 1 2 3 } } [ HS{ 1 2 3 } members natural-sort ] unit-test
@@ -44,3 +45,14 @@ IN: hash-sets.tests
     HS{ { 1 2 } { 2 1 } } over adjoin
     HS{ { 2 1 } { 1 2 } } over adjoin
 ] unit-test
+
+! make sure growth and capacity use same load-factor
+{ t } [
+    100 iota
+    [ [ <hash-set> ] map ]
+    [ [ HS{ } clone [ '[ _ adjoin ] each-integer ] keep ] map ] bi
+    [ [ array>> length ] bi@ = ] 2all?
+] unit-test
+
+! non-integer capacity not allowed
+[ 0.75 <hash-set> ] must-fail
index 02cb8a869ee2ec51147b48290d150be4feb8993e..d994a572ce43d8005bb6de0aa9967ddeec540d87 100644 (file)
@@ -36,7 +36,7 @@ TUPLE: hash-set
     [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
 
 : <hash-array> ( n -- array )
-    1 + next-power-of-2 2 * ((empty)) <array> ; inline
+    3 * 1 + 2/ next-power-of-2 ((empty)) <array> ; inline
 
 : reset-hash ( n hash -- )
     swap <hash-array> >>array init-hash ; inline
@@ -59,7 +59,7 @@ TUPLE: hash-set
     [ array>> 2dup hash@ 0 f (new-key@) ] keep swap
     [ over [ hash-deleted- ] [ hash-count+ ] if swap or t ] [ 2drop f ] if ; inline
 
-: set-nth-item ( key seq n -- )
+: set-nth-item ( key array n -- )
     2 fixnum+fast set-slot ; inline
 
 : (adjoin) ( key hash -- ? )
@@ -69,7 +69,7 @@ TUPLE: hash-set
     [ (adjoin) drop ] curry each ; inline
 
 : hash-large? ( hash -- ? )
-    [ count>> 3 fixnum*fast ]
+    [ count>> 1 fixnum+fast 3 fixnum*fast ]
     [ array>> length>> 1 fixnum-shift-fast ] bi fixnum>= ; inline
 
 : each-member ( ... array quot: ( ... elt -- ... ) -- ... )
@@ -88,6 +88,7 @@ TUPLE: hash-set
 PRIVATE>
 
 : <hash-set> ( capacity -- hash-set )
+    integer>fixnum-strict
     [ 0 0 ] dip <hash-array> hash-set boa ; inline
 
 M: hash-set in?