Recently we had a bug that was caught during the review where the
isLeader check was mistakenly omitted from the usual pattern of
traversing an EC:
for (EquivalenceClasses<int>::iterator I = EC.begin(), E = EC.end(); I != E; ++I) { if (!I->isLeader()) continue // <----------- this line for (EquivalenceClasses<int>::member_iterator MI = EC.member_begin(I); MI != EC.member_end(); ++MI) ...
I don't think that anything bad really happens if the check is omitted
other than some overhead but that made me think if we could have an
iterator in the outer loop that only traverses the leaders/classes:
for (EquivalenceClasses<int>::leader_iterator I = EC.leader_begin(), E = EC.leader_end(); I != E; ++I) { for (EquivalenceClasses<int>::member_iterator MI = EC.member_begin(I); MI != EC.member_end(); ++MI) ...
EC internally uses a set, so leader_iterator is built on top of the
set::iterator. In addition, it also needs the end iterator to avoid
over-running the set when looking for the leader.
Let me know if this seems like a good idea and then I will update the
patch with unit tests.