Last active
August 29, 2015 14:17
-
-
Save asterwolf/c919ff7fba7e998d0b45 to your computer and use it in GitHub Desktop.
COMP B12 - Lab5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ArrayList is a container class that grows as needed. | |
* Need to add destructor, copy constructor, and, later, | |
* operator= | |
* | |
* @author: Diego Diaz | |
* Course: COMP B12 | |
* Created on: Feb 23, 2015 | |
* Source File: ArrayList.h | |
*/ | |
#ifndef ARRAYLIST_H_ | |
#define ARRAYLIST_H_ | |
#include <iostream> // Normally wouldn't do this, but needed to support output in destructor | |
template<typename T> | |
class ArrayList { | |
private: | |
T *array; | |
int length; | |
int capacity; | |
/* | |
* Change capacity to that specified by newCapacity. | |
* @param newCapacity the new capacity | |
*/ | |
void changeCapacityTo(int newCapacity){ | |
T *newArray = new T[newCapacity]; | |
this -> capacity = newCapacity; | |
if(newCapacity < this ->length) | |
length = newCapacity; | |
for(int i = 0; i < length; ++i) | |
newArray[i] = array[i]; | |
delete[] array; | |
array = newArray; | |
} | |
public: | |
/* | |
* Initialize list with a capacity of 8 | |
*/ | |
ArrayList(){ | |
capacity = 8; | |
length = 0; | |
array = new T[capacity]; | |
} | |
/* | |
* Copy constructor | |
*/ | |
ArrayList (const ArrayList& other){ | |
capacity = other.length; | |
length = other.length; | |
array = new T[capacity]; | |
for(int i = 0; i < other.length; i++) | |
array[i] = other.array[i]; | |
} | |
virtual ~ArrayList() { | |
std::cout << "Destructing ArrayList at " << array << std::endl; | |
delete [] array; | |
array = NULL; | |
} | |
/* | |
* Adds item to list, at index, shifting items as necessary and increasing | |
* capacity of list as necessary. If capacity must increase, it must always | |
* be a power of 2. Note that if index is beyond capacity, capacity must be | |
* increased to allow adding the item at that index. Also, length should | |
* reflect the HIGHEST index (plus one, naturally) at which an item is | |
* stored, even if lower-indexed slots contain undefined values. | |
* | |
* @param item item to add to list | |
*/ | |
void add(int index, T item){ | |
if(index < length && index >= 0){ | |
length++; | |
if(capacity < length){ | |
changeCapacityTo(capacity * 2); | |
} | |
for(int i = length - 1; i > index; i--){ | |
array[i] = array[i - 1]; | |
} | |
array[index] = item; | |
} | |
else if (index == length){ | |
length++; | |
if(capacity < length){ | |
changeCapacityTo(capacity * 2); | |
} | |
array[index] = item; | |
} | |
// else{ | |
// length = index + 1; | |
// if(capacity < length){ | |
// int newCapacity = capacity; | |
// while (newCapacity < length){ | |
// newCapacity *= 2; | |
// } | |
// changeCapacityTo(newCapacity); | |
// } | |
// array[index] = item; | |
// } | |
} | |
/* | |
* Add item to end of list | |
* @param item item to add to list | |
*/ | |
void add(T item){ | |
if(length >= capacity) | |
changeCapacityTo(2 * capacity); | |
array[length++] = item; | |
} | |
/* | |
* Return item at index. For now, we assume index is legal. | |
* Later we will throw an exception when index is illegal. | |
* @param index index of item to return | |
* @return item at index | |
*/ | |
T get(int index) const { | |
return array[index]; | |
} | |
/* | |
* Return capacity | |
* @return capacity | |
*/ | |
int getCapacity() const { | |
return capacity; | |
} | |
/* | |
* Return current length | |
* @return current length | |
*/ | |
int getLength() const { | |
return length; | |
} | |
T& operator[](int index){ | |
if(index >= length) { | |
std::ostringstream err; | |
err << "Index [" << index << "] Out of range"; | |
throw std::out_of_range(err.str()); | |
} | |
return array[index]; | |
} | |
}; | |
#endif /* ARRAYLIST_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Tester program for ArrayListTemplate. | |
* @author: Diego Diaz | |
* Course: COMP B12 | |
* Created on: Mar 10, 2015 | |
* Source File: testArrayListTemplate.cpp | |
*/ | |
#include <iostream> | |
#include "ArrayListTemplate.h" | |
using namespace std; | |
template<typename T> | |
void printArrayList(ArrayList<T> arrayList) { | |
for(int i = 0; i < arrayList.getLength(); ++i) { | |
T item = arrayList.get(i); | |
cout << i << ":" << item << endl; | |
} | |
} | |
template<typename T> | |
void testArrayListTemplate(T startItem) { | |
ArrayList<T> arrayList; | |
const int NUM_ITEMS = 10; | |
T itemToAdd = startItem; | |
for(int i = 0; i < NUM_ITEMS; ++i) { | |
arrayList.add(itemToAdd); | |
itemToAdd += startItem; | |
} | |
try { | |
arrayList[NUM_ITEMS / 2] = startItem; | |
arrayList[NUM_ITEMS + 1] = arrayList[NUM_ITEMS + 1] | |
+ arrayList[NUM_ITEMS + 1]; | |
} | |
catch (out_of_range error) { | |
// use cout so that error output synched with stdout | |
cout << error.what() << endl; | |
} | |
printArrayList(arrayList); | |
} | |
int main(int argc, char *argv[]) { | |
testArrayListTemplate(2); | |
testArrayListTemplate(string("ab")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pathawks: I think some people in class might be peaking around. Hopefully my comments will help them understand what I did.