mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Wrote tests for Toy_Table
I hope that's it, I don't wanna do that again XD
This commit is contained in:
@@ -9,59 +9,24 @@
|
||||
#define MIN_CAPACITY 16
|
||||
|
||||
//utils
|
||||
static Toy_Table* adjustTableCapacity(Toy_Table* oldTable, unsigned int newCapacity) {
|
||||
//allocate and zero a new table in memory
|
||||
Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table));
|
||||
|
||||
newTable->capacity = newCapacity;
|
||||
newTable->count = 0;
|
||||
newTable->minPsl = 0;
|
||||
newTable->maxPsl = 0;
|
||||
|
||||
//unlike other structures, the empty space in a table needs to be null
|
||||
memset(newTable + 1, 0, newTable->capacity * sizeof(Toy_TableEntry));
|
||||
|
||||
if (oldTable == NULL) { //for initial allocations
|
||||
return newTable;
|
||||
}
|
||||
|
||||
//for each entry in the old table, copy it into the new table
|
||||
for (int i = 0; i < oldTable->capacity; i++) {
|
||||
Toy_insertTable(&newTable, oldTable->data[i].key, oldTable->data[i].value);
|
||||
}
|
||||
|
||||
//clean up and return
|
||||
free(oldTable);
|
||||
return newTable;
|
||||
}
|
||||
|
||||
//exposed functions
|
||||
Toy_Table* Toy_allocateTable() {
|
||||
return adjustTableCapacity(NULL, MIN_CAPACITY);
|
||||
}
|
||||
|
||||
void Toy_freeTable(Toy_Table* table) {
|
||||
//TODO: slip in a call to free the complex values here
|
||||
|
||||
free(table);
|
||||
}
|
||||
|
||||
void Toy_insertTable(Toy_Table** table, Toy_Value key, Toy_Value value) {
|
||||
if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET);
|
||||
exit(-1); //TODO: #127
|
||||
}
|
||||
|
||||
//expand the capacity
|
||||
if ((*table)->capacity < (*table)->count * (1 / 0.75f)) {
|
||||
(*table) = adjustTableCapacity(*table, (*table)->capacity * 2);
|
||||
}
|
||||
|
||||
//insert
|
||||
static void probeAndInsert(Toy_Table** table, Toy_Value key, Toy_Value value) {
|
||||
//make the entry
|
||||
unsigned int probe = Toy_hashValue(key) % (*table)->capacity;
|
||||
Toy_TableEntry entry = (Toy_TableEntry){ .key = key, .value = value, .psl = 0 };
|
||||
|
||||
//probe
|
||||
while (true) {
|
||||
//if we're overriding an existing value
|
||||
if (TOY_VALUE_IS_EQUAL((*table)->data[probe].key, key)) {
|
||||
(*table)->data[probe] = entry;
|
||||
|
||||
//TODO: benchmark the psl optimisation
|
||||
(*table)->minPsl = entry.psl < (*table)->minPsl ? entry.psl : (*table)->minPsl;
|
||||
(*table)->maxPsl = entry.psl > (*table)->maxPsl ? entry.psl : (*table)->maxPsl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//if this spot is free, insert and return
|
||||
if (TOY_VALUE_IS_NULL((*table)->data[probe].key)) {
|
||||
(*table)->data[probe] = entry;
|
||||
@@ -88,7 +53,65 @@ void Toy_insertTable(Toy_Table** table, Toy_Value key, Toy_Value value) {
|
||||
}
|
||||
}
|
||||
|
||||
Toy_Value Toy_lookupTableValue(Toy_Table** table, Toy_Value key) {
|
||||
static Toy_Table* adjustTableCapacity(Toy_Table* oldTable, unsigned int newCapacity) {
|
||||
//allocate and zero a new table in memory
|
||||
Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table));
|
||||
|
||||
if (newTable == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Table' of %u capacity\n" TOY_CC_RESET, newCapacity);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
newTable->capacity = newCapacity;
|
||||
newTable->count = 0;
|
||||
newTable->minPsl = 0;
|
||||
newTable->maxPsl = 0;
|
||||
|
||||
//unlike other structures, the empty space in a table needs to be null
|
||||
memset(newTable + 1, 0, newTable->capacity * sizeof(Toy_TableEntry));
|
||||
|
||||
if (oldTable == NULL) { //for initial allocations
|
||||
return newTable;
|
||||
}
|
||||
|
||||
//for each entry in the old table, copy it into the new table
|
||||
for (int i = 0; i < oldTable->capacity; i++) {
|
||||
if (!TOY_VALUE_IS_NULL(oldTable->data[i].key)) {
|
||||
probeAndInsert(&newTable, oldTable->data[i].key, oldTable->data[i].value);
|
||||
}
|
||||
}
|
||||
|
||||
//clean up and return
|
||||
free(oldTable);
|
||||
return newTable;
|
||||
}
|
||||
|
||||
//exposed functions
|
||||
Toy_Table* Toy_allocateTable() {
|
||||
return adjustTableCapacity(NULL, MIN_CAPACITY);
|
||||
}
|
||||
|
||||
void Toy_freeTable(Toy_Table* table) {
|
||||
//TODO: slip in a call to free the complex values here
|
||||
|
||||
free(table);
|
||||
}
|
||||
|
||||
void Toy_insertTable(Toy_Table** table, Toy_Value key, Toy_Value value) {
|
||||
if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET);
|
||||
exit(-1); //TODO: #127
|
||||
}
|
||||
|
||||
//expand the capacity
|
||||
if ((*table)->count > (*table)->capacity * 0.8) {
|
||||
(*table) = adjustTableCapacity(*table, (*table)->capacity * 2);
|
||||
}
|
||||
|
||||
probeAndInsert(table, key, value);
|
||||
}
|
||||
|
||||
Toy_Value Toy_lookupTable(Toy_Table** table, Toy_Value key) {
|
||||
if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET);
|
||||
exit(-1); //TODO: #127
|
||||
@@ -96,7 +119,6 @@ Toy_Value Toy_lookupTableValue(Toy_Table** table, Toy_Value key) {
|
||||
|
||||
//lookup
|
||||
unsigned int probe = Toy_hashValue(key) % (*table)->capacity;
|
||||
unsigned int counter = 0;
|
||||
|
||||
while (true) {
|
||||
//found the entry
|
||||
@@ -104,18 +126,17 @@ Toy_Value Toy_lookupTableValue(Toy_Table** table, Toy_Value key) {
|
||||
return (*table)->data[probe].value;
|
||||
}
|
||||
|
||||
//if the psl is too big, or empty slot
|
||||
if ((*table)->data[probe].psl > counter || TOY_VALUE_IS_NULL((*table)->data[probe].key)) {
|
||||
//if its an empty slot
|
||||
if (TOY_VALUE_IS_NULL((*table)->data[probe].key)) {
|
||||
return TOY_VALUE_TO_NULL();
|
||||
}
|
||||
|
||||
//adjust and continue
|
||||
probe = (probe + 1) % (*table)->capacity;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
void Toy_removeTableEntry(Toy_Table** table, Toy_Value key) {
|
||||
void Toy_removeTable(Toy_Table** table, Toy_Value key) {
|
||||
if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET);
|
||||
exit(-1); //TODO: #127
|
||||
@@ -123,7 +144,6 @@ void Toy_removeTableEntry(Toy_Table** table, Toy_Value key) {
|
||||
|
||||
//lookup
|
||||
unsigned int probe = Toy_hashValue(key) % (*table)->capacity;
|
||||
unsigned int counter = 0;
|
||||
unsigned int wipe = probe; //wiped at the end
|
||||
|
||||
while (true) {
|
||||
@@ -132,31 +152,31 @@ void Toy_removeTableEntry(Toy_Table** table, Toy_Value key) {
|
||||
break;
|
||||
}
|
||||
|
||||
//if the psl is too big, or empty slot
|
||||
if ((*table)->data[probe].psl > counter || TOY_VALUE_IS_NULL((*table)->data[probe].key)) {
|
||||
//if its an empty slot
|
||||
if (TOY_VALUE_IS_NULL((*table)->data[probe].key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//adjust and continue
|
||||
probe = (probe + 1) % (*table)->capacity;
|
||||
counter++;
|
||||
}
|
||||
|
||||
//shift down the later entries (past the probing point)
|
||||
//shift along the later entries
|
||||
for (unsigned int i = (*table)->minPsl; i < (*table)->maxPsl; i++) {
|
||||
unsigned int p = (probe + i + 0) % (*table)->capacity; //prev
|
||||
unsigned int u = (probe + i + 1) % (*table)->capacity; //current
|
||||
|
||||
//if the psl is too big, or an empty slot, stop
|
||||
if ((*table)->data[u].psl > (counter + i) || TOY_VALUE_IS_NULL((*table)->data[u].key)) {
|
||||
break;
|
||||
}
|
||||
|
||||
(*table)->data[p] = (*table)->data[u];
|
||||
(*table)->data[p].psl--;
|
||||
wipe = wipe % (*table)->capacity;
|
||||
|
||||
//if you hit something where it should be, or nothing at all, stop
|
||||
if (TOY_VALUE_IS_NULL((*table)->data[u].key) || (*table)->data[p].psl == 0) {
|
||||
wipe = u;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//finally, wipe the removed entry
|
||||
(*table)->data[wipe] = (Toy_TableEntry){ .key = TOY_VALUE_TO_NULL(), .value = TOY_VALUE_TO_NULL(), .psl = 0 };
|
||||
(*table)->count--;
|
||||
}
|
||||
Reference in New Issue
Block a user