Skip to content

Commit

Permalink
hash-sets: reuse (rehash).
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Mar 8, 2013
1 parent a4b4f4b commit db4e659
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions core/hash-sets/hash-sets.factor
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ TUPLE: hash-set
: set-nth-item ( key seq n -- )
2 fixnum+fast set-slot ; inline

: (rehash) ( hash seq -- )
swap [ dupd new-key@ set-nth-item ] curry each ; inline
: (adjoin) ( key hash -- )
dupd new-key@ set-nth-item ; inline

: (rehash) ( seq hash -- )
[ (adjoin) ] curry each ; inline

: hash-large? ( hash -- ? )
[ count>> 3 fixnum*fast 1 fixnum+fast ]
Expand All @@ -73,7 +76,7 @@ TUPLE: hash-set
[ members { array } declare ]
[ cardinality 1 + ]
[ reset-hash ] tri
] keep swap (rehash) ;
] keep (rehash) ;

: ?grow-hash ( hash -- )
dup hash-large? [ grow-hash ] [ drop ] if ; inline
Expand Down Expand Up @@ -101,7 +104,7 @@ M: hash-set cardinality ( hash -- n )
[ count>> ] [ deleted>> ] bi - ; inline

M: hash-set adjoin ( key hash -- )
dup ?grow-hash dupd new-key@ set-nth-item ;
dup ?grow-hash (adjoin) ;

<PRIVATE

Expand All @@ -128,7 +131,7 @@ M: hash-set equal?
over hash-set? [ set= ] [ 2drop f ] if ;

: >hash-set ( members -- hash-set )
dup length <hash-set> [ [ adjoin ] curry each ] keep ;
dup length <hash-set> [ (rehash) ] keep ;

M: hash-set set-like
drop dup hash-set? [ ?members >hash-set ] unless ; inline
Expand Down

0 comments on commit db4e659

Please sign in to comment.