
/*
 * Instituto Superior de Engenharia do Porto
 *
 * Estruturas de Informação
 *
 * 2000/2001
 *
 * ------------------------------------------
 *
 * Classe de Tabela de Hashing fechado para strings
 *
 * hash_ab_str.cpp
 *
 */

#include "hashf_str.h"


void HashTableStrings :: copia( const HashTableStrings& o )
{
	m_tabela = o.m_tabela;
}


HashTableStrings :: HashTableStrings( int nSize /* = DIM_NORMAL */ )
{
	m_tabela.resize( nSize );
}


HashTableStrings::HashTableStrings( const HashTableStrings& o )
{
	copia( o );
}


bool HashTableStrings::insere( const string& sTexto )
{
	int pos = HashingStrings( sTexto, m_tabela.size() );

	ListString& ls = m_tabela[ pos ];
	ls.insert( ls.begin(), sTexto );

	return true;
}


bool HashTableStrings :: pesquisa( const string& sTexto )
{
	int pos = HashingStrings( sTexto, m_tabela.size() );

	ListString& ls = m_tabela[ pos ];

	if ( ls.empty() ) return false;

	ListString :: iterator pStr = ls.begin();
	while ( *pStr != sTexto &&
			*pStr != ""     &&
			 pStr != ls.end() ) pStr++;

	if ( *pStr == sTexto )
		return true;
	else
		return false;
}


bool HashTableStrings :: elimina( const string& sTexto )
{
	int pos = HashingStrings( sTexto, m_tabela.size() );

	m_tabela[ pos ].remove( sTexto );

	return true;
}


HashTableStrings& HashTableStrings :: operator=( const HashTableStrings& o )
{
	copia( o );
	return *this;
}


int HashingStrings( string sTexto, int nDiv /* = -1 */)
{
	int accum = 0;

	for(int i = 0; i < sTexto.length(); i++) accum += sTexto[i];
	if ( nDiv == -1 )
		return accum;
	else
		return accum % nDiv;
}


