! 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
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
[ 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
[ 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 -- ? )
[ (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 -- ... ) -- ... )
PRIVATE>
: <hash-set> ( capacity -- hash-set )
+ integer>fixnum-strict
[ 0 0 ] dip <hash-array> hash-set boa ; inline
M: hash-set in?