GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
list.c File Reference

List functions implementation. More...

#include "list.h"
#include "pool.h"
#include <stdbool.h>

Go to the source code of this file.

Functions

static void s_list_remove_node (List *list, ListNode *node)
 
static ListNodes_list_itr_node_next (ListItr *itr)
 
List list_init (void)
 
void list_clear (List *list)
 
bool list_is_empty (const List *list)
 
void list_push_front (List *list, void *data)
 
void list_push_back (List *list, void *data)
 
void list_insert (List *list, void *data, unsigned int idx)
 
bool list_swap (List *list, unsigned int idx_a, unsigned int idx_b)
 
int list_get_len (const List *list)
 
void * list_get_at_idx (List *list, unsigned int idx)
 
bool list_remove_at_idx (List *list, unsigned int idx)
 
ListItr list_itr_create (List *list)
 
ListItr rev_list_itr_create (List *list)
 
void * list_itr_next (ListItr *itr)
 
void list_itr_remove_current_node (ListItr *itr)
 
bool list_remove_data (List *list, void *data)
 

Detailed Description

List functions implementation.

Definition in file list.c.

Function Documentation

◆ list_clear()

void list_clear ( List list)

Clear a list.

Go through the list and free each node and set the head and tail to NULL. Note, it doesn't "free" the data at the node.

Note
To reset an existing list to default values, first call list_clear then list_init
Parameters
listpointer to a List to clear

Definition at line 44 of file list.c.

◆ list_get_at_idx()

void * list_get_at_idx ( List list,
unsigned int  idx 
)

Get a List's node at the specified index

Parameters
listpointer to a List
idxindex of the desired ListNode in the list
Returns
a pointer to the data at the index of the list, or NULL if out-of-bounds

Definition at line 219 of file list.c.

◆ list_get_len()

int list_get_len ( const List list)

Get the number of elements in a List

Parameters
listpointer to a List
Returns
The number of elements in the list

Definition at line 214 of file list.c.

◆ list_init()

List list_init ( void  )

Initialize a list.

Set the values of a list to default.

If using this function to reset a list, the list must be freed with list_clear to ensure the list's nodes are deleted properly.

Returns
A List with head and tail reset.

Definition at line 38 of file list.c.

◆ list_insert()

void list_insert ( List list,
void *  data,
unsigned int  idx 
)

Insert data into a List a specific index

If the index specified is larger than the length of the list it will list_push_back() the data instead;

Performs the following operation:

               ┌─────┐
               │ node│
               └─────┘
     ┌─────┐   ┌─────┐   ┌─────┐
     │idx-1│◄─►│ idx │◄─►│idx+1│
     └─────┘   └─────┘   └─────┘
  1. Set new node prev to the node at idx - 1
  2. Set new node next to the node at idx
  3. Set node at idx - 1 next to new node
  4. Set node at idx prev to the new node

Result:

┌─────┐   ┌─────┐   ┌─────┐   ┌─────┐
│idx-1│◄─►│ node│◄─►│ idx │◄─►│idx+1│
└─────┘   └─────┘   └─────┘   └─────┘

Finally, the list is now updated with new node now at the labeled idx:

┌─────┐   ┌─────┐   ┌─────┐   ┌─────┐
│idx-1│◄─►│ idx │◄─►│idx+1│◄─►│idx+2│
└─────┘   └─────┘   └─────┘   └─────┘
Parameters
listpointer to a List
datapointer to data to put into the List
idxdesired index to insert

Definition at line 110 of file list.c.

◆ list_is_empty()

bool list_is_empty ( const List list)

Check if a list is empty

Parameters
listpointer to a List
Returns
true if the list is empty, false otherwise.

Definition at line 62 of file list.c.

◆ list_itr_create()

ListItr list_itr_create ( List list)

Declare a ListItr

Parameters
listpointer to a List
Returns
A new ListItr

Definition at line 257 of file list.c.

◆ list_itr_next()

void * list_itr_next ( ListItr itr)

Get the next data entry in a ListItr

Parameters
itrpointer to the ListItr
Returns
A pointer to the data pointer at the next ListNode if valid, otherwise return NULL.

Definition at line 281 of file list.c.

◆ list_itr_remove_current_node()

void list_itr_remove_current_node ( ListItr itr)

Remove the current ListNode from the iterator.

The "current node" corresponds to the list node associated with the most recently returned valu from list_itr_next()

Parameters
itrpointer to the ListItr
Note
When working with ListItr, use this and not list_remove_at() as it will "break" the iterator.

Definition at line 307 of file list.c.

◆ list_push_back()

void list_push_back ( List list,
void *  data 
)

Append an entry to the tail of a list

Parameters
listpointer to a List
datapointer to data to put into the List

Definition at line 89 of file list.c.

◆ list_push_front()

void list_push_front ( List list,
void *  data 
)

Prepend an entry to the head of a list

Parameters
listpointer to a List
datapointer to data to put into the List

Definition at line 67 of file list.c.

◆ list_remove_at_idx()

bool list_remove_at_idx ( List list,
unsigned int  idx 
)

Remove a List's node at the specified index

Parameters
listpointer to a List
idxindex of the desired ListNode in the list
Returns
true if successfully removed, false if out-of-bounds

Definition at line 237 of file list.c.

◆ list_remove_data()

bool list_remove_data ( List list,
void *  data 
)

Remove a List's node with the matching pointer

Parameters
listpointer to a List
datapointer to data in node in list
Returns
true if successfully removed, false otherwise
Note
When working with ListItr, use list_itr_remove_current_node()

Definition at line 316 of file list.c.

◆ list_swap()

bool list_swap ( List list,
unsigned int  idx_a,
unsigned int  idx_b 
)

Swap the data pointers at the specified indices of a List

If either indices are larger than the length of the list, return false.

Parameters
listpointer to a List
idx_adesired index to swap with idx_b
idx_bdesired index to swap with idx_a
Returns
true if successful, false otherwise

Definition at line 148 of file list.c.

◆ rev_list_itr_create()

ListItr rev_list_itr_create ( List list)

Declare a reverse ListItr

Parameters
listpointer to a List
Returns
A new reverse ListItr

Definition at line 269 of file list.c.

◆ s_list_itr_node_next()

static ListNode * s_list_itr_node_next ( ListItr itr)
static

Get the next ListNode in a ListItr

Note: Use of this function outside of testing is strongly discouraged. Unless you really want to access the ListNode itself, it's preferred to just use list_itr_next .

Parameters
itrpointer to the ListItr
Returns
A pointer to the ListNode in the itr, otherwise return NULL.

Definition at line 287 of file list.c.

◆ s_list_remove_node()

static void s_list_remove_node ( List list,
ListNode node 
)
static

Remove a node from a list.

Remove a ListNode from a List. There are no checks to ensure that the passed node is actually part of the passed list. Handle with care. This is used with the ListItr specifically.

Parameters
listpointer to a List
nodepointer to a ListNode

Definition at line 186 of file list.c.