some refactoring; use C++11 foreach

This commit is contained in:
Christopher Mosher 2013-12-13 23:06:13 -05:00
parent 4f6338422f
commit 70d040a3aa
3 changed files with 20 additions and 18 deletions

View File

@ -28,8 +28,8 @@ public:
std::set<Segment*> all() { std::set<Segment*> all() {
std::set<Segment*> s; std::set<Segment*> s;
for (std::map<const std::string, std::shared_ptr<Segment > >::const_iterator i = cache.begin(); i != cache.end(); ++i) { for (auto i : cache) {
s.insert(i->second.get()); s.insert(i.second.get());
} }
return s; return s;
} }

View File

@ -25,7 +25,7 @@ StateCalculator::StateCalculator(Segment* VSS, Segment* VCC) : VSS(VSS), VCC(VCC
void StateCalculator::recalc(Segment* seg, Segment* VSS, Segment* VCC) { void StateCalculator::recalc(Segment* seg, Segment* VSS, Segment* VCC) {
std::set<Segment*> rSeg; std::set<Segment*> rSeg;
rSeg.insert(seg); rSeg.insert(seg);
recalc(rSeg,VSS,VCC); recalc(rSeg, VSS, VCC);
} }
/* /*
@ -36,20 +36,20 @@ void StateCalculator::recalc(Segment* seg, Segment* VSS, Segment* VCC) {
*/ */
#define SANE (100) #define SANE (100)
void StateCalculator::recalc(const std::set<Segment*>& rSeg, Segment* VSS, Segment* VCC) { void StateCalculator::recalc(const std::set<Segment*>& segs, Segment* VSS, Segment* VCC) {
int sanity(0); int sanity(0);
std::set<Segment*> riSegRecalc(rSeg); std::set<Segment*> changed(segs);
while (!riSegRecalc.empty()) { while (!changed.empty()) {
if (++sanity >= SANE) { if (++sanity >= SANE) {
throw "ERROR: reached maximum iteration limit while recalculating CPU state"; throw "ERROR: reached maximum iteration limit while recalculating CPU state";
} }
StateCalculator c(VSS, VCC); StateCalculator c(VSS, VCC);
for (std::set<Segment*>::const_iterator is = riSegRecalc.begin(); is != riSegRecalc.end(); ++is) { for (auto s : changed) {
c.recalcNode(*is); c.recalcNode(s);
} }
riSegRecalc = c.getChanged(); changed = c.getChanged();
} }
} }
@ -64,16 +64,17 @@ void StateCalculator::recalc(const std::set<Segment*>& rSeg, Segment* VSS, Segme
void StateCalculator::recalcNode(Segment* seg) { void StateCalculator::recalcNode(Segment* seg) {
if (!(seg == this->VSS || seg == this->VCC)) { if (!(seg == this->VSS || seg == this->VCC)) {
Circuit c(seg, this->VSS, this->VCC); Circuit c(seg, this->VSS, this->VCC);
for (auto s : c) {
setSeg(s, c.getValue());
}
}
}
for (std::set<Segment*>::iterator is = c.begin(); is != c.end(); ++is) { void StateCalculator::setSeg(Segment* s, const bool on) {
Segment * s(*is); if (s->on != on) {
if (s->on != c.getValue()) { s->on = on;
s->on = c.getValue(); for (auto t : s->gates) {
for (std::set<Trans*>::iterator it = s->gates.begin(); it != s->gates.end(); ++it) { setTrans(t, on);
Trans * t(*it);
setTrans(t, c.getValue());
}
}
} }
} }
} }

View File

@ -32,6 +32,7 @@ private:
StateCalculator& operator=(const StateCalculator&); StateCalculator& operator=(const StateCalculator&);
void recalcNode(Segment* seg); void recalcNode(Segment* seg);
void setSeg(Segment* s, const bool on);
void setTrans(Trans* t, const bool on); void setTrans(Trans* t, const bool on);
void addRecalc(Segment* seg); void addRecalc(Segment* seg);