Franck Pommereau

new example, bug fixes

* add `VBox` and `HBox` to layout data blocks without the need to play
with Heaps (fix `examples/qs-main.py` accordingly)
with Heaps (fix examples accordingly)
......
......@@ -87,16 +87,19 @@ class CAniTikZ (CAni) :
% (key, self.__class__.__name__))
def ptr (self) :
return Pointer(self)
def tex (self, **tikz) :
def tex (self, head=None, tail=None, **tikz) :
opt = TikZ(tikz)
return cleandoc(r"""\begin{{tikzpicture}}[{opt.tikzpicture}]
{code}
{head}{code}{tail}
\end{{tikzpicture}}
""").format(opt=opt, code="\n ".join(self.tikz(**tikz).splitlines()))
""").format(opt=opt,
head=(head + "\n") if head else "",
tail=("\n" + tail) if tail else "",
code="\n ".join(self.tikz(**tikz).splitlines()))
class Pointer (CAniTikZ) :
def __init__ (self, data) :
self._d = data
self.__dict__.update(_d=data, nodeid=None)
def val (self) :
return self._d
def __tikz__ (self, src, opt) :
......@@ -109,6 +112,10 @@ class Pointer (CAniTikZ) :
return self._d[key]
def __setitem__ (self, key, val) :
self._d[key] = val
def __getattr__ (self, key) :
return getattr(self._d, key)
def __setattr__ (self, key, val) :
setattr(self._d, key, val)
class Value (CAniTikZ) :
def __init__ (self, init=None, **tikz) :
......
all: heap.pdf qs-partition.pdf qs-main.pdf
all: heap.pdf qs-partition.pdf qs-main.pdf stack-push.pdf
gif: qs-partition.gif qs-main.gif stack-push.gif
mp4: qs-partition.mp4 qs-main.mp4 stack-push.mp4
%.pdf: %.py tpl.tex
ln -sf ../codanim .
......@@ -8,5 +10,22 @@ all: heap.pdf qs-partition.pdf qs-main.pdf
pdflatex tpl
cp tpl.pdf $@
%.gif: %.pdf
rm -rf _gif
mkdir _gif
gs -dSAFER -DBATCH -dNOPAUSE -sDEVICE=png16m -r400 -sOutputFile=_gif/%03d.png $<
mogrify -resize 800x600 -format gif _gif/*.png
gifsicle -k64 -d80 -l0 _gif/*.gif -O2 -o $@
rm -rf _gif
%.mp4: %.pdf
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 $@
rm -rf _mp4
clean:
rm -f out.* $$(grep '^*' ../.gitignore)
rm -rf _gif
rm -rf _mp4
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
from codanim.data import Heap, Struct, Value
from codanim.flow import FUNC, BLOCK, ENV, WS, DECL, EXPR, STMT
heap = Heap()
stack = heap.new(Struct({"val": Value(1), "next": None}))
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),
ENV("new", heap.new),
ENV("Struct", Struct),
ENV("Value", Value),
WS("\n "),
DECL("top", EXPR("new(Struct({'val': None, 'next': None}))",
src="malloc(sizeof(StackCell))"),
src="Stack {name} = {init};"),
WS("\n "),
STMT("top.val = u", src="top->val = u;"),
WS("\n "),
STMT("top.next = s", src="top->next = *s;"),
WS("\n "),
STMT("s = top", src="*s = top;"),
WS("\n")),
src="void stackPush (Stack* s, uint u) {{{body}}}\n")
# run code and save its animation
push.IP += 1
push()
with open("out.code", "w") as out :
out.write(push.tex())
# save data and animation
with open("out.tikz", "w") as out :
out.write(v.tex(tail=r"""
\node[above] at (s.north) {\texttt{s}};
\node[above] at (t.north) {\texttt{top}};
"""))