#include #include "lab4alist.h" /***************************************************************** * ArrayList *****************************************************************/ // Default constructor ArrayList::ArrayList() { used = 0; allocated = DEFAULT_SIZE; values = new val_t[DEFAULT_SIZE]; } // Destructor ArrayList::~ArrayList() { delete[] values; } // Insert value "val" into position "pos" void ArrayList::insert(val_t val, unsigned int pos) { // Self expand if we need to if (used == allocated) { // Allocate double-the-space val_t* newvals = new val_t[allocated * 2]; // Copy the values over for (unsigned int i = 0; i < used; i++) newvals[i] = values[i]; // Free up the old memory delete[] values; // Set our pointer to the new memory values = newvals; // Update how much we have allocated allocated *= 2; } // If there are only 2 things in here and we try to insert into slot // 20, just put it at the end. if (pos > used) pos = used; // Make room for (unsigned int i = used; i > pos; i--) { values[i] = values[i - 1]; } // Insert the value values[pos] = val; // Note that there is some additional data in here used++; } // Remove a value at position "pos" void ArrayList::remove(unsigned int pos) { // If we try to remove beyond the end of the list, remove // the end of the list. if (pos > used) pos = used; // If there is nothing in here, just return if (used == 0) return; // Shift everything back. for (unsigned int i = pos; i < used; i++) { values[i] = values[i + 1]; } // Note that we lost a value used--; } // Get a value from the list val_t ArrayList::retrieve(unsigned int pos) { if (pos > used) pos = used; return values[pos]; } // Quickly check if there is data bool ArrayList::isEmpty() { return (used == 0); } // Check how much data we have unsigned int ArrayList::size() { return used; } // Return an iterator to the beginning of our list ArrayListIterator ArrayList::begin() { ArrayListIterator ret; ret.pos = 0; ret.max = &used; ret.alist = this; return ret; } // Return an iterator to the end of our list ArrayListIterator ArrayList::end() { ArrayListIterator ret; ret.pos = used; ret.max = &used; ret.alist = this; return ret; } // If you implement your iterator and LinkedList correctly, // the LinkedList version of this function should be // identical except for the type names. ostream& operator << (ostream& outStream, ArrayList& toPrint) { // Do the special case of an empty list. if (toPrint.isEmpty()) { outStream << "[]"; return outStream; } // Get an iterator ArrayListIterator i = toPrint.begin(); // Print out [ and the first element outStream << "[" << *i; // Update the iterator ++i; // Loop through the rest of the data for (; i != toPrint.end(); ++i) { outStream << ", " << *i; } // Print out the closing ] outStream << "]"; // Return return outStream; } /***************************************************************** * ArrayListIterator *****************************************************************/ // Default constructor ArrayListIterator::ArrayListIterator() { pos = 0; max = NULL; alist = NULL; } // Copy constructor ArrayListIterator::ArrayListIterator(const ArrayListIterator& other) { pos = other.pos; max = other.max; alist = other.alist; } // Equality operator bool const ArrayListIterator::operator ==(ArrayListIterator& other) { return ((alist == other.alist) && (max == other.max) && (pos == other.pos)); } // Inequality operator bool ArrayListIterator::operator !=(const ArrayListIterator& other) { return (!((alist == other.alist) && (max == other.max) && (pos == other.pos))); } // Dereference operator, returns the value in the list currently // "pointed at" by our iterator. val_t ArrayListIterator::operator*() { return alist->values[pos]; } // Advance to the next value ArrayListIterator& ArrayListIterator::operator++() { if (pos < *max) pos++; return *this; } // Go back to the previous value ArrayListIterator& ArrayListIterator::operator--() { if (pos > 0) pos--; return *this; }