#include "stdstuff.h" #include "IntSet.h" // adds a value to the set. returns true on success (space available) // and false on failure (set full) bool IntSet::add (int value) { if (count == MAXVALUES) return false; data[count++] = value; walkInProgress = false; // scrub any ongoing walk return true; } // removes the specfied value from the set. returns // true on success (value existed) and false on failure (value did not exist) bool IntSet::remove (int value) { int i; for (i = 0; i < count; i++) { if (data[i] == value) { // we've found the data value. // overwrite it with the last data value and adjust the count // to reflect the fact that there are now one fewer values in // the set. note that this works even if the value being removed // is the last value in the set. data[i] = data[--count]; walkInProgress = false; // scrub any ongoing walk return true; } } return false; } // returns the number of times that the specified value occurs in the set int IntSet::countOccurrences (int value) const { int i, occurrences = 0; for (i = 0; i < count; i++) { if (data[i] == value) { occurrences++; } } return occurrences; } bool IntSet::startWalk (int &value) { if (count == 0) return false; walkInProgress = true; walkPosition = 0; value = data[walkPosition]; return true; } bool IntSet::continueWalk (int &value) { if (!walkInProgress) { throw range_error ("IntSet::continueWalk invalid call"); } if (++walkPosition == count) { walkInProgress = false; // we've come to the end of the road return false; } value = data[walkPosition]; return true; }