Hashtable


#1

Hi,

Is there any similar class in JUCE which has same functionality of Hashtable or Hashmap in STL? I am using JUCE 1.5.1.
If no, is it allowed to use STL classes from JUCE.

Regards,
Sabu


#2

It’s been mentioned a few times, but no, I’ve never quite got around to doing on one those. It’s fine to mix the std library though, of course! If you search the forum you’ll probably find a few other threads about hash maps.


#3

i have a quick and dirty hash table based on ght_hash library witch is clean and cross-platform, it’s not much but it works for me.

EdoHashTable.h

#ifndef		__EDO_HASH_TABLE_H__
#define		__EDO_HASH_TABLE_H__

#include "Includes.h"
#include "ght_hash_table.h"

class EdoHashTable
{
	public:
		EdoHashTable(const uint32 _tableSize);
		~EdoHashTable();
		const int insertElement (void *dataPointer, const String &key);
		const int insertElement (void *dataPointer, const MemoryBlock &key);
		const int insertElement (void *dataPointer, const int key);
		void *findElement (const String &key);
		void *findElement (const int key);
		void *findElement (const uint8 *keyData, const int keyDataSize);
		void *findElement (const MemoryBlock &key);
		unsigned int getTableSize();
		unsigned int getSize();

	private:
		ght_hash_table_t *ghtTable;
		uint32 tableSize;
};

#endif

EdoHashTable.cpp

#include "EdoHashTable.h"
#include "EdoModulatorManager/EdoModulatorManager.h"

EdoHashTable::EdoHashTable(const uint32 _tableSize) : tableSize(_tableSize), ghtTable(NULL)
{
	Log (T("EdoHashTable::ctor"));

	ghtTable = ght_create (tableSize);

	if (ghtTable == NULL)
	{
		Log (T("EdoHashTable::ctor failed to initialize hash table"));
	}
}

EdoHashTable::~EdoHashTable()
{
	Log (T("EdoHashTable::dtor"));

	if (ghtTable)
	{
		ght_finalize (ghtTable);
	}
}

const int EdoHashTable::insertElement (void *dataPointer, const String &key)
{
	if (ghtTable)
	{
		return (ght_insert (ghtTable, dataPointer, strlen (key.toUTF8()), key.toUTF8()));
	}

	return (-1);
}

const int EdoHashTable::insertElement (void *dataPointer, const MemoryBlock &key)
{
	if (ghtTable)
	{
		return (ght_insert (ghtTable, dataPointer, key.getSize(), key.getData()));
	}

	return (-1);
}

const int EdoHashTable::insertElement (void *dataPointer, const int key)
{
	if (ghtTable)
	{
		return (ght_insert (ghtTable, dataPointer, sizeof(int), &key));
	}

	return (-1);
}

void *EdoHashTable::findElement (const String &key)
{
	if (ghtTable)
	{
		return (ght_get (ghtTable, strlen (key.toUTF8()), key.toUTF8()));
	}

	return (NULL);
}

void *EdoHashTable::findElement (const MemoryBlock &key)
{
	if (ghtTable)
	{
		return (ght_get (ghtTable, key.getSize(), key.getData()));
	}

	return (NULL);
}

void *EdoHashTable::findElement (const uint8 *keyData, const int keyDataSize)
{
	if (ghtTable)
	{
		return (ght_get (ghtTable, keyDataSize, keyData));
	}

	return (NULL);
}

void *EdoHashTable::findElement (const int key)
{
	if (ghtTable)
	{
		return (ght_get (ghtTable, sizeof(int), &key));
	}

	return (NULL);
}

unsigned int EdoHashTable::getTableSize()
{
	if (ghtTable)
	{
		return (ght_table_size (ghtTable));
	}

	return (0);
}

unsigned int EdoHashTable::getSize()
{
	if (ghtTable)
	{
		return (ght_size(ghtTable));
	}

	return (0);
}