- class IntBag {
- private:
- enum { MAXVALUES = 10 };
- int data [MAXVALUES];
- int count; // number of values currently in the bag
- bool walkInProgress;
- int walkPosition; // position of last value returned
- public:
- // constructs an empty bag.
- // for really trivial constructors and methods, the complete
- // implementation is often placed directly in the class definition.
- IntBag () { count = 0; walkInProgress = false; }
- // adds a value to the bag. returns true on success (space available)
- // and false on failure (bag full)
- bool add (int value);
- // removes one occurrence of the specified value from the bag. returns
- // true on success (value existed) and false on failure (value did not exist)
- bool remove (int value);
- // returns the number of times that the specified value occurs in the bag
- int countOccurrences (int value) const;
- // returns the number of values in the bag.
- int size () const { return count; }
- // These two methods make it possible to "walk through" the values in a
- // bag. The values in the bag may be returned in any order. There is no
- // guarantee that they will be returned in numerical order, in the order in
- // which they were entered, or any other particular order. "startWalk" may
- // be called at any time. If returns true on success (value obtained) and
- // false on failure (bag empty). "continueWalk" may only be called if
- // 1/. the bag has not been modified since a walk was
- // begun by calling startWalk AND
- // 2/. the last call to startWalk or ContinueWalk returned true
- // If these conditions are not met, "continueWalk" throws a "range_error"
- // exception. It returns true on success (value obtained) and false on
- // failure (no more values).
- bool startWalk (int &value);
- bool continueWalk (int &value);
- // randomly picks and returns one of the values in the bag.
- // throws a range_error exception if the bag is empty.
- int pickRandom () const;
- };