hashtables: make sure capacity and growth use same load factor.

db4
John Benediktsson 2015-07-14 18:32:40 -07:00
parent 3265f33b6b
commit 6127bb8548
2 changed files with 16 additions and 7 deletions

View File

@ -1,5 +1,5 @@
USING: accessors assocs continuations hashtables io kernel make USING: accessors assocs continuations fry hashtables io kernel
math namespaces prettyprint sequences sequences.private make math namespaces prettyprint sequences sequences.private
tools.test vectors ; tools.test vectors ;
IN: hashtables.tests IN: hashtables.tests
@ -124,8 +124,8 @@ H{ } clone "counting" set
H{ } "x" set H{ } "x" set
100 [ drop "x" get clear-assoc ] each-integer 100 [ drop "x" get clear-assoc ] each-integer
! Crash discovered by erg ! non-integer capacity not allowed
{ t } [ 0.75 <hashtable> dup clone = ] unit-test [ 0.75 <hashtable> ] must-fail
! Another crash discovered by erg ! Another crash discovered by erg
{ } [ { } [
@ -141,6 +141,14 @@ H{ } "x" set
[ [ neg ] dip sq ] assoc-map [ [ neg ] dip sq ] assoc-map
] unit-test ] 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 ! Bug discovered by littledan
{ { 5 5 5 5 } } [ { { 5 5 5 5 } } [
[ [

View File

@ -36,7 +36,7 @@ TUPLE: hashtable
[ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
: <hash-array> ( n -- array ) : <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 -- ) : init-hash ( hash -- )
0 >>count 0 >>deleted drop ; inline 0 >>count 0 >>deleted drop ; inline
@ -83,7 +83,7 @@ TUPLE: hashtable
[ array>> 2dup hash@ 0 f (new-key@) ] keep swap [ array>> 2dup hash@ 0 f (new-key@) ] keep swap
[ over [ hash-deleted- ] [ hash-count+ ] if swap or ] [ 2drop ] if ; inline [ 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 2 fixnum+fast [ set-slot ] 2keep
1 fixnum+fast set-slot ; inline 1 fixnum+fast set-slot ; inline
@ -94,7 +94,7 @@ TUPLE: hashtable
[ swapd (set-at) ] curry assoc-each ; inline [ swapd (set-at) ] curry assoc-each ; inline
: hash-large? ( hash -- ? ) : hash-large? ( hash -- ? )
[ count>> 3 fixnum*fast ] [ count>> 1 fixnum+fast 3 fixnum*fast ]
[ array>> length>> ] bi fixnum>= ; inline [ array>> length>> ] bi fixnum>= ; inline
: each-pair ( ... array quot: ( ... key value -- ... ) -- ... ) : each-pair ( ... array quot: ( ... key value -- ... ) -- ... )
@ -119,6 +119,7 @@ TUPLE: hashtable
PRIVATE> PRIVATE>
: <hashtable> ( n -- hash ) : <hashtable> ( n -- hash )
integer>fixnum-strict
[ 0 0 ] dip <hash-array> hashtable boa ; inline [ 0 0 ] dip <hash-array> hashtable boa ; inline
M: hashtable at* M: hashtable at*