%%%
% Itération
%%%
\def\filedateIteration{2025/07/30}%
\def\fileversionIteration{0.1a}%
\message{-- \filedateIteration\space v\fileversionIteration}%
%
\setKVdefault[Iteration]{Depart=4,Repetitions=3,Images=false,Ratio=false,Fleches=false,Debord=1,Completer=false,Solution=false,CouleurSol=Evidence,Ecart={ ; }}%

\makeatletter
\NewDocumentCommand\PfM@TikzMark{m}{%
  \tikz[remember picture,baseline,inner sep=0pt]{%
    \node[name=Iteration-\theNbDistri,anchor=base] {$#1$};}%
  \stepcounter{NbDistri}%
}%

\NewDocumentCommand\Iteration{om}{%
  \setcounter{NbDistri}{0}%
  \useKVdefault[Iteration]%
  \setKV[Iteration]{#1}%
  \setsepchar{ }\ignoreemptyitems%
  \readlist*\PfM@ListeOp{#2}%
  \reademptyitems%
%  Lecture Itération : \showitems\PfM@ListeOp[]\par
  \ifboolKV[Iteration]{Ratio}{%
    \edef\PfM@ListeCompoAMelanger{}%
    \edef\PfM@RetiensLesIterations{0}%
    \xintFor* ##1 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
      \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOp[\fpeval{##1*2-1}]*\useKV[Iteration]{Repetitions}}}}\do{%
        \edef\PfM@ListeCompoAMelanger{\PfM@ListeCompoAMelanger,##1}%
        \edef\PfM@RetiensLesIterations{\fpeval{\PfM@RetiensLesIterations+1}}%
      }%
    }%
    % Ajout d'éléments par la clé Debord.
    \ifnum\useKV[Iteration]{Debord}>0\relax%
      \edef\PfM@NbElements{0}%
      \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
        \xintFor* ##3 in{\xintSeq{1}{\PfM@ListeOp[\fpeval{##2*2-1}]}}\do{%
          \edef\PfM@NbElements{\fpeval{\PfM@NbElements+1}}%
          \ifnum\useKV[Iteration]{Debord}<\PfM@NbElements\relax%
          \else%
            % \PfM@ListeOp[\fpeval{2*##2}]%
            \edef\PfM@ListeCompoAMelanger{\PfM@ListeCompoAMelanger,##2}%
            \edef\PfM@RetiensLesIterations{\fpeval{\PfM@RetiensLesIterations+1}}%
          \fi%
        }%
      }%
    \fi%
    \MelangeListe{\PfM@ListeCompoAMelanger}{\PfM@RetiensLesIterations}%
    \setsepchar{,}\ignoreemptyitems%
    \readlist*\PfM@ListeRatioAAfficher{\faa}%
    \reademptyitems%
    \xintFor* ##1 in{\xintSeq{1}{\PfM@RetiensLesIterations}}\do{%
      \itemtomacro\PfM@ListeRatioAAfficher[##1]\PfM@RatioTiti%
      \PfM@ListeOp[\fpeval{2*\PfM@RatioTiti}]%
    }%
  }{%
    \ifboolKV[Iteration]{Images}{%
      \xintFor* ##1 in{\xintSeq{1}{\useKV[Iteration]{Repetitions}}}\do{%
        \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
          \xintFor* ##3 in{\xintSeq{1}{\PfM@ListeOp[\fpeval{##2*2-1}]}}\do{%
            \PfM@ListeOp[\fpeval{2*##2}]%
          }%
        }%
      }%
      % Ajout d'éléments par la clé Debord.
      \ifnum\useKV[Iteration]{Debord}>0\relax%
        \edef\PfM@NbElements{0}%
        \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
          \xintFor* ##3 in{\xintSeq{1}{\PfM@ListeOp[\fpeval{##2*2-1}]}}\do{%
            \edef\PfM@NbElements{\fpeval{\PfM@NbElements+1}}%
            \ifnum\useKV[Iteration]{Debord}<\PfM@NbElements\relax%
            \else%
              \PfM@ListeOp[\fpeval{2*##2}]%
            \fi%
          }%
        }%
      \fi%
    }{%
      \ifboolKV[Iteration]{Completer}{\edef\PfM@Repetitions{\fpeval{\useKV[Iteration]{Repetitions}-1}}}{\edef\PfM@Repetitions{\fpeval{\useKV[Iteration]{Repetitions}}}}%
      \edef\PfM@ValeurIntermediaire{\useKV[Iteration]{Depart}}%
      \edef\PfM@ListeSuiteNb{\useKV[Iteration]{Depart}}%
      \xintFor* ##1 in{\xintSeq{1}{\PfM@Repetitions}}\do{%
        \edef\PfMk{##1}%
        \xintFor* ##2 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
          \edef\PfM@ValeurIntermediaire{\fpeval{\PfM@ValeurIntermediaire\PfM@ListeOp[##2]}}%
          \edef\PfM@ListeSuiteNb{\PfM@ListeSuiteNb,\PfM@ValeurIntermediaire}%
        }%
      }%
      \ifboolKV[Iteration]{Completer}{%
        \xintFor* ##3 in{\xintSeq{1}{\useKV[Iteration]{Debord}}}\do{%
          \xintFor* ##2 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
            \edef\PfMk{\fpeval{\PfMk+1}}%
            \edef\PfM@ValeurIntermediaire{\fpeval{\PfM@ValeurIntermediaire\PfM@ListeOp[##2]}}%
            \edef\PfM@ListeSuiteNb{\PfM@ListeSuiteNb,\PfM@ValeurIntermediaire}%
          }%
        }%
      }{}%
      \setsepchar{,}\ignoreemptyitems%
      \readlist*\PfM@ListeRecupere{\PfM@ListeSuiteNb}%
      \reademptyitems%
      \xintFor* ##1 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen*\PfM@Repetitions+1}}}\do{%
        \PfM@TikzMark{\num{\PfM@ListeRecupere[##1]}}\xintifForLast{}{\useKV[Iteration]{Ecart}}%
      }%
      \ifboolKV[Iteration]{Completer}{%
        \ifboolKV[Iteration]{Solution}{%
          \xintFor* ##3 in{\xintSeq{1}{\useKV[Iteration]{Debord}}}\do{%
            \xintFor* ##1 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
              \edef\PfM@Titi{\fpeval{\PfM@ListeOplen*\PfM@Repetitions+1+##1+(##3-1)*\PfM@ListeOplen}}%
              \useKV[Iteration]{Ecart}\PfM@TikzMark{\textcolor{\useKV[Iteration]{CouleurSol}}{\num{\PfM@ListeRecupere[\PfM@Titi]}}}%
            }%
          }%
        }{%
          \xintFor* ##3 in{\xintSeq{1}{\useKV[Iteration]{Debord}}}\do{%
            \xintFor* ##1 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
             \useKV[Iteration]{Ecart}\pointilles[25pt]{}%
            }%
          }%
        }%
      }{}%
      .%
      \ifboolKV[Iteration]{Fleches}{%
        \ifnum\PfM@ListeOplen=1\relax%
          %Fleches avec une opération !
          \IfSubStr{\PfM@ListeOp[1]}{*}{%
            \IfSubStr{\PfM@ListeOp[1]}{+}{%
%              Une multiplication et une addition :)
              \StrBefore{\PfM@ListeOp[1]}{+}[\PfMPartieMul]%
%              PartieMul : \PfMPartieMul\par
              \StrBehind{\PfM@ListeOp[1]}{+}[\PfMPartiePlus]%
%              PartiePlus : \PfMPartiePlus\par
              \StrSubstitute{\PfMPartieMul}{*}{\times}[\PfMPartieMul]%
%              PartieMul : \PfMPartieMul\par
              \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
                \begin{tikzpicture}[remember picture,overlay]%
                  \coordinate[xshift=0em,yshift=2em] (IterationA) at ($(Iteration-##4)!0.5!(Iteration-\fpeval{##4+1})$);
                  \draw[-stealth,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[pos=0.65,below]{\footnotesize$\PfMPartieMul$}(IterationA);%
                  \draw[-stealth,transform canvas={yshift=0.5em}] (IterationA) to node[pos=0.65,above]{\footnotesize$+\num{\PfMPartiePlus}$}(Iteration-\fpeval{##4+1}.north);%
                \end{tikzpicture}%
              }%
            }{\IfSubStr{\PfM@ListeOp[1]}{-}{%
%                une multiplication et une soustraction :)
                \StrBefore{\PfM@ListeOp[1]}{-}[\PfMPartieMul]%
                \StrBehind{\PfM@ListeOp[1]}{-}[\PfMPartieMoins]%
                \StrSubstitute{\PfMPartieMul}{*}{\times}[\PfMPartieMul]%
                \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
                \begin{tikzpicture}[remember picture,overlay]%
                  \coordinate[xshift=0em,yshift=2em] (IterationA) at ($(Iteration-##4)!0.5!(Iteration-\fpeval{##4+1})$);
                  \draw[-stealth,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[pos=0.65,below]{\footnotesize$\PfMPartieMul$}(IterationA);%
                  \draw[-stealth,transform canvas={yshift=0.5em}] (IterationA) to node[pos=0.65,above]{\footnotesize$-\num{\PfMPartieMoins}$}(Iteration-\fpeval{##4+1}.north);%
                \end{tikzpicture}
                }%
              }{%
%                une seule et unique opération : \PfM@ListeOp[1]
                \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
                  \IfSubStr{\PfM@ListeOp[1]}{*}{\StrBehind{\PfM@ListeOp[1]}{*}[\PfMPartieMul]\edef\PfMTiti{\times\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{/}{\StrBehind{\PfM@ListeOp[1]}{/}[\PfMPartieMul]\edef\PfMTiti{\div\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{+}{\StrBehind{\PfM@ListeOp[1]}{+}[\PfMPartieMul]\edef\PfMTiti{+\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{-}{\StrBehind{\PfM@ListeOp[1]}{-}[\PfMPartieMul]\edef\PfMTiti{-\num{\PfMPartieMul}}}{}%
                  \begin{tikzpicture}[remember picture,overlay]%
                    \draw[-stealth,out=30,in=150,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[midway,above]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.north);%
                  \end{tikzpicture}%
                }%
              }%
            }%
          }{%
%            une seule et unique opération : \PfM@ListeOp[1]
            \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
              \IfSubStr{\PfM@ListeOp[1]}{*}{\StrBehind{\PfM@ListeOp[1]}{*}[\PfMPartieMul]\edef\PfMTiti{\times\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{/}{\StrBehind{\PfM@ListeOp[1]}{/}[\PfMPartieMul]\edef\PfMTiti{\div\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{+}{\StrBehind{\PfM@ListeOp[1]}{+}[\PfMPartieMul]\edef\PfMTiti{+\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{-}{\StrBehind{\PfM@ListeOp[1]}{-}[\PfMPartieMul]\edef\PfMTiti{-\num{\PfMPartieMul}}}{}%
              \begin{tikzpicture}[remember picture,overlay]
                \draw[-stealth,out=30,in=150,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[midway,above]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.north);%
              \end{tikzpicture}%
            }%
          }%
        \else%
          \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
            \modulo{##4}{\PfM@ListeOplen}%
            \edef\PfM@Test{\PfM@ListeOp[\fpeval{\the\remainder+1}]}%
            \IfSubStr{\PfM@Test}{*}{\StrBehind{\PfM@Test}{*}[\PfMPartieMul]\edef\PfMTiti{\times\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@Test}{/}{\StrBehind{\PfM@Test}{/}[\PfMPartieMul]\edef\PfMTiti{\div\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@Test}{+}{\StrBehind{\PfM@Test}{+}[\PfMPartieMul]\edef\PfMTiti{+\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@Test}{-}{\StrBehind{\PfM@Test}{-}[\PfMPartieMul]\edef\PfMTiti{-\num{\PfMPartieMul}}}{}%
            \begin{tikzpicture}[remember picture,overlay]
              \draw[-stealth,out=30,in=150,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[midway,above]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.north);%
            \end{tikzpicture}%
          }%
        \fi%
      }{}%
    }%
  }%
}%
\makeatother