Source of ArrayBag.h


  1: //  Created by Frank M. Carrano and Tim Henry.
  2: //  Copyright (c) 2013 __Pearson Education__. All rights reserved.

  4: /** Header file for an array-based implementation of the ADT bag.
  5:  @file ArrayBag.h */

  7: #ifndef _ARRAY_BAG
  8: #define _ARRAY_BAG

 10: #include "BagInterface.h"

 12: template<class ItemType>
 13: class ArrayBag : public BagInterface<ItemType>
 14: {
 15: private:
 16:         static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
 17:         ItemType items[DEFAULT_CAPACITY];      // Array of bag items
 18:    int itemCount;                         // Current count of bag items 
 19:    int maxItems;                          // Max capacity of the bag
 20:    
 21:    // Returns either the index of the element in the array items that
 22:    // contains the given target or -1, if the array does not contain 
 23:    // the target.
 24:    int getIndexOf(const ItemType& target) const;   

 26: public:
 27:         ArrayBag();
 28:         int getCurrentSize() const;
 29:         bool isEmpty() const;
 30:         bool add(const ItemType& newEntry);
 31:         bool remove(const ItemType& anEntry);
 32:         void clear();
 33:         bool contains(const ItemType& anEntry) const;
 34:         int getFrequencyOf(const ItemType& anEntry) const;
 35:    vector<ItemType> toVector() const;
 36: }; // end ArrayBag

 38: #include "ArrayBag.cpp"
 39: #endif