root/labo1/labo1/AltMap.cpp

User picture

Author: machiel.sleeuwaert

Revision: 13 («Previous)


File Size: 1.3 KB

(October 08, 2008 15:33 UTC) Over 3 years ago


  

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

AltMap::AltMap():m_CurrentSize(2),m_UsedSize(0)
{
	m_ArrPTR = new AltMapPair[m_CurrentSize];
}

AltMap::~AltMap()
{

	delete[] m_ArrPTR;
}

void AltMap::Insert(tstring Key, tstring Value)//always add to the end of the array
{
		if(m_UsedSize >= m_CurrentSize){
			makeArrayBigger();
		}
		m_ArrPTR[m_UsedSize].m_Key = Key;
		m_ArrPTR[m_UsedSize].m_Value = Value;
		
		++m_UsedSize;
}



void AltMap::erase(tstring Key)
{
	for(int i=0;i<m_CurrentSize;++i){
		
		if (Key == m_ArrPTR[i].m_Key){
			m_ArrPTR[i].m_Key = "0";
			
		}
	}
}



tstring AltMap::find(tstring Key)
{
	for(int i=0;i<m_CurrentSize;++i){


		if (Key == m_ArrPTR[i].m_Key){
			return m_ArrPTR[i].m_Value;
			
		}
	}
}


void AltMap::change(tstring Key, tstring NewValue)
{
	for(int i=0;i<m_CurrentSize;++i){

		if (Key == m_ArrPTR[i].m_Key){
			m_ArrPTR[i].m_Value = NewValue;

		}
	}
}

void AltMap::makeArrayBigger(){

	int oldsize = m_CurrentSize; //save old size
	m_CurrentSize*=2; //double the size 
	AltMapPair* tempArr = new AltMapPair[m_CurrentSize]; //make temp array of old size
	for(int i =0;i<oldsize;++i){
		tempArr[i] = m_ArrPTR[i]; //copy old array into temp array
	}
	delete[] m_ArrPTR; //delete old array
	m_ArrPTR = tempArr; //assign array to pointer

}