2022-04-04 14:15:02 -04:00
|
|
|
#include "hash.h"
|
|
|
|
#include "env.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct ObjectTable buildTable(size_t capacity)
|
|
|
|
{
|
|
|
|
struct ObjectTable table = {
|
|
|
|
.count = 0,
|
|
|
|
.capacity = capacity,
|
2022-04-05 15:27:41 -04:00
|
|
|
.elements = capacity ? malloc(sizeof(struct EnvElement) * capacity) : NULL,
|
2022-04-04 14:15:02 -04:00
|
|
|
};
|
|
|
|
for (int i = 0; i < table.capacity; i++) {
|
|
|
|
table.elements[i].symbol = NULL;
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2022-04-06 20:12:26 -04:00
|
|
|
void deleteTable(struct ObjectTable* table)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < table->capacity; i++) {
|
|
|
|
if (table->elements[i].symbol) {
|
|
|
|
free(table->elements[i].symbol);
|
|
|
|
cleanObject(&table->elements[i].object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(table->elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef HASHLESS_ENV
|
|
|
|
|
|
|
|
static unsigned long hash(const char* str, struct ObjectTable* table)
|
|
|
|
{
|
|
|
|
unsigned long hash = 5381;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = *str++)) {
|
|
|
|
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash % table->capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void extendTable(struct ObjectTable* table)
|
2022-04-04 14:15:02 -04:00
|
|
|
{
|
2022-04-05 15:27:41 -04:00
|
|
|
if (table->capacity < (table->count + 1) * 2) {
|
|
|
|
struct ObjectTable newTable = buildTable(table->capacity ? table->capacity * 2 : 4);
|
2022-04-04 14:15:02 -04:00
|
|
|
for (int i = 0; i < table->capacity; i++) {
|
|
|
|
if (table->elements[i].symbol) {
|
|
|
|
addToTable(&newTable, table->elements[i].symbol, table->elements[i].object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(table->elements);
|
|
|
|
*table = newTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object* getFromTable(struct ObjectTable* table, const char* name)
|
|
|
|
{
|
2022-04-05 15:27:41 -04:00
|
|
|
if (table->capacity == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-04-06 20:12:26 -04:00
|
|
|
size_t h = hash(name, table);
|
|
|
|
while (table->elements[h].symbol) {
|
2022-04-04 14:15:02 -04:00
|
|
|
if (strcmp(name, table->elements[h].symbol) == 0) {
|
|
|
|
return &table->elements[h].object;
|
|
|
|
}
|
|
|
|
h = (h + 1) % table->capacity;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-06 20:12:26 -04:00
|
|
|
#else
|
|
|
|
|
|
|
|
void extendTable(struct ObjectTable* table)
|
2022-04-04 14:15:02 -04:00
|
|
|
{
|
2022-04-06 20:12:26 -04:00
|
|
|
if (table->count == (table->capacity - 1)) {
|
|
|
|
struct EnvElement* oldElements = table->elements;
|
|
|
|
size_t oldCapacity = table->capacity;
|
|
|
|
table->capacity *= 2;
|
|
|
|
table->elements = malloc(sizeof(struct EnvElement) * table->capacity);
|
|
|
|
for (int i = 0; i < oldCapacity; i++) {
|
|
|
|
table->elements[i] = oldElements[i];
|
|
|
|
}
|
|
|
|
for (size_t i = oldCapacity; i < table->capacity; i++) {
|
|
|
|
table->elements[i].symbol = NULL;
|
2022-04-04 14:15:02 -04:00
|
|
|
}
|
2022-04-07 01:20:59 -04:00
|
|
|
free(oldElements);
|
2022-04-04 14:15:02 -04:00
|
|
|
}
|
2022-04-06 20:12:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object* getFromTable(struct ObjectTable* table, const char* name)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < table->count; i++) {
|
|
|
|
if (strcmp(name, table->elements[i].symbol) == 0) {
|
|
|
|
return &table->elements[i].object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long hash(unused const char* str, struct ObjectTable* table)
|
|
|
|
{
|
|
|
|
return table->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
///
|
|
|
|
/// \param table
|
|
|
|
/// \param name Should be a new allocation
|
|
|
|
/// \param object Should already be cloned
|
|
|
|
void addToTable(struct ObjectTable* table, char* name, Object object)
|
|
|
|
{
|
|
|
|
extendTable(table);
|
|
|
|
size_t h = hash(name, table);
|
|
|
|
while (table->elements[h].symbol) {
|
|
|
|
h = (h + 1) % table->capacity;
|
|
|
|
}
|
|
|
|
table->elements[h].symbol = name;
|
|
|
|
table->elements[h].object = object;
|
|
|
|
table->count += 1;
|
|
|
|
}
|