forked from ground/ground
added more list functions
This commit is contained in:
@@ -32,7 +32,7 @@ GroundValue appendToListStruct(GroundScope* scope, List args) {
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
GroundVariable* capacityField = groundFindVariable(scope, "capacity");
|
||||
if (sizeField == NULL) {
|
||||
if (capacityField == NULL) {
|
||||
ERROR("A field called \"capacity\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
@@ -51,7 +51,7 @@ GroundValue appendToListStruct(GroundScope* scope, List args) {
|
||||
ERROR("Failed to allocate memory when increasing list size!", "MemoryAllocationFailed");
|
||||
}
|
||||
|
||||
ptrField->value.data.intVal = (uint64_t)items;
|
||||
ptrField->value.data.intVal = (int64_t)items;
|
||||
}
|
||||
|
||||
items[sizeField->value.data.intVal - 1] = newValue;
|
||||
@@ -70,7 +70,7 @@ GroundValue listStructAt(GroundScope* scope, List args) {
|
||||
if (sizeField == NULL) {
|
||||
ERROR("A field called \"size\" was not found", "FieldNotFound");
|
||||
}
|
||||
uint64_t size = (uint64_t)sizeField->value.data.intVal;
|
||||
int64_t size = sizeField->value.data.intVal;
|
||||
if (index >= size) {
|
||||
char buffer[512];
|
||||
sprintf(buffer, "Attempt to access list at index %ld when list is of length %ld", index, size);
|
||||
@@ -108,12 +108,12 @@ GroundValue clearListStruct(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundVariable* capacityField = groundFindVariable(scope, "capacity");
|
||||
if (sizeField == NULL) {
|
||||
if (capacityField == NULL) {
|
||||
ERROR("A field called \"capacity\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
sizeField->value.data.intVal = 0;
|
||||
ptrField->value.data.intVal = (uint64_t)calloc(STARTING_ELEMENTS, sizeof(GroundValue));
|
||||
ptrField->value.data.intVal = (int64_t)calloc(STARTING_ELEMENTS, sizeof(GroundValue));
|
||||
capacityField->value.data.intVal = STARTING_ELEMENTS;
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * STARTING_ELEMENTS;
|
||||
|
||||
@@ -133,7 +133,6 @@ GroundValue insertIntoListStruct(GroundScope* scope, List args) {
|
||||
ERROR("A field called \"size\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
|
||||
GroundVariable* memSizeField = groundFindVariable(scope, "memSize");
|
||||
if (memSizeField == NULL) {
|
||||
ERROR("A field called \"memSize\" was not found", "FieldNotFound");
|
||||
@@ -146,7 +145,7 @@ GroundValue insertIntoListStruct(GroundScope* scope, List args) {
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
GroundVariable* capacityField = groundFindVariable(scope, "capacity");
|
||||
if (sizeField == NULL) {
|
||||
if (capacityField == NULL) {
|
||||
ERROR("A field called \"capacity\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
@@ -170,7 +169,8 @@ GroundValue insertIntoListStruct(GroundScope* scope, List args) {
|
||||
if (newBuffer == NULL) {
|
||||
ERROR("Failed to allocate memory when increasing list size!", "MemoryAllocationFailed");
|
||||
}
|
||||
ptrField->value.data.intVal = (uint64_t)newBuffer;
|
||||
ptrField->value.data.intVal = (int64_t)newBuffer;
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * capacity;
|
||||
|
||||
// copy elements from 0 to i-1 into new buffer
|
||||
memcpy(newBuffer, items, sizeof(GroundValue) * index);
|
||||
@@ -211,7 +211,7 @@ GroundValue listStructDelete(GroundScope* scope, List args) {
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
GroundVariable* capacityField = groundFindVariable(scope, "capacity");
|
||||
if (sizeField == NULL) {
|
||||
if (capacityField == NULL) {
|
||||
ERROR("A field called \"capacity\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
@@ -239,11 +239,10 @@ GroundValue listStructDelete(GroundScope* scope, List args) {
|
||||
if (newBuffer == NULL) {
|
||||
ERROR("Failed to allocate memory when decreasing list size!", "MemoryAllocationFailed");
|
||||
}
|
||||
ptrField->value.data.intVal = (uint64_t)newBuffer;
|
||||
ptrField->value.data.intVal = (int64_t)newBuffer;
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * capacity;
|
||||
|
||||
// copy elements from 0 to i-1 into new buffer
|
||||
|
||||
|
||||
if (index > 0)
|
||||
memcpy(newBuffer, items, sizeof(GroundValue) * index);
|
||||
// copy elements 0 to i+1 into new buffer
|
||||
@@ -350,9 +349,96 @@ GroundValue listStructContains(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundValue reverseListStruct(GroundScope* scope, List args) {
|
||||
GroundVariable* sizeField = groundFindVariable(scope, "size");
|
||||
if (sizeField == NULL) {
|
||||
ERROR("A field called \"size\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t size = sizeField->value.data.intVal;
|
||||
|
||||
GroundVariable* capacityField = groundFindVariable(scope, "capacity");
|
||||
if (capacityField == NULL) {
|
||||
ERROR("A field called \"capacity\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "__ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"__ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
GroundValue* newBuffer = calloc(capacity, sizeof(GroundValue));
|
||||
|
||||
int z = 0;
|
||||
for (int i = size - 1; i >= 0; i--, z++) {
|
||||
newBuffer[z] = items[i];
|
||||
}
|
||||
|
||||
ptrField->value.data.intVal = (int64_t)newBuffer;
|
||||
free(items);
|
||||
|
||||
return groundCreateValue(INT, 0);
|
||||
}
|
||||
|
||||
GroundValue findListStruct(GroundScope* scope, List args) {
|
||||
GroundValue targetValue = args.values[0];
|
||||
|
||||
GroundVariable* sizeField = groundFindVariable(scope, "size");
|
||||
if (sizeField == NULL) {
|
||||
ERROR("A field called \"size\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t size = sizeField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "__ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"__ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
for (uint64_t i = 0; i < size; i++) {
|
||||
if (areGroundValuesEqual(items[i], targetValue)) {
|
||||
return groundCreateValue(INT, i);
|
||||
}
|
||||
}
|
||||
|
||||
return groundCreateValue(INT, (int64_t)-1);
|
||||
}
|
||||
|
||||
GroundValue reserveListStruct(GroundScope* scope, List args) {
|
||||
int64_t amount = args.values[0].data.intVal;
|
||||
|
||||
GroundVariable* capacityField = groundFindVariable(scope, "capacity");
|
||||
if (capacityField == NULL) {
|
||||
ERROR("A field called \"capacity\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "__ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"__ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
GroundVariable* memSizeField = groundFindVariable(scope, "memSize");
|
||||
if (memSizeField == NULL) {
|
||||
ERROR("A field called \"memSize\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
if (capacity > amount) {
|
||||
return groundCreateValue(BOOL, false);
|
||||
}
|
||||
|
||||
capacityField->value.data.intVal = amount;
|
||||
items = realloc(items, sizeof(GroundValue) * amount);
|
||||
if (items == NULL) {
|
||||
ERROR("Failed to allocate memory when reserving list space!", "MemoryAllocationFailed");
|
||||
}
|
||||
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * amount;
|
||||
|
||||
return groundCreateValue(BOOL, true);
|
||||
}
|
||||
|
||||
GroundValue createListStruct() {
|
||||
GroundStruct listStruct = groundCreateStruct();
|
||||
|
||||
@@ -372,7 +458,9 @@ GroundValue createListStruct() {
|
||||
groundAddFunctionToStruct(&listStruct, "set", listStructSet, INT, 2, INT, "value", INT, "index"); // replace a value at an index
|
||||
groundAddFunctionToStruct(&listStruct, "isEmpty", listStructIsEmpty, BOOL, 0); // returns true if list is empty, otherwise false
|
||||
groundAddFunctionToStruct(&listStruct, "contains", listStructContains, BOOL, 1, INT, "value"); // returns true if value is in list
|
||||
groundAddFunctionToStruct(&listStruct, "reverse", reverseListStruct, CUSTOM, 0); // return list struct in reverse order
|
||||
groundAddFunctionToStruct(&listStruct, "reverse", reverseListStruct, INT, 0); // return list struct in reverse order
|
||||
groundAddFunctionToStruct(&listStruct, "find", findListStruct, INT, 1, INT, "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);
|
||||
}
|
||||
Reference in New Issue
Block a user