#include "hash.h"

HashTable::HashTable()
{
	//necessary to do this!
	memset(m_Table, 0, sizeof(m_Table));
}

HashTable::~HashTable()
{
	Clear();
}

//Returns an iterator
HashTable::iterator HashTable::Enumerate()
{
	return HashTable::iterator(m_Table, HT_SIZE);
}

//Retrieves a value from the hash table
HashTable::htNode *HashTable::Retrieve(const char *key)
{
	return _FindNode(key);
}

//Adds an entry into the hash table with current time
void HashTable::Store(const char *key, const char *value, bool temporary)
{
	time_t stamp = 0;
	
	if (temporary)
		stamp = time(NULL);

	_Insert(key, value, stamp);
}

//Adds an entry into the hash table with preset time
void HashTable::Store(const char *key, const char *value, time_t stamp)
{
	_Insert(key, value, stamp);
}

//Erases a key
void HashTable::EraseKey(const char *key)
{
	HashTable::htNodeSet *set = _FindNodeSet(key);
	HashTable::htNode *node = _FindNode(key, false);

	if (set && node)
		_Unlink(set, node);
}

//Deletes all keys between the two times.
// 0 specifies "match all"
//All specifies whether permanent keys (time_t = 0) are erased
size_t HashTable::Prune(time_t begin, time_t end, bool all)
{
	HashTable::htNode *node, *temp;
	size_t total = 0;
	bool fire;

	for (uint32_t i=0; i<HT_SIZE; i++)
	{
		if (m_Table[i])
		{
			node = m_Table[i]->head;

			while (node != NULL)
			{
				temp = node->next;
				fire = false;
				if (!begin && !end)
				{
					fire = true;
				} else if (!begin && end) {
					if (node->stamp <= end)
						fire = true;
				} else if (begin && !end) {
					if (node->stamp >= begin)
						fire = true;
				} else {
					if (node->stamp >= begin && node->stamp <= end)
						fire = true;
				}
				if (fire && ((node->stamp == 0) && !all))
					fire = false;
				if (fire)
				{
					_Unlink(m_Table[i], node);
					total++;
				}
				node = temp;
			}
		}
	}
	
	return total;
}

void HashTable::Clear()
{
	HashTable::htNode *node, *temp;

	for (uint32_t i=0; i<HT_SIZE; i++)
	{
		if (m_Table[i])
		{
			node = m_Table[i]->head;

			while (node != NULL)
			{
				temp = node->next;
				delete node;
				node = temp;
			}

			delete m_Table[i];
			m_Table[i] = NULL;
		}
	}
}

bool HashTable::KeyExists(const char *key)
{
	return (_FindNode(key, false) != NULL);
}

size_t HashTable::UsedHashes()
{
	size_t num = 0;

	for (uint32_t i=0; i<HT_SIZE; i++)
	{
		if (m_Table[i])
			num++;
	}

	return num;
}

////////////////////
// Iterator stuff //
////////////////////

HashTable::iterator::iterator(HashTable::htNodeSet **table, uint32_t tableSize)
{
	d = NULL;
	t = table;
	s = tableSize;

	for (r = 0; r<s; r++)
	{
		if (t[r] && t[r]->head)
		{
			d = t[r]->head;
			break;
		}
	}
}

void HashTable::iterator::next()
{
	if (d->next)
	{
		d = d->next;
	} else {
		d=NULL;
		r++;
		for (; r<s; r++)
		{
			if (t[r] && t[r]->head)
			{
				d = t[r]->head;
				break;
			}
		}
	}
}

bool HashTable::iterator::done()
{
	if (d == NULL && r >= s)
		return true;

	return false;
}

const char *HashTable::iterator::val()
{
	return d->val.c_str();
}

const char *HashTable::iterator::key()
{
	return d->key.c_str();
}

time_t HashTable::iterator::stamp()
{
	return d->stamp;
}

uint32_t HashTable::iterator::hash()
{
	return r;
}

HashTable::htNode *HashTable::iterator::operator *()
{
	return d;
}

/////////////////////////////
// Private implementations //
/////////////////////////////

