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 >r >r = r> r> = and ; inline
19 : (assoc2) ( a b alist -- {a,b,val} )
20 [ >r 2dup r> 2= ] find >r 3drop r> ; inline
22 : assoc2 ( a b alist -- value )
23 (assoc2) dup [ third ] when ; inline
25 : set-assoc2 ( value a b alist -- alist )
26 >r rot 3array r> ?push ; inline
28 : hash2@ ( a b hash2 -- a b bucket hash2 )
29 >r 2dup hashcode2 r> [ length mod ] keep ; inline
31 : hash2 ( a b hash2 -- value/f )
32 hash2@ nth [ assoc2 ] [ 2drop f ] if* ;
34 : set-hash2 ( a b value hash2 -- )
35 >r -rot r> hash2@ [ set-assoc2 ] change-nth ;
37 : alist>hash2 ( alist size -- hash2 )
38 <hash2> [ over >r first3 r> set-hash2 ] reduce ; inline