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 growable.private hashtables.private
5 kernel kernel.private math math.private sequences
6 sequences.private sets sets.private slots.private vectors ;
10 { count array-capacity }
11 { deleted array-capacity }
16 : hash@ ( key array -- i )
17 [ hashcode >fixnum ] dip wrap ; inline
19 : probe ( array i probe# -- array i probe# )
20 1 fixnum+fast [ fixnum+fast over wrap ] keep ; inline
22 : no-key ( key array -- array n ? ) nip f f ; inline
24 : (key@) ( key array i probe# -- array n ? )
25 [ 3dup swap array-nth ] dip over +empty+ eq?
31 ] if ; inline recursive
33 : key@ ( key hash -- array n ? )
34 array>> dup length>> 0 eq?
35 [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
37 : <hash-array> ( n -- array )
38 3 * 1 + 2/ next-power-of-2 +empty+ <array> ; inline
40 : reset-hash ( n hash -- )
41 swap <hash-array> >>array init-hash ; inline
43 : (new-key@) ( array key i probe# j -- array i j empty? )
44 [ 2dup swap array-nth ] 2dip pick tombstone?
47 [ nip [ drop ] 3dip t ]
48 [ pick or [ probe ] dip (new-key@) ]
52 [ nip [ drop ] 3dip f ]
53 [ [ probe ] dip (new-key@) ]
55 ] if ; inline recursive
57 : new-key@ ( key hash -- array n ? )
58 [ array>> 2dup hash@ 0 f (new-key@) ] keep swap
59 [ over [ hash-deleted- ] [ hash-count+ ] if swap or t ] [ 2drop f ] if ; inline
61 : set-nth-item ( key array n -- )
62 2 fixnum+fast set-slot ; inline
64 : (adjoin) ( key hash -- ? )
65 dupd new-key@ [ set-nth-item ] dip ; inline
67 : (delete) ( key hash -- ? )
68 [ nip ] [ key@ ] 2bi [
69 [ +tombstone+ ] 2dip set-nth-item
75 : (rehash) ( seq hash -- )
76 '[ _ (adjoin) drop ] each ; inline
78 : hash-large? ( hash -- ? )
79 [ count>> 1 fixnum+fast 3 fixnum*fast ]
80 [ array>> length>> 1 fixnum-shift-fast ] bi fixnum>= ; inline
82 : each-member ( ... array quot: ( ... elt -- ... ) -- ... )
83 '[ dup tombstone? [ drop ] _ if ] each ; inline
85 : grow-hash ( hash -- )
86 { hash-set } declare [
90 ] keep [ (adjoin) drop ] curry each-member ;
92 : ?grow-hash ( hash -- )
93 dup hash-large? [ grow-hash ] [ drop ] if ; inline
97 : <hash-set> ( capacity -- hash-set )
99 [ 0 0 ] dip <hash-array> hash-set boa ; inline
104 M: hash-set clear-set
105 [ init-hash ] [ array>> [ drop +empty+ ] map! drop ] bi ;
113 M: hash-set cardinality
114 [ count>> ] [ deleted>> ] bi - ; inline
116 : rehash ( hash-set -- )
117 [ members ] [ clear-set ] [ (rehash) ] tri ;
120 dup ?grow-hash (adjoin) drop ;
123 dup ?grow-hash (adjoin) ;
126 [ array>> 0 swap ] [ cardinality f <array> ] bi [
127 [ overd set-nth-unsafe 1 + ] curry each-member
131 (clone) [ clone ] change-array ; inline
134 over hash-set? [ set= ] [ 2drop f ] if ;
136 : >hash-set ( members -- hash-set )
137 dup length <hash-set> [ (rehash) ] keep ; inline
140 drop dup hash-set? [ ?members >hash-set ] unless ; inline
142 INSTANCE: hash-set set
144 ! Overrides for performance
148 : and-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
149 '[ dup tombstone? [ drop t ] _ if ] ; inline
151 : not-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
152 '[ dup tombstone? [ drop f ] _ if ] ; inline
154 : array/tester ( hash-set1 hash-set2 -- array quot )
155 [ array>> ] dip '[ _ in? ] ; inline
157 : filter-members ( hash-set array quot: ( elt -- ? ) -- accum )
158 rot cardinality <vector> [
159 '[ dup @ [ _ push-unsafe ] [ drop ] if ] each
164 M: hash-set intersect
166 small/large dupd array/tester not-tombstones
167 filter-members >hash-set
168 ] [ (intersect) >hash-set ] if ;
170 M: hash-set intersects?
172 small/large array/tester not-tombstones any?
173 ] [ small/large sequence/tester any? ] if ;
177 small/large [ array>> ] [ clone ] bi*
178 [ [ adjoin ] curry each-member ] keep
179 ] [ (union) >hash-set ] if ;
183 dupd array/tester [ not ] compose not-tombstones
184 filter-members >hash-set
185 ] [ (diff) >hash-set ] if ;
189 2dup [ cardinality ] bi@ > [ 2drop f ] [
190 array/tester and-tombstones all?
192 ] [ call-next-method ] if ;
196 2dup [ cardinality ] bi@ eq? [
197 array/tester and-tombstones all?
199 ] [ call-next-method ] if ;
201 M: hash-set hashcode*
203 dup cardinality 1 eq?
204 [ members hashcode* ] [ nip cardinality ] if
205 ] recursive-hashcode ;
209 M: f fast-set drop 0 <hash-set> ;
211 M: sequence fast-set >hash-set ;
213 M: sequence duplicates
214 dup length <hash-set> '[ _ ?adjoin ] reject ;
216 M: sequence all-unique?
217 dup length <hash-set> '[ _ ?adjoin ] all? ;