diff --git a/core/hashtables/hashtables-tests.factor b/core/hashtables/hashtables-tests.factor index f188b8610e..bf512296a1 100644 --- a/core/hashtables/hashtables-tests.factor +++ b/core/hashtables/hashtables-tests.factor @@ -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 dup clone = ] unit-test +! non-integer capacity not allowed +[ 0.75 ] 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 + [ [ ] 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 } } [ [ diff --git a/core/hashtables/hashtables.factor b/core/hashtables/hashtables.factor index 13b65680bb..0f6ad5919c 100644 --- a/core/hashtables/hashtables.factor +++ b/core/hashtables/hashtables.factor @@ -36,7 +36,7 @@ TUPLE: hashtable [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline : ( n -- array ) - 1 + next-power-of-2 4 * ((empty)) ; inline + 3 * 1 + 2/ next-power-of-2 2 * ((empty)) ; 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> : ( n -- hash ) + integer>fixnum-strict [ 0 0 ] dip hashtable boa ; inline M: hashtable at*