%%
%% This is file `texsort.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% texsort.dtx  (with options: `package')
%% 
%% Copyright (C) 2000-2021 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
%% UAB VTeX, Mokslininkų 2A, LT-08412 Vilnius, Lithuania
%% --------------------------------------------------------------------------
%% 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.
%%
%% PURPOSE:   Sort/compress numerical lists
%%
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{texsort}
    [2012/05/29 v1.1 Sort/compress alphanumerical list]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{texsort}[2012/05/29]
\newcount\t@mp@r@a
\newcount\t@mp@r@b
\newcount\t@mp@r@c
\def\initarray#1#2{%
  \bgroup
    \@tempcnta=\z@
    \@for\@@refb:=#2\do{%
      \advance\@tempcnta by\@ne
      \bgroup
        \setbox\z@\hbox{%
          \expandafter\global\expandafter\t@mp@r@a\expandafter\add@zero\@@refb\relax
          }%
        \ifdim\wd0>\z@\relax
           \@latex@warning{Not number \@@refb!}%
        \fi
        \expandafter\edef\csname @a@\endcsname{\expandafter\xdef\expandafter
          \noexpand\csname#1@\the\@tempcnta\endcsname{\the\t@mp@r@a}}\@a@
      \egroup
    }%
    \expandafter\xdef\csname #1@lenght\endcsname{\the\@tempcnta}%
  \egroup
  }
\def\outarray#1\sep#2{%
  \bgroup
    \@tempcnta=\z@\relax
    \@tempcntb=\getarraylenght{#1}%
    \loop
    \ifnum\@tempcnta<\@tempcntb
      \advance\@tempcnta by\@ne\relax
      \ifnum\@tempcnta>\@ne\relax#2\fi
      \csname#1@\the\@tempcnta\endcsname
    \repeat
  \egroup
  }
\def\add@zero#1{0#1}
\def\getarrayitem#1#2\to#3{\expandafter\xdef\csname#3\endcsname{\csname#1@#2\endcsname}}
\def\setarrayitem#1#2#3{%
  \bgroup
    \setbox0\hbox{%
      \expandafter\global\expandafter\t@mp@r@a\expandafter\add@zero#3\relax
      }%
    \ifdim\wd0>\z@\relax
      \@latex@warning{Not number #3!}%
    \fi
    \expandafter\xdef\csname#1@#2\endcsname{\the\t@mp@r@a}%
  \egroup
  }
