]> gitweb.factorcode.org Git - factor.git/blob - basis/hash2/hash2.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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 [ = ] 2bi@ and ; inline
18
19 : (assoc2) ( a b alist -- {a,b,val} )
20     [ 2= ] with with find nip ; inline
21
22 : assoc2 ( a b alist -- value )
23     (assoc2) dup [ third ] when ; inline
24
25 : set-assoc2 ( value a b alist -- alist )
26     [ rot 3array ] dip ?push ; inline
27
28 : hash2@ ( a b hash2 -- a b bucket hash2 )
29     [ 2dup hashcode2 ] dip [ length mod ] keep ; inline
30
31 : hash2 ( a b hash2 -- value/f )
32     hash2@ nth dup [ assoc2 ] [ 3drop f ] if ;
33
34 : set-hash2 ( a b value hash2 -- )
35     [ -rot ] dip hash2@ [ set-assoc2 ] change-nth ;
36
37 : alist>hash2 ( alist size -- hash2 )
38     <hash2> [ over [ first3 ] dip set-hash2 ] reduce ; inline