1: // Created by Frank M. Carrano and Tim Henry. 2: // Copyright (c) 2013 __Pearson Education__. All rights reserved. 4: /** Implementation file for the class ArrayBag. 5: @file ArrayBag.cpp */ 7: #include "ArrayBag.h" 8: #include <cstddef> 10: template<class ItemType> 11: ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY) 12: { 13: } // end default constructor 15: template<class ItemType> 16: int ArrayBag<ItemType>::getCurrentSize() const 17: { 18: return itemCount; 19: } // end getCurrentSize 21: template<class ItemType> 22: bool ArrayBag<ItemType>::isEmpty() const 23: { 24: return itemCount == 0; 25: } // end isEmpty 27: template<class ItemType> 28: bool ArrayBag<ItemType>::add(const ItemType& newEntry) 29: { 30: bool hasRoomToAdd = (itemCount < maxItems); 31: if (hasRoomToAdd) 32: { 33: items[itemCount] = newEntry; 34: itemCount++; 35: } // end if 36: 37: return hasRoomToAdd; 38: } // end add 40: /* 41: // STUB 42: template<class ItemType> 43: bool ArrayBag<ItemType>::remove(const ItemType& anEntry) 44: { 45: return false; // STUB 46: } // end remove 47: */ 48: 49: template<class ItemType> 50: bool ArrayBag<ItemType>::remove(const ItemType& anEntry) 51: { 52: int locatedIndex = getIndexOf(anEntry); 53: bool canRemoveItem = !isEmpty() && (locatedIndex > -1); 54: if (canRemoveItem) 55: { 56: itemCount--; 57: items[locatedIndex] = items[itemCount]; 58: } // end if 59: 60: return canRemoveItem; 61: } // end remove 63: /* 64: // STUB 65: template<class ItemType> 66: void ArrayBag<ItemType>::clear() 67: { 68: // STUB 69: } // end clear 70: */ 72: template<class ItemType> 73: void ArrayBag<ItemType>::clear() 74: { 75: itemCount = 0; 76: } // end clear 78: template<class ItemType> 79: int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const 80: { 81: int frequency = 0; 82: int curIndex = 0; // Current array index 83: while (curIndex < itemCount) 84: { 85: if (items[curIndex] == anEntry) 86: { 87: frequency++; 88: } // end if 89: 90: curIndex++; // Increment to next entry 91: } // end while 92: 93: return frequency; 94: } // end getFrequencyOf 96: template<class ItemType> 97: bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const 98: { 99: return getIndexOf(anEntry) > -1; 100: } // end contains 102: /* ALTERNATE 1: First version 103: template<class ItemType> 104: bool ArrayBag<ItemType>::contains(const ItemType& target) const 105: { 106: return getFrequencyOf(target) > 0; 107: } // end contains 109: // ALTERNATE 2: Second version 110: template<class ItemType> 111: bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const 112: { 113: bool found = false; 114: int curIndex = 0; // Current array index 115: while (!found && (curIndex < itemCount)) 116: { 117: if (anEntry == items[curIndex]) 118: { 119: found = true; 120: } // end if 121: 122: curIndex++; // Increment to next entry 123: } // end while 124: 125: return found; 126: } // end contains 127: */ 129: template<class ItemType> 130: vector<ItemType> ArrayBag<ItemType>::toVector() const 131: { 132: vector<ItemType> bagContents; 133: for (int i = 0; i < itemCount; i++) 134: bagContents.push_back(items[i]); 135: 136: return bagContents; 137: } // end toVector 139: // private 140: template<class ItemType> 141: int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const 142: { 143: bool found = false; 144: int result = -1; 145: int searchIndex = 0; 146: 147: // If the bag is empty, itemCount is zero, so loop is skipped 148: while (!found && (searchIndex < itemCount)) 149: { 150: if (items[searchIndex] == target) 151: { 152: found = true; 153: result = searchIndex; 154: } 155: else 156: { 157: searchIndex++; 158: } // end if 159: } // end while 160: 161: return result; 162: } // end getIndexOf