Arrays now store Toy_Values as elements

Fixed #136
This commit is contained in:
2024-10-18 13:06:42 +11:00
parent 09e4cb7b03
commit c3ee92fcef
4 changed files with 24 additions and 33 deletions

View File

@@ -5,15 +5,19 @@
#include <stdlib.h>
Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) {
//TODO: slip in a call to free the complex values here
if (capacity == 0) {
free(paramArray);
return NULL;
}
Toy_Array* array = realloc(paramArray, capacity + sizeof(Toy_Array));
unsigned int originalCapacity = paramArray == NULL ? 0 : paramArray->capacity;
Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array));
if (array == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Array' of %d capacity\n" TOY_CC_RESET, (int)capacity);
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to resize a 'Toy_Array' from %d to %d capacity\n" TOY_CC_RESET, (int)originalCapacity, (int)capacity);
exit(-1);
}

View File

@@ -1,25 +1,13 @@
#pragma once
#include "toy_common.h"
#include "toy_value.h"
//standard generic array
typedef struct Toy_Array { //32 | 64 BITNESS
unsigned int capacity; //4 | 4
unsigned int count; //4 | 4
char data[]; //- | -
Toy_Value data[]; //- | -
} Toy_Array; //8 | 8
TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity);
#define TOY_ALLOCATE_ARRAY(type, count) \
Toy_resizeArray(NULL, sizeof(type)*(count))
#define TOY_FREE_ARRAY(type, array) \
Toy_resizeArray(array, 0)
#define TOY_ADJUST_ARRAY(type, array, newCapacity) \
Toy_resizeArray(array, sizeof(type) * newCapacity)
#define TOY_DOUBLE_ARRAY_CAPACITY(type, array) \
Toy_resizeArray(array, sizeof(type) * array->capacity < 8 ? sizeof(type) * 8 : sizeof(type) * array->capacity * 2)

View File

@@ -1,7 +1,6 @@
#pragma once
#include "toy_common.h"
#include "toy_value.h"
//key-value entry, and probe sequence length - https://programming.guide/robin-hood-hashing.html