//Erases a node
void HashTable::_Unlink(HashTable::htNodeSet *set, HashTable::htNode *node)
{
	if (set->head == node)
	{
		set->head = NULL;
		set->tail = NULL;

		delete node;
	} else {
		HashTable::htNode *link = set->head;

		while (link != NULL)
		{
			if (link->next == node)
			{
				link->next = node->next;

				delete node;

				return;
			}

			link = link->next;
		}
	}
}

//Finds a node by key
HashTable::htNode *HashTable::_FindNode(const char *key, bool autoMake)
{
	HashTable::htNodeSet *set;
	HashTable::htNode *node;

	set = _FindNodeSet(key);
	node = set->head;

	while (node)
	{
		if (node->key.compare(key) == 0)
			return node;
		node = node->next;
	}

	if (autoMake)
		return _InsertIntoNodeSet(set, key, true);

	return NULL;
}

//Inserts a new set of data into a bucket
void HashTable::_Insert(const char *key, const char *value, time_t stamp)
{
	HashTable::htNodeSet *set;
	HashTable::htNode *node;

	set = _FindNodeSet(key);
	node = _InsertIntoNodeSet(set, key);

	node->val.assign(value);
	node->stamp = stamp;
}

//Inserts a new key into a bucket
HashTable::htNode *HashTable::_InsertIntoNodeSet(HashTable::htNodeSet *set, const char *key, bool skip)
{
	HashTable::htNode *node;

	if (!skip)
	{
		node = set->head;
		while (node != NULL)
		{
			if (strcmp(node->key.c_str(), key) == 0)
				return node;
			node = node->next;
		}
	}

	node = new HashTable::htNode;
	node->key.assign(key);
	node->next = NULL;

	if (set->head == NULL)
	{
		set->head = node;
		set->tail = node;
	} else {
		set->tail->next = node;
		set->tail = node;
	}

	return node;
}

//Finds a bucket head by key
HashTable::htNodeSet *HashTable::_FindNodeSet(const char *key)
{
	uint16_t dv = HashTable::HashString(key);

	if (!m_Table[dv])
	{
		m_Table[dv] = new HashTable::htNodeSet;
		memset(m_Table[dv], 0, sizeof(HashTable::htNodeSet));
	}

	return m_Table[dv];
}

uint16_t HashTable::HashString(const char *str)
{
	uint8_t v1 = 0, v2 = 0;
	uint16_t dv = 0;

	v1 = HashTable::_HashString1(str);
	v2 = HashTable::_HashString2(str);

	//Combine the two hashes into an 11bit key
	dv = ((uint16_t)(v1) << 3) ^ (uint16_t)v2;
	dv &= 0x7FF;

	return dv;
}

uint8_t HashTable::_HashString1(const char *str)
{
	uint8_t v = 0;
	while (*str)
		v = Hash_CRCTable1[v ^ (uint8_t)(*str++)];
	return v;
}

uint8_t HashTable::_HashString2(const char *str)
{
	uint8_t v = 0;
	while (*str)
		v = Hash_CRCTable2[v ^ (uint8_t)(*str++)];
	return v;
}

