hash-set
Factor handbook » The language » Collections » Sets » Set implementations » Hash sets

Next:<hash-set> ( capacity -- hash-set )

Vocabulary
hash-sets

Class description
The class of hashtable-like sets. These implement the set mixin.

DefinitionMethods
USING: hash-sets prettyprint.custom sets ;
M: hash-set >pprint-sequence members ;

USING: hash-sets hash-sets.private kernel sets ;
M: hash-set ?adjoin dup ?grow-hash (adjoin) ;

USING: hash-sets hash-sets.private sets ;
M: hash-set ?delete (delete) ;

USING: hash-sets hash-sets.private kernel sets ;
M: hash-set adjoin dup ?grow-hash (adjoin) drop ;

USING: accessors cursors cursors.private hash-sets kernel ;
M: hash-set begin-cursor
dup array>> 0 (inc-hash-set-cursor) <hash-set-cursor> ;
inline

USING: accessors hash-sets kernel math sets ;
M: hash-set cardinality [ count>> ] [ deleted>> ] bi - ; inline

USING: accessors hash-sets hashtables.private kernel sequences
sets ;
M: hash-set clear-set
[ init-hash ] [ array>> [ drop +empty+ ] map! drop ] bi ;

USING: accessors hash-sets kernel ;
M: hash-set clone (clone) [ clone ] change-array ; inline

USING: hash-sets hash-sets.private kernel sets ;
M: hash-set delete (delete) drop ;

USING: hash-sets hash-sets.private kernel sets sets.private ;
M: hash-set diff
over hash-set? [
dupd array/tester [ not ] compose
not-tombstones filter-members >hash-set
] [ (diff) >hash-set ] if ;

USING: accessors cursors cursors.private hash-sets kernel
sequences ;
M: hash-set end-cursor dup array>> length <hash-set-cursor>
; inline

USING: hash-sets kernel sets ;
M: hash-set equal? over hash-set? [ set= ] [ 2drop f ] if ;

USING: hash-sets kernel math sets ;
M: hash-set hashcode*
[
dup cardinality 1 eq?
[ members hashcode* ] [ nip cardinality ] if
] recursive-hashcode ;

USING: hash-sets hash-sets.private kernel sets ;
M: hash-set in? key@ 2nip ;

USING: hash-sets hash-sets.private kernel sets sets.private ;
M: hash-set intersect
over hash-set? [
small/large dupd array/tester not-tombstones
filter-members >hash-set
] [ (intersect) >hash-set ] if ;

USING: hash-sets hash-sets.private kernel sequences sets
sets.private ;
M: hash-set intersects?
over hash-set?
[ small/large array/tester not-tombstones any? ]
[ small/large sequence/tester any? ] if ;

USING: hash-sets mirrors sets ;
M: hash-set make-mirror members make-mirror ;

USING: accessors arrays hash-sets hash-sets.private kernel math
sequences.private sets ;
M: hash-set members
[ array>> 0 swap ] [ cardinality f <array> ] bi
[ [ overd set-nth-unsafe 1 + ] curry each-member ] keep nip
;

USING: hash-sets prettyprint.custom ;
M: hash-set pprint* pprint-object ;

USING: hash-sets kernel prettyprint.custom ;
M: hash-set pprint-delims drop \ HS{ \ } ;

USING: accessors hash-sets hashtables.private kernel math random
sequences.private sets ;
M: hash-set random
dup cardinality
[ drop f ] [
[ 0 ] [ array>> ] [ random ] tri* 1 +
[ [ 2dup array-nth tombstone? [ 1 + ] 2dip ] loop ]
times [ 1 - ] dip array-nth
] if-zero ;

USING: hash-sets kernel sets sets.private ;
M: hash-set set-like
drop dup hash-set? [ ?members >hash-set ] unless ; inline

USING: generic hash-sets hash-sets.private kernel sequences sets
;
M: hash-set set=
over hash-set? [
2dup [ cardinality ] bi@ eq?
[ array/tester and-tombstones all? ] [ 2drop f ] if
] [ M\ hash-set set= (call-next-method) ] if ;

USING: generic hash-sets hash-sets.private kernel math sequences
sets ;
M: hash-set subset?
over hash-set? [
2dup [ cardinality ] bi@ >
[ 2drop f ] [ array/tester and-tombstones all? ] if
] [ M\ hash-set subset? (call-next-method) ] if ;

USING: accessors hash-sets hash-sets.private kernel sets
sets.private ;
M: hash-set union
over hash-set? [
small/large [ array>> ] [ clone ] bi*
[ [ adjoin ] curry each-member ] keep
] [ (union) >hash-set ] if ;