Added some array utils.

This commit is contained in:
2021-06-13 18:16:35 -07:00
parent 928c3c838e
commit 77389ffd15
16 changed files with 242 additions and 45 deletions

77
src/util/array.c Normal file
View File

@ -0,0 +1,77 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "array.h"
void * arrayGet(size_t size, void *array, int32_t index) {
// Cast to char array
char *cArray = (char *)array;
return cArray + (index * (size/sizeof(char)));
}
void arrayShuffle(size_t size, void *array, int32_t arrayLength) {
int32_t x, y;
void *itemDest, *itemSource;
void *temporary = malloc(size);
for(x = 0; x < arrayLength-1; x++) {
// Select random element from remaining elements.
y = u8randRange(x, arrayLength);
itemDest = arrayGet(size, array, y);
itemSource = arrayGet(size, array, x);
// Copy the data into temporary variable
memcpy(temporary, itemDest, size);
// Take the item at this current index, and store from where we just took
memcpy(itemDest, itemSource, size);
// Store the thing from where we just took into here.
memcpy(itemSource, temporary, size);
}
// Cleanup
free(temporary);
}
int32_t arrayFind(size_t size, void *array, int32_t length, void *value) {
int32_t i;
void *item;
for(i = 0; i < length; i++) {
// Get the item
item = arrayGet(size, array, i);
// Compare the bits directly.
if(memcmp(item, &value, size) == 0) return i;
}
// No find.
return -1;
}
void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) {
qsort(array, length, size, sort);
}
void arraySortInt32(int32_t *array, int32_t length) {
arraySort(sizeof(int32_t), array, length, &_arraySorterInt32);
}
int32_t _arraySorterInt32(const void* left, const void* right) {
return *((int32_t *)left) - *((int32_t *)right);
}
void arraySortUint8(uint8_t *array, int32_t length) {
arraySort(sizeof(uint8_t), array, length, &_arraySorterUint8);
}
int32_t _arraySorterUint8(const void* left, const void* right) {
return *((uint8_t *)left) - *((uint8_t *)right);
}

69
src/util/array.h Normal file
View File

@ -0,0 +1,69 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
/**
* Retreive the pointer to an elment within the array of unknown type.
*
* @param size Size of each element within the array.
* @param array Array to get from.
* @param index Index to get
* @return Pointer to the item in the array.
*/
void * arrayGet(size_t size, void *array, int32_t index);
/**
* Randomizes the contents of an array.
*
* @param size Size of each element within the array.
* @param array Array to shuffle.
* @param arrayLength Length of the array.
*/
void arrayShuffle(size_t size, void *array, int32_t arrayLength);
/**
* Find the index within the array that matches the given value.
*
* @param size Size of each element within the array.
* @param array Array to get from.
* @param length Max length to check to.
* @param value Value to look for. This is a literal compare of value == value.
* @returns The index within the array that matches, or -1 if no match.
*/
int32_t arrayFind(size_t size, void *array, int32_t length, void *value);
/**
* Sort an array based on the result of an array sort callback.
*
* @param size Size of each element within the array.
* @param array Array to sort.
* @param length Max length to sort to.
* @param sort Callback to use to sort.
*/
void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort);
/**
* Sort an int32 array.
*
* @param array Array to sort.
* @param length Max length to sort.
*/
void arraySortInt32(int32_t *array, int32_t length);
/** Internal int32_t array sorter. */
int32_t _arraySorterInt32(void *left, void*right);
/**
* Sort a uint8_t array.
*
* @param array Array to sort.
* @param length Max Length to sort.
*/
void arraySortUint8(uint8_t *array, int32_t length);
/** Internal uint8_t array sorter. */
int32_t _arraySorterUint8(const void* left, const void* right);