#include <iostream>
using namespace std;

#include <cassert>

#include "smartArray.h"

//////////////////////////////////////////////////////////////////////////////////////////
//should initialize data to NULL, count and size to 0									//
//////////////////////////////////////////////////////////////////////////////////////////
void init(smartArray &array)
{
	array.data = NULL;
	array.count = 0;
	array.size = 0;
}

//////////////////////////////////////////////////////////////////////////////////////////
//should free the memory for data and then call init to reset all members				//
//////////////////////////////////////////////////////////////////////////////////////////
void free(smartArray &array)
{
	if(array.data!=NULL)
		delete [] array.data;

	init(array);
} 

//////////////////////////////////////////////////////////////////////////////////////////
//should return the count member														//
//////////////////////////////////////////////////////////////////////////////////////////
int count(smartArray array)
{
	return array.count;
} 

//////////////////////////////////////////////////////////////////////////////////////////
//will do nothing if newSize is less than size. However, if newSize > size, this will:	//
//Allocate a new int array with at newSize elements										//
//If data!= NULL copy all the used elements (up to index count-1) into the new array	//
//Free the memory for the old data array												//
//Set data to point to the new memory array												//
//Update size to have the newSize														//
//////////////////////////////////////////////////////////////////////////////////////////
void reserve(smartArray &array, int newSize)
{
	if (newSize > array.size)
	{
		int *newarray = new int[newSize];

		for (int i = 0; i < array.count; i++)
			newarray[i] = array.data[i];

		delete[] array.data;

		array.data = newarray;

		array.size = newSize;
	}
		

}
 
//////////////////////////////////////////////////////////////////////////////////////////
//first, checks if there is enough space in the array: if size==count then calls		//
//reserve(array,count+10) to reserve more space. Then adds the new element at index		//
//count, and increases count.															//
//////////////////////////////////////////////////////////////////////////////////////////
void add(smartArray &array, int element)
{
	if (array.size == array.count)
		reserve(array,array.count+10);
	
	array.data[array.count] = element;

	array.count++;

} 

//////////////////////////////////////////////////////////////////////////////////////////
//returns element at index index. Uses assertions										//
//to make sure index >= 0 and index < count.											//
//////////////////////////////////////////////////////////////////////////////////////////
int elementAt(smartArray array, int index)
{

	assert((index>=0) && (index<array.count));
		
	
	return array.data[index];
}
 
//////////////////////////////////////////////////////////////////////////////////////////
//sets element at index index to newValue. If index == count it uses add function to	//
//ensure that there is enough space. Uses assertion to make sure						//
//index >= 0 and index <= count.														//
//////////////////////////////////////////////////////////////////////////////////////////
void setElementAt(smartArray &array, int index, int newValue)
{

	assert((index>=0) && (index<=array.count));
	
	if (index == array.count)
		add(array, newValue);
	else
		array.data[index] = newValue;
} 

//////////////////////////////////////////////////////////////////////////////////////////
//outputs all the elements from the array to the screen,								//
//in one line, separated by a space														//
//////////////////////////////////////////////////////////////////////////////////////////
void printElements(smartArray &array)
{
	for (int i = 0; i < array.count; i++)
		cout<<array.data[i]<<" ";
	cout<<endl;
} 