tsst
[skippy-xd] / dlist.h
1 /* Skippy - Seduces Kids Into Perversion
2  *
3  * Copyright (C) 2004 Hyriand <hyriand@thegraveyard.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #ifndef SKIPPY_DLIST_H
21 #define SKIPPY_DLIST_H
22
23 struct dlist_element {
24         void *data;
25         struct dlist_element *next;
26         struct dlist_element *prev;
27 };
28
29 typedef struct dlist_element dlist;
30
31 /* first element in list */
32 dlist *dlist_first(dlist *);
33
34 /* last element in list */
35 dlist *dlist_last(dlist *);
36
37 /* add element to the end of the list, returns new element */
38 dlist *dlist_add(dlist *, void *);
39
40 /* add element to the start of the list, returns new element (and thus, start of list) */
41 dlist *dlist_prepend(dlist *, void *);
42
43 /* remove an element from the list, returns another element in the list or 0 */
44 dlist *dlist_remove(dlist *);
45 dlist *dlist_remove_free_data(dlist *);
46 dlist *dlist_remove_nth(dlist *, unsigned int n);
47 dlist *dlist_remove_nth_free_data(dlist *, unsigned int n);
48
49 /* free the list (not the data), returns 0 */
50 dlist *dlist_free(dlist *);
51
52 /* delete a list calling func on each data item */
53 typedef void (*dlist_free_func)(void *);
54 dlist *dlist_free_with_func(dlist *, dlist_free_func);
55
56 /* free the data (not the list) */
57 void dlist_free_data(dlist *);
58
59 /* free both list and data, returns 0 */
60 dlist *dlist_free_with_data(dlist *);
61
62 /* return the length of the list */
63 unsigned int dlist_len(dlist *);
64
65 /* check if l1 and l2 are elements of the same list */
66 int dlist_same(dlist *, dlist *);
67
68 /* reverse a list (swaps data) */
69 void dlist_reverse(dlist *);
70
71 /* duplicate the list (not the data), returns new end */
72 dlist *dlist_dup(dlist *);
73
74 /* find all matching elements (returns new list or 0) */
75 typedef int (*dlist_match_func)(dlist*, void *);
76 dlist *dlist_find_all(dlist *, dlist_match_func, void *);
77
78 /* find an element (returns element or 0) */
79 dlist *dlist_find(dlist *, dlist_match_func, void *);
80
81 /* find an element whose data pointer matches */
82 dlist *dlist_find_data(dlist *, void *);
83
84 /* return nth element or 0 */
85 dlist *dlist_nth(dlist *l, unsigned int n);
86
87 /* swap the data fields of 2 elements */
88 void dlist_swap(dlist *, dlist *);
89
90 /* sort a list (not very efficient, uses bubble-sort) */
91 typedef int (*dlist_cmp_func)(dlist *, dlist *, void *);
92 void dlist_sort(dlist *, dlist_cmp_func, void *);
93
94 #endif /* SKIPPY_DLIST_H */