From 17dc12732cf140a0a72bd3c3e4325206ab4039ae Mon Sep 17 00:00:00 2001 From: daniel Date: Sun, 23 Apr 2017 20:30:15 -0400 Subject: [PATCH] Make wire history better --- src/Wire.cpp | 18 +++++++++++------- src/Wire.h | 5 +++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Wire.cpp b/src/Wire.cpp index 4b9bbb5..3dcb8ac 100644 --- a/src/Wire.cpp +++ b/src/Wire.cpp @@ -7,22 +7,26 @@ Wire::Wire(int number, bool io, string inName) name = inName; value = -1; lastEvent = 0; - history.insert(history.begin(), 60, -1); } -int Wire::getValue(int time) const +int Wire::getValue(int wantedTime) const { - for (auto i = history.begin() + time; i != history.begin(); i--) { - if (i[0] != -1) { - return i[0]; + if (historyTimes.size() == 0 || wantedTime < historyTimes[0]) { + return -1; + } + else { + for (auto i = historyTimes.begin(); i != historyTimes.end(); i++) { + if (*i > wantedTime) { + return historyValues[i - 1 - historyTimes.begin()]; + } } } - return -1; } void Wire::setValue(int newValue, int setTime) { - history[setTime] = newValue; + historyTimes.push_back(setTime); + historyValues.push_back(newValue); if (lastEvent < setTime) { lastEvent = setTime; } diff --git a/src/Wire.h b/src/Wire.h index 4ea5b86..3434540 100644 --- a/src/Wire.h +++ b/src/Wire.h @@ -15,7 +15,7 @@ class Wire { void addGate(Gate* newGate); - int getValue(int time) const; + int getValue(int wantedTime) const; int getNumber() const; string getName() const; @@ -23,7 +23,8 @@ class Wire { private: int WireNumber, value, lastEvent; - vector history; + vector historyTimes; + vector historyValues; string name; bool isPrint; vector gates;