forked from ground/ground
added solstice support to the collections lib
This commit is contained in:
@@ -5,6 +5,6 @@
|
|||||||
#include "hashmap.h"
|
#include "hashmap.h"
|
||||||
|
|
||||||
void ground_init(GroundScope* scope) {
|
void ground_init(GroundScope* scope) {
|
||||||
groundAddValueToScope(scope, "List", createListStruct());
|
initLists(scope);
|
||||||
groundAddValueToScope(scope, "Hashmap", createHashmapStruct());
|
initHashmaps(scope);
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,8 @@
|
|||||||
#include <uthash.h>
|
#include <uthash.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
GroundStruct hashmapStruct = {};
|
||||||
|
|
||||||
GroundValue hashmapStructSet(GroundScope* scope, List args) {
|
GroundValue hashmapStructSet(GroundScope* scope, List args) {
|
||||||
char* key = args.values[0].data.stringVal;
|
char* key = args.values[0].data.stringVal;
|
||||||
GroundValue value = args.values[1];
|
GroundValue value = args.values[1];
|
||||||
@@ -117,8 +119,14 @@ GroundValue hashmapStructRemoveIfPresent(GroundScope* scope, List args) {
|
|||||||
return groundCreateValue(BOOL, true);
|
return groundCreateValue(BOOL, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
GroundValue createHashmapStruct() {
|
GroundValue hashmapStructConstructor(GroundScope* scope, List args) {
|
||||||
GroundStruct hashmapStruct = groundCreateStruct();
|
GroundValue value = groundCreateValue(CUSTOM, &hashmapStruct);
|
||||||
|
value.type = CUSTOM;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void initHashmaps(GroundScope* scope) {
|
||||||
|
hashmapStruct = groundCreateStruct();
|
||||||
|
|
||||||
groundAddFieldToStruct(&hashmapStruct, "private_ptr", groundCreateValue(INT, 0));
|
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, "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
|
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);
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,6 @@ typedef struct {
|
|||||||
UT_hash_handle hh;
|
UT_hash_handle hh;
|
||||||
} HashmapItem;
|
} HashmapItem;
|
||||||
|
|
||||||
GroundValue createHashmapStruct();
|
void initHashmaps(GroundScope* scope);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
const uint8_t STARTING_ELEMENTS = 2;
|
const uint8_t STARTING_ELEMENTS = 2;
|
||||||
|
GroundStruct listStruct = {};
|
||||||
|
|
||||||
|
|
||||||
GroundValue appendToListStruct(GroundScope* scope, List args) {
|
GroundValue appendToListStruct(GroundScope* scope, List args) {
|
||||||
@@ -446,16 +447,42 @@ GroundValue reserveListStruct(GroundScope* scope, List args) {
|
|||||||
return groundCreateValue(BOOL, true);
|
return groundCreateValue(BOOL, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
GroundValue createListStruct() {
|
GroundValue listStructConstructor(GroundScope* scope, List args) {
|
||||||
GroundStruct listStruct = groundCreateStruct();
|
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));
|
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, "size", groundCreateValue(INT, 0)); // number of elements
|
||||||
groundAddFieldToStruct(&listStruct, "memSize", groundCreateValue(INT, sizeof(GroundValue) * STARTING_ELEMENTS)); // number of bytes allocated
|
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, "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, "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, "insert", insertIntoListStruct, INT, 2, ANY, "value", INT, "index"); // insert value at index
|
||||||
groundAddFunctionToStruct(&listStruct, "remove", listStructDelete, INT, 1, INT, "index"); // delete 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, "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
|
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");
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,6 @@
|
|||||||
|
|
||||||
extern const uint8_t STARTING_ELEMENTS;
|
extern const uint8_t STARTING_ELEMENTS;
|
||||||
|
|
||||||
GroundValue createListStruct();
|
void initLists(GroundScope* scope);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user