1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: combinators kernel layouts math math.bitwise
5 math.floating-point math.functions ;
9 GENERIC: number-hashcode ( x -- h )
14 cell-bits 64 = 61 31 ? 2^ 1 - ; inline foldable
17 cell-bits 1 - 2^ ; inline foldable
19 : hash-fraction ( m n -- h )
21 [ 2dup [ P mod zero? ] both? ] [
29 [ abs P mod ] [ P 2 - P ^mod P mod ] bi* *
30 ] dip 0 < [ neg ] when
31 dup -1 = [ drop -2 ] when
36 M: integer number-hashcode 1 hash-fraction ;
38 M: ratio number-hashcode >fraction hash-fraction ;
40 M: float number-hashcode ( x -- h )
42 { [ dup fp-nan? ] [ drop 0 ] }
43 { [ dup fp-infinity? ] [ 0 > 314159 -314159 ? ] }
44 [ double>ratio number-hashcode ]
47 M: complex number-hashcode ( x -- h )
48 >rect [ number-hashcode ] bi@ 1000003 * +
49 cell-bits on-bits bitand dup -1 = [ drop -2 ] when ;