Franck Pommereau

added Box for easy layout

......@@ -28,6 +28,7 @@ class TikZ (object) :
"arrayscope": {},
"structscope": {},
"heapscope": {},
"boxscope": {},
"valueread" : {"very thick": None,
"draw": "blue!50!black",
"fill": "blue!20"},
......@@ -44,6 +45,7 @@ class TikZ (object) :
"heap": {"grow": "left",
"distance": "15mm"},
"group": {"opacity": 0,
"draw": "yellow",
"very thick": None,
"inner sep": "0pt",
"outer sep": "0pt"},
......@@ -93,8 +95,8 @@ class CAniTikZ (CAni) :
{head}{code}{tail}
\end{{tikzpicture}}
""").format(opt=opt,
head=(head + "\n") if head else "",
tail=("\n" + tail) if tail else "",
head=(head.strip("\n") + "\n") if head else "",
tail=("\n" + tail.strip("\n")) if tail else "",
code="\n ".join(self.tikz(**tikz).splitlines()))
class Pointer (CAniTikZ) :
......@@ -379,8 +381,7 @@ class Struct (Aggregate) :
self.__dict__[name] = value
class Heap (CAniTikZ) :
_defaults = {"group": {"opacity": 0,
"inner sep": "5mm"}}
_defaults = {"group": {"inner sep": "5mm"}}
def __init__ (self, **tikz) :
super().__init__(tikz)
self._alloc = {}
......@@ -418,3 +419,51 @@ class Heap (CAniTikZ) :
children = " ".join(f"({nid})" for nid in fit)
yield fr" \node[{opt.group},fit={children}] ({self.nodeid}) {{}};"
yield r"\end{scope}"
_flip = {"right": "below",
"below": "right",
"left": "above",
"above": "left"}
class Box (CAniTikZ) :
def __init__ (self, *content, grow="right", distance="15mm", parent=None, **tikz) :
super().__init__(tikz)
self._grow = grow
self._dist = distance
self._parent = parent
self._data = tuple(self._init(content))
def _init (self, content) :
for obj in content :
if isinstance(obj, CAniTikZ) :
yield obj
elif isinstance(obj, list) :
yield self.__class__(*obj,
grow=(_flip.get(self._grow) if self._parent is None
else self._parent._grow),
distance=self._dist,
parent=self,
**self._o.dict())
else :
raise ValueError("invalid %s content: %r"
% (self.__class__.__name__, obj))
def tikz (self, **tikz) :
opt = TikZ(tikz) + self._o
classname = self.__class__.__name__
nodeid = self.nodeid
return (f"%% {classname} {nodeid} ({self._grow})\n"
+ "\n".join(self._tikz(opt))
+ f"\n%% /{classname} {nodeid}")
def _tikz (self, opt) :
fit = []
yield fr"\begin{{scope}}[{opt.boxscope}]"
for data in self._data :
fit.append(data.nodeid)
yield fr" %% box {self.nodeid} content"
for line in data.tikz(**opt.dict()).splitlines() :
yield " " + line
opt = opt + {"pos": {self._grow:
"{dist} of {prev}".format(dist=self._dist,
prev=(data@None).nodeid)}}
children = " ".join(f"({nid})" for nid in fit)
yield fr" \node[{opt.group},fit={children}] ({self.nodeid}) {{}};"
yield r"\end{scope}"
......
......@@ -6,11 +6,10 @@ mp4: qs-partition.mp4 qs-main.mp4 stack-push.mp4
rm -f out.*
PYTHONPATH=..:$$PYTHONPATH python3 $<
pdflatex tpl
pdflatex tpl
mv -f tpl.pdf $@
%.gif: %.pdf
rm -rf _gif
rm -rf _gif $@
mkdir _gif
gs -dSAFER -DBATCH -dNOPAUSE -sDEVICE=png16m -r400 -sOutputFile=_gif/%03d.png $<
mogrify -resize 800x600 -format gif _gif/*.png
......@@ -18,7 +17,7 @@ mp4: qs-partition.mp4 qs-main.mp4 stack-push.mp4
rm -rf _gif
%.mp4: %.pdf
rm -rf _mp4
rm -rf _mp4 $@
mkdir _mp4
gs -dSAFER -DBATCH -dNOPAUSE -sDEVICE=png16m -r400 -sOutputFile=_mp4/%03d.png $<
ffmpeg -r 1 -s 1024x768 -i _mp4/%03d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p $@
......
No preview for this file type

5.08 MB | W: | H:

5 MB | W: | H:

  • 2-up
  • Swipe
  • Onion skin
No preview for this file type
No preview for this file type
from codanim.data import Array, Heap
from codanim.data import Array, Box
from codanim.flow import FUNC, BLOCK, ENV, WS, XDECL, DECL, PY, EXPR, STMT, WHILE, IF
##
......@@ -87,19 +87,6 @@ sort = FUNC(BLOCK(ENV("t", array),
src="while ({cond}) {{\n{body} }}\n")),
src="void sort (Tab t) {{{body}}}")
# let's play with Heap to layout the various data blocks
# /!\ we must do this before simulating the code
ha = Heap()
ha.new(array)
hs = Heap(heap={"grow": "right",
"distance": "0pt"})
hs.new(first)
hs.new(last)
h = Heap(heap={"grow": "right",
"distance": "5mm"})
h.new(ha)
h.new(hs)
# run code and save its animation
sort.IP += 1
sort()
......@@ -107,5 +94,7 @@ with open("out.code", "w") as out :
out.write(sort.tex())
# save data animation
b = Box(Box(array), Box(first, last, distance="0pt"))
with open("out.tikz", "w") as out :
out.write(h.tex(tikzpicture={"scale": .55}))
out.write(b.tex(tikzpicture={"scale": .55},
tail=r"\node at (-2,1) {};"))
......

841 KB | W: | H:

807 KB | W: | H:

  • 2-up
  • Swipe
  • Onion skin
No preview for this file type
No preview for this file type
from codanim.data import Array, Struct, Value, Heap
from codanim.data import Array, Struct, Value, Box
from codanim.flow import FUNC, BLOCK, ENV, WS, DECL, WHILE, EXPR, DO, STMT, RETURN, IF
a = Array([3, 13, 5, 0, 7, 11, 4, 9, 14, 2, 10, 1, 12, 8, 6],
index=["a", "b"])
t = Struct({"len": Value(15), "val": a.ptr()})
h = Heap()
h.new(a)
h.new(t)
partition = FUNC(BLOCK(ENV("t", t),
WS(" "),
DECL("pivot", EXPR("t.val[(t.len-1)//2]",
......@@ -55,5 +51,7 @@ with open("out.code", "w") as out :
out.write(partition.tex())
# save data animation
b = Box(a, t, grow="left")
with open("out.tikz", "w") as out :
out.write(h.tex(tikzpicture={"scale": .6}))
out.write(b.tex(tikzpicture={"scale": .6},
tail=r"\node at (0,-2) {};"))
......

87.2 KB | W: | H:

83.3 KB | W: | H:

  • 2-up
  • Swipe
  • Onion skin
No preview for this file type
No preview for this file type
from codanim.data import Heap, Struct, Value
from codanim.data import Heap, Struct, Value, Box
from codanim.flow import FUNC, BLOCK, ENV, WS, DECL, EXPR, STMT
heap = Heap()
......@@ -8,15 +8,6 @@ stack = heap.new(Struct({"val": Value(2), "next": stack}))
s = Value(stack, nodeid="s")
t = Value(None, nodeid="t", value={"xshift": "-25mm"})
v = Heap(heap={"grow": "above",
"separation": "0pt"})
h = Heap(heap={"grow": "right"},
)
h.new(t)
h.new(s)
v.new(heap)
v.new(h)
push = FUNC(BLOCK(ENV("s", s),
ENV("u", Value(3)),
ENV("top", t),
......@@ -43,8 +34,9 @@ with open("out.code", "w") as out :
out.write(push.tex())
# save data and animation
b = Box(heap, Box(t, s, grow="right"), grow="above")
with open("out.tikz", "w") as out :
out.write(v.tex(tail=r"""
out.write(b.tex(tail=r"""
\node[above] at (s.north) {\texttt{s}};
\node[above] at (t.north) {\texttt{top}};
"""))
......