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@) ]
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 : (delete) ( key hash -- ? )
69 [ nip ] [ key@ ] 2bi [
70 [ +tombstone+ ] 2dip set-nth-item
76 : (rehash) ( seq hash -- )
77 [ (adjoin) drop ] curry each ; inline
79 : hash-large? ( hash -- ? )
80 [ count>> 1 fixnum+fast 3 fixnum*fast ]
81 [ array>> length>> 1 fixnum-shift-fast ] bi fixnum>= ; inline
83 : each-member ( ... array quot: ( ... elt -- ... ) -- ... )
84 [ if ] curry [ dup tombstone? [ drop ] ] prepose each ; inline
86 : grow-hash ( hash -- )
87 { hash-set } declare [
91 ] keep [ (adjoin) drop ] curry each-member ;
93 : ?grow-hash ( hash -- )
94 dup hash-large? [ grow-hash ] [ drop ] if ; inline
98 : <hash-set> ( capacity -- hash-set )
100 [ 0 0 ] dip <hash-array> hash-set boa ; inline
105 M: hash-set clear-set
106 [ init-hash ] [ array>> [ drop +empty+ ] map! drop ] bi ;
114 M: hash-set cardinality
115 [ count>> ] [ deleted>> ] bi - ; inline
117 : rehash ( hash-set -- )
118 [ members ] [ clear-set ] [ (rehash) ] tri ;
121 dup ?grow-hash (adjoin) drop ;
124 dup ?grow-hash (adjoin) ;
127 [ array>> 0 swap ] [ cardinality f <array> ] bi [
128 [ overd set-nth-unsafe 1 + ] curry each-member
132 (clone) [ clone ] change-array ; inline
135 over hash-set? [ set= ] [ 2drop f ] if ;
137 : >hash-set ( members -- hash-set )
138 dup length <hash-set> [ (rehash) ] keep ; inline
141 drop dup hash-set? [ ?members >hash-set ] unless ; inline
143 INSTANCE: hash-set set
145 ! Overrides for performance
149 : and-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
150 [ if ] curry [ dup tombstone? [ drop t ] ] prepose ; inline
152 : not-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
153 [ if ] curry [ dup tombstone? [ drop f ] ] prepose ; inline
155 : array/tester ( hash-set1 hash-set2 -- array quot )
156 [ array>> ] dip [ in? ] curry ; inline
158 : filter-members ( hash-set array quot: ( elt -- ? ) -- accum )
159 [ dup ] prepose rot cardinality <vector> [
160 [ push-unsafe ] curry [ [ drop ] if ] curry
166 M: hash-set intersect
168 small/large dupd array/tester not-tombstones
169 filter-members >hash-set
170 ] [ (intersect) >hash-set ] if ;
172 M: hash-set intersects?
174 small/large array/tester not-tombstones any?
175 ] [ small/large sequence/tester any? ] if ;
179 small/large [ array>> ] [ clone ] bi*
180 [ [ adjoin ] curry each-member ] keep
181 ] [ (union) >hash-set ] if ;
185 dupd array/tester [ not ] compose not-tombstones
186 filter-members >hash-set
187 ] [ (diff) >hash-set ] if ;
191 2dup [ cardinality ] bi@ > [ 2drop f ] [
192 array/tester and-tombstones all?
194 ] [ call-next-method ] if ;
198 2dup [ cardinality ] bi@ eq? [
199 array/tester and-tombstones all?
201 ] [ call-next-method ] if ;
203 M: hash-set hashcode*
205 dup cardinality 1 eq?
206 [ members hashcode* ] [ nip cardinality ] if
207 ] recursive-hashcode ;
211 M: f fast-set drop 0 <hash-set> ;
213 M: sequence fast-set >hash-set ;
215 M: sequence duplicates
216 dup length <hash-set> [ ?adjoin ] curry reject ;
218 M: sequence all-unique?
219 dup length <hash-set> [ ?adjoin ] curry all? ;