summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/memorygraphs/memorygraphs.sty
blob: 15a13207ec05d8b6fbc2a5db3198821f22fac9b6 (plain)
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
%% memorygraphs.tex
%% Copyright 2018-2019 C. Staps
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status “maintained”.
%
% The Current Maintainer of this work is C. Staps.
%
% This work consists of the files memorygraphs.sty and the documentation in
% memorygraphs.tex and example-fibonacci.tex.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{memorygraphs}[2019/01/06 v0.1.1 Draw memory graphs using TikZ]

\RequirePackage{tikz}
\usetikzlibrary{arrows.meta,calc,positioning,shapes}

\def\pgfaddtoshape#1#2{%
	\begingroup
	\def\pgf@sm@shape@name{#1}%
	#2%
	\endgroup
}

\pgfaddtoshape{rectangle split}{%
	\pgfmathsetcount\c@pgf@counta{\pgfkeysvalueof{/pgf/rectangle split parts}}%
	\edef\parts{\the\c@pgf@counta}%
	\expandafter\xdef\csname pgf@anchor@rectangle split@head north east\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle split@one split north\endcsname}%
	\expandafter\xdef\csname pgf@anchor@rectangle split@head south east\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle split@one split south\endcsname}%
	\expandafter\xdef\csname pgf@anchor@rectangle split@head north west\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle split@north west\endcsname}%
	\expandafter\xdef\csname pgf@anchor@rectangle split@head south west\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle split@south west\endcsname}%
	\pgfmathloop%
		\ifnum\pgfmathcounter>\parts%
		\else%
			\pgf@lib@sh@getalpha\pgf@lib@sh@rs@number{\pgfmathcounter}%
			\expandafter\xdef\csname pgf@anchor@rectangle split@\pgf@lib@sh@rs@number\space center\endcsname{%
				\expandafter\noexpand\csname pgf@anchor@rectangle split@\pgf@lib@sh@rs@number\space north\endcsname%
				\pgf@ya=\pgf@y%
				\expandafter\noexpand\csname pgf@anchor@rectangle split@\pgf@lib@sh@rs@number\space south\endcsname%
				\advance\pgf@ya by-\pgf@y%
				\advance\pgf@y by.5\pgf@ya}%
			\c@pgf@counta=\pgfmathcounter%
			\advance\c@pgf@counta by-1\relax%
			\edef\name{\ifnum\c@pgf@counta=0 head\else arg \the\c@pgf@counta\fi}%
			\expandafter\xdef\csname pgf@anchor@rectangle split@\name\endcsname{%
				\expandafter\noexpand\csname pgf@anchor@rectangle split@\pgf@lib@sh@rs@number\endcsname}%
			\pgfutil@for\pgf@lib@sh@rs@temp:={center,north,east,south,west}\do{%
				\expandafter\xdef\csname pgf@anchor@rectangle split@\name\space\pgf@lib@sh@rs@temp\endcsname{%
					\expandafter\noexpand\csname pgf@anchor@rectangle split@\pgf@lib@sh@rs@number\space\pgf@lib@sh@rs@temp\endcsname}}%
	\repeatpgfmathloop%
}
\pgfaddtoshape{rectangle}{%
	\expandafter\xdef\csname pgf@anchor@rectangle@head north east\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle@north east\endcsname}%
	\expandafter\xdef\csname pgf@anchor@rectangle@head south east\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle@south east\endcsname}%
	\expandafter\xdef\csname pgf@anchor@rectangle@head north west\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle@north west\endcsname}%
	\expandafter\xdef\csname pgf@anchor@rectangle@head south west\endcsname{%
		\expandafter\noexpand\csname pgf@anchor@rectangle@south west\endcsname}%
}

\newcommand{\memorygraphs@arg}[1]{%
	\pgfmathsetcount\c@pgf@counta{#1}%
	\advance\c@pgf@counta by1\relax%
	\expandafter\nodepart\expandafter{\expandafter\pgf@lib@sh@toalpha\expandafter{\the\c@pgf@counta}}}

\newdimen\memorygraphs@marklength
\memorygraphs@marklength=4pt

\tikzset{
	memory graph/.style={
		node distance=1.5em,
	},
	every block/.style={},
	block/.style={
		draw,
		text height=height("I"),
		text depth=depth("I"),
		anchor=mid,
		every block,
	},
	block/.append code={%
		\let\arg\memorygraphs@arg%
		\gdef\memorygraphs@arity{0}%
	},
	block mark north east/.style={
		append after command={
			\pgfextra
				\pgfinterruptpath
				\draw[#1]
					($(\tikzlastnode.head north east)-(\ifnum\memorygraphs@arity=0 .5\pgflinewidth\else0\fi,.5\pgflinewidth)$)
					-- +(0,-\memorygraphs@marklength)
					-- ($(\tikzlastnode.head north east)-(\ifnum\memorygraphs@arity=0 .5\pgflinewidth\else0\fi+\memorygraphs@marklength,.5\pgflinewidth)$)
					-- cycle;
				\endpgfinterruptpath
			\endpgfextra
		},
	},
	block mark south east/.style={
		append after command={
			\pgfextra
				\pgfinterruptpath
				\draw[#1]
					($(\tikzlastnode.head south east)+(\ifnum\memorygraphs@arity=0 -.5\pgflinewidth\else0\fi,.5\pgflinewidth)$)
					-- +(0,\memorygraphs@marklength)
					-- ($(\tikzlastnode.head south east)+(\ifnum\memorygraphs@arity=0 -.5\pgflinewidth\else0\fi-\memorygraphs@marklength,.5\pgflinewidth)$)
					-- cycle;
				\endpgfinterruptpath
			\endpgfextra
		},
	},
	block mark north west/.style={
		append after command={
			\pgfextra
				\pgfinterruptpath
				\draw[#1]
					($(\tikzlastnode.head north west)+(.5\pgflinewidth,-.5\pgflinewidth)$)
					-- +(0,-\memorygraphs@marklength)
					-- ($(\tikzlastnode.head north west)-(-.5\pgflinewidth-\memorygraphs@marklength,.5\pgflinewidth)$)
					-- cycle;
				\endpgfinterruptpath
			\endpgfextra
		},
	},
	block mark south west/.style={
		append after command={
			\pgfextra
				\pgfinterruptpath
				\draw[#1]
					($(\tikzlastnode.head south west)+(.5\pgflinewidth,.5\pgflinewidth)$)
					-- +(0,\memorygraphs@marklength)
					-- ($(\tikzlastnode.head south west)+(.5\pgflinewidth+\memorygraphs@marklength,.5\pgflinewidth)$)
					-- cycle;
				\endpgfinterruptpath
			\endpgfextra
		},
	},
	arity/.style={
		block,
		rectangle split,
		rectangle split parts=#1+1,
		rectangle split horizontal,
		rectangle split every empty part={},
		rectangle split empty part width=.2em,
	},
	arity/.append code={%
		\pgfmathsetcount\c@pgf@counta{\pgfkeysvalueof{/pgf/rectangle split parts}}%
		\ifnum\c@pgf@counta=0\else\advance\c@pgf@counta by-1 \fi
		\xdef\memorygraphs@arity{\the\c@pgf@counta}%
	},
  ref/.style={
		{Circle[length=3pt]}-Latex,
		shorten <=-1.5pt,
		rounded corners=.2em,
	},
}