Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

hash.cpp

c-plus-plus

#include "hash.h"

template<class T>
HashEntry<T>::HashEntry(T data){
	this->data=data;
	this->next=NULL;
}

template<class T>
void HashEntry<T>::SetNext(HashEntry* next){
	this->next=next;
}

template<class T>
HashEntry<T>* HashEntry<T>::Next(){
	return this->next;
}

template<class T>
T& HashEntry<T>::Data(){
	return this->data;
}

template<class T,unsigned n>
Hash<T, n>::Hash(){
	this->lines=n;
	this->rows=(HashEntry<T>**)malloc(this->lines * sizeof(HashEntry<T>*));
	if(this->rows==NULL){
		throw 1;
	}
	this->act=NULL;
	this->actLine=-1;
	this->inserted=0;
	this->notinserted=0;
	for(long i = 0;i<this->lines;i++){
		this->rows[i]=NULL;
	}
}

template<class T,unsigned n>
Hash< T, n>::~Hash(){
	for(long i = 0;i<this->lines;i++){
		while(this->rows[i]!=NULL){
			HashEntry<T> * aux = this->rows[i]->Next();
			delete this->rows[i];
			this->rows[i]=aux;
		}
	}
}

template<class T,unsigned n>
Hash< T, n> & Hash< T, n>::operator<< (const T &obj){
	long hash=this->h(obj);
	if(hash >= this->lines){
		std::cerr<<"Err21 - hash'"<<hash<<"' out of range.\n";
		throw 21;
	}
	HashEntry<T> * entry = new HashEntry<T>(obj);
	entry->SetNext(this->rows[hash]);
	this->rows[hash]=entry;
	this->inserted++;
	return *this;
}

template<class T,unsigned n>
unsigned Hash< T, n>::operator + () const{
	return this->inserted;
}

template<class T,unsigned n>
unsigned Hash< T, n>::operator - () const{
	return this->notinserted;
}

template<class T,unsigned n>
const T * Hash< T, n>::najit(const T &obj){
	unsigned hash=Hash< T, n>::h(obj);
	for(HashEntry<T>* i= this->rows[hash];i!=NULL;i=i->Next()){
		if(i->Data()==obj){
			return &(i->Data());
		}
	}
	return NULL;
}

/**
 * Sets the iterator to first entry. 
 * In case no entry is found, iterator remains invalid
 * and false is returned.
 */
template<class T,unsigned n>
bool Hash< T, n>::prvni(){
	for(long i=0;i<this->lines;i++){
		if(this->rows[i]!=NULL){
			this->act=this->rows[i];
			this->actLine=i;
			return true;
		}
	}
	return false;
}


template<class T,unsigned n>
const T & Hash< T, n>::aktual(){
	return this->act->Data();
}


template<class T,unsigned n>
bool Hash< T, n>::dalsi(){
	if(this->actLine!=-1 and this->act!=NULL){
		if(this->act->Next() != NULL){
			this->act=this->act->Next();
			return true;
		}
		else{
			for(long i=this->actLine+1;i<this->lines;i++){
				if(this->rows[i]!=NULL){
					this->act=this->rows[i];
					this->actLine=i;
					return true;
				}
			}
		}
	}
	this->act=NULL;
	this->actLine=-1;
	return false;
}

Neformátovaný

Přidáno: 11.12.2012
Expirace: Neuvedeno

Avatar
Autor: Onda.Zadnik
Aktivity