Skip to content

Commit ff83de2

Browse files
committed
Rudimentary utility for making heap diagrams.
1 parent 6038163 commit ff83de2

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

www/notes/diagrams.rkt

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

0 commit comments

Comments
 (0)