Angus Leeming <[EMAIL PROTECTED]> writes: | Amazing what happens if you dig deeper > | aleem@pneumon:src-> grep "counters()" *.C */*.C | grep copy | text2.C: par->counters().copy(par->previous()->counters(), | par->counters(), ""); | aleem@pneumon:src-> grep "counters()" *.C */*.C | grep reset | text2.C: par->counters().reset(""); | text2.C: par->counters().reset(""); | text2.C: par->counters().reset("enum"); | text2.C: par->counters().reset("enum"); > | So, Counters::copy() is used only as | Counters::copy(Counters & from, Counters & to); > | and can therefore be simplified to > | void Counters::copy(Counters & from, Counters & to) | { | CounterList::iterator it = counterList.begin(); | CounterList::iterator end = counterList.end(); | for (; it != end; ++it) { | to.set(it->first, from.value(it->first)); | } | } > > | whilst Counters::reset should be written as > | void Counters::reset(string const & match) | { | CounterList::iterator it = counterList.begin(); | CounterList::iterator end = counterList.end(); | if (match.empty()) { | for (; it != end; ++it) { | it->second.reset(); | } | } else { | // reset all counters whose index contains match as a | // sub-string | for (; it != end; ++it) { | if (it->first.find(match) != string::npos) | it->second.reset(); | } | } | }
Hmm I wonder if this reset function shouldn't be split into two functions... void Counters::reset() { CounterList::iterator it = counterList.begin(); CounterList::iterator end = counterList.end(); for (; it != end; ++it) { it->second.reset(); } } Which could be simplified... something similar to: void Counters::reset() { std::for_each(counterList.begin(), counterList.end(), std::memfun(&Counter::reset)); } and void Counters::reset(string const & match) { assert(!match.empty()); CounterList::iterator it = counterList.begin(); CounterList::iterator end = counterList.end(); for (; it != end; ++it) { if (it->first.find(match) != string::npos) it->second.reset(); } } Functions that do only one thing and that does not decide what to do based on the value of arguments are a good thing. -- Lgb