/* Copyright (c) 2022 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "utilities/IOUtilities.h" #include "utilities/StrideArray.h" #include #include #include #include StrideArray StrideArray_init(unsigned int count, unsigned int stride, void * data, bool takeOwnership, bool copy) { assert(stride > 0); StrideArray array = {data, count, stride, takeOwnership}; if (data == NULL) { if (count > 0) { array.data = malloc(count * stride); } array.dataOwned = true; } else if (copy) { array.data = memdup(data, count * stride); } return array; } void StrideArray_free(StrideArray * array) { if (array->dataOwned) { free(array->data); } } void * StrideArray_entryAtIndex(StrideArray * array, unsigned int index) { if (index >= array->count) { return NULL; } return array->data + index * array->stride; } unsigned int StrideArray_indexOfEntry(StrideArray * array, void * entry) { if (entry < array->data) { return UINT_MAX; } size_t offset = entry - array->data; if (offset % array->stride > 0) { return UINT_MAX; } offset /= array->stride; if (offset >= array->count) { return UINT_MAX; } return offset; } void * StrideArray_append(StrideArray * array, void * data) { if (array->dataOwned) { array->data = realloc(array->data, (array->count + 1) * array->stride); if (data != NULL) { memcpy(array->data + array->count * array->stride, data, array->stride); } array->count++; return array->data + (array->count - 1) * array->stride; } return NULL; } void * StrideArray_insert(StrideArray * array, unsigned int index, void * data) { if (array->dataOwned) { array->data = realloc(array->data, (array->count + 1) * array->stride); if (index < array->count) { memmove(array->data + (index + 1) * array->stride, array->data + index * array->stride, array->stride * (array->count - index)); } if (data != NULL) { memcpy(array->data + index * array->stride, data, array->stride); } array->count++; return array->data + index * array->stride; } return NULL; } void StrideArray_remove(StrideArray * array, unsigned int index) { if (array->dataOwned && index < array->count) { if (index < array->count - 1) { memmove(array->data + index * array->stride, array->data + (index + 1) * array->stride, array->stride * (array->count - index - 1)); } array->count--; } } void StrideArray_removeAll(StrideArray * array) { if (array->dataOwned) { array->count = 0; } } void StrideArray_replace(StrideArray * array, unsigned int index, void * data) { if (index < array->count) { memcpy(array->data + index * array->stride, data, array->stride); } }