Then there's also this container...
Code:
struct _Container *CreateContainer(char *name)
{
struct _Container *g = (struct _Container*)malloc(sizeof(struct _Container));
strcpy((char*)&g->Name, name);
g->Tbl = CreateTable(10);
return g;
}
And of course, the infamous hash table set function...
Code:
void *Set(struct _Table *table, char *key, int keyLength, void *value)
{
void *prevValue = NULL;
int index = Hash(key, keyLength) % table->StorageSize;
// if there is no item in the first bucket at the index
if (table->Items[index] == NULL)
{
struct TableItem *item = (struct TableItem*)malloc(sizeof(struct TableItem*));
item->Value = value;
item->NextItem = NULL;
strcpy((char*)&item->Key, key);
table->Items[index] = item;
printf("Set item at index %d in condition 1.\n", index);
return NULL;
}
struct TableItem *iter = table->Items[index];
int found = 0;
// if the item in the first bucket has the same key
if (strcmp(table->Items[index]->Key, key) == 0)
{
found = 1;
prevValue = table->Items[index]->Value->Value;
table->Items[index]->Value->Value = value;
printf("Set item at index %d in condition 2.\n", index);
}
// otherwise look for the item in the list at the index
while (iter->NextItem != NULL && found == 0)
{
iter = iter->NextItem;
// if we found the item in the list at the index
if (strcmp(iter->Key, key) == 0)
{
found = 1;
prevValue = iter->Value->Value;
iter->Value->Value = value;
printf("Set the item at %d in condition 3.\n", index);
}
}
// if the item doesn't exist in the list at the index
if (found == 0)
{
struct TableItem *item = (struct VertexTableItem*)malloc(sizeof(struct TableItem*));
item->Value = value;
item->NextItem = NULL;
Copy((char*)&item->Key, key, keyLength);
iter->NextItem = item;
printf("Set the item at %d in condition 4.\n", index);
}
return prevValue;
}
Aaaaand don't forget the Print function. This is where the program is crashing.
Code:
void PrintTable(struct _Table *table)
{
int index=0;
for (index=0; index < table->StorageSize; index++)
{
struct TableItem *item = table->Items[index];
if (item != NULL)
{
do {
printf("%p->", item);
item = item->NextItem;
} while (item != NULL);
}
printf("NULL\n");
}
}