From aa0c71a47be2732758fc52557ceb1c92d73b0d8b Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Sun, 12 Apr 2026 09:47:27 +1000 Subject: [PATCH] added solstice support to the collections lib --- libs/collections/collections.c | 4 ++-- libs/collections/hashmap.c | 15 +++++++++++--- libs/collections/hashmap.h | 2 +- libs/collections/list.c | 36 ++++++++++++++++++++++++++++++---- libs/collections/list.h | 2 +- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/libs/collections/collections.c b/libs/collections/collections.c index 639fc9d..eef9335 100644 --- a/libs/collections/collections.c +++ b/libs/collections/collections.c @@ -5,6 +5,6 @@ #include "hashmap.h" void ground_init(GroundScope* scope) { - groundAddValueToScope(scope, "List", createListStruct()); - groundAddValueToScope(scope, "Hashmap", createHashmapStruct()); + initLists(scope); + initHashmaps(scope); } \ No newline at end of file diff --git a/libs/collections/hashmap.c b/libs/collections/hashmap.c index 553c19c..eb92c4c 100644 --- a/libs/collections/hashmap.c +++ b/libs/collections/hashmap.c @@ -6,6 +6,8 @@ #include #include +GroundStruct hashmapStruct = {}; + GroundValue hashmapStructSet(GroundScope* scope, List args) { char* key = args.values[0].data.stringVal; GroundValue value = args.values[1]; @@ -117,8 +119,14 @@ GroundValue hashmapStructRemoveIfPresent(GroundScope* scope, List args) { return groundCreateValue(BOOL, true); } -GroundValue createHashmapStruct() { - GroundStruct hashmapStruct = groundCreateStruct(); +GroundValue hashmapStructConstructor(GroundScope* scope, List args) { + GroundValue value = groundCreateValue(CUSTOM, &hashmapStruct); + value.type = CUSTOM; + return value; +} + +void initHashmaps(GroundScope* scope) { + hashmapStruct = groundCreateStruct(); groundAddFieldToStruct(&hashmapStruct, "private_ptr", groundCreateValue(INT, 0)); @@ -128,5 +136,6 @@ GroundValue createHashmapStruct() { groundAddFunctionToStruct(&hashmapStruct, "remove", hashmapStructRemove, INT, 1, STRING, "key"); // remove a key from the hashmap, throws KeyNotFound if the key does not exist groundAddFunctionToStruct(&hashmapStruct, "removeIfPresent", hashmapStructRemoveIfPresent, BOOL, 1, STRING, "key"); // remove a key from the hashmap, returns true if the key was removed or false if it didn't exist - return groundCreateValue(STRUCTVAL, hashmapStruct); + groundAddNativeFunction(scope, "newHashmap", hashmapStructConstructor, CUSTOM, 0); + groundAddNativeFunction(scope, "Hashmap_SOLS_CONSTRUCTOR", hashmapStructConstructor, CUSTOM, 0); } \ No newline at end of file diff --git a/libs/collections/hashmap.h b/libs/collections/hashmap.h index 1258c6a..5c4c53d 100644 --- a/libs/collections/hashmap.h +++ b/libs/collections/hashmap.h @@ -11,6 +11,6 @@ typedef struct { UT_hash_handle hh; } HashmapItem; -GroundValue createHashmapStruct(); +void initHashmaps(GroundScope* scope); #endif \ No newline at end of file diff --git a/libs/collections/list.c b/libs/collections/list.c index 126dc11..77b43dd 100644 --- a/libs/collections/list.c +++ b/libs/collections/list.c @@ -8,6 +8,7 @@ #include const uint8_t STARTING_ELEMENTS = 2; +GroundStruct listStruct = {}; GroundValue appendToListStruct(GroundScope* scope, List args) { @@ -446,16 +447,42 @@ GroundValue reserveListStruct(GroundScope* scope, List args) { return groundCreateValue(BOOL, true); } -GroundValue createListStruct() { - GroundStruct listStruct = groundCreateStruct(); +GroundValue listStructConstructor(GroundScope* scope, List args) { + GroundValue value = groundCreateValue(CUSTOM, &listStruct); + + int64_t startingCapacity = args.values[0].data.intVal; + if (startingCapacity < 1) { + ERROR("List can't be less than 1 element in capacity on initialization!", "OutOfBounds"); + } + + GroundObjectField *sizeField = groundFindField(*value.data.customVal, "size"); + GroundObjectField *capacityField = groundFindField(*value.data.customVal, "capacity"); + GroundObjectField *memSizeField = groundFindField(*value.data.customVal, "memSize"); + GroundObjectField *ptrField = groundFindField(*value.data.customVal, "private_ptr"); GroundValue* items = calloc(STARTING_ELEMENTS, sizeof(GroundValue)); + if (items == NULL) { + ERROR("Failed to allocate memory while creating list!", "MemoryAllocationFailed"); + } + sizeField->value.data.intVal = 0; + capacityField->value.data.intVal = startingCapacity; + memSizeField->value.data.intVal = sizeof(GroundValue) * startingCapacity; + ptrField->value.data.intVal = (int64_t)items; + + value.type = CUSTOM; + + return value; +} + +void initLists(GroundScope* scope) { + listStruct = groundCreateStruct(); groundAddFieldToStruct(&listStruct, "size", groundCreateValue(INT, 0)); // number of elements groundAddFieldToStruct(&listStruct, "memSize", groundCreateValue(INT, sizeof(GroundValue) * STARTING_ELEMENTS)); // number of bytes allocated groundAddFieldToStruct(&listStruct, "capacity", groundCreateValue(INT, STARTING_ELEMENTS)); // number of elements that can fit in the currently allocated space - groundAddFieldToStruct(&listStruct, "private_ptr", groundCreateValue(INT, items)); // pointer to internal list struct + groundAddFieldToStruct(&listStruct, "private_ptr", groundCreateValue(INT, 0)); // pointer to internal list struct + groundAddFunctionToStruct(&listStruct, "init", listStructConstructor, INT, 1, INT, "startingCapacity"); // init struct (ground constructor) groundAddFunctionToStruct(&listStruct, "append", appendToListStruct, INT, 1, ANY, "value"); // append item to end of list groundAddFunctionToStruct(&listStruct, "insert", insertIntoListStruct, INT, 2, ANY, "value", INT, "index"); // insert value at index groundAddFunctionToStruct(&listStruct, "remove", listStructDelete, INT, 1, INT, "index"); // delete value at index @@ -468,5 +495,6 @@ GroundValue createListStruct() { groundAddFunctionToStruct(&listStruct, "find", findListStruct, INT, 1, ANY, "value"); // return index of value in list, if not found, returns -1 groundAddFunctionToStruct(&listStruct, "reserve", reserveListStruct, BOOL, 1, INT, "amount"); // ensure list capacity >= amount. returns true if the list's capacity was expanded - return groundCreateValue(STRUCTVAL, listStruct); + groundAddNativeFunction(scope, "newList", listStructConstructor, CUSTOM, 1, INT, "startingCapacity"); + groundAddNativeFunction(scope, "List_SOLS_CONSTRUCTOR", listStructConstructor, CUSTOM, 1, INT, "startingCapacity"); } \ No newline at end of file diff --git a/libs/collections/list.h b/libs/collections/list.h index 0b7b7d1..644ecc3 100644 --- a/libs/collections/list.h +++ b/libs/collections/list.h @@ -6,6 +6,6 @@ extern const uint8_t STARTING_ELEMENTS; -GroundValue createListStruct(); +void initLists(GroundScope* scope); #endif \ No newline at end of file