2 * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 typedef struct GListCell {
34 struct GListCell *next;
35 struct GListCell *prev;
38 #define GLCELL_DATA(c) ((c)->data)
39 #define GLCELL_NEXT(c) ((c)->next)
40 #define GLCELL_PREV(c) ((c)->prev)
42 typedef int (*GListCellComparator)(const void *, const void *);
43 typedef int (*GListDataFreeFunc)(const void *);
45 typedef struct GList {
49 GListCellComparator cmpFunc;
50 GListDataFreeFunc freeFunc;
53 #define GLIST_HEAD(l) ((l) ? (l)->head : NULL)
54 #define GLIST_TAIL(l) ((l) ? (l)->tail : NULL)
55 #define GLIST_LENGTH(l) ((l) ? (l)->length : 0)
59 GListCell* GListPop(GList *l);
60 GList* GListPush(GList *l, void *ptr);
61 GListCell* GListShift(GList *l);
62 GList* GListUnshift(GList *l, void *ptr);
63 GList* GListInsert(GList *l, GListCell *prev, void *ptr);
64 GList* GListDelete(GList *l, GListCell *c);
65 GListCell* GListGet(GList *l, int n);
67 GListCell* GListFind(GList *l, void *ptr);
68 GList* GListSort(GList *l);
69 GList* GListUniq(GList *l);
71 void GListFree(GList *l);
72 void GListFreeCell(GList *l, GListCell* c);
74 GList* GListTruncate(GList *l, int n);
75 GList* GListCopy(GList *l);
76 GList* GListConcat(GList *l1, GList *l2);
77 GList* GListDifference(GList *l1, GList *l2);
78 GList* GListUnion(GList *l1, GList *l2);
79 GList* GListIntersect(GList *l1, GList *l2);
81 #define GListForeach(cell, l) \
82 for ((cell) = GLIST_HEAD(l); (cell) != NULL; (cell) = GLCELL_NEXT(cell))
84 #define GListForeachBack(cell, l) \
85 for ((cell) = GLIST_TAIL(l); (cell) != NULL; (cell) = GLCELL_PREV(cell))
87 #define GListForeachCell(cell, initcell) \
88 for ((cell) = (initcell); (cell) != NULL; (cell) = GLCELL_NEXT(cell))
90 #define GListForeachCellBack(cell, initcell) \
91 for ((cell) = (initcell); (cell) != NULL; (cell) = GLCELL_PREV(cell))