//Contains last two bytes of standard CRC32
const uint8_t Hash_CRCTable1[256] = {
  0x00, 0x96, 0x2c, 0xba, 0x19, 0x8f,
  0x35, 0xa3, 0x32, 0xa4, 0x1e, 0x88,
  0x2b, 0xbd, 0x07, 0x91, 0x64, 0xf2,
  0x48, 0xde, 0x7d, 0xeb, 0x51, 0xc7,
  0x56, 0xc0, 0x7a, 0xec, 0x4f, 0xd9,
  0x63, 0xf5, 0xc8, 0x5e, 0xe4, 0x72,
  0xd1, 0x47, 0xfd, 0x6b, 0xfa, 0x6c,
  0xd6, 0x40, 0xe3, 0x75, 0xcf, 0x59,
  0xac, 0x3a, 0x80, 0x16, 0xb5, 0x23,
  0x99, 0x0f, 0x9e, 0x08, 0xb2, 0x24,
  0x87, 0x11, 0xab, 0x3d, 0x90, 0x06,
  0xbc, 0x2a, 0x89, 0x1f, 0xa5, 0x33,
  0xa2, 0x34, 0x8e, 0x18, 0xbb, 0x2d,
  0x97, 0x01, 0xf4, 0x62, 0xd8, 0x4e,
  0xed, 0x7b, 0xc1, 0x57, 0xc6, 0x50,
  0xea, 0x7c, 0xdf, 0x49, 0xf3, 0x65,
  0x58, 0xce, 0x74, 0xe2, 0x41, 0xd7,
  0x6d, 0xfb, 0x6a, 0xfc, 0x46, 0xd0,
  0x73, 0xe5, 0x5f, 0xc9, 0x3c, 0xaa,
  0x10, 0x86, 0x25, 0xb3, 0x09, 0x9f,
  0x0e, 0x98, 0x22, 0xb4, 0x17, 0x81,
  0x3b, 0xad, 0x20, 0xb6, 0x0c, 0x9a,
  0x39, 0xaf, 0x15, 0x83, 0x12, 0x84,
  0x3e, 0xa8, 0x0b, 0x9d, 0x27, 0xb1,
  0x44, 0xd2, 0x68, 0xfe, 0x5d, 0xcb,
  0x71, 0xe7, 0x76, 0xe0, 0x5a, 0xcc,
  0x6f, 0xf9, 0x43, 0xd5, 0xe8, 0x7e,
  0xc4, 0x52, 0xf1, 0x67, 0xdd, 0x4b,
  0xda, 0x4c, 0xf6, 0x60, 0xc3, 0x55,
  0xef, 0x79, 0x8c, 0x1a, 0xa0, 0x36,
  0x95, 0x03, 0xb9, 0x2f, 0xbe, 0x28,
  0x92, 0x04, 0xa7, 0x31, 0x8b, 0x1d,
  0xb0, 0x26, 0x9c, 0x0a, 0xa9, 0x3f,
  0x85, 0x13, 0x82, 0x14, 0xae, 0x38,
  0x9b, 0x0d, 0xb7, 0x21, 0xd4, 0x42,
  0xf8, 0x6e, 0xcd, 0x5b, 0xe1, 0x77,
  0xe6, 0x70, 0xca, 0x5c, 0xff, 0x69,
  0xd3, 0x45, 0x78, 0xee, 0x54, 0xc2,
  0x61, 0xf7, 0x4d, 0xdb, 0x4a, 0xdc,
  0x66, 0xf0, 0x53, 0xc5, 0x7f, 0xe9,
  0x1c, 0x8a, 0x30, 0xa6, 0x05, 0x93,
  0x29, 0xbf, 0x2e, 0xb8, 0x02, 0x94,
  0x37, 0xa1, 0x1b, 0x8d};

//Contains second pair of bytes from standard CRC32
const uint8_t Hash_CRCTable2[256] = {
  0x00, 0x07, 0x0e, 0x09, 0x6d, 0x6a,
  0x63, 0x64, 0xdb, 0xdc, 0xd5, 0xd2,
  0xb6, 0xb1, 0xb8, 0xbf, 0xb7, 0xb0,
  0xb9, 0xbe, 0xda, 0xdd, 0xd4, 0xd3,
  0x6c, 0x6b, 0x62, 0x65, 0x01, 0x06,
  0x0f, 0x08, 0x6e, 0x69, 0x60, 0x67,
  0x03, 0x04, 0x0d, 0x0a, 0xb5, 0xb2,
  0xbb, 0xbc, 0xd8, 0xdf, 0xd6, 0xd1,
  0xd9, 0xde, 0xd7, 0xd0, 0xb4, 0xb3,
  0xba, 0xbd, 0x02, 0x05, 0x0c, 0x0b,
  0x6f, 0x68, 0x61, 0x66, 0xdc, 0xdb,
  0xd2, 0xd5, 0xb1, 0xb6, 0xbf, 0xb8,
  0x07, 0x00, 0x09, 0x0e, 0x6a, 0x6d,
  0x64, 0x63, 0x6b, 0x6c, 0x65, 0x62,
  0x06, 0x01, 0x08, 0x0f, 0xb0, 0xb7,
  0xbe, 0xb9, 0xdd, 0xda, 0xd3, 0xd4,
  0xb2, 0xb5, 0xbc, 0xbb, 0xdf, 0xd8,
  0xd1, 0xd6, 0x69, 0x6e, 0x67, 0x60,
  0x04, 0x03, 0x0a, 0x0d, 0x05, 0x02,
  0x0b, 0x0c, 0x68, 0x6f, 0x66, 0x61,
  0xde, 0xd9, 0xd0, 0xd7,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