list.h

Exposes functions to handle the list data structure.

  • Author: Edward Jones
  • Date: 2021-09-17

Source file

all_list

Return whether all elements of a list of booleans are true.

  • Param l: List to check

Returns: true iff all elements of the list are true

bool all_list(List* l);

any_list

Return whether there is some element in a list of booleans which is true.

  • Param l: List to check

Returns: true iff at at least one element of a list is true.

bool any_list(List* l);

append_list

Append a value to a list.

  • Param l: Pointer to the list to affect
  • Param v: Pointer to the node to add

Returns: true iff successful

bool append_list(List* l, void* v);

append_list_node

Append a list node to a list.

  • Param l: Pointer to the list to affect
  • Param ln: Pointer to the node to add

Returns: true iff successful

bool append_list_node(List* l, ListNode* ln);

cconcat_list

Concatenate a list in place, affecting exactly one list (takes time linear in the length of the second list).

  • Param r: List to output
  • Param l: List to concatenate to r
void cconcat_list(List* r, List* l);

concat_list

Concatenate a pair of lists into another.

  • Param r: The list outputted
  • Param l1: The first list to concatenate.
  • Param l2: The second list to concatenate.
void concat_list(List* r, List* l1, List* l2);

dest_list

Destroy a list. Does not affect list elements.

  • Param l: Pointer to the list to destroy.
  • Param freeNodes: Iff not false, frees the memory used by the contained ListNodes
  • Param ed: Element destructor called on the data field of each ListNode or NULL
void dest_list(List* l, Destructor ed);

dest_list_iter

Destroy an iterator.

  • Param i: Pointer to the iterator to destroy
void dest_list_iter(ListIter* i);

dest_list_node

Destroy a list node.

  • Param ln: Pointer to the list node to destroy
  • Param ed: Element destructor to be called on the data field of the list node or NULL
void dest_list_node(ListNode* ln, Destructor ed);

dest_reversed_list_iter

Destroy a reversed iterator.

  • Param i: Pointer to the iterator to destroy
void dest_reversed_list_iter(ReversedListIter* i);

iconcat_list

Concatenate a pair of lists in place. Takes constant time and the second list is no longer valid and should be.

  • Param l1: List to concatenate (left)
  • Param l2: List to concatenate (right)
void iconcat_list(List* r, List* l);

in_list

Checks whether a given element is in a list (by reference-equality).

  • Param l: Pointer to the list to check
  • Param val: Pointer to the value to test

Returns: true iff the value is in the list

bool in_list(List* l, void* val);

in_list_eq

Tests whether there exists an element in a given list which is equal under some function.

  • Param m: Maybe container for the value found to be equal to val under cmp
  • Param l: Pointer to the list to check
  • Param cmp: Comparator function to check, val is placed into the first argument.
  • Param val: The value to test against
void in_list_eq(Maybe* m, List* l, Comparator cmp, void* val);

is_empty_list

Return whether a list is empty.

  • Param l: List to check

Returns: false iff the list is not empty

bool is_empty_list(List* l);

iter_list

Move the iterator to the next element in the list.

  • Param v: A location where the value at the current point in the list will be written
  • Param i: Pointer to the iterator to use

Returns: false if there are no more elements to iterate, true otherwise

bool iter_list(void** v, ListIter* i);

iter_list_nodes

Move the iterator to the next node in the list.

  • Param n: A location where a pointer to the current node will be written
  • Param i: Pointer to the iterator to use

Returns: false if there are no more elements to iterate, true otherwise

bool iter_list_nodes(ListNode** n, ListIter* i);

iter_list_reversed

Move the iterator to the next element in the list (when iterated backwards).

  • Param val: A location where the value at the current point in the list will be written
  • Param i: Pointer to the iterator to use

Returns: false if there are no more elements to iterate, true otherwise

bool iter_list_reversed(void** val, ReversedListIter* i);

make_list

Initialise a list.

  • Param l: Pointer to the area of memory to initialise
void make_list(List* l);

make_list_iter

Initialise an iterator for a list.

  • Param i: Pointer to the iterator to initialise
  • Param l: Pointer to the list which the iterator will run over
void make_list_iter(ListIter* i, List* l);

make_list_node

Initialise a list node.

  • Param ln: Pointer to the memory to initialise
  • Param data: The data to store in the node
void make_list_node(ListNode* ln, void* data);

make_reversed_list_iter

Initialise a reverse-iterator for a list.

  • Param i: Pointer to the iterator to initialise
  • Param l: Pointer to the list which the iterator will run over
void make_reversed_list_iter(ReversedListIter* i, List* l);

prepend_list

Add a value to the head of a list.

  • Param l: pointer to the list to change
  • Param v: Pointer to the value to prepend

Returns: true iff successful

bool prepend_list(List* l, void* v);

prepend_list_node

Add a node to the front of a list.

  • Param l: Pointer to the list to change
  • Param ln: Pointer to the node to add

Returns: true iff successfil

bool prepend_list_node(List* l, ListNode* ln);

remove_list_node

Remove a node from a list.

  • Param l: Pointer to the list which contains ln
  • Param ln: Pointer to the node to remove

Returns: Returns true iff successful

bool remove_list_node(List* l, ListNode* ln);

set_sublist

Declare whether all nodes of a list are now entirely contained within another, preventing double-frees.

  • Param l: List to modify
  • Param is_sublist: Whether l is a sublist of another list
void set_sublist(List* l, bool is_sublist);