@@ -20,6 +20,7 @@ jobs:
|
|||||||
run: >
|
run: >
|
||||||
clang-format --dry-run -Werror
|
clang-format --dry-run -Werror
|
||||||
include/bitset.h source/bitset.c
|
include/bitset.h source/bitset.c
|
||||||
|
include/list.h source/list.c
|
||||||
include/sprite.h source/sprite.c
|
include/sprite.h source/sprite.c
|
||||||
include/splash_screen.h source/splash_screen.c
|
include/splash_screen.h source/splash_screen.c
|
||||||
# Currently contains only the code that has been formatted to conform
|
# Currently contains only the code that has been formatted to conform
|
||||||
|
|||||||
+2
-2
@@ -200,7 +200,7 @@ bool list_swap(List* list, unsigned int idx_a, unsigned int idx_b);
|
|||||||
*
|
*
|
||||||
* @return a pointer to the data at the index of the list, or NULL if out-of-bounds
|
* @return a pointer to the data at the index of the list, or NULL if out-of-bounds
|
||||||
*/
|
*/
|
||||||
void* list_get_at_idx(List *list, unsigned int idx);
|
void* list_get_at_idx(List* list, unsigned int idx);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a List's node at the specified index
|
* Remove a List's node at the specified index
|
||||||
@@ -210,7 +210,7 @@ void* list_get_at_idx(List *list, unsigned int idx);
|
|||||||
*
|
*
|
||||||
* @return `true` if successfully removed, `false` if out-of-bounds
|
* @return `true` if successfully removed, `false` if out-of-bounds
|
||||||
*/
|
*/
|
||||||
bool list_remove_at_idx(List *list, unsigned int idx);
|
bool list_remove_at_idx(List* list, unsigned int idx);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of elements in a @ref List
|
* Get the number of elements in a @ref List
|
||||||
|
|||||||
+50
-44
@@ -1,7 +1,9 @@
|
|||||||
#include <stdbool.h>
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a node from a list.
|
* Remove a node from a list.
|
||||||
*
|
*
|
||||||
@@ -12,7 +14,7 @@
|
|||||||
* @param list pointer to a @ref List
|
* @param list pointer to a @ref List
|
||||||
* @param node pointer to a @ref ListNode
|
* @param node pointer to a @ref ListNode
|
||||||
*/
|
*/
|
||||||
static void _list_remove_node(List *list, ListNode *node);
|
static void s_list_remove_node(List* list, ListNode* node);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the next @ref ListNode in a @ref ListItr
|
* Get the next @ref ListNode in a @ref ListItr
|
||||||
@@ -25,23 +27,23 @@ static void _list_remove_node(List *list, ListNode *node);
|
|||||||
*
|
*
|
||||||
* @return A pointer to the @ref ListNode in the itr, otherwise return NULL.
|
* @return A pointer to the @ref ListNode in the itr, otherwise return NULL.
|
||||||
*/
|
*/
|
||||||
static ListNode* _list_itr_node_next(ListItr* itr);
|
static ListNode* s_list_itr_node_next(ListItr* itr);
|
||||||
|
|
||||||
|
|
||||||
List list_create(void)
|
List list_create(void)
|
||||||
{
|
{
|
||||||
List list = { .head = NULL, .tail = NULL, .len = 0 };
|
List list = {.head = NULL, .tail = NULL, .len = 0};
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_clear(List* list)
|
void list_clear(List* list)
|
||||||
{
|
{
|
||||||
if(list_is_empty(list)) return;
|
if (list_is_empty(list))
|
||||||
|
return;
|
||||||
|
|
||||||
ListItr itr = list_itr_create(list);
|
ListItr itr = list_itr_create(list);
|
||||||
ListNode* ln;
|
ListNode* ln;
|
||||||
|
|
||||||
while((ln = _list_itr_node_next(&itr)))
|
while ((ln = s_list_itr_node_next(&itr)))
|
||||||
{
|
{
|
||||||
POOL_FREE(ListNode, ln);
|
POOL_FREE(ListNode, ln);
|
||||||
}
|
}
|
||||||
@@ -56,9 +58,9 @@ bool list_is_empty(const List* list)
|
|||||||
return list->len == 0;
|
return list->len == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_push_front(List *list, void* data)
|
void list_push_front(List* list, void* data)
|
||||||
{
|
{
|
||||||
ListNode *node = POOL_GET(ListNode);
|
ListNode* node = POOL_GET(ListNode);
|
||||||
|
|
||||||
node->data = data;
|
node->data = data;
|
||||||
node->prev = NULL;
|
node->prev = NULL;
|
||||||
@@ -78,7 +80,7 @@ void list_push_front(List *list, void* data)
|
|||||||
list->len++;
|
list->len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_push_back(List *list, void* data)
|
void list_push_back(List* list, void* data)
|
||||||
{
|
{
|
||||||
ListNode* node = POOL_GET(ListNode);
|
ListNode* node = POOL_GET(ListNode);
|
||||||
node->data = data;
|
node->data = data;
|
||||||
@@ -101,13 +103,13 @@ void list_push_back(List *list, void* data)
|
|||||||
|
|
||||||
void list_insert(List* list, void* data, unsigned int idx)
|
void list_insert(List* list, void* data, unsigned int idx)
|
||||||
{
|
{
|
||||||
if(idx >= list->len)
|
if (idx >= list->len)
|
||||||
{
|
{
|
||||||
list_push_back(list, data);
|
list_push_back(list, data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(idx == 0)
|
if (idx == 0)
|
||||||
{
|
{
|
||||||
list_push_front(list, data);
|
list_push_front(list, data);
|
||||||
return;
|
return;
|
||||||
@@ -121,11 +123,11 @@ void list_insert(List* list, void* data, unsigned int idx)
|
|||||||
ListItr itr = list_itr_create(list);
|
ListItr itr = list_itr_create(list);
|
||||||
ListNode* ln;
|
ListNode* ln;
|
||||||
|
|
||||||
while((ln = _list_itr_node_next(&itr)))
|
while ((ln = s_list_itr_node_next(&itr)))
|
||||||
{
|
{
|
||||||
if(idx == curr_idx++)
|
if (idx == curr_idx++)
|
||||||
{
|
{
|
||||||
ListNode *node = POOL_GET(ListNode);
|
ListNode* node = POOL_GET(ListNode);
|
||||||
node->prev = ln->prev;
|
node->prev = ln->prev;
|
||||||
node->next = ln;
|
node->next = ln;
|
||||||
ln->prev->next = node;
|
ln->prev->next = node;
|
||||||
@@ -139,8 +141,10 @@ void list_insert(List* list, void* data, unsigned int idx)
|
|||||||
|
|
||||||
bool list_swap(List* list, unsigned int idx_a, unsigned int idx_b)
|
bool list_swap(List* list, unsigned int idx_a, unsigned int idx_b)
|
||||||
{
|
{
|
||||||
if(idx_a >= list->len || idx_b >= list->len) return false;
|
if (idx_a >= list->len || idx_b >= list->len)
|
||||||
if(idx_a == idx_b) return true; // swapping with yourself isn't technically an error
|
return false;
|
||||||
|
if (idx_a == idx_b)
|
||||||
|
return true; // swapping with yourself isn't technically an error
|
||||||
|
|
||||||
unsigned int curr_idx = 0;
|
unsigned int curr_idx = 0;
|
||||||
unsigned int max_idx = idx_a > idx_b ? idx_a : idx_b;
|
unsigned int max_idx = idx_a > idx_b ? idx_a : idx_b;
|
||||||
@@ -152,19 +156,18 @@ bool list_swap(List* list, unsigned int idx_a, unsigned int idx_b)
|
|||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
ln = _list_itr_node_next(&itr);
|
ln = s_list_itr_node_next(&itr);
|
||||||
if(idx_a == curr_idx)
|
if (idx_a == curr_idx)
|
||||||
{
|
{
|
||||||
node_a = ln;
|
node_a = ln;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(idx_b == curr_idx)
|
if (idx_b == curr_idx)
|
||||||
{
|
{
|
||||||
node_b = ln;
|
node_b = ln;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
} while (max_idx != curr_idx++);
|
||||||
while(max_idx != curr_idx++);
|
|
||||||
|
|
||||||
// Just swap the data pointers
|
// Just swap the data pointers
|
||||||
void* tmp = node_a->data;
|
void* tmp = node_a->data;
|
||||||
@@ -174,24 +177,24 @@ bool list_swap(List* list, unsigned int idx_a, unsigned int idx_b)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _list_remove_node(List *list, ListNode *node)
|
static void s_list_remove_node(List* list, ListNode* node)
|
||||||
{
|
{
|
||||||
if(node->prev && !node->next) // end of list
|
if (node->prev && !node->next) // end of list
|
||||||
{
|
{
|
||||||
node->prev->next = NULL;
|
node->prev->next = NULL;
|
||||||
list->tail = node->prev;
|
list->tail = node->prev;
|
||||||
}
|
}
|
||||||
else if(node->prev && node->next) // somewhere in between
|
else if (node->prev && node->next) // somewhere in between
|
||||||
{
|
{
|
||||||
node->prev->next = node->next;
|
node->prev->next = node->next;
|
||||||
node->next->prev = node->prev;
|
node->next->prev = node->prev;
|
||||||
}
|
}
|
||||||
else if(node->next && !node->prev) // beginning of list
|
else if (node->next && !node->prev) // beginning of list
|
||||||
{
|
{
|
||||||
node->next->prev = NULL;
|
node->next->prev = NULL;
|
||||||
list->head = node->next;
|
list->head = node->next;
|
||||||
}
|
}
|
||||||
else if(!node->prev && !node->next) // only element in list
|
else if (!node->prev && !node->next) // only element in list
|
||||||
{
|
{
|
||||||
list->head = NULL;
|
list->head = NULL;
|
||||||
list->tail = NULL;
|
list->tail = NULL;
|
||||||
@@ -209,15 +212,17 @@ int list_get_len(const List* list)
|
|||||||
|
|
||||||
void* list_get_at_idx(List* list, unsigned int idx)
|
void* list_get_at_idx(List* list, unsigned int idx)
|
||||||
{
|
{
|
||||||
if(idx >= list_get_len(list)) return NULL;
|
if (idx >= list_get_len(list))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
int curr_idx = 0;
|
int curr_idx = 0;
|
||||||
ListItr itr = list_itr_create(list);
|
ListItr itr = list_itr_create(list);
|
||||||
void* data = NULL;
|
void* data = NULL;
|
||||||
|
|
||||||
while((data = list_itr_next(&itr)))
|
while ((data = list_itr_next(&itr)))
|
||||||
{
|
{
|
||||||
if (idx == curr_idx++) return data;
|
if (idx == curr_idx++)
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -225,17 +230,18 @@ void* list_get_at_idx(List* list, unsigned int idx)
|
|||||||
|
|
||||||
bool list_remove_at_idx(List* list, unsigned int idx)
|
bool list_remove_at_idx(List* list, unsigned int idx)
|
||||||
{
|
{
|
||||||
if(idx >= list_get_len(list)) return false;
|
if (idx >= list_get_len(list))
|
||||||
|
return false;
|
||||||
|
|
||||||
int len = 0;
|
int len = 0;
|
||||||
ListItr itr = list_itr_create(list);
|
ListItr itr = list_itr_create(list);
|
||||||
ListNode* ln;
|
ListNode* ln;
|
||||||
|
|
||||||
while((ln = _list_itr_node_next(&itr)))
|
while ((ln = s_list_itr_node_next(&itr)))
|
||||||
{
|
{
|
||||||
if(idx == len++)
|
if (idx == len++)
|
||||||
{
|
{
|
||||||
_list_remove_node(list, ln);
|
s_list_remove_node(list, ln);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -244,8 +250,7 @@ bool list_remove_at_idx(List* list, unsigned int idx)
|
|||||||
|
|
||||||
ListItr list_itr_create(List* list)
|
ListItr list_itr_create(List* list)
|
||||||
{
|
{
|
||||||
ListItr itr =
|
ListItr itr = {
|
||||||
{
|
|
||||||
.list = list,
|
.list = list,
|
||||||
.next_node = !list_is_empty(list) ? list->head : NULL,
|
.next_node = !list_is_empty(list) ? list->head : NULL,
|
||||||
.current_node = NULL,
|
.current_node = NULL,
|
||||||
@@ -257,8 +262,7 @@ ListItr list_itr_create(List* list)
|
|||||||
|
|
||||||
ListItr rev_list_itr_create(List* list)
|
ListItr rev_list_itr_create(List* list)
|
||||||
{
|
{
|
||||||
ListItr itr =
|
ListItr itr = {
|
||||||
{
|
|
||||||
.list = list,
|
.list = list,
|
||||||
.next_node = !list_is_empty(list) ? list->tail : NULL,
|
.next_node = !list_is_empty(list) ? list->tail : NULL,
|
||||||
.current_node = NULL,
|
.current_node = NULL,
|
||||||
@@ -270,20 +274,21 @@ ListItr rev_list_itr_create(List* list)
|
|||||||
|
|
||||||
void* list_itr_next(ListItr* itr)
|
void* list_itr_next(ListItr* itr)
|
||||||
{
|
{
|
||||||
ListNode* ln = _list_itr_node_next(itr);
|
ListNode* ln = s_list_itr_node_next(itr);
|
||||||
return ln ? ln->data : NULL;
|
return ln ? ln->data : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ListNode* _list_itr_node_next(ListItr* itr)
|
static ListNode* s_list_itr_node_next(ListItr* itr)
|
||||||
{
|
{
|
||||||
if(!itr->next_node) return NULL;
|
if (!itr->next_node)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
itr->current_node = itr->next_node;
|
itr->current_node = itr->next_node;
|
||||||
|
|
||||||
ListNode* ln = itr->next_node;
|
ListNode* ln = itr->next_node;
|
||||||
ListNode* next_itr_node = (itr->direction == LIST_ITR_FORWARD) ? ln->next : ln->prev;
|
ListNode* next_itr_node = (itr->direction == LIST_ITR_FORWARD) ? ln->next : ln->prev;
|
||||||
|
|
||||||
if(next_itr_node)
|
if (next_itr_node)
|
||||||
{
|
{
|
||||||
itr->next_node = next_itr_node;
|
itr->next_node = next_itr_node;
|
||||||
return ln;
|
return ln;
|
||||||
@@ -295,8 +300,9 @@ static ListNode* _list_itr_node_next(ListItr* itr)
|
|||||||
|
|
||||||
void list_itr_remove_current_node(ListItr* itr)
|
void list_itr_remove_current_node(ListItr* itr)
|
||||||
{
|
{
|
||||||
if(!itr || !itr->current_node) return;
|
if (!itr || !itr->current_node)
|
||||||
|
return;
|
||||||
ListNode* tmp_prev = itr->current_node->prev;
|
ListNode* tmp_prev = itr->current_node->prev;
|
||||||
_list_remove_node(itr->list, itr->current_node);
|
s_list_remove_node(itr->list, itr->current_node);
|
||||||
itr->current_node = tmp_prev;
|
itr->current_node = tmp_prev;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user