M: tuple prepare-object emit-tuple ;
M: tombstone prepare-object
- state>> "((tombstone))" "((empty))" ?
+ state>> "+tombstone+" "+empty+" ?
"hashtables.private" lookup-word def>> first
[ emit-tuple ] cache-eql-object ;
tuple
{ "state" } define-tuple-class
-"((empty))" "hashtables.private" create-word
+"+empty+" "hashtables.private" create-word
{ f } "tombstone" "hashtables.private" lookup-word
slots>tuple 1quotation ( -- value ) define-inline
-"((tombstone))" "hashtables.private" create-word
+"+tombstone+" "hashtables.private" create-word
{ t } "tombstone" "hashtables.private" lookup-word
slots>tuple 1quotation ( -- value ) define-inline
: no-key ( key array -- array n ? ) nip f f ; inline
: (key@) ( key array i probe# -- array n ? )
- [ 3dup swap array-nth ] dip over ((empty)) eq?
+ [ 3dup swap array-nth ] dip over +empty+ eq?
[ 4drop no-key ] [
[ = ] dip swap
[ drop rot drop t ]
[ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
: <hash-array> ( n -- array )
- 3 * 1 + 2/ next-power-of-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
: (new-key@) ( key array i probe# j -- array i j empty? )
[ 2dup swap array-nth ] 2dip pick tombstone?
[
- rot ((empty)) eq?
+ rot +empty+ eq?
[ nip [ drop ] 3dip t ]
[ pick or [ probe ] dip (new-key@) ]
if
key@ 2nip ;
M: hash-set clear-set
- [ init-hash ] [ array>> [ drop ((empty)) ] map! drop ] bi ;
+ [ init-hash ] [ array>> [ drop +empty+ ] map! drop ] bi ;
M: hash-set delete
[ nip ] [ key@ ] 2bi [
- [ ((tombstone)) ] 2dip set-nth-item
+ [ +tombstone+ ] 2dip set-nth-item
hash-deleted+
] [
3drop
ARTICLE: "hashtables.private" "Hashtable implementation details"
"This hashtable implementation uses only one auxiliary array in addition to the hashtable tuple itself. The array stores keys in even slots and values in odd slots. Values are looked up with a hashing strategy that uses quadratic probing to resolve collisions."
$nl
-"There are two special objects: the " { $link ((tombstone)) } " marker and the " { $link ((empty)) } " marker. Neither of these markers can be used as hashtable keys."
+"There are two special objects: the " { $link +tombstone+ } " marker and the " { $link +empty+ } " marker. Neither of these markers can be used as hashtable keys."
$nl
"The " { $snippet "count" } " slot is the number of entries including deleted entries, and " { $snippet "deleted" } " is the number of deleted entries."
{ $subsections
HELP: key@
{ $values { "key" "a key" } { "hash" hashtable } { "array" "the underlying array of the hashtable" } { "n" "the index of the key" } { "?" "a boolean indicating whether the key was present" } }
-{ $description "Searches the hashtable for the key using a quadratic probing strategy. Searches stop if either the key or an " { $link ((empty)) } " sentinel is found. Searches skip the " { $link ((tombstone)) } " sentinel." } ;
+{ $description "Searches the hashtable for the key using a quadratic probing strategy. Searches stop if either the key or an " { $link +empty+ } " sentinel is found. Searches skip the " { $link +tombstone+ } " sentinel." } ;
{ key@ new-key@ } related-words
: no-key ( key array -- array n ? ) nip f f ; inline
: (key@) ( key array i probe# -- array n ? )
- [ 3dup swap array-nth ] dip over ((empty)) eq?
+ [ 3dup swap array-nth ] dip over +empty+ eq?
[ 4drop no-key ] [
[ = ] dip swap
[ drop rot drop t ]
[ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
: <hash-array> ( n -- array )
- 3 * 1 + 2/ next-power-of-2 2 * ((empty)) <array> ; inline
+ 3 * 1 + 2/ next-power-of-2 2 * +empty+ <array> ; inline
: init-hash ( hash -- )
0 >>count 0 >>deleted drop ; inline
: (new-key@) ( key array i probe# j -- array i j empty? )
[ 2dup swap array-nth ] 2dip pick tombstone?
[
- rot ((empty)) eq?
+ rot +empty+ eq?
[ nip [ drop ] 3dip t ]
[ pick or [ probe ] dip (new-key@) ]
if
key@ [ 3 fixnum+fast slot t ] [ 2drop f f ] if ;
M: hashtable clear-assoc
- [ init-hash ] [ array>> [ drop ((empty)) ] map! drop ] bi ;
+ [ init-hash ] [ array>> [ drop +empty+ ] map! drop ] bi ;
M: hashtable delete-at
[ nip ] [ key@ ] 2bi [
- [ ((tombstone)) dup ] 2dip set-nth-pair
+ [ +tombstone+ dup ] 2dip set-nth-pair
hash-deleted+
] [
3drop