| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
% |
| 3 |
% This is a helper package with an elementary double-ended-queue |
| 4 |
% (deque) datastructure, |
| 5 |
% featuring O(1) index access and O(N) creation, deletion, copy. |
| 6 |
% |
| 7 |
% The following macros are supplied: |
| 8 |
% |
| 9 |
% \pgfplotsdequenewempty |
| 10 |
% \pgfplotsdequecopy |
| 11 |
% \pgfplotsdequepushback |
| 12 |
% \pgfplotsdequepopfront |
| 13 |
% \pgfplotsdequecheckempty |
| 14 |
% \pgfplotsdequeforeach |
| 15 |
% |
| 16 |
% Copyright 2007/2008 by Christian Feuersänger. |
| 17 |
% |
| 18 |
% This program is free software: you can redistribute it and/or modify |
| 19 |
% it under the terms of the GNU General Public License as published by |
| 20 |
% the Free Software Foundation, either version 3 of the License, or |
| 21 |
% (at your option) any later version. |
| 22 |
% |
| 23 |
% This program is distributed in the hope that it will be useful, |
| 24 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 |
% GNU General Public License for more details. |
| 27 |
% |
| 28 |
% You should have received a copy of the GNU General Public License |
| 29 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 30 |
% |
| 31 |
% |
| 32 |
% |
| 33 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 34 |
|
| 35 |
% IMPLEMENTATION NOTES: |
| 36 |
% I allocate an array which provides storage capacity and two pointers |
| 37 |
% (indices) into that array. |
| 38 |
% |
| 39 |
% The deque is stored as |
| 40 |
% - an array: \csname pgfpldq@<name>\endcsname |
| 41 |
% - the pointers: |
| 42 |
% \csname pgfpldq@<name>@beg\endcsname |
| 43 |
% \csname pgfpldq@<name>@end\endcsname (one after the end) |
| 44 |
% |
| 45 |
% ATTENTION: <name> may NOT be a control sequence! |
| 46 |
% this is in contrast to my other container structures. |
| 47 |
|
| 48 |
|
| 49 |
% Allocates a new, empty deque #1. |
| 50 |
% |
| 51 |
% #1 the name of a deque (NO control sequence!) |
| 52 |
% #2 is an integer denoting the maximum capacity. For now, this |
| 53 |
% capacity is fixed afterwards and denotes the maximum number of |
| 54 |
% entries. |
| 55 |
\def\pgfplotsdequenewempty#1{% |
| 56 |
\pgfutil@ifnextchar\capacity{% |
| 57 |
\pgfplotsdequenewempty@{#1}% |
| 58 |
}{% |
| 59 |
\pgfplots@error{Expected \string\capacity\{the capacity\} after \string\pgfplotsdequenewempty...}% |
| 60 |
\pgfplotsdequenewempty@{#1}\capacity{100}% |
| 61 |
}% |
| 62 |
}% |
| 63 |
\def\pgfplotsdequenewempty@#1\capacity#2{% |
| 64 |
\pgfplotsarrayresize{pgfpldq@#1}{#2}% |
| 65 |
\pgfutil@namedef{pgfpldq@#1@beg}{0}% |
| 66 |
\pgfutil@namedef{pgfpldq@#1@end}{0}% |
| 67 |
}% |
| 68 |
|
| 69 |
\def\pgfplotsdequecopy#1\to#2{% |
| 70 |
\pgfutil@namelet{pgfpldq@#1@beg}{pgfpldq@#2@beg}% |
| 71 |
\pgfutil@namelet{pgfpldq@#1@end}{pgfpldq@#2@end}% |
| 72 |
\pgfplotsarraycopy{pgfpldq@#1}\to{pgfpldq@#2}% |
| 73 |
}% |
| 74 |
|
| 75 |
\def\pgfplotsdequepushback#1\to#2{% |
| 76 |
\pgfplotsarrayset{\csname pgfpldq@#2@end\endcsname}\of{pgfpldq@#2}\to{#1}% |
| 77 |
\c@pgfplotsarray@tmp=\csname pgfpldq@#2@end\endcsname |
| 78 |
\advance\c@pgfplotsarray@tmp by1 |
| 79 |
\ifnum\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{pgfpldq@#2} |
| 80 |
\c@pgfplotsarray@tmp=0 |
| 81 |
\fi |
| 82 |
\expandafter\edef\csname pgfpldq@#2@end\endcsname{\the\c@pgfplotsarray@tmp}% |
| 83 |
\pgfplotsdequeifempty{#2}{\pgfplots@error{Error: \string\pgfplotsdeque\space capacity has been reached - it was too small. Sorry, I didn't write auto-enlargement...}}{}% |
| 84 |
}% |
| 85 |
|
| 86 |
\def\pgfplotsdequepopfront#1\to#2{% |
| 87 |
\pgfplotsarrayselect{\csname pgfpldq@#1@beg\endcsname}\of{pgfpldq@#1}\to{#2}% |
| 88 |
\c@pgfplotsarray@tmp=\csname pgfpldq@#1@beg\endcsname |
| 89 |
\advance\c@pgfplotsarray@tmp by1 |
| 90 |
\ifnum\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{pgfpldq@#1} |
| 91 |
\c@pgfplotsarray@tmp=0 |
| 92 |
\fi |
| 93 |
\expandafter\edef\csname pgfpldq@#1@beg\endcsname{\the\c@pgfplotsarray@tmp}% |
| 94 |
}% |
| 95 |
|
| 96 |
% invokes #2 if deque '#1' is empty and '#3' if it is not empty. |
| 97 |
\def\pgfplotsdequeifempty#1#2#3{% |
| 98 |
\ifnum\csname pgfpldq@#1@beg\endcsname=\csname pgfpldq@#1@end\endcsname\relax |
| 99 |
#2% |
| 100 |
\else |
| 101 |
#3% |
| 102 |
\fi |
| 103 |
}% |
| 104 |
|
| 105 |
|
| 106 |
\long\def\pgfplotsdequeforeach#1\as#2#3{% |
| 107 |
\c@pgfplotsarray@tmp=\csname pgfpldq@#1@beg\endcsname |
| 108 |
\long\def\pgfplotsdequeforeach@next{\pgfplotsdequeforeach@iter{#1}{#2}{#3}}% |
| 109 |
\pgfplotsdequeforeach@next |
| 110 |
}% |
| 111 |
\long\def\pgfplotsdequeforeach@iter#1#2#3{% |
| 112 |
\ifnum\c@pgfplotsarray@tmp=\csname pgfpldq@#1@end\endcsname |
| 113 |
\def\pgfplotsdequeforeach@next{}% |
| 114 |
\else |
| 115 |
\pgfplotsarrayselect\c@pgfplotsarray@tmp\of pgfpldq@#1\to#2% |
| 116 |
\edef\pgfplotsdequeforeach@{\the\c@pgfplotsarray@tmp}% |
| 117 |
#3\relax |
| 118 |
\c@pgfplotsarray@tmp=\pgfplotsdequeforeach@ |
| 119 |
\advance\c@pgfplotsarray@tmp by1 |
| 120 |
\ifnum\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{pgfpldq@#1} |
| 121 |
\c@pgfplotsarray@tmp=0 |
| 122 |
\fi |
| 123 |
\fi |
| 124 |
\pgfplotsdequeforeach@next |
| 125 |
}% |