1 ! Copyright (C) 2010 Daniel Ehrenberg
2 ! Copyright (C) 2005, 2011 John Benediktsson, Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays combinators growable.private hash-sets
5 hashtables.private kernel kernel.private math math.private
6 sequences sequences.private sets sets.private slots.private
11 { count array-capacity }
12 { deleted array-capacity }
17 : hash@ ( key array -- i )
18 [ hashcode >fixnum ] dip wrap ; inline
20 : probe ( array i probe# -- array i probe# )
21 1 fixnum+fast [ fixnum+fast over wrap ] keep ; inline
23 : no-key ( key array -- array n ? ) nip f f ; inline
25 : (key@) ( key array i probe# -- array n ? )
26 [ 3dup swap array-nth ] dip over +empty+ eq?
32 ] if ; inline recursive
34 : key@ ( key hash -- array n ? )
35 array>> dup length>> 0 eq?
36 [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
38 : <hash-array> ( n -- array )
39 3 * 1 + 2/ next-power-of-2 +empty+ <array> ; inline
41 : reset-hash ( n hash -- )
42 swap <hash-array> >>array init-hash ; inline
44 : (new-key@) ( key array i probe# j -- array i j empty? )
45 [ 2dup swap array-nth ] 2dip pick tombstone?
48 [ nip [ drop ] 3dip t ]
49 [ pick or [ probe ] dip (new-key@) ]
52 [ [ pick ] dip = ] 2dip rot
53 [ nip [ drop ] 3dip f ]
54 [ [ probe ] dip (new-key@) ]
56 ] if ; inline recursive
58 : new-key@ ( key hash -- array n ? )
59 [ array>> 2dup hash@ 0 f (new-key@) ] keep swap
60 [ over [ hash-deleted- ] [ hash-count+ ] if swap or t ] [ 2drop f ] if ; inline
62 : set-nth-item ( key array n -- )
63 2 fixnum+fast set-slot ; inline
65 : (adjoin) ( key hash -- ? )
66 dupd new-key@ [ set-nth-item ] dip ; inline
68 : (rehash) ( seq hash -- )
69 [ (adjoin) drop ] curry each ; inline
71 : hash-large? ( hash -- ? )
72 [ count>> 1 fixnum+fast 3 fixnum*fast ]
73 [ array>> length>> 1 fixnum-shift-fast ] bi fixnum>= ; inline
75 : each-member ( ... array quot: ( ... elt -- ... ) -- ... )
76 [ if ] curry [ dup tombstone? [ drop ] ] prepose each ; inline
78 : grow-hash ( hash -- )
79 { hash-set } declare [
83 ] keep [ (adjoin) drop ] curry each-member ;
85 : ?grow-hash ( hash -- )
86 dup hash-large? [ grow-hash ] [ drop ] if ; inline
90 : <hash-set> ( capacity -- hash-set )
92 [ 0 0 ] dip <hash-array> hash-set boa ; inline
98 [ init-hash ] [ array>> [ drop +empty+ ] map! drop ] bi ;
101 [ nip ] [ key@ ] 2bi [
102 [ +tombstone+ ] 2dip set-nth-item
108 M: hash-set cardinality
109 [ count>> ] [ deleted>> ] bi - ; inline
111 : rehash ( hash-set -- )
112 [ members ] [ clear-set ] [ (rehash) ] tri ;
115 dup ?grow-hash (adjoin) drop ;
118 dup ?grow-hash (adjoin) ;
121 [ array>> 0 swap ] [ cardinality f <array> ] bi [
122 [ [ over ] dip set-nth-unsafe 1 + ] curry each-member
126 (clone) [ clone ] change-array ; inline
129 over hash-set? [ set= ] [ 2drop f ] if ;
131 : >hash-set ( members -- hash-set )
132 dup length <hash-set> [ (rehash) ] keep ; inline
135 drop dup hash-set? [ ?members >hash-set ] unless ; inline
137 INSTANCE: hash-set set
139 ! Overrides for performance
143 : and-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
144 [ if ] curry [ dup tombstone? [ drop t ] ] prepose ; inline
146 : not-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
147 [ if ] curry [ dup tombstone? [ drop f ] ] prepose ; inline
149 : array/tester ( hash-set1 hash-set2 -- array quot )
150 [ array>> ] dip [ in? ] curry ; inline
152 : filter-members ( hash-set array quot: ( elt -- ? ) -- accum )
153 [ dup ] prepose rot cardinality <vector> [
154 [ push-unsafe ] curry [ [ drop ] if ] curry
160 M: hash-set intersect
162 small/large dupd array/tester not-tombstones
163 filter-members >hash-set
164 ] [ (intersect) >hash-set ] if ;
166 M: hash-set intersects?
168 small/large array/tester not-tombstones any?
169 ] [ small/large sequence/tester any? ] if ;
173 small/large [ array>> ] [ clone ] bi*
174 [ [ adjoin ] curry each-member ] keep
175 ] [ (union) >hash-set ] if ;
179 dupd array/tester [ not ] compose not-tombstones
180 filter-members >hash-set
181 ] [ (diff) >hash-set ] if ;
185 2dup [ cardinality ] bi@ > [ 2drop f ] [
186 array/tester and-tombstones all?
188 ] [ call-next-method ] if ;
192 2dup [ cardinality ] bi@ eq? [
193 array/tester and-tombstones all?
195 ] [ call-next-method ] if ;
197 M: hash-set hashcode*
199 dup cardinality 1 eq?
200 [ members hashcode* ] [ nip cardinality ] if
201 ] recursive-hashcode ;
205 M: f fast-set drop 0 <hash-set> ;
207 M: sequence fast-set >hash-set ;
209 M: sequence duplicates
210 dup length <hash-set> [ ?adjoin ] curry reject ;
212 M: sequence all-unique?
213 dup length <hash-set> [ ?adjoin ] curry all? ;