hashtables: reuse (rehash).
parent
04af5f2ffb
commit
eca5253017
|
@ -87,8 +87,11 @@ TUPLE: hashtable
|
||||||
2 fixnum+fast [ set-slot ] 2keep
|
2 fixnum+fast [ set-slot ] 2keep
|
||||||
1 fixnum+fast set-slot ; inline
|
1 fixnum+fast set-slot ; inline
|
||||||
|
|
||||||
: (rehash) ( hash alist -- )
|
: (set-at) ( value key hash -- )
|
||||||
swap [ swapd dupd new-key@ set-nth-pair ] curry assoc-each ; inline
|
dupd new-key@ set-nth-pair ; inline
|
||||||
|
|
||||||
|
: (rehash) ( alist hash -- )
|
||||||
|
[ swapd (set-at) ] curry assoc-each ; inline
|
||||||
|
|
||||||
: hash-large? ( hash -- ? )
|
: hash-large? ( hash -- ? )
|
||||||
[ count>> 3 fixnum*fast 1 fixnum+fast ]
|
[ count>> 3 fixnum*fast 1 fixnum+fast ]
|
||||||
|
@ -99,7 +102,7 @@ TUPLE: hashtable
|
||||||
[ >alist { array } declare ]
|
[ >alist { array } declare ]
|
||||||
[ assoc-size 1 + ]
|
[ assoc-size 1 + ]
|
||||||
[ reset-hash ] tri
|
[ reset-hash ] tri
|
||||||
] keep swap (rehash) ;
|
] keep (rehash) ;
|
||||||
|
|
||||||
: ?grow-hash ( hash -- )
|
: ?grow-hash ( hash -- )
|
||||||
dup hash-large? [ grow-hash ] [ drop ] if ; inline
|
dup hash-large? [ grow-hash ] [ drop ] if ; inline
|
||||||
|
@ -127,10 +130,10 @@ M: hashtable assoc-size ( hash -- n )
|
||||||
[ count>> ] [ deleted>> ] bi - ; inline
|
[ count>> ] [ deleted>> ] bi - ; inline
|
||||||
|
|
||||||
: rehash ( hash -- )
|
: rehash ( hash -- )
|
||||||
dup >alist [ dup clear-assoc ] dip (rehash) ;
|
[ >alist ] [ clear-assoc ] [ (rehash) ] tri ;
|
||||||
|
|
||||||
M: hashtable set-at ( value key hash -- )
|
M: hashtable set-at ( value key hash -- )
|
||||||
dup ?grow-hash dupd new-key@ set-nth-pair ;
|
dup ?grow-hash (set-at) ;
|
||||||
|
|
||||||
: associate ( value key -- hash )
|
: associate ( value key -- hash )
|
||||||
1 <hashtable> [ set-at ] keep ; inline
|
1 <hashtable> [ set-at ] keep ; inline
|
||||||
|
@ -168,7 +171,7 @@ M: assoc new-assoc drop <hashtable> ; inline
|
||||||
M: f new-assoc drop <hashtable> ; inline
|
M: f new-assoc drop <hashtable> ; inline
|
||||||
|
|
||||||
: >hashtable ( assoc -- hashtable )
|
: >hashtable ( assoc -- hashtable )
|
||||||
H{ } assoc-clone-like ;
|
[ >alist ] [ assoc-size <hashtable> ] bi [ (rehash) ] keep ;
|
||||||
|
|
||||||
M: hashtable assoc-like
|
M: hashtable assoc-like
|
||||||
drop dup hashtable? [ >hashtable ] unless ; inline
|
drop dup hashtable? [ >hashtable ] unless ; inline
|
||||||
|
|
Loading…
Reference in New Issue