blob: da38c2f95b29505702c98757afa8bb13e0870490 [file] [log] [blame]
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -07001#pragma once
2
3#include <stdbool.h>
4#include <stdlib.h>
5
6struct list_node_t;
7typedef struct list_node_t list_node_t;
8
9struct list_t;
10typedef struct list_t list_t;
11
12typedef void (*list_free_cb)(void *data);
13typedef bool (*list_iter_cb)(void *data);
14
Zach Johnson8d436962015-03-02 14:42:02 -080015// Returns a new, empty list. Returns NULL if not enough memory could be allocated
16// for the list structure. The returned list must be freed with |list_free|. The
17// |callback| specifies a function to be called whenever a list element is removed
18// from the list. It can be used to release resources held by the list element, e.g.
19// memory or file descriptor. |callback| may be NULL if no cleanup is necessary on
20// element removal.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070021list_t *list_new(list_free_cb callback);
Zach Johnson8d436962015-03-02 14:42:02 -080022
23// Frees the list. This function accepts NULL as an argument, in which case it
24// behaves like a no-op.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070025void list_free(list_t *list);
26
Zach Johnson8d436962015-03-02 14:42:02 -080027// Returns true if |list| is empty (has no elements), false otherwise.
28// |list| may not be NULL.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070029bool list_is_empty(const list_t *list);
Zach Johnson8d436962015-03-02 14:42:02 -080030
31// Returns true if the list contains |data|, false otherwise.
32// |list| may not be NULL.
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070033bool list_contains(const list_t *list, const void *data);
Zach Johnson8d436962015-03-02 14:42:02 -080034
35// Returns the length of the |list|. |list| may not be NULL.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070036size_t list_length(const list_t *list);
Zach Johnson8d436962015-03-02 14:42:02 -080037
38// Returns the first element in the list without removing it. |list| may not
39// be NULL or empty.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070040void *list_front(const list_t *list);
Zach Johnson8d436962015-03-02 14:42:02 -080041
42// Returns the last element in the list without removing it. |list| may not
43// be NULL or empty.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070044void *list_back(const list_t *list);
45
Zach Johnson8d436962015-03-02 14:42:02 -080046// Inserts |data| after |prev_node| in |list|. |data|, |list|, and |prev_node|
47// may not be NULL. This function does not make a copy of |data| so the pointer
48// must remain valid at least until the element is removed from the list or the
49// list is freed. Returns true if |data| could be inserted, false otherwise
50// (e.g. out of memory).
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070051bool list_insert_after(list_t *list, list_node_t *prev_node, void *data);
Zach Johnson8d436962015-03-02 14:42:02 -080052
53// Inserts |data| at the beginning of |list|. Neither |data| nor |list| may be NULL.
54// This function does not make a copy of |data| so the pointer must remain valid
55// at least until the element is removed from the list or the list is freed.
56// Returns true if |data| could be inserted, false otherwise (e.g. out of memory).
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070057bool list_prepend(list_t *list, void *data);
Zach Johnson8d436962015-03-02 14:42:02 -080058
59// Inserts |data| at the end of |list|. Neither |data| nor |list| may be NULL.
60// This function does not make a copy of |data| so the pointer must remain valid
61// at least until the element is removed from the list or the list is freed.
62// Returns true if |data| could be inserted, false otherwise (e.g. out of memory).
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070063bool list_append(list_t *list, void *data);
Zach Johnson8d436962015-03-02 14:42:02 -080064
65// Removes |data| from the list. Neither |list| nor |data| may be NULL. If |data|
66// is inserted multiple times in the list, this function will only remove the first
67// instance. If a free function was specified in |list_new|, it will be called back
68// with |data|. This function returns true if |data| was found in the list and removed,
69// false otherwise.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070070bool list_remove(list_t *list, void *data);
Zach Johnson8d436962015-03-02 14:42:02 -080071
72// Removes all elements in the list. Calling this function will return the list to the
73// same state it was in after |list_new|. |list| may not be NULL.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070074void list_clear(list_t *list);
75
Zach Johnson8d436962015-03-02 14:42:02 -080076// Iterates through the entire |list| and calls |callback| for each data element.
77// If the list is empty, |callback| will never be called. It is safe to mutate the
78// list inside the callback. If an element is added before the node being visited,
79// there will be no callback for the newly-inserted node. Neither |list| nor
80// |callback| may be NULL.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -070081void list_foreach(const list_t *list, list_iter_cb callback);
82
Zach Johnson8d436962015-03-02 14:42:02 -080083// Returns an iterator to the first element in |list|. |list| may not be NULL.
84// The returned iterator is valid as long as it does not equal the value returned
85// by |list_end|.
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070086list_node_t *list_begin(const list_t *list);
Zach Johnson8d436962015-03-02 14:42:02 -080087
88// Returns an iterator that points past the end of the list. In other words,
89// this function returns the value of an invalid iterator for the given list.
90// When an iterator has the same value as what's returned by this function, you
91// may no longer call |list_next| with the iterator. |list| may not be NULL.
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070092list_node_t *list_end(const list_t *list);
Zach Johnson8d436962015-03-02 14:42:02 -080093
94// Given a valid iterator |node|, this function returns the next value for the
95// iterator. If the returned value equals the value returned by |list_end|, the
96// iterator has reached the end of the list and may no longer be used for any
97// purpose.
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070098list_node_t *list_next(const list_node_t *node);
Zach Johnson8d436962015-03-02 14:42:02 -080099
100// Returns the value stored at the location pointed to by the iterator |node|.
101// |node| must not equal the value returned by |list_end|.
Sharvil Nanavati540e7ca2014-04-24 16:22:45 -0700102void *list_node(const list_node_t *node);