blob: ca5e32e5ea2eb6cfcb3346d469f205700f0b9e8f (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
(define-module (language python hash)
#:use-module (oop goops)
#:use-module (oop pf-objects)
#:export (py-hash complexity xy pyhash-N))
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
(define N #xefffffffffffffff)
(define pyhash-N N)
(define-inlinable (xy v seed)
(modulo
(logxor seed
(+ (py-hash v)
#x9e3779b9
(ash seed 6)
(ash seed -2)))
N))
(define complexity 10)
;; The default is to use guile's hash function
(define-method (py-hash x) (hash x N))
(define-method (py-hash (x <pair>))
(define i 0)
(let lp ((x x))
(if (< i complexity)
(begin
(set! i (+ i 1))
(if (pair? x)
(xy (lp (car x)) (lp (cdr x)))
(py-hash x)))
0)))
(define-method (py-hash (x <vector>))
(let ((n (min complexity (vector-length x))))
(let lp ((i 0) (s 0))
(if (< i n)
(lp (+ i 1)
(xy (py-hash (vector-ref x i)) s))
s))))
(define-method (py-hash (x <p>))
(aif it (ref x '__hash__)
(it)
(hash x complexity)))
|