Re: Control break pattern
Roedy Green <see_website@mindprod.com.invalid> wrote:
You have a list sorted by some category. You want to do something
special prior to processes sing the first of a string of dups and
something special after.
Since at seeing a so-far-unique item you cannot yet foresee if
it is the start of a group or a singleton, you need to delay the
processing of each item:
e.g.:
int[] some_ints={1,2,3,3,4}; int aux;
boolean group=false, notfirst=false;
for (int cur: some_ints) {
if (group) {
process(aux); if (aux!=cur) { endGroup(); group=false; }
} else if (notfirst) {
if (aux==cur) {group=true; startGroup(); } process(aux);
}
aux=cur; notfirst=true;
}
process(aux); if (group) { endGroup(); }
If your definition of "dups" is less strict, and includes
singleton items, then it's a bit simpler: (same vars as before)
startGroup();
for(int cur: some_ints) {
if (notfirst) { if (aux!=cur) { endGroup(); startGroup(); } }
notfirst=true; aux=cur; process(cur);
}
endGroup();
Then there is the generalisation, the control break where you have
several levels of keys.
I guess, the first approach can be extended:
int[] some_ints={1,2,3,3,4}; int aux;
boolean group1=false,group2=false,..., first=true;
for (int cur: some_ints) {
if (first) { aux=cur; first=false; continue; }
//repeat for N=1,2,...
if (groupN) {
if (!keymatchN(aux,cur)) { groupN=false; prepareEndGroupN(); }
} else {
if (keymatchN(aux,cur)) { groupN=true; prepareStartGroupN(); }
}
//end-repeat
doAllPreparedStartGroups(); // emit all the group starts
process(aux);
doAllPreparedEndGroups(); // emit all the group ends
aux=cur;
}
If there are several levels of keys, I wonder how the list can
be sorted by all of them at the same time, though, so probably
it will just group together the other keys, if two items subsequent
by the sorted key happen to have an equal other key.
These start-end-groups are then not necessarily properly
nesting, and I don't see how this would really work out in xml.