Showing
11 changed files
with
249 additions
and
32 deletions
1 | class ExecEnv (dict) : | 1 | class ExecEnv (dict) : |
2 | + _ValueClass = None | ||
2 | def __init__ (self, *l, **k) : | 3 | def __init__ (self, *l, **k) : |
3 | super().__init__(*l, **k) | 4 | super().__init__(*l, **k) |
4 | def __setitem__ (self, key, val) : | 5 | def __setitem__ (self, key, val) : |
5 | old = self.get(key, None) | 6 | old = self.get(key, None) |
6 | try : | 7 | try : |
7 | old.set(val) | 8 | old.set(val) |
8 | - except AttributeError : | 9 | + except : |
10 | + if not isinstance(val, self._ValueClass) : | ||
11 | + val = self._ValueClass(val) | ||
9 | super().__setitem__(key, val) | 12 | super().__setitem__(key, val) |
10 | def __getitem__ (self, key) : | 13 | def __getitem__ (self, key) : |
11 | old = self.get(key, None) | 14 | old = self.get(key, None) |
12 | try : | 15 | try : |
13 | return old.get() | 16 | return old.get() |
14 | - except AttributeError : | 17 | + except : |
15 | return super().__getitem__(key) | 18 | return super().__getitem__(key) |
16 | def exec (self, code) : | 19 | def exec (self, code) : |
17 | exec(code, self) | 20 | exec(code, self) |
... | @@ -25,13 +28,13 @@ class CAni (object) : | ... | @@ -25,13 +28,13 @@ class CAni (object) : |
25 | return self._env.get("IP", 1) | 28 | return self._env.get("IP", 1) |
26 | @IP.setter | 29 | @IP.setter |
27 | def IP (self, val) : | 30 | def IP (self, val) : |
28 | - self._env["IP"] = val | 31 | + dict.__setitem__(self._env, "IP", val) |
29 | @property | 32 | @property |
30 | def RET (self) : | 33 | def RET (self) : |
31 | return self._env.get("RET", None) | 34 | return self._env.get("RET", None) |
32 | @RET.setter | 35 | @RET.setter |
33 | def RET (self, val) : | 36 | def RET (self, val) : |
34 | - self._env["RET"] = val | 37 | + dict.__setitem__(self._env, "RET", val) |
35 | def exec (self, code) : | 38 | def exec (self, code) : |
36 | self._env.exec(code) | 39 | self._env.exec(code) |
37 | def eval (self, code) : | 40 | def eval (self, code) : | ... | ... |
1 | from itertools import chain | 1 | from itertools import chain |
2 | from inspect import cleandoc | 2 | from inspect import cleandoc |
3 | from collections import defaultdict | 3 | from collections import defaultdict |
4 | -from . import CAni | 4 | +from . import CAni, ExecEnv |
5 | 5 | ||
6 | class dopt (dict) : | 6 | class dopt (dict) : |
7 | def __str__ (self) : | 7 | def __str__ (self) : |
... | @@ -48,6 +48,7 @@ class TikZ (object) : | ... | @@ -48,6 +48,7 @@ class TikZ (object) : |
48 | "inner sep": "0pt", | 48 | "inner sep": "0pt", |
49 | "outer sep": "0pt"}, | 49 | "outer sep": "0pt"}, |
50 | "alloc": {}, | 50 | "alloc": {}, |
51 | + "index": {"scale": ".9"}, | ||
51 | "ticks": {"gray": None, | 52 | "ticks": {"gray": None, |
52 | "scale": ".7"}} | 53 | "scale": ".7"}} |
53 | def __init__ (self, options, **default) : | 54 | def __init__ (self, options, **default) : |
... | @@ -79,13 +80,13 @@ class CAniTikZ (CAni) : | ... | @@ -79,13 +80,13 @@ class CAniTikZ (CAni) : |
79 | self._nodeid[char] = num + 1 | 80 | self._nodeid[char] = num + 1 |
80 | self._o = TikZ(tikz, **self._defaults) | 81 | self._o = TikZ(tikz, **self._defaults) |
81 | def __matmul__ (self, key) : | 82 | def __matmul__ (self, key) : |
82 | - if key == 0 : | 83 | + if key == None : |
83 | return self | 84 | return self |
84 | else : | 85 | else : |
85 | raise ValueError("invalid index %r for %s object" | 86 | raise ValueError("invalid index %r for %s object" |
86 | % (key, self.__class__.__name__)) | 87 | % (key, self.__class__.__name__)) |
87 | def ptr (self) : | 88 | def ptr (self) : |
88 | - return Pointer(self@0) | 89 | + return Pointer(self) |
89 | def tex (self, **tikz) : | 90 | def tex (self, **tikz) : |
90 | opt = TikZ(tikz) | 91 | opt = TikZ(tikz) |
91 | return cleandoc(r"""\begin{{tikzpicture}}[{opt.tikzpicture}] | 92 | return cleandoc(r"""\begin{{tikzpicture}}[{opt.tikzpicture}] |
... | @@ -102,8 +103,12 @@ class Pointer (CAniTikZ) : | ... | @@ -102,8 +103,12 @@ class Pointer (CAniTikZ) : |
102 | if self._d is None : | 103 | if self._d is None : |
103 | return "" | 104 | return "" |
104 | else : | 105 | else : |
105 | - tgt = (self._d@0).nodeid | 106 | + tgt = (self._d@None).nodeid |
106 | return fr"\draw[{opt.pointer}] ({src}) -- ({tgt});" | 107 | return fr"\draw[{opt.pointer}] ({src}) -- ({tgt});" |
108 | + def __getitem__ (self, key) : | ||
109 | + return self._d[key] | ||
110 | + def __setitem__ (self, key, val) : | ||
111 | + self._d[key] = val | ||
107 | 112 | ||
108 | class Value (CAniTikZ) : | 113 | class Value (CAniTikZ) : |
109 | def __init__ (self, init=None, **tikz) : | 114 | def __init__ (self, init=None, **tikz) : |
... | @@ -173,6 +178,8 @@ class Value (CAniTikZ) : | ... | @@ -173,6 +178,8 @@ class Value (CAniTikZ) : |
173 | except : | 178 | except : |
174 | return f"\node at ({self.nodeid}) {{{value}\strut}};" | 179 | return f"\node at ({self.nodeid}) {{{value}\strut}};" |
175 | 180 | ||
181 | +ExecEnv._ValueClass = Value | ||
182 | + | ||
176 | class Aggregate (CAniTikZ) : | 183 | class Aggregate (CAniTikZ) : |
177 | def __init__ (self, init, **tikz) : | 184 | def __init__ (self, init, **tikz) : |
178 | super().__init__(tikz) | 185 | super().__init__(tikz) |
... | @@ -199,10 +206,8 @@ class Aggregate (CAniTikZ) : | ... | @@ -199,10 +206,8 @@ class Aggregate (CAniTikZ) : |
199 | def __matmul__ (self, key) : | 206 | def __matmul__ (self, key) : |
200 | if key in self._d : | 207 | if key in self._d : |
201 | return self._d[key] | 208 | return self._d[key] |
202 | - elif key == 0 : | 209 | + elif key == None : |
203 | return self._first | 210 | return self._first |
204 | - elif key == -1 : | ||
205 | - return self._last | ||
206 | else : | 211 | else : |
207 | raise ValueError("invalid index %r for %s object" | 212 | raise ValueError("invalid index %r for %s object" |
208 | % (key, self.__class__.__name__)) | 213 | % (key, self.__class__.__name__)) |
... | @@ -240,16 +245,11 @@ class Aggregate (CAniTikZ) : | ... | @@ -240,16 +245,11 @@ class Aggregate (CAniTikZ) : |
240 | def _nodes (self, opt) : | 245 | def _nodes (self, opt) : |
241 | grow = opt.aggregate["grow"] | 246 | grow = opt.aggregate["grow"] |
242 | anchor = opp(grow) | 247 | anchor = opp(grow) |
243 | - prev = None | ||
244 | for key, val in self._d.items() : | 248 | for key, val in self._d.items() : |
245 | - if prev is None : | 249 | + yield val._node(opt) |
246 | - yield val._node(opt) | 250 | + opt = (opt / "pos") + {"value": {"anchor": anchor}, |
247 | - opt = (opt / "pos") + {"value": {"anchor": anchor}, | 251 | + "pos": {"at": f"({val.nodeid}.{grow})"}} |
248 | - "pos": {"at": f"({val.nodeid}.{grow})"}} | 252 | + first, last = self._first.nodeid, self._last.nodeid |
249 | - else : | ||
250 | - yield val._node(opt) | ||
251 | - prev = val | ||
252 | - first, last = (self@0).nodeid, (self@-1).nodeid | ||
253 | yield fr"\node[{opt.group},fit=({first}) ({last})] ({self.nodeid}) {{}};" | 253 | yield fr"\node[{opt.group},fit=({first}) ({last})] ({self.nodeid}) {{}};" |
254 | def _ticks (self, opt) : | 254 | def _ticks (self, opt) : |
255 | side = opt.aggregate.get("ticks", None) | 255 | side = opt.aggregate.get("ticks", None) |
... | @@ -305,24 +305,71 @@ class Aggregate (CAniTikZ) : | ... | @@ -305,24 +305,71 @@ class Aggregate (CAniTikZ) : |
305 | yield "}" | 305 | yield "}" |
306 | 306 | ||
307 | class Array (Aggregate) : | 307 | class Array (Aggregate) : |
308 | - _defaults = {"aggregate": {"index": "west"}} | 308 | + _defaults = {"aggregate": {"index": "north"}} |
309 | def __init__ (self, init, index=[], **tikz) : | 309 | def __init__ (self, init, index=[], **tikz) : |
310 | super().__init__(init, **tikz) | 310 | super().__init__(init, **tikz) |
311 | self._o.aggregatescope = self._o.arrayscope | 311 | self._o.aggregatescope = self._o.arrayscope |
312 | - # register index | 312 | + self._i = {} |
313 | + for i in index : | ||
314 | + self.index(i) | ||
315 | + def index (self, name, init=None) : | ||
316 | + self._i[name] = self._env[name] = Value(init) | ||
313 | def _ticks (self, opt) : | 317 | def _ticks (self, opt) : |
314 | for t in super()._ticks(opt) : | 318 | for t in super()._ticks(opt) : |
315 | yield t | 319 | yield t |
316 | - # yield index | 320 | + side = opt.aggregate.get("index", None) |
321 | + if not side : | ||
322 | + return | ||
323 | + anchor = opp(side) | ||
324 | + anim = defaultdict(list) | ||
325 | + for name, value in self._i.items() : | ||
326 | + value.stop() | ||
327 | + for val, start, stop in value._h : | ||
328 | + for step in range(start, stop+1) : | ||
329 | + anim[step].append((name, val)) | ||
330 | + mina = defaultdict(set) | ||
331 | + for step, keys in anim.items() : | ||
332 | + mina[tuple(sorted(keys))].add(step) | ||
333 | + def minstep (item) : | ||
334 | + return tuple(sorted(item[1])) | ||
335 | + for info, steps in sorted(mina.items(), key=minstep) : | ||
336 | + xpos = defaultdict(list) | ||
337 | + for name, value in info : | ||
338 | + if value is not None : | ||
339 | + xpos[value].append(name) | ||
340 | + if not xpos : | ||
341 | + continue | ||
342 | + when = ",".join(str(s) for s in sorted(steps)) | ||
343 | + yield fr"\uncover<{when}>{{" | ||
344 | + for value, names in xpos.items() : | ||
345 | + label = self._index(names, opt) | ||
346 | + try : | ||
347 | + cell = (self@value).nodeid | ||
348 | + except ValueError : | ||
349 | + continue | ||
350 | + yield (fr" \node[{opt.index},anchor={anchor},at=({cell}.{side})]" | ||
351 | + fr" {{{label}}};") | ||
352 | + yield "}" | ||
353 | + def _index (self, names, opt) : | ||
354 | + label = ",".join(sorted(names)) | ||
355 | + return fr"{label}\strut" | ||
317 | 356 | ||
318 | class Struct (Aggregate) : | 357 | class Struct (Aggregate) : |
319 | _defaults = {"aggregate": {"grow": "south", | 358 | _defaults = {"aggregate": {"grow": "south", |
320 | "ticks": "west"}} | 359 | "ticks": "west"}} |
321 | def __init__ (self, init, **tikz) : | 360 | def __init__ (self, init, **tikz) : |
361 | + self.__dict__.update(_d={}, _o=None, _first=None, _last=None, nodeid=None) | ||
322 | super().__init__(init, **tikz) | 362 | super().__init__(init, **tikz) |
323 | self._o.aggregatescope = self._o.structscope | 363 | self._o.aggregatescope = self._o.structscope |
324 | def _tick (self, key, opt) : | 364 | def _tick (self, key, opt) : |
325 | return fr".{key}\strut" | 365 | return fr".{key}\strut" |
366 | + def __getattr__ (self, name) : | ||
367 | + return self[name] | ||
368 | + def __setattr__ (self, name, value) : | ||
369 | + if name in self._d : | ||
370 | + self[name] = value | ||
371 | + else : | ||
372 | + self.__dict__[name] = value | ||
326 | 373 | ||
327 | class Heap (CAniTikZ) : | 374 | class Heap (CAniTikZ) : |
328 | _defaults = {"group": {"opacity": 0, | 375 | _defaults = {"group": {"opacity": 0, |
... | @@ -360,7 +407,7 @@ class Heap (CAniTikZ) : | ... | @@ -360,7 +407,7 @@ class Heap (CAniTikZ) : |
360 | yield r" }" | 407 | yield r" }" |
361 | opt = opt + {"pos": {opt.heap["grow"]: | 408 | opt = opt + {"pos": {opt.heap["grow"]: |
362 | "{dist} of {prev}".format(dist=opt.heap["distance"], | 409 | "{dist} of {prev}".format(dist=opt.heap["distance"], |
363 | - prev=(data@0).nodeid)}} | 410 | + prev=(data@None).nodeid)}} |
364 | children = " ".join(f"({nid})" for nid in fit) | 411 | children = " ".join(f"({nid})" for nid in fit) |
365 | yield fr" \node[{opt.group},fit={children}] ({self.nodeid}) {{}};" | 412 | yield fr" \node[{opt.group},fit={children}] ({self.nodeid}) {{}};" |
366 | yield r"\end{scope}" | 413 | yield r"\end{scope}" | ... | ... |
... | @@ -130,7 +130,7 @@ class XDECL (_CODE) : | ... | @@ -130,7 +130,7 @@ class XDECL (_CODE) : |
130 | 130 | ||
131 | class DECL (_CODE) : | 131 | class DECL (_CODE) : |
132 | _fields = ["name"] | 132 | _fields = ["name"] |
133 | - _options = ["init"] | 133 | + _options = ["init", "animate"] |
134 | def __call__ (self) : | 134 | def __call__ (self) : |
135 | if self.init is not None : | 135 | if self.init is not None : |
136 | self.init() | 136 | self.init() |
... | @@ -147,7 +147,7 @@ class DECL (_CODE) : | ... | @@ -147,7 +147,7 @@ class DECL (_CODE) : |
147 | return src + " " + "".join(self._tex()) | 147 | return src + " " + "".join(self._tex()) |
148 | def _tex (self) : | 148 | def _tex (self) : |
149 | tail = r"\PY{{c+c1}}{{/* {value} */}}" | 149 | tail = r"\PY{{c+c1}}{{/* {value} */}}" |
150 | - for value, start, stop in self._cell.hist : | 150 | + for value, start, stop in self._env.get(self.name)._h : |
151 | if value is not None : | 151 | if value is not None : |
152 | yield (r"\onlyshow{{{start}-{stop}}}{{{value}}}" | 152 | yield (r"\onlyshow{{{start}-{stop}}}{{{value}}}" |
153 | r"").format(start=start or 1, | 153 | r"").format(start=start or 1, | ... | ... |
1 | -all: heap.pdf | 1 | +all: heap.pdf qs-partition.pdf |
2 | 2 | ||
3 | %.pdf: %.py tpl.tex | 3 | %.pdf: %.py tpl.tex |
4 | ln -sf ../codanim . | 4 | ln -sf ../codanim . |
5 | - python $< > out.tikz | 5 | + rm -f out.* |
6 | + python $< | ||
6 | latexmk -pdf tpl | 7 | latexmk -pdf tpl |
7 | cp tpl.pdf $@ | 8 | cp tpl.pdf $@ |
8 | latexmk -C tpl | 9 | latexmk -C tpl | ... | ... |
No preview for this file type
... | @@ -6,4 +6,5 @@ p = h.new(Struct({"data": 2, "next": p})) | ... | @@ -6,4 +6,5 @@ p = h.new(Struct({"data": 2, "next": p})) |
6 | p = h.new(Struct({"data": 3, "next": p})) | 6 | p = h.new(Struct({"data": 3, "next": p})) |
7 | h.new(Value(p, value={"yshift": "-1cm"})) | 7 | h.new(Value(p, value={"yshift": "-1cm"})) |
8 | 8 | ||
9 | -print(h.tex(tikzpicture={"scale": .7})) | 9 | +with open("out.tikz", "w") as out : |
10 | + out.write(h.tex(tikzpicture={"scale": .7})) | ... | ... |
examples/pygments.sty
0 → 100644
1 | +\ProvidesPackage{pygments} | ||
2 | + | ||
3 | +\usepackage{fancyvrb} | ||
4 | +\usepackage{color} | ||
5 | + | ||
6 | +\makeatletter | ||
7 | +\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% | ||
8 | + \let\PY@ul=\relax \let\PY@tc=\relax% | ||
9 | + \let\PY@bc=\relax \let\PY@ff=\relax} | ||
10 | +\def\PY@tok#1{\csname PY@tok@#1\endcsname} | ||
11 | +\def\PY@toks#1+{\ifx\relax#1\empty\else% | ||
12 | + \PY@tok{#1}\expandafter\PY@toks\fi} | ||
13 | +\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% | ||
14 | + \PY@it{\PY@bf{\PY@ff{#1}}}}}}} | ||
15 | +\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} | ||
16 | + | ||
17 | +\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} | ||
18 | +\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} | ||
19 | +\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} | ||
20 | +\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
21 | +\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
22 | +\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} | ||
23 | +\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
24 | +\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} | ||
25 | +\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
26 | +\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} | ||
27 | +\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} | ||
28 | +\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} | ||
29 | +\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} | ||
30 | +\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} | ||
31 | +\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} | ||
32 | +\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} | ||
33 | +\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} | ||
34 | +\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} | ||
35 | +\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
36 | +\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} | ||
37 | +\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
38 | +\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
39 | +\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} | ||
40 | +\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} | ||
41 | +\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} | ||
42 | +\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} | ||
43 | +\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
44 | +\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
45 | +\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} | ||
46 | +\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} | ||
47 | +\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} | ||
48 | +\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} | ||
49 | +\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} | ||
50 | +\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} | ||
51 | +\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} | ||
52 | +\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} | ||
53 | +\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} | ||
54 | +\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} | ||
55 | +\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} | ||
56 | +\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
57 | +\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
58 | +\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
59 | +\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
60 | +\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} | ||
61 | +\expandafter\def\csname PY@tok@fm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} | ||
62 | +\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} | ||
63 | +\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} | ||
64 | +\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} | ||
65 | +\expandafter\def\csname PY@tok@vm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} | ||
66 | +\expandafter\def\csname PY@tok@sa\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
67 | +\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
68 | +\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
69 | +\expandafter\def\csname PY@tok@dl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
70 | +\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
71 | +\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
72 | +\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} | ||
73 | +\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
74 | +\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
75 | +\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
76 | +\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
77 | +\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
78 | +\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | ||
79 | +\expandafter\def\csname PY@tok@ch\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} | ||
80 | +\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} | ||
81 | +\expandafter\def\csname PY@tok@cpf\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} | ||
82 | +\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} | ||
83 | +\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} | ||
84 | + | ||
85 | +\def\PYZbs{\char`\\} | ||
86 | +\def\PYZus{\char`\_} | ||
87 | +\def\PYZob{\char`\{} | ||
88 | +\def\PYZcb{\char`\}} | ||
89 | +\def\PYZca{\char`\^} | ||
90 | +\def\PYZam{\char`\&} | ||
91 | +\def\PYZlt{\char`\<} | ||
92 | +\def\PYZgt{\char`\>} | ||
93 | +\def\PYZsh{\char`\#} | ||
94 | +\def\PYZpc{\char`\%} | ||
95 | +\def\PYZdl{\char`\$} | ||
96 | +\def\PYZhy{\char`\-} | ||
97 | +\def\PYZsq{\char`\'} | ||
98 | +\def\PYZdq{\char`\"} | ||
99 | +\def\PYZti{\char`\~} | ||
100 | +% for compatibility with earlier versions | ||
101 | +\def\PYZat{@} | ||
102 | +\def\PYZlb{[} | ||
103 | +\def\PYZrb{]} | ||
104 | +\makeatother |
examples/qs-partition.pdf
0 → 100644
No preview for this file type
examples/qs-partition.py
0 → 100644
1 | +from codanim.data import Array, Struct, Value, Heap | ||
2 | +from codanim.flow import FUNC, BLOCK, ENV, WS, DECL, WHILE, EXPR, DO, STMT, RETURN, IF | ||
3 | + | ||
4 | +a = Array([3, 13, 5, 0, 7, 11, 4, 9, 14, 2, 10, 1, 12, 8, 6], | ||
5 | + index=["a", "b"]) | ||
6 | +t = Struct({"len": Value(15), "val": a.ptr()}) | ||
7 | + | ||
8 | +h = Heap() | ||
9 | +h.new(a) | ||
10 | +h.new(t) | ||
11 | + | ||
12 | +partition = FUNC(BLOCK(ENV("t", t), | ||
13 | + WS(" "), | ||
14 | + DECL("pivot", EXPR("t.val[(t.len-1)//2]", | ||
15 | + src="t.val[(t.len-1)/2]"), | ||
16 | + animate=True, | ||
17 | + src="int {name} = {init};"), | ||
18 | + WS("\n "), | ||
19 | + DECL("a", EXPR("-1"), | ||
20 | + animate=True, | ||
21 | + src="uint {name} = {init};"), | ||
22 | + WS("\n "), | ||
23 | + DECL("b", EXPR("t.len"), | ||
24 | + animate=True, | ||
25 | + src="uint {name} = {init};"), | ||
26 | + WS("\n "), | ||
27 | + WHILE(EXPR("1", src="1"), | ||
28 | + BLOCK(WS(" "), | ||
29 | + DO(STMT("a+=1", src="a++;"), | ||
30 | + EXPR("t.val[a] < pivot"), | ||
31 | + src="do {{ {body} }} while ({cond});"), | ||
32 | + WS("\n "), | ||
33 | + DO(STMT("b-=1", src="b--;"), | ||
34 | + EXPR("t.val[b] > pivot"), | ||
35 | + src="do {{ {body} }} while ({cond});"), | ||
36 | + WS("\n "), | ||
37 | + IF(EXPR("a >= b"), | ||
38 | + RETURN(EXPR("b"), | ||
39 | + src="return {value};"), | ||
40 | + src="if ({cond}) {{ {then} }}"), | ||
41 | + WS("\n "), | ||
42 | + STMT("_old = t.val[a], t.val[b]", | ||
43 | + "t.val[b], t.val[a] = _old", | ||
44 | + src="swap(t, a, b);"), | ||
45 | + WS("\n")), | ||
46 | + src="while ({cond}) {{\n{body} }}"), | ||
47 | + WS("\n")), | ||
48 | + src="uint partition (Tab t) {{\n{body}}}\n") | ||
49 | + | ||
50 | +partition.IP += 1 # do not highlight first step | ||
51 | +partition() # simulate execution | ||
52 | + | ||
53 | +# save code animation | ||
54 | +with open("out.code", "w") as out : | ||
55 | + out.write(partition.tex()) | ||
56 | + | ||
57 | +# save data animation | ||
58 | +with open("out.tikz", "w") as out : | ||
59 | + out.write(h.tex(tikzpicture={"scale": .6})) |
... | @@ -4,6 +4,7 @@ | ... | @@ -4,6 +4,7 @@ |
4 | 4 | ||
5 | \usepackage{fancyvrb} | 5 | \usepackage{fancyvrb} |
6 | \usepackage{adjustbox} | 6 | \usepackage{adjustbox} |
7 | +\usepackage{pygments} | ||
7 | 8 | ||
8 | \usepackage{tikz} | 9 | \usepackage{tikz} |
9 | \usetikzlibrary{arrows} | 10 | \usetikzlibrary{arrows} |
... | @@ -25,8 +26,9 @@ | ... | @@ -25,8 +26,9 @@ |
25 | 26 | ||
26 | \begin{document} | 27 | \begin{document} |
27 | 28 | ||
28 | -\begin{frame} | 29 | +\begin{frame}[fragile] |
29 | - \input{out.tikz} | 30 | + \IfFileExists{out.code}{\VerbatimInput[commandchars=\\\{\}]{out.code}}{} |
31 | + \IfFileExists{out.tikz}{\input{out.tikz}}{} | ||
30 | \end{frame} | 32 | \end{frame} |
31 | 33 | ||
32 | \end{document} | 34 | \end{document} | ... | ... |
-
Please register or login to post a comment