factor/basis/hash2/hash2.factor

41 lines
1.1 KiB
Factor
Raw Normal View History

2009-04-18 22:53:22 -04:00
! Copyright (C) 2007 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences arrays math vectors locals ;
2007-09-20 18:09:08 -04:00
IN: hash2
! Little ad-hoc datastructure used to map two numbers
! to a single value.
! Created for the NFC mapping table.
! We could use a hashtable of 2arrays, but that
! involves creating too many objects.
! Does not allow duplicate keys.
: hashcode2 ( a b -- hashcode )
swap 8 shift + ; inline
: <hash2> ( size -- hash2 ) f <array> ;
: 2= ( a b pair -- ? )
2009-02-02 15:26:54 -05:00
first2 [ = ] bi-curry@ bi* and ; inline
2007-09-20 18:09:08 -04:00
: (assoc2) ( a b alist -- {a,b,val} )
2008-11-29 16:21:23 -05:00
[ 2= ] with with find nip ; inline
2007-09-20 18:09:08 -04:00
: assoc2 ( a b alist -- value )
(assoc2) dup [ third ] when ; inline
2009-04-18 22:53:22 -04:00
:: set-assoc2 ( value a b alist -- alist )
{ a b value } alist ?push ; inline
2007-09-20 18:09:08 -04:00
: hash2@ ( a b hash2 -- a b bucket hash2 )
2008-11-29 14:29:00 -05:00
[ 2dup hashcode2 ] dip [ length mod ] keep ; inline
2007-09-20 18:09:08 -04:00
: hash2 ( a b hash2 -- value/f )
2008-11-29 16:21:23 -05:00
hash2@ nth dup [ assoc2 ] [ 3drop f ] if ;
2007-09-20 18:09:08 -04:00
2009-04-18 22:53:22 -04:00
:: set-hash2 ( a b value hash2 -- )
value a b hash2 hash2@ [ set-assoc2 ] change-nth ;
2007-09-20 18:09:08 -04:00
: alist>hash2 ( alist size -- hash2 )
2008-11-29 14:29:00 -05:00
<hash2> [ over [ first3 ] dip set-hash2 ] reduce ; inline