|
| 1 | +#lang racket |
| 2 | +(provide make-heap-diagram) |
| 3 | +(require pict) |
| 4 | +(require pict/code) |
| 5 | + |
| 6 | +(define pi 3.14) |
| 7 | + |
| 8 | + |
| 9 | +(define n 40) |
| 10 | + |
| 11 | +(define (make-imm-cell i) |
| 12 | + (cc-superimpose |
| 13 | + (code #,i) |
| 14 | + (rectangle n n))) |
| 15 | + |
| 16 | +(define (make-cons-cell) |
| 17 | + (cb-superimpose (rectangle n n) |
| 18 | + (code cons))) |
| 19 | + |
| 20 | + |
| 21 | +(define (make-box-cell) |
| 22 | + (cb-superimpose (rectangle n n) |
| 23 | + (code box))) |
| 24 | + |
| 25 | +(define (make-vect-cell) |
| 26 | + (cb-superimpose (rectangle n n) |
| 27 | + (code vect))) |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +(define (fwd-pts-to a b p) |
| 32 | + (pin-arrow-line 7 p |
| 33 | + a cc-find |
| 34 | + b lt-find |
| 35 | + #:start-angle (/ pi 2) |
| 36 | + #:end-angle (- (/ pi 2)) |
| 37 | + #:start-pull 1/4 |
| 38 | + #:end-pull 1/2)) |
| 39 | + |
| 40 | + |
| 41 | +#| |
| 42 | +(define rax (make-cons-cell)) |
| 43 | +(define m |
| 44 | + (let ((a (make-imm-cell 1)) |
| 45 | + (b (make-cons-cell)) |
| 46 | + (c (make-imm-cell 2)) |
| 47 | + (d (make-cons-cell)) |
| 48 | + (e (make-imm-cell 3)) |
| 49 | + (f (make-imm-cell ''()))) |
| 50 | + (define pre |
| 51 | + (foldr (λ (p1 p2) |
| 52 | + (hc-append 0 p1 p2)) |
| 53 | + (rectangle 0 n) |
| 54 | + (list a b c d e f))) |
| 55 | + (define heap |
| 56 | + (vc-append 0 (fwd-pts-to d e (fwd-pts-to b c pre)) |
| 57 | + (text "heap"))) |
| 58 | +
|
| 59 | + (define all |
| 60 | + (hc-append n (vc-append 0 rax (text "rax")) heap)) |
| 61 | +
|
| 62 | + (define q |
| 63 | + (fwd-pts-to rax heap all)) |
| 64 | + |
| 65 | + (inset q 20))) |
| 66 | +|# |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +(define (make-cell v) |
| 71 | + (match v |
| 72 | + [`(cons ,_) (make-cons-cell)] |
| 73 | + [`(box ,_) (make-box-cell)] |
| 74 | + [`(vect ,_) (make-vect-cell)] |
| 75 | + [_ (make-imm-cell v)])) |
| 76 | + |
| 77 | +(define (add-arrows spec cells p) |
| 78 | + ;(printf "~a~n" spec) |
| 79 | + (match spec |
| 80 | + ['() p] |
| 81 | + [(cons `(_ ,i) s) |
| 82 | + (add-arrows s |
| 83 | + cells |
| 84 | + (fwd-pts-to (list-ref cells (sub1 (- (length cells) (length s)))) |
| 85 | + (list-ref cells i) |
| 86 | + p))] |
| 87 | + [(cons _ s) (add-arrows s cells p)])) |
| 88 | + |
| 89 | +(define (make-heap-diagram spec) |
| 90 | + (match spec |
| 91 | + [(cons (and `(,_ ,i) r) h) |
| 92 | + (define rax (make-cell r)) |
| 93 | + (define heap (map make-cell h)) |
| 94 | + (define heap/arrows |
| 95 | + (add-arrows (rest spec) heap |
| 96 | + (foldr (λ (p1 p2) |
| 97 | + (hc-append 0 p1 p2)) |
| 98 | + (rectangle 0 n) |
| 99 | + heap))) |
| 100 | + |
| 101 | + (define heap/arrows/label |
| 102 | + (vc-append |
| 103 | + 0 |
| 104 | + heap/arrows |
| 105 | + (text "heap"))) |
| 106 | + |
| 107 | + (define rax/label |
| 108 | + (vc-append 0 rax (text "rax"))) |
| 109 | + |
| 110 | + (inset |
| 111 | + (fwd-pts-to rax (list-ref heap i) (hc-append n rax/label heap/arrows/label)) |
| 112 | + (* n 2))])) |
| 113 | +#; |
| 114 | +(make-heap-diagram |
| 115 | + '((cons 0) |
| 116 | + 1 |
| 117 | + (cons 2) |
| 118 | + 2 |
| 119 | + (cons 4) |
| 120 | + 3 |
| 121 | + '())) |
| 122 | + |
| 123 | +#; |
| 124 | +(make-heap-diagram |
| 125 | + '((cons 4) |
| 126 | + 3 |
| 127 | + '() |
| 128 | + 2 |
| 129 | + (cons 0) |
| 130 | + 1 |
| 131 | + (cons 2))) |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +#; |
| 137 | +(let ((a (make-imm-cell 3)) |
| 138 | + (b (make-imm-cell ''())) |
| 139 | + (c (make-imm-cell 2)) |
| 140 | + (d (make-cons-cell)) |
| 141 | + (e (make-imm-cell 1)) |
| 142 | + (f (make-cons-cell)) |
| 143 | + (g (make-cocell ''()))) |
| 144 | + (define pre |
| 145 | + (foldr (λ (p1 p2) |
| 146 | + (hc-append 0 p1 p2)) |
| 147 | + (rectangle 0 n) |
| 148 | + (list a b c d e f g))) |
| 149 | + (inset (fwd-pts-to f g (fwd-pts-to d e (fwd-pts-to b c pre))) 20)) |
| 150 | + |
0 commit comments