/**
 * 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 pointer.
 * 
 * @param size Size of each element within the array.
 * @param array Array to get from.
 * @param length Max length to check to.
 * @param value Pointer to the value to look for. This is memory compared.
 * @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);

/**
 * Compares each item in an array with the specified value, and returns true if 
 * found within the array. This is a shorthand for arrayFind without an int32_t 
 * being returned.
 * 
 * @param size Size of each element within the array.
 * @param array Array to get from.
 * @param length Max length to check to.
 * @param value Pointer to the value to look for. This is memory compared.
 * @returns True if the array contains the value, otherwise false.
 */
bool arrayContains(size_t size, void *array, int32_t length, void *value);

/**
 * Copies the raw contents from the source array to the destination array.
 * 
 * @param size Size of each element within the array.
 * @param source Source array.
 * @param length Length of the source array.
 * @param dest Destination array.
 */
void arrayCopy(size_t size, void *source, int32_t length, void *dest);

/**
 * 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(const void *left, const 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);

/**
 * Find the index of a string within a string array. (Array of char pointers).
 * 
 * @param array Array to check
 * @param arrayLength Length of th earray.
 * @param value The value to search for.
 * @return The index that the strings exists within the array.
 */
int32_t arrayFindString(char **array, int32_t arrayLength, char *value);