| 1 |
|
| 2 |
|
| 3 |
;;; Copied from |
| 4 |
;;; Efficient Nondestructive Equality Checking for Trees and Graphs |
| 5 |
;;; Michael D. Adams and R. Kent Dybvig |
| 6 |
;;; ICFP 2008 |
| 7 |
|
| 8 |
(library (ikarus.equal) |
| 9 |
(export equal?) |
| 10 |
(import (except (ikarus) equal?)) |
| 11 |
|
| 12 |
(module UNSAFE |
| 13 |
(< <= > >= = + - vector-ref vector-length car cdr) |
| 14 |
(import |
| 15 |
(rename (ikarus system $vectors) |
| 16 |
($vector-length vector-length) |
| 17 |
($vector-ref vector-ref)) |
| 18 |
(rename (ikarus system $pairs) |
| 19 |
($car car) |
| 20 |
($cdr cdr)) |
| 21 |
(rename (ikarus system $fx) |
| 22 |
($fx+ +) |
| 23 |
($fx- -) |
| 24 |
($fx< <) |
| 25 |
($fx> >) |
| 26 |
($fx>= >=) |
| 27 |
($fx<= <=) |
| 28 |
($fx= =)))) |
| 29 |
|
| 30 |
|
| 31 |
(define (union-find ht x y) |
| 32 |
(import UNSAFE) |
| 33 |
(define-struct box (content)) |
| 34 |
(define eq-hashtable-ref hashtable-ref) |
| 35 |
(define eq-hashtable-set! hashtable-set!) |
| 36 |
(define (find b) |
| 37 |
(let ([n (box-content b)]) |
| 38 |
(if (box? n) |
| 39 |
(let loop ([b b] [n n]) |
| 40 |
(let ([nn (box-content n)]) |
| 41 |
(if (box? nn) (begin (set-box-content! b nn) (loop n nn)) n))) |
| 42 |
b))) |
| 43 |
(let ([bx (eq-hashtable-ref ht x #f)] |
| 44 |
[by (eq-hashtable-ref ht y #f)]) |
| 45 |
(if (not bx) |
| 46 |
(if (not by) |
| 47 |
(let ([b (make-box 1)]) |
| 48 |
(eq-hashtable-set! ht x b) |
| 49 |
(eq-hashtable-set! ht y b) |
| 50 |
#f) |
| 51 |
(let ([ry (find by)]) (eq-hashtable-set! ht x ry) #f)) |
| 52 |
(if (not by) |
| 53 |
(let ([rx (find bx)]) (eq-hashtable-set! ht y rx) #f) |
| 54 |
(let ([rx (find bx)] [ry (find by)]) |
| 55 |
(or (eq? rx ry) |
| 56 |
(let ([nx (box-content rx)] [ny (box-content ry)]) |
| 57 |
(if (> nx ny) |
| 58 |
(begin |
| 59 |
(set-box-content! ry rx) |
| 60 |
(set-box-content! rx (+ nx ny)) |
| 61 |
#f) |
| 62 |
(begin |
| 63 |
(set-box-content! rx ry) |
| 64 |
(set-box-content! ry (+ ny nx)) |
| 65 |
#f))))))))) |
| 66 |
|
| 67 |
(define (pre? x y k) |
| 68 |
(import UNSAFE) |
| 69 |
(cond |
| 70 |
[(eq? x y) k] |
| 71 |
[(pair? x) |
| 72 |
(and (pair? y) |
| 73 |
(if (<= k 0) |
| 74 |
k |
| 75 |
(let ([k (pre? (car x) (car y) (- k 1))]) |
| 76 |
(and k (pre? (cdr x) (cdr y) k)))))] |
| 77 |
[(vector? x) |
| 78 |
(and (vector? y) |
| 79 |
(let ([n (vector-length x)]) |
| 80 |
(and (= (vector-length y) n) |
| 81 |
(let f ([i 0] [k k]) |
| 82 |
(if (or (= i n) (<= k 0)) |
| 83 |
k |
| 84 |
(let ([k (pre? |
| 85 |
(vector-ref x i) |
| 86 |
(vector-ref y i) |
| 87 |
(- k 1))]) |
| 88 |
(and k (f (+ i 1) k))))))))] |
| 89 |
[(string? x) (and (string? y) (string=? x y) k)] |
| 90 |
[(bytevector? x) (and (bytevector? y) (bytevector=? x y) k)] |
| 91 |
[else (and (eqv? x y) k)])) |
| 92 |
|
| 93 |
(define (interleave? x y k) |
| 94 |
(import UNSAFE) |
| 95 |
(let ([ht #f]) |
| 96 |
(define (call-union-find x y) |
| 97 |
(unless ht (set! ht (make-eq-hashtable))) |
| 98 |
(union-find ht x y)) |
| 99 |
(define (e? x y k) |
| 100 |
(if (<= k 0) |
| 101 |
(if (= k kb) (fast? x y (random (* 2 k0))) (slow? x y k)) |
| 102 |
(fast? x y k))) |
| 103 |
(define (slow? x y k) |
| 104 |
(cond |
| 105 |
[(eq? x y) k] |
| 106 |
[(pair? x) |
| 107 |
(and (pair? y) |
| 108 |
(if (call-union-find x y) |
| 109 |
0 |
| 110 |
(let ([k (e? (car x) (car y) (- k 1))]) |
| 111 |
(and k (e? (cdr x) (cdr y) k)))))] |
| 112 |
[(vector? x) |
| 113 |
(and (vector? y) |
| 114 |
(let ([n (vector-length x)]) |
| 115 |
(and (= (vector-length y) n) |
| 116 |
(if (call-union-find x y) |
| 117 |
0 |
| 118 |
(let f ([i 0] [k (- k 1)]) |
| 119 |
(if (= i n) |
| 120 |
k |
| 121 |
(let ([k (e? (vector-ref x i) |
| 122 |
(vector-ref y i) |
| 123 |
k)]) |
| 124 |
(and k (f (+ i 1) k)))))))))] |
| 125 |
[(string? x) (and (string? y) (string=? x y) k)] |
| 126 |
[(bytevector? x) (and (bytevector? y) (bytevector=? x y) k)] |
| 127 |
[else (and (eqv? x y) k)])) |
| 128 |
(define (fast? x y k) |
| 129 |
(let ([k (- k 1)]) |
| 130 |
(cond |
| 131 |
[(eq? x y) k] |
| 132 |
[(pair? x) |
| 133 |
(and (pair? y) |
| 134 |
(let ([k (e? (car x) (car y) k)]) |
| 135 |
(and k (e? (cdr x) (cdr y) k))))] |
| 136 |
[(vector? x) |
| 137 |
(and (vector? y) |
| 138 |
(let ([n (vector-length x)]) |
| 139 |
(and (= (vector-length y) n) |
| 140 |
(let f ([i 0] [k k]) |
| 141 |
(if (= i n) |
| 142 |
k |
| 143 |
(let ([k (e? (vector-ref x i) |
| 144 |
(vector-ref y i) |
| 145 |
k)]) |
| 146 |
(and k (f (+ i 1) k))))))))] |
| 147 |
[(string? x) (and (string? y) (string=? x y) k)] |
| 148 |
[(bytevector? x) (and (bytevector? y) (bytevector=? x y) k)] |
| 149 |
[else (and (eqv? x y) k)]))) |
| 150 |
(and (e? x y k) #t))) |
| 151 |
|
| 152 |
(define k0 400) |
| 153 |
|
| 154 |
(define kb -40) |
| 155 |
|
| 156 |
(define (interleave-equal? x y) |
| 157 |
(interleave? x y k0)) |
| 158 |
|
| 159 |
(define (precheck/interleave-equal? x y) |
| 160 |
(let ([k (pre? x y k0)]) |
| 161 |
(and k (or (> k 0) (interleave? x y 0))))) |
| 162 |
|
| 163 |
(define (equal? x y) |
| 164 |
(precheck/interleave-equal? x y)) |
| 165 |
|
| 166 |
) |