%
% Copyright 2018 Luca Cappelletti
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{adigraph}
[2018/03/10 v1.3 LaTeX package for creating augmenting directed graphs]

\RequirePackage{etoolbox}
\RequirePackage{fp}
\RequirePackage{xparse}
\RequirePackage{xstring}
\RequirePackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\newcommand*{\AdigraphRom}[1]{\expandafter\@slowromancap\romannumeral #1@}
\makeatother

\newcounter{AdigraphTotalNodeCounter}
\newcounter{AdigraphCurrentNodeCounter}
\newcounter{AdigraphNumberOfPaths}
\newcounter{AdigraphCurrentPathNumber}

\NewDocumentCommand{\AdigraphDrawNode}{m}{%
	%
	% #1 is node normalized id
	%
	\letcs{\nodeX}{adigraphNode#1X}
	\letcs{\nodeY}{adigraphNode#1Y}
	\letcs{\nodeLabel}{adigraphNode#1Label}
	\letcs{\nodeColor}{adigraphNode#1Color}
	%
	\node[vertex,\nodeColor] (#1) at (\nodeX,\nodeY) {\nodeLabel};
}

\NewDocumentCommand{\AdigraphDrawEdge}{m m}{%
	%
	% #1 is first node normalized id
	% #2 is second node normalized id
	%
	\ifbool{adigraphEdge#1v#2Drawn}{%
		% This edge was already drawn
	}{%
		\letcs{\firstX}{adigraphNode#1X}
		\letcs{\firstY}{adigraphNode#1Y}
		\letcs{\secondX}{adigraphNode#2X}
		\letcs{\secondY}{adigraphNode#2Y}

		\providebool{equalX}
		\FPifeq{\firstX}{\secondX}
		\booltrue{equalX}
		\else
		\boolfalse{equalX}
		\fi

		\providebool{ltX}
		\FPiflt{\firstX}{\secondX}
		\booltrue{ltX}
		\else
		\boolfalse{ltX}
		\fi

		\providebool{ltY}
		\FPiflt{\firstY}{\secondY}
		\booltrue{ltY}
		\else
		\boolfalse{ltY}
		\fi

		\ifbool{ltX}{%
			\def\AdigraphLabelPosition{above}
		}{%
			\ifbool{equalX}{%
				\ifbool{ltY}{%
					\def\AdigraphLabelPosition{left}
				}{%
					\def\AdigraphLabelPosition{right}
				}
			}{%
				\def\AdigraphLabelPosition{below}
			}
		}

		\booltrue{adigraphEdge#1v#2Drawn}
		\letcs{\edgeLabelPosition}{adigraphEdge#1v#2LabelPosition}
		\letcs{\edgeWeight}{adigraphEdge#1v#2Weight}
		\ifcsdef{adigraphEdge#2v#1Weight}{%
			\letcs{\AdigraphTwinEdgeWeight}{adigraphEdge#2v#1Weight}
		}{%
			\def\AdigraphTwinEdgeWeight{0}
		}
		\letcs{\edgeColor}{adigraphEdge#1v#2Color}
		\ifbool{adigraphEdge#1v#2HasLabel}{%
			\letcs{\edgeLabel}{adigraphEdge#1v#2Label}
		}{%
			\letcs{\edgeLabel}{adigraphEdge#1v#2Weight}
		}
		\ifnumequal{\edgeWeight}{0}{%
			% We don't draw edges with weight 0
		}{%
			\ifnumequal{\AdigraphTwinEdgeWeight}{0}{%
				% The twin edge won't be drawn
				% This edge will be a straight line
				\draw[edge,\edgeColor] (#1) to node[\edgeLabelPosition, \AdigraphLabelPosition] {\edgeLabel}(#2);
			}{%
				% Both edges have to be drawn, they will be curly
				\draw[edge,\edgeColor] (#1) to [bend right=20] node[\edgeLabelPosition, \AdigraphLabelPosition] {\edgeLabel}(#2);
			}%
		}%
	}
}

\NewDocumentCommand{\AdigraphGenerateNodeName}{m m G{black}}{}
\NewDocumentCommand{\AdigraphMemorizeNode}{m m G{black}}{}

\NewDocumentCommand{\AdigraphBuildNode}{> { \SplitArgument{ 1 } {,} } m > { \SplitArgument{ 1 } {,} } m m}{%
	%
	% #1 -> given node name, not normalized
	% #2 -> list of coordinates and color
	% #3 -> optional label
	%
	\RenewDocumentCommand{\AdigraphGenerateNodeName}{m G{black}}{%
		\IfInteger{##1}{%
			\def\AdigraphNodeName{\AdigraphRom{##1}}%
			\listgadd{\AdigraphNodeList}{\AdigraphRom{##1}}
		}{%
			\def\AdigraphNodeName{##1}%
			\listgadd{\AdigraphNodeList}{##1}
		}%
		\csdef{adigraphNode\AdigraphNodeName Color}{##2}%
		\ifblank{#3}{
			\csdef{adigraphNode\AdigraphNodeName Label}{##1}%
		}{
			\csdef{adigraphNode\AdigraphNodeName Label}{#3}%
		}
	}
	\RenewDocumentCommand{\AdigraphMemorizeNode}{m G{}}{%
		\ifblank{##2}{
			\newcount\radius
			\newcount\myi
			\newcount\myn
			\newcount\mydpi
			\radius ##1\relax
			\myi \value{AdigraphCurrentNodeCounter}\relax
			\myn \value{AdigraphTotalNodeCounter}\relax
			\FPset{\mydpi}{6.283185307179586476}
			\FPmul{\numerator}{\the\myi}{\mydpi}
			\FPdiv{\anomaly}{\numerator}{\the\myn}
			\FPcos{\nodeX}{\anomaly}
			\FPsin{\nodeY}{\anomaly}
			\FPmul{\nodeX}{\nodeX}{\the\radius}
			\FPmul{\nodeY}{\nodeY}{\the\radius}
			\csedef{adigraphNode\AdigraphNodeName X}{\nodeX}%
			\csedef{adigraphNode\AdigraphNodeName Y}{\nodeY}%
			\stepcounter{AdigraphCurrentNodeCounter}
		}{
			\csdef{adigraphNode\AdigraphNodeName X}{##1}%
			\csdef{adigraphNode\AdigraphNodeName Y}{##2}%
		}
	}%
	\AdigraphGenerateNodeName#1
	\AdigraphMemorizeNode#2
}

\NewDocumentCommand{\AdigraphMemorizeEdge}{m m G{black}}{}

\NewDocumentCommand{\AdigraphBuildEdge}{> {\SplitArgument{2}{,}} m m m m}{%
	%
	% #1 -> List of edge nodes and its color, validated
	% #2 -> Weight
	% #3 -> Label, defaults to weight
	% #4 -> Label position, defaults to midway
	%
	\RenewDocumentCommand{\AdigraphMemorizeEdge}{m m G{black}}{%
		\IfInteger{##1}{%
			\IfInteger{##2}{%
				\def\AdigraphEdgeName{\AdigraphRom{##1}v\AdigraphRom{##2}}%
				\listgadd{\AdigraphEdgeList}{\AdigraphRom{##1},\AdigraphRom{##2}}
			}{%
				\def\AdigraphEdgeName{\AdigraphRom{##1}v##2}%
				\listgadd{\AdigraphEdgeList}{\AdigraphRom{##1},##2}
			}%
		}{%
			\IfInteger{##2}{%
				\def\AdigraphEdgeName{##1v\AdigraphRom{##2}}%
				\listgadd{\AdigraphEdgeList}{##1,\AdigraphRom{##2}}
			}{%
				\def\AdigraphEdgeName{##1v##2}%
				\listgadd{\AdigraphEdgeList}{##1,##2}
			}%
		}%
		\newbool{adigraphEdge\AdigraphEdgeName HasLabel}
		\newbool{adigraphEdge\AdigraphEdgeName Drawn}
		\ifblank{#3}{%
			\boolfalse{adigraphEdge\AdigraphEdgeName HasLabel}
		}{%
			\booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
		}%
		\ifblank{#2}{%
			\booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
			\csdef{adigraphEdge\AdigraphEdgeName Weight}{1}%
		}{%
			\csdef{adigraphEdge\AdigraphEdgeName Weight}{#2}%
		}%
		\csdef{adigraphEdge\AdigraphEdgeName Label}{#3}%
		\csdef{adigraphEdge\AdigraphEdgeName Color}{##3}%
		\csdef{adigraphEdge\AdigraphEdgeName LabelPosition}{#4}%
	}
	\AdigraphMemorizeEdge#1
}

\NewDocumentCommand{\AdigraphBuildEdgeWrapper}{m G{} G{} G{midway}}{%
	%
	% #1 -> List of edge nodes and its color
	% #2 -> Weight
	% #3 -> Label
	% #4 -> Label position
	%
	\ifblank{#1}{%
		%Do nothing, this is the tail of the list
	}{%
		\AdigraphBuildEdge{#1}{#2}{#3}{#4}
	}
}

\NewDocumentCommand{\AdigraphElaboratePath}{m m G{black}}{}

\NewDocumentCommand{\AdigraphSimpleSum}{m m m}{%
	%
	% #1 -> First addendum
	% #2 -> Second addendum
	% #3 -> Variable to store sum
	%
	\newcount\AdigraphCounter
	\AdigraphCounter #1\relax
	\advance\AdigraphCounter #2\relax
	\FPset{#3}{\the\AdigraphCounter}
}

\NewDocumentCommand{\AdigraphPathBuilder}{m m m}{%
	%
	% #1 -> first node
	% #2 -> second node
	% #3 -> units
	%
	\ifcsdef{adigraphEdge#1v#2Weight}{%
		\letcs{\AdigraphWeightA}{adigraphEdge#1v#2Weight}
	}{%
		\def\AdigraphWeightA{0}
		\newbool{adigraphEdge#1v#2HasLabel}
		\ifcsdef{adigraphEdge#2v#1LabelPosition}{%
			\csdef{adigraphEdge#1v#2LabelPosition}{\csuse{adigraphEdge#2v#1LabelPosition}}
		}{%
			\csdef{adigraphEdge#1v#2LabelPosition}{midway}
		}
		\csdef{adigraphEdge#1v#2LabelPosition}{midway}
		\newbool{adigraphEdge#1v#2Drawn}
		\csdef{adigraphEdge#1v#2Color}{black}%
		\listgadd{\AdigraphEdgeList}{#1,#2}
	}
	\ifcsdef{adigraphEdge#2v#1Weight}{%
		\letcs{\AdigraphWeightB}{adigraphEdge#2v#1Weight}
	}{%
		\def\AdigraphWeightB{0}
		\newbool{adigraphEdge#2v#1HasLabel}
		\ifcsdef{adigraphEdge#1v#2LabelPosition}{%
			\csdef{adigraphEdge#2v#1LabelPosition}{\csuse{adigraphEdge#1v#2LabelPosition}}
		}{%
			\csdef{adigraphEdge#2v#1LabelPosition}{midway}
		}
		\newbool{adigraphEdge#2v#1Drawn}
		\csdef{adigraphEdge#2v#1Color}{black}%
		\listgadd{\AdigraphEdgeList}{#2,#1}
	}

	\ifnum\value{AdigraphCurrentPathNumber}=\value{AdigraphNumberOfPaths}
		\csdef{adigraphEdge#1v#2Color}{blue}%
		\csdef{adigraphEdge#2v#1Color}{red}%
	\else
		% Leaves the default colors
	\fi
	\expandafter\AdigraphSimpleSum\expandafter{\AdigraphWeightA}{-#3}{\resultA}
	\expandafter\AdigraphSimpleSum\expandafter{\AdigraphWeightB}{#3}{\resultB}
	\cslet{adigraphEdge#1v#2Weight}{\resultA}%
	\cslet{adigraphEdge#2v#1Weight}{\resultB}%
}

\NewDocumentCommand{\AdigraphBuildPath}{> { \SplitList{,} } m m}{%
	%
	% #1 -> List of nodes
	% #2 -> Units to send over path
	%
	\RenewDocumentCommand{\AdigraphElaboratePath}{m}{%
		\IfInteger{##1}{%
			\def\AdigraphCurrentNode{\AdigraphRom{##1}}%
		}{%
			\def\AdigraphCurrentNode{##1}%
		}%
		\ifdef{\AdigraphLastParsedNode}{%
			\expandafter\expandafter\expandafter\AdigraphPathBuilder\expandafter\expandafter\expandafter{\expandafter\AdigraphLastParsedNode\expandafter}\expandafter{\AdigraphCurrentNode}{#2}
		}{%
			% This is the first iteration
		}
		\IfInteger{##1}{%
			\def\AdigraphLastParsedNode{\AdigraphRom{##1}}%
		}{%
			\def\AdigraphLastParsedNode{##1}%
		}%
	}
	\ProcessList{#1}{\AdigraphElaboratePath}
	\undef{\AdigraphLastParsedNode}
}

\NewDocumentCommand{\AdigraphBuildNodeWrapper}{m m G{}}{
	\ifblank{#1}{
		%end of list
	}{
		\AdigraphBuildNode{#1}{#2}{#3}
	}
}

\NewDocumentCommand{\AdigraphNodeBuilder}{ > { \SplitArgument{ 2 } {:} } m }{%
	\AdigraphBuildNodeWrapper#1
}

\NewDocumentCommand{\AdigraphEdgeBuilder}{ > { \SplitArgument{ 3 } {:} } m }{%
	\AdigraphBuildEdgeWrapper#1
}

\NewDocumentCommand{\AdigraphEdgeDrawer}{ > { \SplitArgument{ 1 } {,} } m }{%
	\AdigraphDrawEdge#1
}

\NewDocumentCommand{\AdigraphNodeCounter}{m G{}}{%
	\ifblank{#2}{
		\stepcounter{AdigraphTotalNodeCounter}
	}{}
}

\NewDocumentCommand{\AdigraphNodeCounterWrapper}{m > { \SplitArgument{ 1 } {,} } m m m }{%
	\ifblank{#1}{}{
		\AdigraphNodeCounter#2
	}
}

\NewDocumentCommand{\AdigraphNodeCounterSecondWrapper}{ > { \SplitArgument{ 3 } {:} } m }{%
	\AdigraphNodeCounterWrapper#1
}

\NewDocumentCommand{\AdigraphNodesCounter}{> { \SplitList{;} } m}{%
	\ProcessList{#1}{\AdigraphNodeCounterSecondWrapper}
}

\NewDocumentCommand{\AdigraphProcessNodes}{> { \SplitList{;} } m}{%
	\ProcessList{#1}{\AdigraphNodeBuilder}
	\forlistloop{\AdigraphDrawNode}{\AdigraphNodeList}
}

\NewDocumentCommand{\AdigraphProcessEdges}{> { \SplitList{;} } m}{%
	\ProcessList{#1}{\AdigraphEdgeBuilder}
}

\NewDocumentCommand{\AdigraphProcessPaths}{> { \SplitArgument{ 1 } {:} } m}{%
	\stepcounter{AdigraphCurrentPathNumber}
	\AdigraphBuildPath#1
}

\NewDocumentCommand{\AdigraphCountPaths}{m}{%
	\stepcounter{AdigraphNumberOfPaths}
}

\NewDocumentCommand{\AdigraphCalculateOrientation}{m m m}{%
	\letcs{\firstX}{adigraphNode#2X}
	\letcs{\firstY}{adigraphNode#2Y}
	\letcs{\secondX}{adigraphNode#3X}
	\letcs{\secondY}{adigraphNode#3Y}

	\providebool{equalX}
	\FPifeq{\firstX}{\secondX}
	\booltrue{equalX}
	\else
	\boolfalse{equalX}
	\fi

	\ifnumequal{\secondX-\firstX}{0}{%
		\FPset{#1}{90}
	}{%
		\FPsub{\deltaY}{\secondY}{\firstY}
		\FPsub{\deltaX}{\secondX}{\firstX}
		\FPdiv{\deltaDiv}{\deltaY}{\deltaX}
		\FParctan{\arcResult}{\deltaDiv}
		\FPmul{#1}{\arcResult}{57.2958}
	}
}

\NewDocumentCommand{\AdigraphExecuteCutBuilder}{m m G{black}}{%
\ifblank{#1}{%
% Reached end of list
}{%
\IfInteger{#1}{%
	\def\AdigraphFirstNode{\AdigraphRom{#1}}%
}{%
	\def\AdigraphFirstNode{#1}%
}%
\IfInteger{#2}{%
	\def\AdigraphSecondNode{\AdigraphRom{#2}}%
}{%
	\def\AdigraphSecondNode{#2}%
}%
\AdigraphCalculateOrientation{\orientation}{\AdigraphFirstNode}{\AdigraphSecondNode}
\draw[dashed,#3,rotate=\orientation] ([yshift=10pt]$(\AdigraphFirstNode)!0.7!(\AdigraphSecondNode)$) -- ([yshift=-10pt]$(\AdigraphFirstNode)!0.7!(\AdigraphSecondNode)$);
}
}

\NewDocumentCommand{\AdigraphCutBuilder}{> { \SplitArgument { 2 } { , } } m}{%
	\AdigraphExecuteCutBuilder #1
}

\NewDocumentCommand{\AdigraphProcessCuts}{> { \SplitList{;} } m}{%
	\ProcessList{#1}{\AdigraphCutBuilder}
}

\NewDocumentCommand{\Adigraph}{m m m m m}{%
	%
	% #1 -> Vertices
	% #2 -> Edges
	% #3 -> Augmenting paths
	% #4 -> Cuts
	% #5 -> Current augmenting paths
	%
	\def\AdigraphNodeList{}
	\def\AdigraphEdgeList{}
	\letcs{\AdigraphAugmentingPaths}{#3}
	\begin{tikzpicture}
		\tikzset{%
			vertex/.style={circle,draw,minimum size=2em},
			edge/.style={->,> = latex}
		}

		% vertices
		\ifblank{#1}{%
			% List is empty
		}{%
			\AdigraphNodesCounter{#1}
			\AdigraphProcessNodes{#1}
		}

		\ifblank{#2}{%
			% List is empty
		}{%
			\AdigraphProcessEdges{#2}
		}

		\ifblank{\AdigraphAugmentingPaths}{%
			% List is empty
		}{%
			\ifblank{#5}{%
				\defcounter{AdigraphNumberOfPaths}{100000}
			}{%
			}
			\forlistloop{\AdigraphCountPaths}{\AdigraphAugmentingPaths}
			\forlistloop{\AdigraphProcessPaths}{\AdigraphAugmentingPaths}
		}

		\ifblank{#2}{%
			% List is empty
		}{%
			\forlistloop{\AdigraphEdgeDrawer}{\AdigraphEdgeList}
		}

		% %cuts
		% \ifblank{#4}{%
		% 	% List is empty
		% }{%
		% 	\AdigraphProcessCuts{#4}
		% }
	\end{tikzpicture}
}

\NewDocumentCommand{\AdigraphProcessAugmentingPaths}{m}{}
\NewDocumentCommand{\AdigraphProcessAugmentingPathsList}{m}{}

\NewDocumentCommand{\NewAdigraph}{m m G{}}{%
	%
	% #1 -> Variable to assign to as command
	% #2 -> Nodes
	% #3 -> Edges
	%
	\ifcsdef{#1}{
		\PackageError{adigraph}{%
			You have already define an adigraph called #1.
		}{%
			Identify the duplicate and change its name, e.g. Second#1
		}
	}{
		\csdef{Adigraph#1AugmentingPaths}{}%
		\expandafter\RenewDocumentCommand{\AdigraphProcessAugmentingPaths}{m}{%
			\listcsgadd{Adigraph#1AugmentingPaths}{##1}
		}
		\expandafter\RenewDocumentCommand{\AdigraphProcessAugmentingPathsList}{> { \SplitList{;} } m}{%
			\AdigraphProcessAugmentingPaths##1
		}
		\expandafter\NewDocumentCommand\expandafter{\csname #1\endcsname}{m G{}}{%
			%
			% ##1 -> Augmenting path
			%
			\setcounter{AdigraphCurrentNodeCounter}{0}
			\setcounter{AdigraphTotalNodeCounter}{0}
			\setcounter{AdigraphNumberOfPaths}{0}
			\setcounter{AdigraphCurrentPathNumber}{0}
			\AdigraphProcessAugmentingPathsList{##1}
			\Adigraph{#2}{#3}{Adigraph#1AugmentingPaths}{##2}{##1}
		}%
	}
}

\NewDocumentCommand{\RenewAdigraph}{m m G{}}{%
	%
	% #1 -> Variable to assign to as command
	% #2 -> Nodes
	% #3 -> Edges
	%
	\ifcsdef{#1}{
		\csdef{Adigraph#1AugmentingPaths}{}%
		\expandafter\RenewDocumentCommand{\AdigraphProcessAugmentingPaths}{m}{%
			\listcsgadd{Adigraph#1AugmentingPaths}{##1}
		}
		\expandafter\RenewDocumentCommand{\AdigraphProcessAugmentingPathsList}{> { \SplitList{;} } m}{%
			\AdigraphProcessAugmentingPaths##1
		}
		\expandafter\RenewDocumentCommand\expandafter{\csname #1\endcsname}{m G{}}{%
			%
			% ##1 -> Augmenting path
			%
			\setcounter{AdigraphCurrentNodeCounter}{0}
			\setcounter{AdigraphTotalNodeCounter}{0}
			\setcounter{AdigraphNumberOfPaths}{0}
			\setcounter{AdigraphCurrentPathNumber}{0}
			\AdigraphProcessAugmentingPathsList{##1}
			\Adigraph{#2}{#3}{Adigraph#1AugmentingPaths}{##2}{##1}
		}%
	}{
		\PackageError{adigraph}{%
			You cannot renew a graph called #1 without having it defined first.
		}{%
			Create the first graph using \textbackslash NewAdigraph
		}
	}
}


\endinput
%%
%% End of file `adigraph.sty'.