root/week1oplossing/IntLijst.cpp

User picture

Author: machiel.sleeuwaert

Revision: 13 («Previous)

(Oct 15 14:12 2008 UTC) Over 3 years ago


  

 
Show/hide line numbers
#include "IntLijst.h"

IntLijst::IntLijst():m_DynArrPtr(0),m_AantalElementen(0),m_Capaciteit(BASE_SIZE)
{
	m_DynArrPtr = new int[BASE_SIZE];
}



IntLijst::~IntLijst()
{
	delete[] m_DynArrPtr;
}

void IntLijst::push_back(int value)
{
	if(m_AantalElementen>=m_Capaciteit)
	{
		vergrootArray();
	}
	
	m_DynArrPtr[m_AantalElementen]= value;
	m_AantalElementen++;
}

int IntLijst::pop_back()
{
	if(m_AantalElementen > 0)
	{
		--m_AantalElementen;
		return m_DynArrPtr[m_AantalElementen +1];
	}
	return -1;
}

int IntLijst::at(int positie) const 
{
	if(positie<m_AantalElementen)
		return m_DynArrPtr[positie];
	else
		return -1;
}

void IntLijst::insert(int value, int positie)
{
	if(positie < m_AantalElementen)
	{
		if(m_AantalElementen+1 >= m_Capaciteit)
		{
			vergrootArray();
		}
		
		for (int arrayPos = m_AantalElementen - 1; arrayPos >= positie; --arrayPos)
		{
			m_DynArrPtr[arrayPos+1] = m_DynArrPtr[arrayPos];
		}

		m_DynArrPtr[positie] = value;

		++m_AantalElementen;
	}
}

void IntLijst::change(int positie, int newvalue)
{
	if(positie<m_AantalElementen)
	{
			m_DynArrPtr[positie]=newvalue;
	}
}

int IntLijst::getLength() const 
{
	return m_AantalElementen;
}

void IntLijst::vergrootArray()
{
	m_Capaciteit *= 2;
	int * tempArrPtr = new int[m_Capaciteit];

	for(int i=0;i<m_AantalElementen;++i)
	{
		tempArrPtr[i]=m_DynArrPtr[i];
	}

	delete[] m_DynArrPtr;

	m_DynArrPtr = tempArrPtr;
}

IntLijst::IntLijst(const IntLijst &p):m_DynArrPtr(0),m_AantalElementen(0),m_Capaciteit(BASE_SIZE)
{
	
	m_DynArrPtr = new int[BASE_SIZE];

	for (int i=0;i<p.getLength();++i)
	{
		push_back(p.at(i));
	}
}

IntLijst& IntLijst::operator=(const IntLijst &p)
{
	if (&p == this)
		return *this;

	if (m_AantalElementen>0){
		delete[] m_DynArrPtr;
		m_DynArrPtr = new int[BASE_SIZE];
	}

	

	for (int i=0;i<p.getLength();++i)
	{
		push_back(p.at(i));
	}

	return *this;
}

IntLijst IntLijst::operator+(const IntLijst &p)
{

	IntLijst temp;

	for (int i=0;i<m_AantalElementen;++i)
	{
		temp.push_back(m_DynArrPtr[i]);
	}	

	for (int i=0;i<p.getLength();++i)
	{
		temp.push_back(p.at(i));
	}


	return temp;
}

IntLijst& IntLijst::operator+=(const IntLijst &p)
{

	for (int i=0;i<p.getLength();++i)
	{
		push_back(p.at(i));
	}

	return *this;
}

bool IntLijst::operator!=(const IntLijst &p)
{
	if (m_Capaciteit != p.getLength())
		return true;

	for (int i=0;i<m_Capaciteit;++i)
	{
		if (m_DynArrPtr[i] != p.at(i))
			return true;
	}
	return false;
}

bool IntLijst::operator ==(const IntLijst &p)
{
	if (m_Capaciteit != p.getLength())
		return false;

	for (int i=0;i<m_Capaciteit;++i)
	{
		if (m_DynArrPtr[i] != p.at(i))
			return false;
	}
	return true;
}

void IntLijst::operator >>(tstringstream &p)
{
	for (int i=0;i<m_AantalElementen;++i)
	{
		p << "[" << i << "]= " << m_DynArrPtr[i] << " \n ";
			
	}
}

int& IntLijst::operator[] (int index)
{
	if (index > m_AantalElementen || index < 0)
		
		index = 0;
	return m_DynArrPtr[index];
}

tstringstream& operator<<(tstringstream& stream, IntLijst& lijst)
{
	for (int i=0;i<lijst.m_AantalElementen;++i)
	{
		stream << "[" << i << "]= " << lijst.m_DynArrPtr[i] << " \n ";

	}
	return stream;
}
tstring operator+(tstring string, IntLijst& lijst)
{
	tstringstream temp;
	temp << string;
	temp << lijst;
	return temp.str();
}