-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,825 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include "array2d.h" | ||
|
||
#include <string.h> | ||
|
||
#include "seika/memory.h" | ||
#include "seika/assert.h" | ||
|
||
#define SKA_ARRAY2D_IS_VALID_COORD(ARRAY2D, ROW, COL) (!((int32)ROW >= ARRAY2D->size.w || (int32)COL >= ARRAY2D->size.h)) | ||
|
||
SkaArray2D* ska_array2d_create(size_t rows, size_t cols, size_t elementSize) { | ||
SkaArray2D* newArray = SKA_MEM_ALLOCATE(SkaArray2D); | ||
newArray->data = SKA_MEM_ALLOCATE_SIZE(cols * sizeof(void*)); | ||
for (size_t i = 0; i < cols; i++) { | ||
newArray->data[i] = SKA_MEM_ALLOCATE_SIZE(rows * elementSize); | ||
} | ||
newArray->size = (SkaSize2Di){ .w = (int32)rows, .h = (int32)cols }; | ||
newArray->elementSize = elementSize; | ||
return newArray; | ||
} | ||
|
||
void ska_array2d_destroy(SkaArray2D* array2d) { | ||
for (int32 i = 0; i < array2d->size.h; i++) { | ||
SKA_MEM_FREE(array2d->data[i]); | ||
} | ||
SKA_MEM_FREE(array2d->data); | ||
SKA_MEM_FREE(array2d); | ||
} | ||
|
||
void* ska_array2d_get(SkaArray2D* array2d, size_t x, size_t y) { | ||
if (!SKA_ARRAY2D_IS_VALID_COORD(array2d, x, y)) { | ||
return NULL; | ||
} | ||
return (void*)((char*)array2d->data[y] + x * array2d->elementSize); | ||
} | ||
|
||
bool ska_array2d_set(SkaArray2D* array2d, size_t x, size_t y, void* newValue) { | ||
if (!SKA_ARRAY2D_IS_VALID_COORD(array2d, x, y)) { | ||
return false; | ||
} | ||
memcpy((void*)((char*)array2d->data[y] + x * array2d->elementSize), newValue, array2d->elementSize); | ||
return true; | ||
} | ||
|
||
void ska_array2d_resize(SkaArray2D* array2d, size_t newX, size_t newY) { | ||
// Reallocate memory for data array | ||
const size_t width = SKA_MATH_MAX(newX, 1); | ||
const size_t height = SKA_MATH_MAX(newY, 1); | ||
|
||
array2d->data = ska_mem_reallocate(array2d->data, height * sizeof(void*)); | ||
for (size_t i = 0; i < height; i++) { | ||
if (i < array2d->size.h) { | ||
// Resize existing rows | ||
array2d->data[i] = ska_mem_reallocate(array2d->data[i], width * array2d->elementSize); | ||
} else { | ||
// Allocate memory for new rows | ||
array2d->data[i] = SKA_MEM_ALLOCATE_SIZE(width * array2d->elementSize); | ||
} | ||
} | ||
|
||
array2d->size = (SkaSize2Di){ .w = (int32)newX, .h = (int32)newY }; | ||
|
||
if (newX == 0 && newY == 0) { | ||
ska_array2d_reset(array2d); | ||
} | ||
} | ||
|
||
void ska_array2d_clear(SkaArray2D* array2d) { | ||
ska_array2d_resize(array2d, 0, 0); | ||
} | ||
|
||
void ska_array2d_reset(SkaArray2D* array2d) { | ||
ska_array2d_reset_default(array2d, NULL); | ||
} | ||
|
||
void ska_array2d_reset_default(SkaArray2D* array2d, void* defaultValue) { | ||
if (defaultValue) { | ||
for (int32 y = 0; y < array2d->size.h; y++) { | ||
for (int32 x = 0; x < array2d->size.w; x++) { | ||
ska_array2d_set(array2d, x, y, defaultValue); | ||
} | ||
} | ||
} else { | ||
for (int32 i = 0; i < array2d->size.h; i++) { | ||
memset(array2d->data[i], 0, array2d->size.w * array2d->elementSize); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include "seika/math/math.h" | ||
|
||
typedef struct SkaArray2D { | ||
void** data; | ||
SkaSize2Di size; // w = rows, h = columns | ||
size_t elementSize; | ||
} SkaArray2D; | ||
|
||
SkaArray2D* ska_array2d_create(size_t rows, size_t cols, size_t elementSize); | ||
void ska_array2d_destroy(SkaArray2D* array2d); | ||
void* ska_array2d_get(SkaArray2D* array2d, size_t x, size_t y); | ||
bool ska_array2d_set(SkaArray2D* array2d, size_t x, size_t y, void* newValue); | ||
void ska_array2d_resize(SkaArray2D* array2d, size_t newX, size_t newY); | ||
void ska_array2d_clear(SkaArray2D* array2d); | ||
// Maintains elements in the arrays but zeroes out data | ||
void ska_array2d_reset(SkaArray2D* array2d); | ||
// Maintains elements in the arrays but copy memory from the passed in default value | ||
void ska_array2d_reset_default(SkaArray2D* array2d, void* defaultValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "array_list.h" | ||
|
||
#include <string.h> | ||
|
||
#include "seika/memory.h" | ||
#include "seika/assert.h" | ||
|
||
#define SKA_ARRAY_LIST_DEFAULT_CAPACITY 16 | ||
|
||
SkaArrayList* ska_array_list_create_default_capacity(size_t valueSize) { | ||
return ska_array_list_create(valueSize, SKA_ARRAY_LIST_DEFAULT_CAPACITY); | ||
} | ||
|
||
SkaArrayList* ska_array_list_create(size_t valueSize, size_t initialCapacity) { | ||
SkaArrayList* newList = SKA_MEM_ALLOCATE(SkaArrayList); | ||
newList->data = SKA_MEM_ALLOCATE_SIZE(initialCapacity * valueSize); | ||
newList->valueSize = valueSize; | ||
newList->capacity = initialCapacity; | ||
newList->initialCapacity = initialCapacity; | ||
return newList; | ||
} | ||
|
||
void ska_array_list_destroy(SkaArrayList* list) { | ||
SKA_MEM_FREE(list->data); | ||
SKA_MEM_FREE(list); | ||
} | ||
|
||
void ska_array_list_push_back(SkaArrayList* list, const void* value) { | ||
if (list->size >= list->capacity) { | ||
list->capacity *= 2; | ||
list->data = ska_mem_reallocate(list->data, list->capacity * list->valueSize); | ||
} | ||
memcpy((char*)list->data + list->size * list->valueSize, value, list->valueSize); | ||
list->size++; | ||
} | ||
|
||
void* ska_array_list_get(SkaArrayList* list, size_t index) { | ||
SKA_ASSERT_FMT(index < list->size, "Attempting to access out of bounds index '%d", index); | ||
return (char*)list->data + index * list->valueSize; | ||
} | ||
|
||
bool ska_array_list_remove(SkaArrayList* list, const void* value) { | ||
size_t index = 0; | ||
while (index < list->size) { | ||
if (memcmp((char*)list->data + index * list->valueSize, value, list->valueSize) == 0) { | ||
// Found the element, remove it | ||
for (size_t i = index + 1; i < list->size; ++i) { | ||
memcpy((char*)list->data + (i - 1) * list->valueSize, (char*)list->data + i * list->valueSize, list->valueSize); | ||
} | ||
list->size--; | ||
return true; | ||
} else { | ||
index++; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool ska_array_list_remove_by_index(SkaArrayList* list, size_t index) { | ||
if (index < list->size) { | ||
// Shift elements after the removed element | ||
for (size_t i = index + 1; i < list->size; ++i) { | ||
memcpy((char*)list->data + (i - 1) * list->valueSize, (char*)list->data + i * list->valueSize, list->valueSize); | ||
} | ||
list->size--; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool ska_array_list_is_empty(SkaArrayList *list) { | ||
return list->size == 0; | ||
} | ||
|
||
void ska_array_list_clear(SkaArrayList *list) { | ||
list->size = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <stddef.h> | ||
#include <stdbool.h> | ||
|
||
#define SKA_ARRAY_LIST_GET_ARRAY(LIST, TYPE) ((TYPE*)((LIST)->data)) | ||
#define SKA_ARRAY_LIST_FOR_EACH(LIST, TYPE, VALUE) for(TYPE* VALUE = SKA_ARRAY_LIST_GET_ARRAY(LIST, TYPE), *VALUE##_end = VALUE + (LIST)->size; VALUE < VALUE##_end; VALUE++) | ||
|
||
// Generic array list, use 'ska_array_list_create' to allocate on the heap | ||
typedef struct SkaArrayList { | ||
void* data; | ||
size_t valueSize; // Size of each element of the data | ||
size_t size; // How many elements are in the array | ||
size_t capacity; // Max elements allowed in array before resizing | ||
size_t initialCapacity; // Min capacity for the array list | ||
} SkaArrayList; | ||
|
||
// Will create a new array list with all it's element values set to the passed in 'valueSize' | ||
SkaArrayList* ska_array_list_create_default_capacity(size_t valueSize); | ||
SkaArrayList* ska_array_list_create(size_t valueSize, size_t initialCapacity); | ||
// Will complete destroy the array list freeing up all its memory | ||
void ska_array_list_destroy(SkaArrayList* list); | ||
// Inserts an item at the end of the list | ||
void ska_array_list_push_back(SkaArrayList* list, const void* value); | ||
// Returns an item from the list from specified index | ||
void* ska_array_list_get(SkaArrayList* list, size_t index); | ||
// Removes the first item from the list who is equal to the passed in value | ||
bool ska_array_list_remove(SkaArrayList* list, const void* value); | ||
bool ska_array_list_remove_by_index(SkaArrayList* list, size_t index); | ||
// Returns true if the array list size == 0 | ||
bool ska_array_list_is_empty(SkaArrayList *list); | ||
// Will remove all items from the array list | ||
void ska_array_list_clear(SkaArrayList *list); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "array_utils.h" | ||
|
||
static inline void ska_array_utils_swap(int32* xp, int32* yp) { | ||
int32 temp = *xp; | ||
*xp = *yp; | ||
*yp = temp; | ||
} | ||
|
||
// Function to perform Selection Sort | ||
void ska_array_utils_selection_sort_int(int32 arr[], int32 arraySize) { | ||
int32 min_idx; | ||
|
||
// One by one move boundary of unsorted subarray | ||
for (int32 i = 0; i < arraySize - 1; i++) { | ||
|
||
// Find the minimum element in unsorted array | ||
min_idx = i; | ||
for (int32 j = i + 1; j < arraySize; j++) { | ||
if (arr[j] < arr[min_idx]) { | ||
min_idx = j; | ||
} | ||
} | ||
|
||
// Swap the found minimum element | ||
// with the first element | ||
ska_array_utils_swap(&arr[min_idx], &arr[i]); | ||
} | ||
} | ||
|
||
void ska_array_utils_remove_item_uint32(uint32 array[], size_t* size, uint32 item, uint32 emptyValue) { | ||
SKA_ARRAY_UTILS_REMOVE_ARRAY_ITEM(array, *size, item, emptyValue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <stddef.h> | ||
|
||
#include "seika/defines.h" | ||
|
||
#define SKA_ARRAY_UTILS_REMOVE_ARRAY_ITEM(ARRAY, ARRAY_SIZE, ELEMENT, EMPTY_VALUE) \ | ||
{ \ | ||
const size_t temp_arac_size = (ARRAY_SIZE); \ | ||
for (size_t temp_arac_loop_index = 0; temp_arac_loop_index < temp_arac_size; temp_arac_loop_index++) { \ | ||
if (ARRAY[temp_arac_loop_index] == ELEMENT) { \ | ||
ARRAY[temp_arac_loop_index] = ARRAY[temp_arac_loop_index + 1]; \ | ||
ARRAY[temp_arac_loop_index + 1] = EMPTY_VALUE; \ | ||
(ARRAY_SIZE)--; \ | ||
} \ | ||
if (ARRAY[temp_arac_loop_index] == EMPTY_VALUE && temp_arac_loop_index + 1 < temp_arac_size) { \ | ||
ARRAY[temp_arac_loop_index] = ARRAY[temp_arac_loop_index + 1]; \ | ||
ARRAY[temp_arac_loop_index + 1] = EMPTY_VALUE; \ | ||
} \ | ||
} \ | ||
} | ||
|
||
// Function to perform Selection Sort | ||
void ska_array_utils_selection_sort_int(int32 arr[], int32 arraySize); | ||
|
||
void ska_array_utils_remove_item_uint32(uint32 array[], size_t* size, uint32 item, uint32 emptyValue); |
Oops, something went wrong.