Dawn/src/util/array.c

81 lines
2.1 KiB
C

/**
* 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) {
return (char *)array + (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 = randUint8Range(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 and compare.
item = arrayGet(size, array, i);
if(memcmp(item, value, size) == 0) return i;
}
// No find.
return -1;
}
bool arrayContains(size_t size, void *array, int32_t length, void *value) {
int32_t i = arrayFind(size, array, length, value);
return i != -1;
}
void arrayCopy(size_t size, void *source, int32_t length, void *dest) {
memcpy(dest, source, size * length);
}
void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) {
qsort(array, length, size, sort);
}
// Common Sorters:
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);
}