Gama C Library
Gama C API Documentation
Loading...
Searching...
No Matches
body_list.h File Reference

Provides a dynamic, NULL-terminated pointer list implementation. More...

#include "body.h"
#include <stdlib.h>

Go to the source code of this file.

Macros

#define gm_ptr_list_for_each(item, list)
 A macro for iterating over a gmPtrList.
#define gm_bodies_for_each(item, list)
 A macro for iterating over a gmBodies list.

Typedefs

typedef void ** gmPtrList
 A dynamic, NULL-terminated array of generic pointers.
typedef gmBody ** gmBodies
 A specialized pointer list for gmBody pointers.

Functions

size_t gm_ptr_list_length (gmPtrList list)
 Calculates the number of elements in a pointer list.
int gm_ptr_list_is_empty (gmPtrList list)
 Checks if a pointer list is empty.
size_t gm_ptr_list_count (gmPtrList list, void *obj)
 Counts the occurrences of a specific pointer in the list.
gmPtrList gm_ptr_list_push (gmPtrList list, void *obj)
 Adds a pointer to the end of the list.
gmPtrList gm_ptr_list_pop (gmPtrList list)
 Removes the last element from the list.
gmPtrList gm_ptr_list_remove (gmPtrList list, void *obj)
 Removes all occurrences of a specific pointer from the list.
gmPtrList gm_ptr_list_pop_at (gmPtrList list, size_t idx)
 Removes an element at a specific index.
gmPtrList gm_ptr_list_insert_at (gmPtrList list, size_t idx, void *value)
 Inserts a pointer at a specific index.
int gm_ptr_list_find (gmPtrList list, void *value)
 Finds the index of a specific pointer.
void * gm_ptr_list_get (gmPtrList list, size_t index)
 Retrieves the element at a specific index.
void * gm_ptr_list_last (gmPtrList list)
 Retrieves the last element of the list.
void gm_ptr_list_clear (gmPtrList list)
 Frees the memory used by the list.

Detailed Description

Provides a dynamic, NULL-terminated pointer list implementation.

This file contains a generic pointer list (gmPtrList) and a specialized version for physics bodies (gmBodies). The lists automatically resize and are always NULL-terminated, making them easy to iterate.

Note
The functions in this file that return a new list (e.g., push, pop) allocate new memory. It is the caller's responsibility to free the original list pointer to prevent memory leaks.

Macro Definition Documentation

◆ gm_bodies_for_each

#define gm_bodies_for_each ( item,
list )
Value:
for (size_t i = 0; (list != NULL) && (item = list[i]) != NULL; i++)

A macro for iterating over a gmBodies list.

Parameters
itemA gmBody* variable to hold the current item.
listThe gmBodies list to iterate over.

◆ gm_ptr_list_for_each

#define gm_ptr_list_for_each ( item,
list )
Value:
for (size_t i = 0; (list != NULL) && (item = list[i]) != NULL; i++)

A macro for iterating over a gmPtrList.

Parameters
itemThe variable to hold the current item (e.g., gmBody* item).
listThe gmPtrList to iterate over.

Typedef Documentation

◆ gmBodies

typedef gmBody** gmBodies

A specialized pointer list for gmBody pointers.

◆ gmPtrList

typedef void** gmPtrList

A dynamic, NULL-terminated array of generic pointers.

Function Documentation

◆ gm_ptr_list_clear()

void gm_ptr_list_clear ( gmPtrList list)

Frees the memory used by the list.

Parameters
listThe list to clear.

◆ gm_ptr_list_count()

size_t gm_ptr_list_count ( gmPtrList list,
void * obj )

Counts the occurrences of a specific pointer in the list.

Parameters
listThe list to search.
objThe pointer to count.
Returns
The number of times obj appears in the list.

◆ gm_ptr_list_find()

int gm_ptr_list_find ( gmPtrList list,
void * value )

Finds the index of a specific pointer.

Parameters
listThe list to search.
valueThe pointer to find.
Returns
The index of the first occurrence of the value, or -1 if not found.

◆ gm_ptr_list_get()

void * gm_ptr_list_get ( gmPtrList list,
size_t index )

Retrieves the element at a specific index.

Parameters
listThe list.
indexThe index of the element to retrieve.
Returns
The pointer at the specified index, or NULL if the index is out of bounds.

◆ gm_ptr_list_insert_at()

gmPtrList gm_ptr_list_insert_at ( gmPtrList list,
size_t idx,
void * value )

Inserts a pointer at a specific index.

Parameters
listThe list to modify.
idxThe index at which to insert the value.
valueThe pointer to insert.
Returns
A new pointer to the resized list. The original list should be freed.

◆ gm_ptr_list_is_empty()

int gm_ptr_list_is_empty ( gmPtrList list)

Checks if a pointer list is empty.

Parameters
listThe list to check.
Returns
1 if the list is NULL or has zero length, 0 otherwise.

◆ gm_ptr_list_last()

void * gm_ptr_list_last ( gmPtrList list)

Retrieves the last element of the list.

Parameters
listThe list.
Returns
The last pointer in the list, or NULL if the list is empty.

◆ gm_ptr_list_length()

size_t gm_ptr_list_length ( gmPtrList list)

Calculates the number of elements in a pointer list.

Parameters
listThe NULL-terminated pointer list.
Returns
The number of elements, excluding the NULL terminator.

◆ gm_ptr_list_pop()

gmPtrList gm_ptr_list_pop ( gmPtrList list)

Removes the last element from the list.

Parameters
listThe list to modify.
Returns
A new pointer to the resized list. The original list should be freed.

◆ gm_ptr_list_pop_at()

gmPtrList gm_ptr_list_pop_at ( gmPtrList list,
size_t idx )

Removes an element at a specific index.

Parameters
listThe list to modify.
idxThe index of the element to remove.
Returns
A new pointer to the resized list. The original list should be freed.

◆ gm_ptr_list_push()

gmPtrList gm_ptr_list_push ( gmPtrList list,
void * obj )

Adds a pointer to the end of the list.

Parameters
listThe list to append to.
objThe pointer to add.
Returns
A new pointer to the resized list. The original list should be freed.

◆ gm_ptr_list_remove()

gmPtrList gm_ptr_list_remove ( gmPtrList list,
void * obj )

Removes all occurrences of a specific pointer from the list.

Parameters
listThe list to modify.
objThe pointer to remove.
Returns
A new pointer to the resized list. The original list should be freed.