1: // Created by Frank M. Carrano and Tim Henry. 2: // Copyright (c) 2013 __Pearson Education__. All rights reserved. 4: /** Implementation file for the class ArrayList. 5: @file ArrayList.cpp */ 7: #include "ArrayList.h" // Header file 9: template<class ItemType> 10: ArrayList<ItemType>::ArrayList() : itemCount(0), maxItems(DEFAULT_CAPACITY) 11: { 12: } // end default constructor 14: template<class ItemType> 15: bool ArrayList<ItemType>::isEmpty() const 16: { 17: return itemCount == 0; 18: } // end isEmpty 20: template<class ItemType> 21: int ArrayList<ItemType>::getLength() const 22: { 23: return itemCount; 24: } // end getLength 26: template<class ItemType> 27: bool ArrayList<ItemType>::insert(int newPosition, const ItemType& newEntry) 28: { 29: bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1) && 30: (itemCount < maxItems); 31: if (ableToInsert) 32: { 33: // Make room for new entry by shifting all entries at 34: // positions >= newPosition toward the end of the array 35: // (no shift if newPosition == itemCount + 1) 36: for (int pos = itemCount; pos >= newPosition; pos--) 37: items[pos] = items[pos - 1]; 38: 39: // Insert new entry 40: items[newPosition - 1] = newEntry; 41: itemCount++; // Increase count of entries 42: } // end if 43: 44: return ableToInsert; 45: } // end insert 47: template<class ItemType> 48: bool ArrayList<ItemType>::remove(int position) 49: { 50: bool ableToRemove = (position >= 1) && (position <= itemCount); 51: if (ableToRemove) 52: { 53: // Remove entry by shifting all entries after the one at 54: // position toward the beginning of the array 55: // (no shift if position == itemCount) 56: for (int fromIndex = position, toIndex = fromIndex - 1; fromIndex < itemCount; 57: fromIndex++, toIndex++) 58: items[toIndex] = items[fromIndex]; 59: 60: itemCount--; // Decrease count of entries 61: } // end if 62: 63: return ableToRemove; 64: } // end remove 66: template<class ItemType> 67: void ArrayList<ItemType>::clear() 68: { 69: itemCount = 0; 70: } // end clear 72: template<class ItemType> 73: ItemType ArrayList<ItemType>::getEntry(int position) const throw(PrecondViolatedExcep) 74: { 75: // Enforce precondition 76: bool ableToGet = (position >= 1) && (position <= itemCount); 77: if (ableToGet) 78: return items[position - 1]; 79: else 80: { 81: string message = "getEntry() called with an empty list or "; 82: message = message + "invalid position."; 83: throw(PrecondViolatedExcep(message)); 84: } // end if 85: } // end getEntry 87: template<class ItemType> 88: void ArrayList<ItemType>::setEntry(int position, const ItemType& newEntry) throw(PrecondViolatedExcep) 89: { 90: // Enforce precondition 91: bool ableToSet = (position >= 1) && (position <= itemCount); 92: if (ableToSet) 93: items[position - 1] = newEntry; 94: else 95: { 96: string message = "setEntry() called with an empty list or "; 97: message = message + "invalid position."; 98: throw(PrecondViolatedExcep(message)); 99: } // end if 100: } // end setEntry 102: // End of implementation file.