| 1 |
;;; Copyright (C) 2008 Abdulaziz Ghuloum, R. Kent Dybvig |
| 2 |
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
;;; copy of this software and associated documentation files (the "Software"), |
| 6 |
;;; to deal in the Software without restriction, including without limitation |
| 7 |
;;; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
;;; and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
;;; Software is furnished to do so, subject to the following conditions: |
| 10 |
;;; |
| 11 |
;;; The above copyright notice and this permission notice shall be included in |
| 12 |
;;; all copies or substantial portions of the Software. |
| 13 |
;;; |
| 14 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
;;; DEALINGS IN THE SOFTWARE. |
| 21 |
|
| 22 |
(module (define-table) ;;;((define-table maker accessor mutator)) |
| 23 |
(define-syntax maker |
| 24 |
(syntax-rules () |
| 25 |
[(_ make-inner x t) (make-inner t x)] |
| 26 |
[(_ make-inner x t1 t2 ...) |
| 27 |
(let ([v (make-vector t1)]) |
| 28 |
(do ([i 0 (fx+ i 1)]) |
| 29 |
((fx= i t1)) |
| 30 |
(vector-set! v i (maker make-inner x t2 ...))) |
| 31 |
v)])) |
| 32 |
(define-syntax accessor |
| 33 |
(syntax-rules () |
| 34 |
[(_ inner-ref tbl i t) (inner-ref tbl i)] |
| 35 |
[(_ inner-ref tbl i t1 t2 ...) |
| 36 |
(let ([d (* t2 ...)]) |
| 37 |
(accessor inner-ref (vector-ref tbl (fxdiv i d)) |
| 38 |
(fxmod i d) t2 ...))])) |
| 39 |
(define-syntax mutator |
| 40 |
(syntax-rules () |
| 41 |
[(_ inner-set! tbl i x t) (inner-set! tbl i x)] |
| 42 |
[(_ inner-set! tbl i x t1 t2 ...) |
| 43 |
(let ([d (* t2 ...)]) |
| 44 |
(mutator inner-set! (vector-ref tbl (fxdiv i d)) |
| 45 |
(fxmod i d) x t2 ...))])) |
| 46 |
(define-syntax define-table |
| 47 |
(lambda (x) |
| 48 |
(define accessor-code |
| 49 |
(lambda (x) |
| 50 |
(syntax-case x () |
| 51 |
[(inner-ref tbl i t) #'(inner-ref tbl i)] |
| 52 |
[(inner-ref tbl i t1 t2 ...) |
| 53 |
(with-syntax ([(d) (generate-temporaries '(d))]) |
| 54 |
(with-syntax ([body (accessor-code |
| 55 |
#'(inner-ref |
| 56 |
(vector-ref tbl (fxdiv i d)) |
| 57 |
(fxmod i d) t2 ...))]) |
| 58 |
#'(let ([d (* t2 ...)]) body)))]))) |
| 59 |
(syntax-case x () |
| 60 |
[(_ (make-table table-ref table-set! table-ref-code) |
| 61 |
(make-inner inner-ref inner-set!) size dim ...) |
| 62 |
(with-syntax ([(t1 t2 ...) (generate-temporaries #'(size dim ...))] |
| 63 |
[code (accessor-code |
| 64 |
#'(inner-ref tbl i |
| 65 |
(/ size (* dim ...)) |
| 66 |
dim ...))]) |
| 67 |
#'(module (make-table table-ref table-set! table-ref-code) |
| 68 |
(define t2 dim) ... |
| 69 |
(define t1 (/ size (* t2 ...))) |
| 70 |
(define make-table (lambda (x) (maker make-inner x t1 t2 ...))) |
| 71 |
(define table-ref-code '(lambda (tbl i) code)) |
| 72 |
(define table-ref (lambda (tbl i) (accessor inner-ref tbl i t1 t2 ...))) |
| 73 |
(define table-set! (lambda (tbl i x) (mutator inner-set! tbl i x t1 t2 ...)))))])))) |
| 74 |
|
| 75 |
(define (with-output-to-file* file thunk) |
| 76 |
(when (file-exists? file) (delete-file file)) |
| 77 |
(with-output-to-file file thunk)) |
| 78 |
|
| 79 |
(define common-equal? |
| 80 |
(lambda (x y) |
| 81 |
(cond |
| 82 |
[(eq? x y) #t] |
| 83 |
[(vector? x) |
| 84 |
(and (vector? y) |
| 85 |
(let ([n (vector-length x)]) |
| 86 |
(and (fx= (vector-length y) n) |
| 87 |
(let f ([i 0]) |
| 88 |
(or (fx= i n) |
| 89 |
(and (eq? (vector-ref x i) (vector-ref y i)) |
| 90 |
(f (fx+ i 1))))))))] |
| 91 |
[(pair? x) (and (pair? y) (eq? (car x) (car y)) (eq? (cdr x) (cdr y)))] |
| 92 |
[else (equal? x y)]))) |
| 93 |
|
| 94 |
(define cache '()) |
| 95 |
#;(define commonize ; 5.8s |
| 96 |
(lambda (x) |
| 97 |
(or (find (lambda (y) (common-equal? y x)) cache) |
| 98 |
(begin (set! cache (cons x cache)) x)))) |
| 99 |
#;(define commonize ; 2.6s |
| 100 |
(let ([cache-table (make-hashtable equal-hash common-equal?)]) |
| 101 |
(lambda (x) |
| 102 |
(or (hashtable-ref cache-table x #f) |
| 103 |
(begin |
| 104 |
(set! cache (cons x cache)) ; for sizeof |
| 105 |
(hashtable-set! cache-table x x) |
| 106 |
x))))) |
| 107 |
(define commonize ; 1.9s |
| 108 |
(lambda (x) |
| 109 |
(let ([v (find (lambda (y) (common-equal? y x)) cache)]) |
| 110 |
(if v |
| 111 |
(begin (set! cache (cons v (remq v cache))) v) |
| 112 |
(begin (set! cache (cons x cache)) x))))) |
| 113 |
|
| 114 |
(define commonize* |
| 115 |
(lambda (x) |
| 116 |
(cond |
| 117 |
[(vector? x) |
| 118 |
(let ([n (vector-length x)]) |
| 119 |
(do ([i 0 (fx+ i 1)]) |
| 120 |
((fx= i n)) |
| 121 |
(vector-set! x i (commonize* (vector-ref x i))))) |
| 122 |
(commonize x)] |
| 123 |
[(bytevector? x) (commonize x)] |
| 124 |
[(pair? x) |
| 125 |
(set-car! x (commonize* (car x))) |
| 126 |
(set-cdr! x (commonize* (cdr x))) |
| 127 |
(commonize x)] |
| 128 |
[else x]))) |
| 129 |
|
| 130 |
(define (sizeof ls) |
| 131 |
(apply + |
| 132 |
(map (lambda (x) |
| 133 |
(cond |
| 134 |
[(vector? x) (* (vector-length x) ptr-bytes)] |
| 135 |
[(bytevector? x) (bytevector-length x)] |
| 136 |
[(pair? x) (* ptr-bytes 2)] |
| 137 |
[(fixnum? x) 0] |
| 138 |
[(char? x) 0] |
| 139 |
[else (error 'sizeof "unexpected ~s" x)])) |
| 140 |
ls))) |
| 141 |
|
| 142 |
(define (hex->num x) (string->number x 16)) |