Now uses the benefits of shared strings to simplify the hash table for value lookups.
Added a more compact way of storing shared string hash chains. git-svn-id: http://picoc.googlecode.com/svn/trunk@44 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
86af5318da
commit
a9a93f3aa4
55
table.c
55
table.c
|
@ -20,14 +20,14 @@ void TableInit(struct Table *Tbl, struct TableEntry **HashTable, int Size, int O
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check a hash table entry for a key */
|
/* check a hash table entry for a key */
|
||||||
static int TableSearch(struct Table *Tbl, const char *Key, int Len, int *AddAt)
|
static int TableSearch(struct Table *Tbl, const char *Key, int *AddAt)
|
||||||
{
|
{
|
||||||
struct TableEntry *Entry;
|
struct TableEntry *Entry;
|
||||||
int HashValue = TableHash(Key, Len) % Tbl->Size;
|
int HashValue = ((unsigned long)Key) % Tbl->Size;
|
||||||
|
|
||||||
for (Entry = Tbl->HashTable[HashValue]; Entry != NULL; Entry = Entry->Next)
|
for (Entry = Tbl->HashTable[HashValue]; Entry != NULL; Entry = Entry->Next)
|
||||||
{
|
{
|
||||||
if (strncmp(Entry->Key, Key, Len) == 0 && Entry->Key[Len] == '\0')
|
if (Entry->p.v.Key == Key)
|
||||||
return HashValue; /* found */
|
return HashValue; /* found */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,18 +35,18 @@ static int TableSearch(struct Table *Tbl, const char *Key, int Len, int *AddAt)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set an identifier to a value. returns FALSE if it already exists */
|
/* set an identifier to a value. returns FALSE if it already exists.
|
||||||
|
* Key must be a shared string from StrRegister() */
|
||||||
int TableSet(struct Table *Tbl, const char *Key, struct Value *Val)
|
int TableSet(struct Table *Tbl, const char *Key, struct Value *Val)
|
||||||
{
|
{
|
||||||
int AddAt;
|
int AddAt;
|
||||||
int KeyLen = strlen(Key);
|
int HashPos = TableSearch(Tbl, Key, &AddAt);
|
||||||
int HashPos = TableSearch(Tbl, Key, KeyLen, &AddAt);
|
|
||||||
|
|
||||||
if (HashPos == -1)
|
if (HashPos == -1)
|
||||||
{ /* add it to the table */
|
{ /* add it to the table */
|
||||||
struct TableEntry *NewEntry = VariableAlloc(NULL, sizeof(struct TableEntry), Tbl->OnHeap);
|
struct TableEntry *NewEntry = VariableAlloc(NULL, sizeof(struct TableEntry), Tbl->OnHeap);
|
||||||
NewEntry->Key = Key;
|
NewEntry->p.v.Key = Key;
|
||||||
NewEntry->Val = Val;
|
NewEntry->p.v.Val = Val;
|
||||||
NewEntry->Next = Tbl->HashTable[AddAt];
|
NewEntry->Next = Tbl->HashTable[AddAt];
|
||||||
Tbl->HashTable[AddAt] = NewEntry;
|
Tbl->HashTable[AddAt] = NewEntry;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
@ -55,34 +55,49 @@ int TableSet(struct Table *Tbl, const char *Key, struct Value *Val)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* find a value in a table. returns FALSE if not found */
|
/* find a value in a table. returns FALSE if not found.
|
||||||
|
* Key must be a shared string from StrRegister() */
|
||||||
int TableGet(struct Table *Tbl, const char *Key, struct Value **Val)
|
int TableGet(struct Table *Tbl, const char *Key, struct Value **Val)
|
||||||
{
|
{
|
||||||
int AddAt;
|
int AddAt;
|
||||||
int HashPos = TableSearch(Tbl, Key, strlen(Key), &AddAt);
|
int HashPos = TableSearch(Tbl, Key, &AddAt);
|
||||||
if (HashPos == -1)
|
if (HashPos == -1)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
*Val = Tbl->HashTable[HashPos]->Val;
|
*Val = Tbl->HashTable[HashPos]->p.v.Val;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* check a hash table entry for an identifier */
|
||||||
|
static int TableSearchIdentifier(struct Table *Tbl, const char *Key, int Len, int *AddAt)
|
||||||
|
{
|
||||||
|
struct TableEntry *Entry;
|
||||||
|
int HashValue = TableHash(Key, Len) % Tbl->Size;
|
||||||
|
|
||||||
|
for (Entry = Tbl->HashTable[HashValue]; Entry != NULL; Entry = Entry->Next)
|
||||||
|
{
|
||||||
|
if (strncmp(Entry->p.Key, Key, Len) == 0 && Entry->p.Key[Len] == '\0')
|
||||||
|
return HashValue; /* found */
|
||||||
|
}
|
||||||
|
|
||||||
|
*AddAt = HashValue; /* didn't find it in the chain */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* set an identifier and return the identifier. share if possible */
|
/* set an identifier and return the identifier. share if possible */
|
||||||
const char *TableSetKey(struct Table *Tbl, const char *Ident, int IdentLen)
|
const char *TableSetIdentifier(struct Table *Tbl, const char *Ident, int IdentLen)
|
||||||
{
|
{
|
||||||
int AddAt;
|
int AddAt;
|
||||||
int HashPos = TableSearch(Tbl, Ident, IdentLen, &AddAt);
|
int HashPos = TableSearchIdentifier(Tbl, Ident, IdentLen, &AddAt);
|
||||||
|
|
||||||
if (HashPos != -1)
|
if (HashPos != -1)
|
||||||
return Tbl->HashTable[HashPos]->Key;
|
return Tbl->HashTable[HashPos]->p.Key;
|
||||||
else
|
else
|
||||||
{ /* add it to the table */
|
{ /* add it to the table - we economise by not allocating the whole structure here */
|
||||||
struct TableEntry *NewEntry = HeapAlloc(sizeof(struct TableEntry) + IdentLen + 1);
|
struct TableEntry *NewEntry = HeapAlloc(sizeof(struct TableEntry *) + IdentLen + 1);
|
||||||
NewEntry->Key = (void *)NewEntry + sizeof(struct TableEntry);
|
strncpy((char *)NewEntry->p.Key, Ident, IdentLen);
|
||||||
strncpy((char *)NewEntry->Key, Ident, IdentLen);
|
|
||||||
NewEntry->Val = NULL;
|
|
||||||
NewEntry->Next = Tbl->HashTable[AddAt];
|
NewEntry->Next = Tbl->HashTable[AddAt];
|
||||||
Tbl->HashTable[AddAt] = NewEntry;
|
Tbl->HashTable[AddAt] = NewEntry;
|
||||||
return NewEntry->Key;
|
return NewEntry->p.Key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue