]> gitweb.factorcode.org Git - factor.git/blob - basis/hash2/hash2.factor
Create basis vocab root
[factor.git] / basis / hash2 / hash2.factor
1 USING: kernel sequences arrays math vectors ;
2 IN: hash2
3
4 ! Little ad-hoc datastructure used to map two numbers
5 ! to a single value.
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.
10
11 : hashcode2 ( a b -- hashcode )
12     swap 8 shift + ; inline
13
14 : <hash2> ( size -- hash2 ) f <array> ;
15
16 : 2= ( a b pair -- ? )
17     first2 swapd >r >r = r> r> = and ; inline
18
19 : (assoc2) ( a b alist -- {a,b,val} )
20     [ >r 2dup r> 2= ] find >r 3drop r> ; inline
21
22 : assoc2 ( a b alist -- value )
23     (assoc2) dup [ third ] when ; inline
24
25 : set-assoc2 ( value a b alist -- alist )
26     >r rot 3array r> ?push ; inline
27
28 : hash2@ ( a b hash2 -- a b bucket hash2 )
29     >r 2dup hashcode2 r> [ length mod ] keep ; inline
30
31 : hash2 ( a b hash2 -- value/f )
32     hash2@ nth [ assoc2 ] [ 2drop f ] if* ;
33
34 : set-hash2 ( a b value hash2 -- )
35     >r -rot r> hash2@ [ set-assoc2 ] change-nth ;
36
37 : alist>hash2 ( alist size -- hash2 )
38     <hash2> [ over >r first3 r> set-hash2 ] reduce ; inline