cdsa
Arena allocators and friends
Loading...
Searching...
No Matches
list.h File Reference
#include <assert.h>
#include <stddef.h>
#include "arena.h"

Data Structures

struct  List
 Represents a doubly linked list. More...
 
struct  ListItem
 Represents a single item of a list. More...
 

Macros

#define list_for_each(item, self)   for (auto(item) = (self)->begin; item; (item) = (item)->next)
 Iterate over all items of a list.
 

Typedefs

typedef int ListDataCompare(const void *, const void *, void *)
 Data comparison function.
 
typedef void * ListDataCopy(Arena *, void *, const void *, long)
 Data copy function.
 

Functions

static List list_create (Arena *arena, long size, ListDataCompare *compare)
 Create a new list.
 
static void list_insert (List *self, long index, void *data)
 Insert a new item into a list.
 
static void list_append (List *self, void *data)
 Append a new item to the back of a list.
 
static void * list_pop (List *self, long index)
 Remove an item from a list.
 
static void * list_remove (List *self, const void *data)
 Remove the first occurrence of a matching item from a list.
 
static void * list_get (const List *self, long index)
 Retrieve an item from a list.
 
static void * list_find (const List *self, const void *data)
 Find the first occurrence of a matching item of a list.
 
static long list_index (const List *self, const void *data)
 Retrieve the index of the first occurrence of a matching item of a list.
 
static long list_count (const List *self, const void *data)
 Count the number of matching items of a list.
 
static void list_sort (List *self, void *context)
 Sort the items of a list.
 
static void list_reverse (List *self)
 Reverse the order of items in a list.
 
static List list_clone (const List *self, Arena *arena)
 Create a clone of a list.
 
static ListItemlist_items (const List *self, Arena *arena)
 Retrieve an array of list items.
 

Macro Definition Documentation

◆ list_for_each

#define list_for_each (   item,
  self 
)    for (auto(item) = (self)->begin; item; (item) = (item)->next)

Iterate over all items of a list.

Parameters
itemCurrent list item
selfPointer to a list

Function Documentation

◆ list_create()

static List list_create ( Arena arena,
long  size,
ListDataCompare compare 
)
static

Create a new list.

Parameters
arenaPointer to an arena allocator
sizeSize of item data in bytes (optional)
comparePointer to a data comparison function (optional)
Returns
New list instance
Note
If size == 0, the data pointers will be directly assigned rather than copied

◆ list_insert()

static void list_insert ( List self,
long  index,
void *  data 
)
static

Insert a new item into a list.

Parameters
selfPointer to a list
indexIndex of the new item
dataPointer to the item data
Note
A negative index is interpreted as from the back (self->length - index)

◆ list_append()

static void list_append ( List self,
void *  data 
)
static

Append a new item to the back of a list.

Parameters
selfPointer to a list
dataPointer to the item data

◆ list_pop()

static void * list_pop ( List self,
long  index 
)
static

Remove an item from a list.

Parameters
selfPointer to a list
indexIndex of the item to remove
Returns
Pointer to the item data, or nullptr if the list is empty
Note
A negative index is interpreted as from the back (self->length - index)

◆ list_remove()

static void * list_remove ( List self,
const void *  data 
)
static

Remove the first occurrence of a matching item from a list.

Parameters
selfPointer to a list
dataPointer to the item data to match
Returns
Pointer to the item data, or nullptr if no matching item is found
Note
This function requires a data comparison function to be set

◆ list_get()

static void * list_get ( const List self,
long  index 
)
static

Retrieve an item from a list.

Parameters
selfPointer to a list
indexIndex of the item to retrieve
Returns
Pointer to the item data, or nullptr if the list is empty
Note
A negative index is interpreted as from the back (self->length - index)

◆ list_find()

static void * list_find ( const List self,
const void *  data 
)
static

Find the first occurrence of a matching item of a list.

Parameters
selfPointer to a list
dataPointer to the item data to match
Returns
Pointer to the item data, or nullptr if no matching item is found
Note
This function requires a data comparison function to be set

◆ list_index()

static long list_index ( const List self,
const void *  data 
)
static

Retrieve the index of the first occurrence of a matching item of a list.

Parameters
selfPointer to a list
dataPointer to the item data to match
Returns
Index of the matching item, or -1 if no matching item is found
Note
This function requires a data comparison function to be set

◆ list_count()

static long list_count ( const List self,
const void *  data 
)
static

Count the number of matching items of a list.

Parameters
selfPointer to a list
dataPointer to the item data to match
Returns
Number of matching items
Note
This function requires a data comparison function to be set

◆ list_sort()

static void list_sort ( List self,
void *  context 
)
static

Sort the items of a list.

Parameters
selfPointer to a list
contextPointer to a user-provided context for the comparison function (optional)
Note
This function requires a data comparison function to be set

◆ list_reverse()

static void list_reverse ( List self)
static

Reverse the order of items in a list.

Parameters
selfPointer to a list

◆ list_clone()

static List list_clone ( const List self,
Arena arena 
)
static

Create a clone of a list.

Parameters
selfPointer to a list
arenaPointer to an arena allocator (optional)
Returns
Cloned list instance
Note
If no arena allocator is passed, the arena allocator of the list is used

◆ list_items()

static ListItem * list_items ( const List self,
Arena arena 
)
static

Retrieve an array of list items.

Parameters
selfPointer to a list
arenaPointer to an arena allocator (optional)
Returns
Pointer to an array of items
Note
If no arena allocator is passed, the arena allocator of the list is used