\def\getarraylenght#1{\@ifundefined{#1@lenght}{0}{\csname #1@lenght\endcsname}}
\def\setarraylenght#1#2{\expandafter\xdef\csname #1@lenght\endcsname{#2}}
\def\upheap#1#2{%
  \bgroup
    \getarrayitem{#1}{#2}\to{vara}%
    \@namedef{#1@0}{\maxdimen}%
    \@tempcntb=#2\relax
    \loop
    \@tempcnta=\@tempcntb
    \divide\@tempcnta by2\getarrayitem{#1}{\the\@tempcnta}\to{varb}%
    \ifnum\varb>\vara\relax
    \else
      \getarrayitem{#1}{\the\@tempcnta}\to{varb}%
      \setarrayitem{#1}{\the\@tempcntb}{\varb}%
      \divide\@tempcntb by2\relax
    \repeat
    \setarrayitem{#1}{\the\@tempcntb}{\vara}%
  \egroup
  }
\def\insertheapelem#1#2{%
  \bgroup
    \@tempcnta=\getarraylenght{#1}\relax
    \advance\@tempcnta by1\relax
    \setarraylenght{#1}{\the\@tempcnta}\relax
    \setarrayitem{#1}{\the\@tempcnta}{#2}%
    \upheap{#1}{\the\@tempcnta}%
  \egroup
  }
\newcount\@tempcntd
\def\downheap#1#2{%
  \bgroup
    \getarrayitem{#1}{#2}\to{vara}%
    \@tempcntb=\getarraylenght{#1}%
    \t@mp@r@c=\@tempcntb\relax
    \divide\@tempcntb by2\relax
    \t@mp@r@b=\@tempcntb\relax
    \@tempcntb=#2\relax
    \ifnum\@tempcntb>\t@mp@r@b\relax
    \else
      \loop
      \@tempcnta=\@tempcntb \advance\@tempcnta by\@tempcntb\relax
      \@tempcntd=\@tempcnta \advance\@tempcntd by1\relax
      \ifnum\@tempcnta<\t@mp@r@c\relax
        \getarrayitem{#1}{\the\@tempcnta}\to{temp@@a}%
        \getarrayitem{#1}{\the\@tempcntd}\to{temp@@b}%
        \ifnum\temp@@a<\temp@@b\relax
          \@tempcnta=\@tempcntd
        \fi
      \fi
      \getarrayitem{#1}{\the\@tempcnta}\to{temp@@a}%
      \ifnum\vara>\temp@@a\relax
        \t@mp@r@b=-1\relax
      \else
        \getarrayitem{#1}{\the\@tempcnta}\to{temp@@a}%
        \setarrayitem{#1}{\the\@tempcntb}{\temp@@a}%
        \@tempcntb=\@tempcnta
      \fi
      \ifnum\@tempcntb>\t@mp@r@b\relax
      \else
      \repeat
    \fi
    \setarrayitem{#1}{\the\@tempcntb}{\vara}%
  \egroup
  }

\def\removetop#1\to#2{%
  \bgroup
    \getarrayitem{#1}{1}\to{temp@@c}%
    \expandafter\xdef\csname #2\endcsname{\temp@@c}%
    \@tempcnta=\getarraylenght{#1}%
    \getarrayitem{#1}{\the\@tempcnta}\to{temp@@a}%
    \setarrayitem{#1}{1}{\temp@@a}%
    \setarrayitem{#1}{\the\@tempcnta}{}%
    \advance\@tempcnta by-1\relax
    \setarraylenght{#1}{\the\@tempcnta}%
    \downheap{#1}{1}%
  \egroup
  }
\long\def\g@addto@macrobeg#1#2{%
  \begingroup
    \def\@a@{#2}%
    \toks@\expandafter\expandafter\expandafter{\expandafter\@a@#1}%
    \xdef#1{\the\toks@}%
  \endgroup
  }
\def\sortlistarray#1\to#2{%
  \bgroup
    \@for\@@refb:=#1\do{%
      \insertheapelem{@tempa@}{\@@refb}%
      }%
    \@tempcntb=\getarraylenght{@tempa@}%
    \setarraylenght{#2}{\the\@tempcntb}%
    \@for\@@refb:=#1\do{%
      \removetop{@tempa@}\to{aka}%
      \setarrayitem{#2}{\the\@tempcntb}{\aka}%
      \advance\@tempcntb by-1\relax
      }%
  \egroup
  }
\def\sortlist#1\to#2\sep#3{%
  \bgroup
    \@for\@@refb:=#1\do{%
      \insertheapelem{@tempa@}{\@@refb}%
      }%
    \@tempcntb=\getarraylenght{@tempa@}%
    \expandafter\def\csname#2\endcsname{}%
    \expandafter\def\expandafter\@a@\expandafter{\csname#2\endcsname}%
    \@for\@@refb:=#1\do{%
      \removetop{@tempa@}\to{aka}%
      \advance\@tempcntb by-1\relax
      \expandafter\expandafter\expandafter
        \g@addto@macrobeg\expandafter\@a@\expandafter{\aka}%
      \ifnum\@tempcntb>0\relax
        \expandafter\expandafter\expandafter
          \g@addto@macrobeg\expandafter\@a@\expandafter{#3}%
      \fi
      }%
  \egroup
  }
\def\compresslist#1\to#2\sep#3#4{%
  \bgroup
    \expandafter\def\csname#2\endcsname{}%
    \expandafter\def\expandafter\@a@\expandafter{\csname#2\endcsname}%
    \t@mp@r@a=-1\relax
    \@tempcnta=-2\relax
    \expandafter\@for\expandafter\@@refb\expandafter:\expandafter=#1\do{%
       \ifnum\t@mp@r@a=-1\relax
         \t@mp@r@a=\@@refb\relax
         \t@mp@r@b=\@@refb\relax
         \t@mp@r@c=\@@refb\relax
         \expandafter\expandafter\expandafter
           \g@addto@macro\expandafter\@a@\expandafter{\the\t@mp@r@a}%
       \else
         \t@mp@r@c=\@@refb\relax
         \ifnum\t@mp@r@c=\t@mp@r@a\relax
           \expandafter\expandafter\expandafter
             \g@addto@macro\expandafter\@a@\expandafter{#3}%
           \expandafter\expandafter\expandafter
             \g@addto@macro\expandafter\@a@\expandafter{\the\t@mp@r@c}%
           \t@mp@r@b=\t@mp@r@c\relax
         \else
           \@tempcnta=\t@mp@r@b\relax
           \advance\@tempcnta by1\relax
           \ifnum\@tempcnta=\t@mp@r@c\relax
             \t@mp@r@b=\t@mp@r@c\relax
           \else
             \@tempcnta=\t@mp@r@a\relax
             \advance\@tempcnta by-\t@mp@r@b\relax
             \ifnum\@tempcnta<-1\relax
               \expandafter\expandafter\expandafter
                 \g@addto@macro\expandafter\@a@\expandafter{#4}%
               \expandafter\expandafter\expandafter
                 \g@addto@macro\expandafter\@a@\expandafter{\the\t@mp@r@b}%
               \expandafter\expandafter\expandafter
                 \g@addto@macro\expandafter\@a@\expandafter{#3}%
               \expandafter\expandafter\expandafter
                 \g@addto@macro\expandafter\@a@\expandafter{\the\t@mp@r@c}%
             \else
               \ifnum\t@mp@r@a<\t@mp@r@b
                 \expandafter\expandafter\expandafter
                   \g@addto@macro\expandafter\@a@\expandafter{#3}%
                 \expandafter\expandafter\expandafter
                   \g@addto@macro\expandafter\@a@\expandafter{\the\t@mp@r@b}%
               \fi
               \expandafter\expandafter\expandafter
                 \g@addto@macro\expandafter\@a@\expandafter{#3}%
               \expandafter\expandafter\expandafter
                 \g@addto@macro\expandafter\@a@\expandafter{\the\t@mp@r@c}%
             \fi
             \t@mp@r@a=\t@mp@r@c\relax
             \t@mp@r@b=\t@mp@r@c\relax
           \fi
         \fi
       \fi
       }%
    \ifnum\t@mp@r@a<\t@mp@r@c\relax
      \advance\t@mp@r@a by-\t@mp@r@c\relax
      \ifnum\t@mp@r@a<-1\relax
        \expandafter\expandafter\expandafter
          \g@addto@macro\expandafter\@a@\expandafter{#4}%
      \else
        \expandafter\expandafter\expandafter
          \g@addto@macro\expandafter\@a@\expandafter{#3}%
      \fi
      \expandafter\expandafter\expandafter
        \g@addto@macro\expandafter\@a@\expandafter{\the\t@mp@r@c}%
    \fi
  \egroup
  }
\endinput
%%
%% End of file `texsort.sty'.
