Franck Pommereau

new example, bug fixes

...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
7 *.fls 7 *.fls
8 *.nav 8 *.nav
9 *.vrb 9 *.vrb
10 -*.out
11 *.snm 10 *.snm
12 *.synctex.gz 11 *.synctex.gz
13 *.toc 12 *.toc
......
1 + * add `VBox` and `HBox` to layout data blocks without the need to play
2 + with Heaps (fix `examples/qs-main.py` accordingly)
...@@ -216,7 +216,7 @@ class Aggregate (CAniTikZ) : ...@@ -216,7 +216,7 @@ class Aggregate (CAniTikZ) :
216 def __setitem__ (self, key, val) : 216 def __setitem__ (self, key, val) :
217 self._d[key].set(val) 217 self._d[key].set(val)
218 def __len__ (self) : 218 def __len__ (self) :
219 - return len(self.d) 219 + return len(self._d)
220 def stop (self) : 220 def stop (self) :
221 for v in self._d.values() : 221 for v in self._d.values() :
222 v.stop() 222 v.stop()
......
...@@ -170,9 +170,10 @@ class FunctionReturn (Exception) : ...@@ -170,9 +170,10 @@ class FunctionReturn (Exception) :
170 self.RET = RET 170 self.RET = RET
171 171
172 class RETURN (_CODE) : 172 class RETURN (_CODE) :
173 - _fields = ["value"] 173 + _options = ["value"]
174 def __call__ (self) : 174 def __call__ (self) :
175 - self.value() 175 + if self.value is not None :
176 + self.value()
176 self._at.add(self.IP) 177 self._at.add(self.IP)
177 self.IP += 1 178 self.IP += 1
178 raise FunctionReturn(self.RET) 179 raise FunctionReturn(self.RET)
......
1 -all: heap.pdf qs-partition.pdf 1 +all: heap.pdf qs-partition.pdf qs-main.pdf
2 2
3 %.pdf: %.py tpl.tex 3 %.pdf: %.py tpl.tex
4 ln -sf ../codanim . 4 ln -sf ../codanim .
5 rm -f out.* 5 rm -f out.*
6 python $< 6 python $<
7 - latexmk -pdf tpl 7 + pdflatex tpl
8 + pdflatex tpl
8 cp tpl.pdf $@ 9 cp tpl.pdf $@
9 - latexmk -C tpl
10 10
11 clean: 11 clean:
12 - rm -f $$(grep '^*' ../.gitignore) 12 + rm -f out.* $$(grep '^*' ../.gitignore)
......
No preview for this file type
No preview for this file type
1 +from codanim.data import Array, Heap
2 +from codanim.flow import FUNC, BLOCK, ENV, WS, XDECL, DECL, PY, EXPR, STMT, WHILE, IF
3 +
4 +##
5 +## quicksort partitioning
6 +##
7 +
8 +array = Array([3, 13, 5, 0, 7, 11, 4, 9, 14, 2, 10, 1, 12, 8, 6],
9 + index=["a", "b", "p"])
10 +first = Array(15,
11 + aggregate={"grow": "north",
12 + "ticks": "west",
13 + "index": None})
14 +last = Array(15,
15 + index=["top"],
16 + aggregate={"grow": "north",
17 + "ticks": None,
18 + "index": "east"})
19 +
20 +def part (tab, first, last) :
21 + t = [tab[i] for i in range(first, last+1)]
22 + a, b, pivot = -1, len(t), t[(len(t)-1)//2]
23 + while True :
24 + while True :
25 + a += 1
26 + if t[a] >= pivot :
27 + break
28 + while True :
29 + b -= 1
30 + if t[b] <= pivot :
31 + break
32 + if a >= b :
33 + for i, v in enumerate(t) :
34 + tab[i+first] = v
35 + return b
36 + t[a], t[b] = t[b], t[a]
37 +
38 +sort = FUNC(BLOCK(ENV("t", array),
39 + ENV("part", part),
40 + ENV("top", None),
41 + ENV("_first", first),
42 + ENV("_last", last),
43 + WS("\n "),
44 + XDECL("p", "a", "b",
45 + src="uint p, a, b;"),
46 + WS("\n "),
47 + DECL("first", EXPR("_first", src="stackNew(t.len)"),
48 + src="Stack {name} = {init};"),
49 + PY("top = 0"),
50 + WS("\n "),
51 + DECL("last", EXPR("_last", src="stackNew(t.len)"),
52 + src="Stack {name} = {init};"),
53 + WS("\n "),
54 + STMT("first[top] = 0",
55 + src="stackPush(&first, 0);"),
56 + WS("\n "),
57 + STMT("last[top] = (len(t)-1); top += 1",
58 + src="stackPush(&last, t.len-1);"),
59 + WS("\n "),
60 + WHILE(EXPR("top > 0", src="!stackEmpty(first)"),
61 + BLOCK(WS(" "),
62 + STMT("a, first[top-1] = first[top-1], None",
63 + src="a = stackPop(&first);"),
64 + WS("\n "),
65 + STMT("b, last[top-1] = last[top-1], None; top -= 1",
66 + src="b = stackPop(&last);"),
67 + WS("\n "),
68 + IF(EXPR("a < b"),
69 + BLOCK(WS(" "),
70 + STMT("p = a + part(t, a, b)",
71 + src="p = a + partition(slice(t, a, b));"),
72 + WS("\n "),
73 + STMT("first[top] = a",
74 + src="stackPush(&first, a);"),
75 + WS("\n "),
76 + STMT("last[top] = p; top += 1",
77 + src="stackPush(&last, p);"),
78 + WS("\n "),
79 + STMT("first[top] = p+1",
80 + src="stackPush(&first, p+1);"),
81 + WS("\n "),
82 + STMT("last[top] = b; top += 1",
83 + src="stackPush(&last, b);"),
84 + WS("\n")),
85 + src="if ({cond}) {{\n{then} }}"),
86 + WS("\n")),
87 + src="while ({cond}) {{\n{body} }}\n")),
88 + src="void sort (Tab t) {{{body}}}")
89 +
90 +# let's play with Heap to layout the various data blocks
91 +# /!\ we must do this before simulating the code
92 +ha = Heap()
93 +ha.new(array)
94 +hs = Heap(heap={"grow": "right",
95 + "distance": "0pt"})
96 +hs.new(first)
97 +hs.new(last)
98 +h = Heap(heap={"grow": "right",
99 + "distance": "5mm"})
100 +h.new(ha)
101 +h.new(hs)
102 +
103 +# run code and save its animation
104 +sort.IP += 1
105 +sort()
106 +with open("out.code", "w") as out :
107 + out.write(sort.tex())
108 +
109 +# save data animation
110 +with open("out.tikz", "w") as out :
111 + out.write(h.tex(tikzpicture={"scale": .55}))
No preview for this file type
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
12 \usetikzlibrary{arrows.meta} 12 \usetikzlibrary{arrows.meta}
13 \usetikzlibrary{positioning} 13 \usetikzlibrary{positioning}
14 14
15 +\setbeamersize{text margin left=5mm,text margin right=5mm}
16 +\setlength{\headsep}{-5mm}
17 +
15 \def\hl #1{{\setbox0=\hbox{#1}% 18 \def\hl #1{{\setbox0=\hbox{#1}%
16 \adjustbox{set vsize={\ht0}{\dp0}}{\hbox to \wd0{\hss\colorbox{yellow}{#1}\hss}}}} 19 \adjustbox{set vsize={\ht0}{\dp0}}{\hbox to \wd0{\hss\colorbox{yellow}{#1}\hss}}}}
17 20
...@@ -26,9 +29,14 @@ ...@@ -26,9 +29,14 @@
26 29
27 \begin{document} 30 \begin{document}
28 31
29 -\begin{frame}[fragile] 32 +\begin{frame}[t,fragile]
30 - \IfFileExists{out.code}{\VerbatimInput[commandchars=\\\{\}]{out.code}}{} 33 + \vbox to 0pt{%
31 - \IfFileExists{out.tikz}{\input{out.tikz}}{} 34 + \IfFileExists{out.code}{\VerbatimInput[commandchars=\\\{\}]{out.code}}{}
35 + \vss
36 + }
37 + \vbox to \textheight{%
38 + \vss\IfFileExists{out.tikz}{\input{out.tikz}}{}%
39 + }
32 \end{frame} 40 \end{frame}
33 41
34 \end{document} 42 \end{document}
......