Added a dynamic array.

This commit is contained in:
2021-09-05 00:34:29 -07:00
parent ee9334478f
commit d43c582629
20 changed files with 403 additions and 25 deletions

View File

@ -11,6 +11,10 @@ void * arrayGet(size_t size, void *array, int32_t index) {
return (char *)array + (index * (size/sizeof(char)));
}
void arraySet(size_t size, void *array, int32_t index, void *data) {
memcpy(arrayGet(size, array, index), data, size);
}
void arrayShuffle(size_t size, void *array, int32_t arrayLength) {
int32_t x, y;
void *itemDest, *itemSource;
@ -104,4 +108,19 @@ void arrayRewind(size_t size, void *source, int32_t length, int32_t start,
// Cleanup the temporary array.
free(temporary);
}
void arraySplice(
size_t size, void *array, int32_t start, int32_t count, int32_t length
) {
// Take the end of the array...
int32_t takeStart = start + count;
int32_t takeLength = length - takeStart;
void *temporary = malloc(size * takeLength);
arrayCopy(size, arrayGet(size, array, takeStart), takeLength, temporary);
// Now copy it back into the original array
arrayCopy(size, temporary, takeLength, arrayGet(size, array, start));
free(temporary);
}

View File

@ -18,6 +18,17 @@
*/
void * arrayGet(size_t size, void *array, int32_t index);
/**
* Set an object into the array.
*
* @param size Size of the element to set.
* @param array Array to set in to.
* @param index Index to set into.
* @param data Data to set into the array.
* @return void*
*/
void arraySet(size_t size, void *array, int32_t index, void *data);
/**
* Randomizes the contents of an array.
*
@ -114,4 +125,18 @@ int32_t arrayFindString(char **array, int32_t arrayLength, char *value);
*/
void arrayRewind(size_t size, void *source, int32_t length, int32_t start,
int32_t count
);
/**
* Remove some elements from an array. This will cause the elements after the
* removed items to be shifted down into their place.
*
* @param size Size of each element within the array.
* @param array Array itself.
* @param start The first index you want to remove.
* @param count The count of elements to remove.
* @param length The length of the total array.
*/
void arraySplice(
size_t size, void *array, int32_t start, int32_t count, int32_t length
);

43
src/util/dynarray.c Normal file
View File

@ -0,0 +1,43 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dynarray.h"
void dynArrayInit(dynarray_t *array, size_t size, int32_t total) {
array->total = total;
array->size = size;
array->length = 0;
array->data = malloc(size * total);
}
void * dynArrayGet(dynarray_t *array, int32_t i) {
return arrayGet(array->size, array->data, i);
}
void dynArraySet(dynarray_t *array, int32_t i, void *data) {
array->length = mathMax(array->length, i+1);
arraySet(array->size, array->data, i, data);
}
int32_t dynArrayPush(dynarray_t *array, void *data) {
int32_t i = array->length;
dynArraySet(array, i, data);
return i;
}
void dynArrayPop(dynarray_t *array) {
array->length -= 1;
}
void dynArraySplice(dynarray_t *array, int32_t start, int32_t count) {
arraySplice(array->size, array->data, start, count, array->length);
array->length -= count;
}
void dynArrayDispose(dynarray_t *array) {
free(array->data);
}

75
src/util/dynarray.h Normal file
View File

@ -0,0 +1,75 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "array.h"
/**
* Create a dynamic array. Dynamic arrays are a more highlevel array style that
* can offer great flexibility and ease-of-use, at the cost of memory, memory
* fragmentation and some slight performance. Great for when you need something
* like a traditional array, without the hassle of a linked list.
*
* @param array Array to initialize.
* @param size Size of each element within the array.
* @param total Total number of elements that the array can hold.
*/
void dynArrayInit(dynarray_t *array, size_t size, int32_t total);
/**
* Returns the pointer to an index within the array.
*
* @param array Array to get from.
* @param i Index to get
* @return Pointer to that element.
*/
void * dynArrayGet(dynarray_t *array, int32_t i);
/**
* Set something into the array.
*
* @param array Array to set into.
* @param i Index to set into.
* @param data Pointer to the data to set.
*/
void dynArraySet(dynarray_t *array, int32_t i, void *data);
/**
* Push an element into the array.
*
* @param array Array to push in to.
* @param data Pointer to the data to push.
* @return The index that the data was pushed in to.
*/
int32_t dynArrayPush(dynarray_t *array, void *data);
/**
* "Pops" (Removes) the last element from the array.
*
* @param array Array to pop.
*/
void dynArrayPop(dynarray_t *array);
/**
* Removes elements from out of the array, and shuffles the remaining items.
* This is useful for removing a specific item from an array and keeping the
* holes out of the array. This will readjust the indexes of all elements that
* appear after the spliced index.
*
* @param array Array to splice.
* @param start First index to splice.
* @param count Count of elements to splice
*/
void dynArraySplice(dynarray_t *array, int32_t start, int32_t count);
/**
* Dispose a previously created dynamic array.
*
* @param array Dynamic array to dispose.
*/
void dynArrayDispose(dynarray_t *array);