Added Full House

This commit is contained in:
2021-06-23 09:42:13 -07:00
parent d632662520
commit 127257cf99
9 changed files with 132 additions and 45 deletions

View File

@ -8,9 +8,7 @@
#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)));
return (char *)array + (index * (size/sizeof(char)));
}
void arrayShuffle(size_t size, void *array, int32_t arrayLength) {
@ -44,34 +42,38 @@ int32_t arrayFind(size_t size, void *array, int32_t length, void *value) {
void *item;
for(i = 0; i < length; i++) {
// Get the item
// Get the item and compare.
item = arrayGet(size, array, i);
// Compare the bits directly.
if(memcmp(item, &value, size) == 0) return 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);
qsort(array, length, size, (_CoreCrtNonSecureSearchSortCompareFunction)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) {
int32_t _arraySorterInt32(void* left, 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) {
int32_t _arraySorterUint8(void* left, void* right) {
return *((uint8_t *)left) - *((uint8_t *)right);
}

View File

@ -28,16 +28,39 @@ void * arrayGet(size_t size, void *array, int32_t index);
void arrayShuffle(size_t size, void *array, int32_t arrayLength);
/**
* Find the index within the array that matches the given value.
* 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 Value to look for. This is a literal compare of value == value.
* @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.
*
@ -56,7 +79,7 @@ void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort);
*/
void arraySortInt32(int32_t *array, int32_t length);
/** Internal int32_t array sorter. */
int32_t _arraySorterInt32(const void *left, const void* right);
int32_t _arraySorterInt32(void *left, void* right);
/**
* Sort a uint8_t array.
@ -66,4 +89,4 @@ int32_t _arraySorterInt32(const void *left, const void* right);
*/
void arraySortUint8(uint8_t *array, int32_t length);
/** Internal uint8_t array sorter. */
int32_t _arraySorterUint8(const void* left, const void* right);
int32_t _arraySorterUint8(void* left, void* right);