1 USING: kernel sequences arrays math vectors ;
4 ! Little ad-hoc datastructure used to map two numbers
6 ! Created for the NFC mapping table.
7 ! We could use a hashtable of 2arrays, but that
8 ! involves creating too many objects.
9 ! Does not allow duplicate keys.
11 : hashcode2 ( a b -- hashcode )
12 swap 8 shift + ; inline
14 : <hash2> ( size -- hash2 ) f <array> ;
16 : 2= ( a b pair -- ? )
17 first2 swapd [ = ] 2bi@ and ; inline
19 : (assoc2) ( a b alist -- {a,b,val} )
20 [ 2= ] with with find nip ; inline
22 : assoc2 ( a b alist -- value )
23 (assoc2) dup [ third ] when ; inline
25 : set-assoc2 ( value a b alist -- alist )
26 [ rot 3array ] dip ?push ; inline
28 : hash2@ ( a b hash2 -- a b bucket hash2 )
29 [ 2dup hashcode2 ] dip [ length mod ] keep ; inline
31 : hash2 ( a b hash2 -- value/f )
32 hash2@ nth dup [ assoc2 ] [ 3drop f ] if ;
34 : set-hash2 ( a b value hash2 -- )
35 [ -rot ] dip hash2@ [ set-assoc2 ] change-nth ;
37 : alist>hash2 ( alist size -- hash2 )
38 <hash2> [ over [ first3 ] dip set-hash2 ] reduce ; inline