12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #define IMPORT_CORE
- #include "core/List.h"
- #include <assert.h>
- #include "core/Utility.h"
- static void ensureCapacity(List* l) {
- if(l->length >= l->capacity) {
- reserveListEntries(l, l->capacity + maxSize(4lu, l->capacity / 4));
- }
- }
- static void setSize(List* l, size_t n) {
- l->capacity = n;
- l->data = cReallocate(l->data, l->dataSize * n);
- }
- static void* getPointer(const List* l, size_t index) {
- return (char*)l->data + (l->dataSize * index);
- }
- void initList(List* l, size_t dataSize) {
- *l = (List){0, 0, dataSize, nullptr};
- }
- void destroyList(List* l) {
- cFree(l->data);
- *l = (List){0};
- }
- void reserveListEntries(List* l, size_t n) {
- if(n > l->capacity) {
- setSize(l, n);
- }
- }
- void addListData(List* l, const void* data) {
- memcpy(addEmptyListData(l), data, l->dataSize);
- }
- void addLastListData(List* l) {
- addListData(l, getListLast(l));
- }
- void* addEmptyListData(List* l) {
- ensureCapacity(l);
- return getPointer(l, l->length++);
- }
- void* getListIndex(const List* l, size_t index) {
- assert(index < l->length);
- return getPointer(l, index);
- }
- void* getListLast(const List* l) {
- return getListIndex(l, l->length - 1);
- }
- void clearList(List* l) {
- l->length = 0;
- }
- void removeListIndexBySwap(List* l, size_t index) {
- assert(index < l->length);
- size_t length = l->length - 1;
- if(index != length) {
- memcpy(getPointer(l, index), getPointer(l, length), l->dataSize);
- }
- l->length = length;
- }
- void removeListIndex(List* l, size_t index) {
- assert(index < l->length);
- l->length--;
- char* p = getPointer(l, index);
- char* end = getListEnd(l);
- while(p != end) {
- *p = *(p + l->dataSize);
- p++;
- }
- }
- void removeListLast(List* l) {
- removeListIndexBySwap(l, l->length - 1);
- }
- void* getListStart(const List* l) {
- return l->data;
- }
- void* getListEnd(const List* l) {
- return getPointer(l, l->length);
- }
|