| 
									
										
										
										
											2010-02-26 11:01:57 -05:00
										 |  |  | ! Copyright (C) 2010 Daniel Ehrenberg | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | ! Copyright (C) 2005, 2011 John Benediktsson, Slava Pestov. | 
					
						
							| 
									
										
										
										
											2010-02-26 11:01:57 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2014-02-07 01:38:48 -05:00
										 |  |  | USING: accessors arrays growable.private hash-sets | 
					
						
							|  |  |  | hashtables.private kernel kernel.private math math.private | 
					
						
							|  |  |  | sequences sequences.private sets sets.private slots.private | 
					
						
							|  |  |  | vectors ;
 | 
					
						
							| 
									
										
										
										
											2010-02-26 11:01:57 -05:00
										 |  |  | IN: hash-sets | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | TUPLE: hash-set | 
					
						
							|  |  |  | { count array-capacity } | 
					
						
							|  |  |  | { deleted array-capacity } | 
					
						
							|  |  |  | { array array } ;
 | 
					
						
							| 
									
										
										
										
											2010-02-26 11:01:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : hash@ ( key array -- i )
 | 
					
						
							|  |  |  |     [ hashcode >fixnum ] dip wrap ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : probe ( array i probe# -- array i probe# )
 | 
					
						
							|  |  |  |     1 fixnum+fast [ fixnum+fast over wrap ] keep ; 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?
 | 
					
						
							|  |  |  |     [ 4drop no-key ] [ | 
					
						
							|  |  |  |         [ = ] dip swap
 | 
					
						
							|  |  |  |         [ drop rot drop t ] | 
					
						
							|  |  |  |         [ probe (key@) ] | 
					
						
							|  |  |  |         if
 | 
					
						
							|  |  |  |     ] if ; inline recursive
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : key@ ( key hash -- array n ? )
 | 
					
						
							|  |  |  |     array>> dup length>> 0 eq?
 | 
					
						
							|  |  |  |     [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <hash-array> ( n -- array )
 | 
					
						
							|  |  |  |     1 + next-power-of-2 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?
 | 
					
						
							|  |  |  |         [ nip [ drop ] 3dip t ] | 
					
						
							|  |  |  |         [ pick or [ probe ] dip (new-key@) ] | 
					
						
							|  |  |  |         if
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         [ [ pick ] dip = ] 2dip rot
 | 
					
						
							|  |  |  |         [ nip [ drop ] 3dip f ] | 
					
						
							|  |  |  |         [ [ probe ] dip (new-key@) ] | 
					
						
							|  |  |  |         if
 | 
					
						
							|  |  |  |     ] if ; inline recursive
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:58:03 -04:00
										 |  |  | : new-key@ ( key hash -- array n ? )
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  |     [ array>> 2dup hash@ 0 f (new-key@) ] keep swap
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:58:03 -04:00
										 |  |  |     [ over [ hash-deleted- ] [ hash-count+ ] if swap or t ] [ 2drop f ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : set-nth-item ( key seq n -- )
 | 
					
						
							|  |  |  |     2 fixnum+fast set-slot ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:58:03 -04:00
										 |  |  | : (adjoin) ( key hash -- ? )
 | 
					
						
							|  |  |  |     dupd new-key@ [ set-nth-item ] dip ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-08 01:02:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (rehash) ( seq hash -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:58:03 -04:00
										 |  |  |     [ (adjoin) drop ] curry each ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : hash-large? ( hash -- ? )
 | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:07 -05:00
										 |  |  |     [ count>> 3 fixnum*fast ] | 
					
						
							|  |  |  |     [ array>> length>> 1 fixnum-shift-fast ] bi fixnum>= ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-07 01:38:48 -05:00
										 |  |  | : each-member ( ... array quot: ( ... elt -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2013-03-26 18:19:43 -04:00
										 |  |  |     [ if ] curry [ dup tombstone? [ drop ] ] prepose each ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-21 02:02:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | : grow-hash ( hash -- )
 | 
					
						
							|  |  |  |     { hash-set } declare [ | 
					
						
							| 
									
										
										
										
											2013-03-21 02:02:28 -04:00
										 |  |  |         [ array>> ] | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  |         [ cardinality 1 + ] | 
					
						
							|  |  |  |         [ reset-hash ] tri
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:58:03 -04:00
										 |  |  |     ] keep [ (adjoin) drop ] curry each-member ;
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ?grow-hash ( hash -- )
 | 
					
						
							|  |  |  |     dup hash-large? [ grow-hash ] [ drop ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 09:15:27 -05:00
										 |  |  | : <hash-set> ( capacity -- hash-set )
 | 
					
						
							| 
									
										
										
										
											2014-01-08 12:55:45 -05:00
										 |  |  |     [ 0 0 ] dip <hash-array> hash-set boa ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 09:15:27 -05:00
										 |  |  | M: hash-set in? | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  |      key@ 2nip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 09:15:27 -05:00
										 |  |  | M: hash-set clear-set | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  |     [ init-hash ] [ array>> [ drop ((empty)) ] map! drop ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 09:15:27 -05:00
										 |  |  | M: hash-set delete | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  |     [ nip ] [ key@ ] 2bi [ | 
					
						
							|  |  |  |         [ ((tombstone)) ] 2dip set-nth-item | 
					
						
							|  |  |  |         hash-deleted+ | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         3drop
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 09:15:27 -05:00
										 |  |  | M: hash-set cardinality | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  |     [ count>> ] [ deleted>> ] bi - ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-08 09:15:27 -05:00
										 |  |  | : rehash ( hash-set -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-08 01:04:52 -05:00
										 |  |  |     [ members ] [ clear-set ] [ (rehash) ] tri ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:58:03 -04:00
										 |  |  | M: hash-set adjoin | 
					
						
							|  |  |  |     dup ?grow-hash (adjoin) drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: hash-set ?adjoin | 
					
						
							| 
									
										
										
										
											2013-03-08 01:02:02 -05:00
										 |  |  |     dup ?grow-hash (adjoin) ;
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: hash-set members | 
					
						
							| 
									
										
										
										
											2014-02-07 01:38:48 -05:00
										 |  |  |     [ array>> 0 swap ] [ cardinality f <array> ] bi [ | 
					
						
							|  |  |  |         [ [ over ] dip set-nth-unsafe 1 + ] curry each-member | 
					
						
							|  |  |  |     ] keep nip ;
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: hash-set clone | 
					
						
							|  |  |  |     (clone) [ clone ] change-array ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: hash-set equal? | 
					
						
							|  |  |  |     over hash-set? [ set= ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2011-10-19 14:35:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : >hash-set ( members -- hash-set )
 | 
					
						
							| 
									
										
										
										
											2013-03-26 20:51:59 -04:00
										 |  |  |     dup length <hash-set> [ (rehash) ] keep ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: hash-set set-like | 
					
						
							|  |  |  |     drop dup hash-set? [ ?members >hash-set ] unless ; inline
 | 
					
						
							| 
									
										
										
										
											2010-02-26 11:01:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: hash-set set | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 18:19:43 -04:00
										 |  |  | ! Overrides for performance | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:11:06 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : and-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
 | 
					
						
							|  |  |  |     [ if ] curry [ dup tombstone? [ drop t ] ] prepose ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : not-tombstones ( quot: ( elt -- ? ) -- quot: ( elt -- ? ) )
 | 
					
						
							|  |  |  |     [ if ] curry [ dup tombstone? [ drop f ] ] prepose ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : array/tester ( hash-set1 hash-set2 -- array quot )
 | 
					
						
							|  |  |  |     [ array>> ] dip [ in? ] curry ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 20:24:38 -04:00
										 |  |  | : filter-members ( hash-set array quot: ( elt -- ? ) -- accum )
 | 
					
						
							|  |  |  |     [ dup ] prepose rot cardinality <vector> [ | 
					
						
							|  |  |  |         [ push-unsafe ] curry [ [ drop ] if ] curry
 | 
					
						
							|  |  |  |         compose each
 | 
					
						
							|  |  |  |     ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:11:06 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:24:45 -04:00
										 |  |  | M: hash-set intersect | 
					
						
							|  |  |  |     over hash-set? [ | 
					
						
							| 
									
										
										
										
											2013-03-26 20:24:38 -04:00
										 |  |  |         small/large dupd array/tester not-tombstones | 
					
						
							|  |  |  |         filter-members >hash-set | 
					
						
							| 
									
										
										
										
											2013-03-26 19:24:45 -04:00
										 |  |  |     ] [ (intersect) >hash-set ] if ;
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:00:31 -04:00
										 |  |  | M: hash-set intersects? | 
					
						
							|  |  |  |     over hash-set? [ | 
					
						
							| 
									
										
										
										
											2013-03-26 19:11:06 -04:00
										 |  |  |         small/large array/tester not-tombstones any?
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:00:31 -04:00
										 |  |  |     ] [ small/large sequence/tester any? ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 17:17:10 -04:00
										 |  |  | M: hash-set union | 
					
						
							|  |  |  |     over hash-set? [ | 
					
						
							|  |  |  |         small/large [ array>> ] [ clone ] bi*
 | 
					
						
							|  |  |  |         [ [ adjoin ] curry each-member ] keep
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:24:45 -04:00
										 |  |  |     ] [ (union) >hash-set ] if ;
 | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:24:45 -04:00
										 |  |  | M: hash-set diff | 
					
						
							|  |  |  |     over hash-set? [ | 
					
						
							| 
									
										
										
										
											2013-03-26 20:24:38 -04:00
										 |  |  |         dupd array/tester [ not ] compose not-tombstones | 
					
						
							|  |  |  |         filter-members >hash-set | 
					
						
							| 
									
										
										
										
											2013-03-26 19:24:45 -04:00
										 |  |  |     ] [ (diff) >hash-set ] if ;
 | 
					
						
							| 
									
										
										
										
											2013-03-26 18:19:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 19:11:06 -04:00
										 |  |  | M: hash-set subset? | 
					
						
							|  |  |  |     over hash-set? [ | 
					
						
							|  |  |  |         2dup [ cardinality ] bi@ > [ 2drop f ] [ | 
					
						
							|  |  |  |             array/tester and-tombstones all?
 | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] [ call-next-method ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: hash-set set= | 
					
						
							|  |  |  |     over hash-set? [ | 
					
						
							|  |  |  |         2dup [ cardinality ] bi@ eq? [ | 
					
						
							|  |  |  |             array/tester and-tombstones all?
 | 
					
						
							|  |  |  |         ] [ 2drop f ] if
 | 
					
						
							|  |  |  |     ] [ call-next-method ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 18:19:43 -04:00
										 |  |  | ! Default methods | 
					
						
							| 
									
										
										
										
											2013-03-08 00:43:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: f fast-set drop 0 <hash-set> ;
 | 
					
						
							| 
									
										
										
										
											2010-02-26 11:01:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 14:35:25 -04:00
										 |  |  | M: sequence fast-set >hash-set ;
 | 
					
						
							| 
									
										
										
										
											2010-02-26 12:07:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sequence duplicates | 
					
						
							| 
									
										
										
										
											2012-08-24 19:36:30 -04:00
										 |  |  |     dup length <hash-set> [ ?adjoin not ] curry filter ;
 | 
					
						
							| 
									
										
										
										
											2010-04-13 07:43:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sequence all-unique? | 
					
						
							| 
									
										
										
										
											2013-03-11 13:21:02 -04:00
										 |  |  |     dup length <hash-set> [ ?adjoin ] curry all? ;
 |