2008-10-12 20:53:28 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include "picoc.h"
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-01-29 17:26:04 -05:00
|
|
|
/* quick hash function */
|
2008-10-12 20:53:28 -04:00
|
|
|
static unsigned int TableHash(const Str *Key)
|
|
|
|
{
|
2009-01-23 06:34:12 -05:00
|
|
|
if (Key->Len == 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return ((*Key->Str << 24) | (Key->Str[Key->Len-1] << 16) | (Key->Str[Key->Len >> 1] << 8)) ^ Key->Len;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
/* initialise a table */
|
2009-01-29 17:26:04 -05:00
|
|
|
void TableInit(struct Table *Tbl, struct TableEntry **HashTable, int Size, int OnHeap)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
Tbl->Size = Size;
|
2009-01-29 17:26:04 -05:00
|
|
|
Tbl->OnHeap = OnHeap;
|
2008-10-12 20:53:28 -04:00
|
|
|
Tbl->HashTable = HashTable;
|
2009-01-29 17:26:04 -05:00
|
|
|
memset(HashTable, '\0', sizeof(struct TableEntry *) * Size);
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
2009-01-29 17:26:04 -05:00
|
|
|
/* check a hash table entry for a key */
|
2008-10-12 20:53:28 -04:00
|
|
|
static int TableSearch(struct Table *Tbl, const Str *Key, int *AddAt)
|
|
|
|
{
|
2009-01-29 17:26:04 -05:00
|
|
|
struct TableEntry *Entry;
|
|
|
|
int HashValue = TableHash(Key) % Tbl->Size;;
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-01-29 17:26:04 -05:00
|
|
|
for (Entry = Tbl->HashTable[HashValue]; Entry != NULL; Entry = Entry->Next)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
2009-01-29 17:26:04 -05:00
|
|
|
if (StrEqual(&Entry->Key, Key))
|
|
|
|
return HashValue; /* found */
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
2009-01-29 17:26:04 -05:00
|
|
|
*AddAt = HashValue; /* didn't find it in the chain */
|
2008-10-12 20:53:28 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
/* set an identifier to a value. returns FALSE if it already exists */
|
2008-12-20 22:13:25 -05:00
|
|
|
int TableSet(struct Table *Tbl, const Str *Key, struct Value *Val)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
int HashPos;
|
|
|
|
int AddAt;
|
|
|
|
|
|
|
|
HashPos = TableSearch(Tbl, Key, &AddAt);
|
|
|
|
|
2008-12-20 22:13:25 -05:00
|
|
|
if (HashPos == -1)
|
2009-01-29 17:26:04 -05:00
|
|
|
{ /* add it to the table */
|
|
|
|
struct TableEntry *NewEntry = VariableAlloc(NULL, sizeof(struct TableEntry), Tbl->OnHeap);
|
|
|
|
NewEntry->Key = *Key;
|
|
|
|
NewEntry->Val = Val;
|
|
|
|
NewEntry->Next = Tbl->HashTable[AddAt];
|
|
|
|
Tbl->HashTable[AddAt] = NewEntry;
|
|
|
|
return TRUE;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
2009-01-26 03:57:32 -05:00
|
|
|
|
|
|
|
return FALSE;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
/* find a value in a table. returns FALSE if not found */
|
2008-12-18 19:22:52 -05:00
|
|
|
int TableGet(struct Table *Tbl, const Str *Key, struct Value **Val)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
int HashPos;
|
|
|
|
int AddAt;
|
|
|
|
|
|
|
|
HashPos = TableSearch(Tbl, Key, &AddAt);
|
|
|
|
|
|
|
|
if (HashPos == -1)
|
2008-12-18 19:22:52 -05:00
|
|
|
return FALSE;
|
|
|
|
|
2009-01-29 17:26:04 -05:00
|
|
|
*Val = Tbl->HashTable[HashPos]->Val;
|
2008-12-18 19:22:52 -05:00
|
|
|
return TRUE;
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|