-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtikzlibraryfunprog.code.tex
More file actions
1409 lines (1315 loc) · 47.9 KB
/
tikzlibraryfunprog.code.tex
File metadata and controls
1409 lines (1315 loc) · 47.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\usetikzlibrary {matrix}
\usetikzlibrary {positioning}
\usetikzlibrary {fit}
\usetikzlibrary {shapes.multipart}
\makeatletter
\pgfkeys{
funprog/.is family,
funprog/.search also=/tikz,
}
\let\funprog@datastructtype\empty
\let\funprog@datastructid\empty
\let\funprog@datastruct@element\empty
\def\funprog@datastruct@elementsize{1cm}
\def\funprog@datastruct@wrapperpadding{5mm}
\def\funprog@datastruct@wrapperlevel{0}
\let\funprog@placeholdersize\empty
\newcounter{placeholder}
\tikzset{
element/.forward to={funprog, data struct element},
element size/.forward to={funprog, data struct element size},
wrapper/.forward to={funprog, data struct wrapper level},
wrapper padding/.forward to={funprog, data struct wrapper padding},
index/.forward to={funprog, \funprog@datastructtype/index},
key value/.forward to={funprog, \funprog@datastructtype/key value},
separator/.forward to={funprog, \funprog@datastructtype/separator},
placeholder/.forward to={funprog, \funprog@datastructtype/placeholder},
elements before/.forward to={funprog, \funprog@datastructtype/elements before},
elements between/.forward to={funprog, \funprog@datastructtype/elements between},
elements after/.forward to={funprog, \funprog@datastructtype/elements after},
every elements before/.forward to={funprog, \funprog@datastructtype/every elements before},
every elements between/.forward to={funprog, \funprog@datastructtype/every elements between},
every elements after/.forward to={funprog, \funprog@datastructtype/every elements after},
}
\pgfkeys{
funprog,
data struct type/.store in=\funprog@datastructtype,
data struct id/.store in=\funprog@datastructid,
data struct element/.store in=\funprog@datastruct@element,
data struct element size/.store in=\funprog@datastruct@elementsize,
data struct wrapper level/.store in=\funprog@datastruct@wrapperlevel,
data struct wrapper level/.default=1,
data struct wrapper padding/.store in=\funprog@datastruct@wrapperpadding,
data struct alias/.style={
append after command={
\pgfextra
\node[name=#1, fit=(\tikzlastnode), inner sep=0pt] {};
\endpgfextra
},
},
reset placeholder counter/.code={
\setcounter{placeholder}{0}
},
next placeholder counter/.code={
\stepcounter{placeholder}
},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% tuple
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\let\funprog@tuple@delimiter@left\empty
\let\funprog@tuple@delimiter@right\empty
\tikzset{
every tuple/.style={},
tuple/.forward to={funprog, tuple},
tuple delimiter/.forward to={funprog, tuple/delimiter},
}
\pgfkeys{
funprog,
tuple/.style={
data struct type=tuple,
data struct id=#1,
data struct element=#1,
data struct alias=tuple #1,
left delimiter=\funprog@tuple@delimiter@left,
right delimiter=\funprog@tuple@delimiter@right,
nodes={
anchor=center,
inner sep=0pt,
outer sep=0pt,
},
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
outer sep=-.5\pgflinewidth,
inner sep=0pt,
every tuple,
reset placeholder counter,
},
tuple/separator/.style={
node contents={\,,\,\,},
},
tuple/placeholder/.style={
next placeholder counter,
alias={\funprog@datastructid placeholder\the\value{placeholder}},
node contents={},
minimum width=#1 * \funprog@datastruct@elementsize + 2 * \funprog@datastruct@wrapperlevel * \funprog@datastruct@wrapperpadding,
minimum height=\funprog@datastruct@elementsize + 2 * \funprog@datastruct@wrapperpadding,
},
tuple/placeholder/.default=1,
tuple/delimiter/left/.store in=\funprog@tuple@delimiter@left,
tuple/delimiter/right/.store in=\funprog@tuple@delimiter@right,
tuple/delimiter/.is choice,
tuple/delimiter/parenthesis/.style={
tuple/delimiter/left=(,
tuple/delimiter/right=),
},
tuple/delimiter/braces/.style={
tuple/delimiter/left=\{,
tuple/delimiter/right=\},
},
tuple/delimiter/brackets/.style={
tuple/delimiter/left=[,
tuple/delimiter/right=],
},
tuple/delimiter=parenthesis,
tuple/index/.style={
alias=\funprog@datastructid#1,
node contents={$\funprog@datastruct@element_{#1}$},
minimum width=\funprog@datastruct@elementsize,
},
tuple/elements before/.style={
node contents={\ldots},
},
tuple/elements between/.style={
node contents={\ldots},
},
tuple/elements after/.style={
node contents={\ldots},
},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% map
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{
every map/.style={},
map/.forward to={funprog, map},
}
\pgfkeys{
funprog,
map/.style={
data struct type=map,
data struct id=#1,
data struct element=#1,
data struct alias=map #1,
nodes={
draw,
anchor=center,
inner sep=0pt,
outer sep=.5\pgflinewidth,
minimum size=\funprog@datastruct@elementsize,
},
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
outer sep=-.5\pgflinewidth,
inner sep=0pt,
every list,
},
map/index/.style={
alias=\funprog@datastructid#1,
%node contents={$\funprog@datastruct@element_{#1}$},
key value={$k_{#1}$}{$v_{#1}$}
},
map/key value/.style 2 args={
node contents={},
path picture={
\coordinate (_funprog_n) at ($ (path picture bounding box.north) + (0, -.5\pgflinewidth) $);
\coordinate (_funprog_s) at ($ (path picture bounding box.south) + (0, .5\pgflinewidth) $);
\coordinate (_funprog_k) at ($ (_funprog_n)!.25!(_funprog_s) $);
\coordinate (_funprog_v) at ($ (_funprog_n)!.75!(_funprog_s) $);
\node (last key) [draw=none, fill=none, minimum size=0, anchor=base, yshift={height("x") / -2}] at (_funprog_k) {#1};
\node (last value) [draw=none, fill=none, minimum size=0, anchor=base, yshift={height("x") / -2}] at (_funprog_v) {#2};
\draw [line width=\pgflinewidth]
(path picture bounding box.west) -- (path picture bounding box.east);
},
%append after command={
%\pgfextra
%\coordinate (_funprog_x) ($ (\tikzlastnode.west)!.5!(\tikzlastnode.east) $);
%\draw [line width=.75\pgflinewidth]
%($(\tikzlastnode.west) + (\pgflinewidth, 0) $) --
%($(_funprog_x) + (-1mm, 0)$) --
%($(_funprog_x) + (0, -1mm)$) --
%($(_funprog_x) + (1mm, 0)$) --
%($ (\tikzlastnode.east) + (-\pgflinewidth, 0) $);
%\coordinate (_funprog_n) at ($ (\tikzlastnode.north) + (0, -.5\pgflinewidth) $);
%\coordinate (_funprog_s) at ($ (\tikzlastnode.south) + (0, .5\pgflinewidth) $);
%\coordinate (_funprog_k) at ($ (_funprog_n)!.25!(_funprog_s) $);
%\coordinate (_funprog_v) at ($ (_funprog_n)!.75!(_funprog_s) $);
%%\draw (n) circle [radius=1pt];
%%\draw (s) circle [radius=1pt];
%%\draw (k) circle [radius=1pt];
%%\draw (v) circle [radius=1pt];
%%\draw (\tikzlastnode.center) circle [radius=1pt];
%%\coordinate (a) at ($ (\tikzlastnode.north)!.25!(\tikzlastnode.south) $);
%%\draw [very thin, red] (\tikzlastnode.west |- k) -- (\tikzlastnode.east |- k);
%%\draw [very thin, red] (\tikzlastnode.west |- v) -- (\tikzlastnode.east |- v);
%%\node (k1) [anchor=base, yshift={height("x") / -2}] at (k) {x};
%\node (last key) [draw=none, minimum size=0, anchor=base, yshift={height("x") / -2}] at (_funprog_k) {#1};
%\node (last value) [draw=none, minimum size=0, anchor=base, yshift={height("x") / -2}] at (_funprog_v) {#2};
%\endpgfextra
%}
},
map/every elements before/.style={},
map/elements before/.default=.5,
map/elements before/.style={
draw=none,
fill=none,
every elements before,
node contents={},
outer sep=0pt,
minimum width=#1 * \funprog@datastruct@elementsize,
minimum height=\funprog@datastruct@elementsize,
path picture={
\draw [dashed]
(path picture bounding box.north west) -- (path picture bounding box.north east)
(path picture bounding box.south east) -- (path picture bounding box.south west);
},
%append after command={
%\pgfextra
%\draw [dashed]
%(\tikzlastnode.north west) -- (\tikzlastnode.north east)
%(\tikzlastnode.south west) -- (\tikzlastnode.south east);
%\endpgfextra
%},
},
map/every elements between/.style={},
map/elements between/.default=.5,
map/elements between/.style={
draw=none,
fill=none,
every elements between,
node contents={},
outer sep=0pt,
minimum width=#1 * \funprog@datastruct@elementsize,
minimum height=\funprog@datastruct@elementsize,
path picture={
\draw [dashed]
(path picture bounding box.north west) -- (path picture bounding box.north east)
(path picture bounding box.south east) -- (path picture bounding box.south west);
},
%append after command={
%\pgfextra
%\draw [dashed]
%(\tikzlastnode.north west) -- (\tikzlastnode.north east)
%(\tikzlastnode.south west) -- (\tikzlastnode.south east);
%\endpgfextra
%},
},
map/every elements after/.style={},
map/elements after/.default=.5,
map/elements after/.style={
draw=none,
fill=none,
every elements after,
node contents={},
outer sep=0pt,
minimum width=#1 * \funprog@datastruct@elementsize,
minimum height=\funprog@datastruct@elementsize,
path picture={
\draw [dashed]
(path picture bounding box.north west) -- (path picture bounding box.north east)
(path picture bounding box.south east) -- (path picture bounding box.south west);
},
%append after command={
%\pgfextra
%\draw [dashed]
%(\tikzlastnode.north west) -- (\tikzlastnode.north east)
%(\tikzlastnode.south west) -- (\tikzlastnode.south east);
%\endpgfextra
%},
},
}
% empty collection ------------------------------------------------------------
\tikzset{
every empty collection/.style={},
empty collection/.forward to={funprog, empty collection},
}
\pgfkeys{
funprog,
empty collection/.style={
every empty collection,
%data struct id=#1,
%data struct element=#1,
%data struct alias=list #1,
alias=empty collection #1,
draw,
densely dashed,
node contents={},
inner sep=0pt,
outer sep=.5\pgflinewidth,
minimum height=\funprog@datastruct@elementsize,
minimum width=.3 * \funprog@datastruct@elementsize,
},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% list
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{
every list/.style={},
list/.forward to={funprog, list},
}
\pgfkeys{
funprog,
list/.style={
data struct type=list,
data struct id=#1,
data struct element=#1,
data struct alias=list #1,
nodes={
draw,
anchor=center,
inner sep=0pt,
outer sep=0pt,
minimum size=\funprog@datastruct@elementsize + 2 * \funprog@datastruct@wrapperlevel * \funprog@datastruct@wrapperpadding,
},
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
outer sep=-.5\pgflinewidth,
inner sep=0pt,
every list,
reset placeholder counter,
},
list/.default=a,
list/placeholder/.style={
next placeholder counter,
alias={\funprog@datastructid placeholder\the\value{placeholder}},
node contents={},
minimum width=#1 * \funprog@datastruct@elementsize + 2 * \funprog@datastruct@wrapperlevel * \funprog@datastruct@wrapperpadding,
},
list/placeholder/.default=1,
list/index/.style={
alias=\funprog@datastructid#1,
node contents={$\funprog@datastruct@element_{#1}$},
},
list/every elements before/.style={},
list/elements before/.default=.5,
list/elements before/.style={
draw=none,
fill=none,
every elements before,
node contents={},
outer sep=0pt,
minimum width=#1 * \funprog@datastruct@elementsize,
path picture={
\draw [dashed]
(path picture bounding box.north west) -- (path picture bounding box.north east)
(path picture bounding box.south east) -- (path picture bounding box.south west);
},
%append after command={
%\pgfextra
%\draw [dashed]
%(\tikzlastnode.north west) -- (\tikzlastnode.north east)
%(\tikzlastnode.south west) -- (\tikzlastnode.south east);
%\endpgfextra
%},
},
list/every elements between/.style={},
list/elements between/.default=.5,
list/elements between/.style={
draw=none,
fill=none,
every elements between,
outer sep=0pt,
node contents={},
minimum width=#1 * \funprog@datastruct@elementsize,
path picture={
\draw [dashed]
(path picture bounding box.north west) -- (path picture bounding box.north east)
(path picture bounding box.south east) -- (path picture bounding box.south west);
},
%append after command={
%\pgfextra{
%\draw [draw=\pgfkeysvalueof{/pgf/color}, dashed]
%(\tikzlastnode.north west) -- (\tikzlastnode.north east)
%(\tikzlastnode.south west) -- (\tikzlastnode.south east);
%}
%},
},
list/every elements after/.style={},
list/elements after/.default=.5,
list/elements after/.style={
draw=none,
fill=none,
every elements after,
node contents={},
outer sep=0pt,
minimum width=#1 * \funprog@datastruct@elementsize,
path picture={
\draw [dashed]
(path picture bounding box.north west) -- (path picture bounding box.north east)
(path picture bounding box.south east) -- (path picture bounding box.south west);
},
%append after command={
%\pgfextra
%\draw [dashed]
%(\tikzlastnode.north west) -- (\tikzlastnode.north east)
%(\tikzlastnode.south west) -- (\tikzlastnode.south east);
%\endpgfextra
%},
},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% interval
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{
every interval/.style={},
interval/.forward to={funprog, interval},
interval amplitude/.forward to={funprog, interval/amplitude},
interval segments/.forward to={funprog, interval/segments},
}
\pgfkeys{
funprog,
interval/.style={
decorate,
decoration={
show path construction,
lineto code={
\path
let
\p1=(\tikzinputsegmentfirst),
\p2=(\tikzinputsegmentlast),
\n1={abs(\x2 - \x1)},
\n2={abs(\y2 - \y1) + \funprog@interval@amplitude}
in
node at ($ (\p1) + (0, \n2) $) [name=interval #1, anchor=north west, minimum width=\n1, minimum height=2 * \n2, inner sep=0, outer sep=0] {};
\foreach [remember=\pos as \lastpos (initially 0), count=\i] \pos/\style in \funprog@interval@segments{
\draw
[/tikz/every interval, /funprog/interval/style=\style]
let
\p1=(\tikzinputsegmentfirst),
\p2=(\tikzinputsegmentlast),
\n1={veclen(\x2 - \x1, \y2 - \y1)},
\p{unit}=({(\x2 - \x1) / \n1}, {(\y2 - \y1) / \n1}),
\n2={\pgf@x * sign(\x2 - \x1)},
\n3={\pgf@y * sign(\y2 - \y1)},
\p{start}=(\x1 + \lastpos * \n1 * \x{unit}, \y1 + \lastpos * \n1 * \y{unit}),
\p{end}=(\x1 + \pos * \n1 * \x{unit}, \y1 + \pos * \n1 * \y{unit}),
\n4={int(\i + 1)}
in
(\p{start})
coordinate (interval #1 mark \i)
-- +(-\y{unit} * \funprog@interval@amplitude, \x{unit} * \funprog@interval@amplitude)
coordinate (interval #1 mark top \i)
-- +(0, 0)
-- +(\y{unit} * \funprog@interval@amplitude, -\x{unit} * \funprog@interval@amplitude)
coordinate (interval #1 mark bottom \i)
-- +(0, 0)
-- (\p{end})
coordinate (interval #1 mark \n4)
-- +(-\y{unit} * \funprog@interval@amplitude, \x{unit} * \funprog@interval@amplitude)
coordinate (interval #1 mark top \n4)
-- +(0, 0)
-- +(\y{unit} * \funprog@interval@amplitude, -\x{unit} * \funprog@interval@amplitude)
coordinate (interval #1 mark bottom \n4)
-- +(0, 0)
;
}
}
}
},
interval/amplitude/.store in=\funprog@interval@amplitude,
interval/amplitude=5mm,
interval/segments/.store in=\funprog@interval@segments,
interval/segments={1/solid},
interval/style/.is choice,
interval/style/solid/.style={solid},
interval/style/dashed/.style={dashed},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iteration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{
iteration position/.forward to={funprog, iteration/position},
iteration direction/.forward to={funprog, iteration/direction},
enter iteration start angle/.forward to={funprog, iteration/enter start angle},
enter iteration delta angle/.forward to={funprog, iteration/enter delta angle},
exit iteration start angle/.forward to={funprog, iteration/exit start angle},
exit iteration delta angle/.forward to={funprog, iteration/exit delta angle},
every enter iteration/.style={},
enter iteration/.forward to={
funprog, iteration/enter/\pgfkeysvalueof{/funprog/iteration/position}/\pgfkeysvalueof{/funprog/iteration/direction}=#1
},
every iteration/.style={},
iteration/.forward to={
funprog, iteration/direct/\pgfkeysvalueof{/funprog/iteration/position}/\pgfkeysvalueof{/funprog/iteration/direction}=#1
},
every exit iteration/.style={},
exit iteration/.forward to={
funprog, iteration/exit/\pgfkeysvalueof{/funprog/iteration/position}/\pgfkeysvalueof{/funprog/iteration/direction}=#1
},
}
\pgfkeys{
funprog,
iteration/position/.initial=above,
iteration/direction/.initial=from left to right,
iteration/enter start angle/.initial=30,
iteration/enter delta angle/.initial=90,
iteration/exit start angle/.initial=150,
iteration/exit delta angle/.initial=90,
iteration/enter/above/from left to right/.style={
decorate,
decoration={
show path construction,
moveto code={
\draw [
start angle=\pgfkeysvalueof{/funprog/iteration/enter start angle},
delta angle=\pgfkeysvalueof{/funprog/iteration/enter delta angle},
radius=1cm,
every enter iteration,
#1
] (\tikzinputsegmentfirst) arc coordinate (last iteration);
},
}
},
iteration/enter/above/from right to left/.style={
decorate,
decoration={
show path construction,
moveto code={
\draw [
start angle=180 - \pgfkeysvalueof{/funprog/iteration/enter start angle},
delta angle=-\pgfkeysvalueof{/funprog/iteration/enter delta angle},
radius=1cm,
every enter iteration,
#1
] (\tikzinputsegmentfirst) arc coordinate (last iteration);
},
}
},
iteration/direct/above/from left to right/.style={
decorate,
decoration={
show path construction,
lineto code={
\draw [bend left=45, every iteration, #1] (\tikzinputsegmentfirst) to coordinate (last iteration) (\tikzinputsegmentlast);
},
}
},
iteration/direct/above/from right to left/.style={
decorate,
decoration={
show path construction,
lineto code={
\draw [bend right=45, every iteration, #1] (\tikzinputsegmentfirst) to (\tikzinputsegmentlast);
},
}
},
iteration/exit/above/from left to right/.style={
decorate,
decoration={
show path construction,
moveto code={
\draw [
start angle=\pgfkeysvalueof{/funprog/iteration/exit start angle},
delta angle=-\pgfkeysvalueof{/funprog/iteration/exit delta angle},
radius=1cm,
every exit iteration,
#1
] (\tikzinputsegmentfirst) arc coordinate (last iteration);
},
}
},
iteration/exit/above/from right to left/.style={
decorate,
decoration={
show path construction,
moveto code={
\draw [
start angle=180 - \pgfkeysvalueof{/funprog/iteration/exit start angle},
delta angle=\pgfkeysvalueof{/funprog/iteration/exit delta angle},
radius=1cm,
every exit iteration,
#1
] (\tikzinputsegmentfirst) arc coordinate (last iteration);
},
}
},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% measure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\funprog@measure@position@above{1}
\def\funprog@measure@position@below{-1}
\let\funprog@measure@position\funprog@measure@position@above
\tikzset{
measure/.forward to={funprog, measure},
measure amplitude/.forward to={funprog, measure amplitude},
measure overflow/.forward to={funprog, measure overflow},
measure position/.forward to={funprog, measure position},
every measure arrow/.style={
-{Triangle[angle=30:1mm 2mm]},
},
}
\pgfkeys{
funprog,
measure amplitude/.initial=5mm,
measure overflow/.initial=1mm,
measure position/.is choice,
measure position/above/.code={
\let\funprog@measure@position\funprog@measure@position@above
},
measure position/below/.code={
\let\funprog@measure@position\funprog@measure@position@below
},
measure/.default={},
measure/.style={
decorate,
decoration={
show path construction,
lineto code={
\path let
\p1=(\tikzinputsegmentfirst),
\p2=(\tikzinputsegmentlast),
\p3=(\x2 - \x1, \y2 - \y1),
\n1={veclen(\x3, \y3)},
\p4=(-\y3 / \n1, \x3 / \n1),
\n2={\funprog@measure@position * \pgfkeysvalueof{/funprog/measure amplitude}},
\n3={\pgfkeysvalueof{/funprog/measure overflow} * \funprog@measure@position * sign(\pgfkeysvalueof{/funprog/measure amplitude})}
in
coordinate (last measure point a) at ($ (\p1) + (\x4 * \n2, \y4 * \n2) $)
coordinate (last measure point b) at ($ (\p2) + (\x4 * \n2, \y4 * \n2) $)
coordinate (last measure point c) at ($ (last measure point a) + (\x4 * \n3, \y4 * \n3) $)
coordinate (last measure point d) at ($ (last measure point b) + (\x4 * \n3, \y4 * \n3) $)
coordinate (last measure label) at ($ (last measure point a)!.5!(last measure point b) $)
;
\draw (\tikzinputsegmentfirst) -- (last measure point c);
\draw (\tikzinputsegmentlast) -- (last measure point d);
\path let
\p1=(\tikzinputsegmentfirst),
\p2=(\tikzinputsegmentlast),
\p3=(\x2 - \x1, \y2 - \y1),
\n1={atan2(\y3, \x3)}
in
node (last measure label) at (last measure label) [rotate=\n1] {#1};
\draw [every measure arrow] (last measure label) -- (last measure point a);
\draw [every measure arrow] (last measure label) -- (last measure point b);
}
}
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% brace
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{
every brace/.style={},
brace/.forward to={funprog, brace},
brace amplitude/.forward to={funprog, brace amplitude},
brace raise/.forward to={funprog, brace raise},
}
\pgfkeys{
funprog,
brace amplitude/.initial=1mm,
brace raise/.initial=0,
brace/.style={
decorate,
decoration={
show path construction,
lineto code={
\draw
[every brace, decorate, decoration={brace, amplitude=\pgfkeysvalueof{/funprog/brace amplitude}, raise=\pgfkeysvalueof{/funprog/brace raise}}]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
\draw let
\p1=(\tikzinputsegmentfirst),
\p2=(\tikzinputsegmentlast),
\p3=($ (\p2) - (\p1) $),
\n1={veclen(\x3, \y3)},
\p4=(-\y3 / \n1, \x3 / \n1),
\n2={\pgfkeysvalueof{/funprog/brace amplitude} + \pgfkeysvalueof{/funprog/brace raise}},
\p5=(\x4 * \n2, \y4 * \n2)
in
coordinate (last brace) at ($ ($ (\p1)!.5!(\p2) $) + (\x5, \y5) $);
}
}
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\pgfutil@useanchor#1#2{\csname pgf@anchor@#1@#2\endcsname}
\def\funprog@function@north{1}
\def\funprog@function@south{2}
\def\funprog@function@east{3}
\def\funprog@function@west{4}
\let\funprog@function@in\funprog@function@north
\let\funprog@function@out\funprog@function@south
\def\funprog@function@arity{1}
\tikzset{
every function/.style={},
function/.forward to={funprog, function=#1},
function arity/.forward to={funprog, function/arity=#1},
function in/.forward to={funprog, function/in=#1},
function out/.forward to={funprog, function/out=#1},
function port width/.forward to={funprog, function/port width},
function port height/.forward to={funprog, function/port height},
function port funnel width/.forward to={funprog, function/port funnel width},
function port separation/.forward to={funprog, function/port separation},
}
\pgfkeys{
funprog,
function/.style={
alias=function #1,
draw,
shape=function,
every function,
node contents=#1,
},
%
function/.cd,
port width/.initial=2.5mm,
port height/.initial=1mm,
port funnel width/.initial=1.75mm,
port separation/.initial=2.5mm,
north ports/.initial=1,
south ports/.initial=1,
east ports/.initial=0,
west ports/.initial=0,
}
\def\funprog@function@update{
\pgfkeys{
funprog,
function/.cd,
north ports=0,
south ports=0,
east ports=0,
west ports=0,
}
%
\ifnum\funprog@function@in=\funprog@function@north
\pgfkeysalso{/funprog/function/north ports=\funprog@function@arity}
\fi
\ifnum\funprog@function@in=\funprog@function@south
\pgfkeysalso{/funprog/function/south ports=\funprog@function@arity}
\fi
\ifnum\funprog@function@in=\funprog@function@west
\pgfkeysalso{/funprog/function/west ports=\funprog@function@arity}
\fi
\ifnum\funprog@function@in=\funprog@function@east
\pgfkeysalso{/funprog/function/east ports=\funprog@function@arity}
\fi
%
\ifnum\funprog@function@out=\funprog@function@north
\pgfkeysalso{/funprog/function/north ports=1}
\fi
\ifnum\funprog@function@out=\funprog@function@south
\pgfkeysalso{/funprog/function/south ports=1}
\fi
\ifnum\funprog@function@out=\funprog@function@west
\pgfkeysalso{/funprog/function/west ports=1}
\fi
\ifnum\funprog@function@out=\funprog@function@east
\pgfkeysalso{/funprog/function/east ports=1}
\fi
}
\pgfkeys{
funprog,
function/.cd,
in/.is choice,
in/north/.code={
\let\funprog@function@in\funprog@function@north
\funprog@function@update
},
in/south/.code={
\let\funprog@function@in\funprog@function@south
\funprog@function@update
},
in/east/.code={
\let\funprog@function@in\funprog@function@east
\funprog@function@update
},
in/west/.code={
\let\funprog@function@in\funprog@function@west
\funprog@function@update
},
%
out/.is choice,
out/north/.code={
\let\funprog@function@out\funprog@function@north
\funprog@function@update
},
out/south/.code={
\let\funprog@function@out\funprog@function@south
\funprog@function@update
},
out/east/.code={
\let\funprog@function@out\funprog@function@east
\funprog@function@update
},
out/west/.code={
\let\funprog@function@out\funprog@function@west
\funprog@function@update
},
%
arity/.code={
\def\funprog@function@arity{#1}
\funprog@function@update
},
%
in=north,
out=south,
arity=1
}
% sets \pgf@x and \pgf@y to the south west corner of the function box
\def\funprog@function@anchor@southwest{
\pgfmathsetlength\@tempdima{\pgfkeysvalueof{/funprog/function/port width} + \pgfkeysvalueof{/funprog/function/port separation}}
%
% Calculate x
%
% First, is width < minimum width?
\pgf@x=\wd\pgfnodeparttextbox%
\pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/inner xsep}}%
\advance\pgf@x by 2\pgf@xc%
\pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/minimum width}}%
\ifdim\pgf@x<\pgf@xb%
% yes, too small. Enlarge...
\pgf@x=\pgf@xb%
\fi%
\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/funprog/function/north ports} * \@tempdima}
\ifdim\pgf@x<\pgf@xa%
% yes, too small. Enlarge...
\pgf@x=\pgf@xa%
\fi%
\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/funprog/function/south ports} * \@tempdima}
\ifdim\pgf@x<\pgf@xa%
% yes, too small. Enlarge...
\pgf@x=\pgf@xa%
\fi%
% Now, calculate left border: .5\wd\pgfnodeparttextbox - .5 \pgf@x - outer sep
\pgf@x=-.5\pgf@x%
\advance\pgf@x by.5\wd\pgfnodeparttextbox%
\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
\advance\pgf@x by-\pgf@xa%
%
% Calculate y
%
% First, is height+depth < minimum height?
\pgf@y=\ht\pgfnodeparttextbox%
\advance\pgf@y by\dp\pgfnodeparttextbox%
\pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
\advance\pgf@y by 2\pgf@yc%
\pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
\ifdim\pgf@y<\pgf@yb%
% yes, too small. Enlarge...
\pgf@y=\pgf@yb%
\fi%
\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/funprog/function/east ports} * \@tempdima}
\ifdim\pgf@y<\pgf@ya%
% yes, too small. Enlarge...
\pgf@y=\pgf@ya%
\fi%
\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/funprog/function/west ports} * \@tempdima}
\ifdim\pgf@y<\pgf@ya%
% yes, too small. Enlarge...
\pgf@y=\pgf@ya%
\fi%
% Now, calculate upper border: .5\ht-.5\dp - .5 \pgf@y - outer sep
\pgf@y=-.5\pgf@y%
\advance\pgf@y by-.5\dp\pgfnodeparttextbox%
\advance\pgf@y by.5\ht\pgfnodeparttextbox%
\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
\advance\pgf@y by-\pgf@ya%
}
% sets \pgf@x and \pgf@y to the north east corner of the function box
\def\funprog@function@anchor@northeast{
\pgfmathsetlength\@tempdima{\pgfkeysvalueof{/funprog/function/port width} + \pgfkeysvalueof{/funprog/function/port separation}}
%
% Calculate x
%
% First, is width < minimum width?
\pgf@x=\the\wd\pgfnodeparttextbox%
\pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/inner xsep}}%
\advance\pgf@x by 2\pgf@xc%
\pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/minimum width}}%
\ifdim\pgf@x<\pgf@xb%
% yes, too small. Enlarge...
\pgf@x=\pgf@xb%
\fi%
\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/funprog/function/north ports} * \@tempdima}
\ifdim\pgf@x<\pgf@xa%
% yes, too small. Enlarge...
\pgf@x=\pgf@xa%
\fi%
\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/funprog/function/south ports} * \@tempdima}
\ifdim\pgf@x<\pgf@xa%
% yes, too small. Enlarge...
\pgf@x=\pgf@xa%
\fi%
% Now, calculate right border: .5\wd\pgfnodeparttextbox + .5 \pgf@x + outer sep
\pgf@x=.5\pgf@x%
\advance\pgf@x by.5\wd\pgfnodeparttextbox%
\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
\advance\pgf@x by\pgf@xa%
%
% Calculate y
%
% First, is height+depth < minimum height?
\pgf@y=\ht\pgfnodeparttextbox%
\advance\pgf@y by\dp\pgfnodeparttextbox%
\pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
\advance\pgf@y by 2\pgf@yc%
\pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
\ifdim\pgf@y<\pgf@yb%
% yes, too small. Enlarge...
\pgf@y=\pgf@yb%
\fi%
\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/funprog/function/east ports} * \@tempdima}
\ifdim\pgf@y<\pgf@ya%
% yes, too small. Enlarge...
\pgf@y=\pgf@ya%
\fi%
\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/funprog/function/west ports} * \@tempdima}
\ifdim\pgf@y<\pgf@ya%
% yes, too small. Enlarge...
\pgf@y=\pgf@ya%
\fi%
% Now, calculate upper border: .5\ht-.5\dp + .5 \pgf@y + outer sep
\pgf@y=.5\pgf@y%
\advance\pgf@y by-.5\dp\pgfnodeparttextbox%
\advance\pgf@y by.5\ht\pgfnodeparttextbox%
\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
\advance\pgf@y by\pgf@ya%
}
% draws a port
\def\funprog@function@portpath{
\pgf@xa=0pt
\pgf@ya=0pt
\advance\pgf@xa by\halfportseparation
\advance\pgf@xa by\halfportshadow
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
\pgf@xb=\pgf@xa \pgf@yb=\pgf@ya
\advance\pgf@xa by-\halfportshadow
\advance\pgf@ya by\portheight
{
\edef\funprog@xa{\the\pgf@xa}
\edef\funprog@ya{\the\pgf@ya}
\edef\funprog@xb{\the\pgf@xb}
\edef\funprog@yb{\the\pgf@yb}
\pgfcurveto
{\pgfpoint{\funprog@xb}{\funprog@yb + \halfportheight}}
{\pgfpoint{\funprog@xa}{\funprog@ya - \halfportheight}}
{\pgfpoint{\funprog@xa}{\funprog@ya}}
}
\pgf@xb=\pgf@xa \pgf@yb=\pgf@ya
\advance\pgf@xa by\portwidth
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
\pgf@xb=\pgf@xa \pgf@yb=\pgf@ya
\advance\pgf@xa by-\halfportshadow
\advance\pgf@ya by-\portheight
{
\edef\funprog@xa{\the\pgf@xa}
\edef\funprog@ya{\the\pgf@ya}
\edef\funprog@xb{\the\pgf@xb}
\edef\funprog@yb{\the\pgf@yb}
\pgfcurveto
{\pgfpoint{\funprog@xb}{\funprog@yb - \halfportheight}}
{\pgfpoint{\funprog@xa}{\funprog@ya + \halfportheight}}
{\pgfpoint{\funprog@xa}{\funprog@ya}}
}