| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* list.h: Linked lists. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef MOWGLI_SRC_LIBMOWGLI_CONTAINER_LIST_H_INCLUDE_GUARD |
| 25 |
#define MOWGLI_SRC_LIBMOWGLI_CONTAINER_LIST_H_INCLUDE_GUARD 1 |
| 26 |
|
| 27 |
#include "core/stdinc.h" |
| 28 |
#include "platform/attributes.h" |
| 29 |
|
| 30 |
/* macros for linked lists */ |
| 31 |
#define MOWGLI_LIST_FOREACH(n, head) \ |
| 32 |
for (n = (head); n; n = n->next) |
| 33 |
#define MOWGLI_LIST_FOREACH_NEXT(n, head) \ |
| 34 |
for (n = (head); n->next; n = n->next) |
| 35 |
#define MOWGLI_LIST_FOREACH_PREV(n, tail) \ |
| 36 |
for (n = (tail); n; n = n->prev) |
| 37 |
|
| 38 |
#define MOWGLI_LIST_LENGTH(list) (list)->count |
| 39 |
|
| 40 |
#define MOWGLI_LIST_FOREACH_SAFE(n, tn, head) \ |
| 41 |
for (n = (head), tn = n ? n->next : NULL; n != NULL; n = tn, tn = n ? n->next : NULL) |
| 42 |
|
| 43 |
/* list node struct */ |
| 44 |
typedef struct mowgli_node_ mowgli_node_t; |
| 45 |
typedef struct mowgli_list_ mowgli_list_t; |
| 46 |
|
| 47 |
struct mowgli_node_ |
| 48 |
{ |
| 49 |
struct mowgli_node_ *next, *prev; |
| 50 |
|
| 51 |
void *data; /* pointer to real structure */ |
| 52 |
}; |
| 53 |
|
| 54 |
/* node list struct */ |
| 55 |
struct mowgli_list_ |
| 56 |
{ |
| 57 |
mowgli_node_t *head, *tail; |
| 58 |
size_t count; /* how many entries in the list */ |
| 59 |
}; |
| 60 |
|
| 61 |
extern mowgli_node_t *mowgli_node_create(void) |
| 62 |
MOWGLI_FATTR_MALLOC; |
| 63 |
|
| 64 |
extern void mowgli_node_free(mowgli_node_t *n); |
| 65 |
extern void mowgli_node_add(void *data, mowgli_node_t *n, mowgli_list_t *l); |
| 66 |
extern void mowgli_node_add_head(void *data, mowgli_node_t *n, mowgli_list_t *l); |
| 67 |
extern void mowgli_node_add_before(void *data, mowgli_node_t *n, mowgli_list_t *l, mowgli_node_t *before); |
| 68 |
extern void mowgli_node_add_after(void *data, mowgli_node_t *n, mowgli_list_t *l, mowgli_node_t *before); |
| 69 |
extern void mowgli_node_insert(void *data, mowgli_node_t *n, mowgli_list_t *l, size_t position); |
| 70 |
extern ssize_t mowgli_node_index(mowgli_node_t *n, mowgli_list_t *l); |
| 71 |
extern void mowgli_node_delete(mowgli_node_t *n, mowgli_list_t *l); |
| 72 |
extern mowgli_node_t *mowgli_node_find(void *data, mowgli_list_t *l); |
| 73 |
extern void mowgli_node_move(mowgli_node_t *m, mowgli_list_t *oldlist, mowgli_list_t *newlist); |
| 74 |
extern mowgli_node_t *mowgli_node_nth(mowgli_list_t *l, size_t pos); |
| 75 |
extern void *mowgli_node_nth_data(mowgli_list_t *l, size_t pos); |
| 76 |
|
| 77 |
typedef int (*mowgli_list_comparator_t)(mowgli_node_t *n, mowgli_node_t *n2, void *opaque); |
| 78 |
|
| 79 |
extern mowgli_list_t *mowgli_list_create(void) |
| 80 |
MOWGLI_FATTR_MALLOC; |
| 81 |
|
| 82 |
extern void mowgli_list_free(mowgli_list_t *l); |
| 83 |
extern void mowgli_list_concat(mowgli_list_t *l, mowgli_list_t *l2); |
| 84 |
extern void mowgli_list_reverse(mowgli_list_t *l); |
| 85 |
extern void mowgli_list_sort(mowgli_list_t *l, mowgli_list_comparator_t comp, void *opaque); |
| 86 |
|
| 87 |
#endif /* MOWGLI_SRC_LIBMOWGLI_CONTAINER_LIST_H_INCLUDE_GUARD */ |