add .gitignore
[tedtools.git] / glist.h
1 /*
2  * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29 #ifndef __GLIST_H__
30 #define __GLIST_H__
31
32 typedef struct GListCell {
33         void                            *data;
34         struct GListCell        *next;
35         struct GListCell        *prev;
36 } GListCell;
37
38 #define GLCELL_DATA(c)  ((c)->data)
39 #define GLCELL_NEXT(c)  ((c)->next)
40 #define GLCELL_PREV(c)  ((c)->prev)
41
42 typedef  int (*GListCellComparator)(const void *, const void *);
43 typedef  int (*GListDataFreeFunc)(const void *);
44
45 typedef struct GList {
46         GListCell       *head;
47         GListCell       *tail;
48         int     length;
49         GListCellComparator     cmpFunc;
50         GListDataFreeFunc       freeFunc;
51 } GList;
52
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)
56
57
58
59 GListCell*      GListPop(GList *l);
60 GList*  GListPush(GList *l, void *ptr);
61 GList*  GListPushCell(GList *l, GListCell* c);
62 GListCell*      GListShift(GList *l);
63 GList*  GListUnshift(GList *l, void *ptr);
64 GList*  GListUnshiftCell(GList *l, GListCell* c);
65 GList*  GListInsert(GList *l, GListCell *prev, void *ptr);
66 GList*  GListDelete(GList *l, GListCell *c);
67 GListCell* GListGet(GList *l, int n);
68
69 GListCell* GListFind(GList *l, void *ptr);
70 GList* GListSort(GList *l);
71 GList* GListUniq(GList *l);
72
73 void    GListFree(GList *l);
74 void    GListFreeCell(GList *l, GListCell* c);
75
76 GList*  GListTruncate(GList *l, int n);
77 GList*  GListCopy(GList *l);
78 GList*  GListConcat(GList *l1, GList *l2);
79 GList*  GListDifference(GList *l1, GList *l2);
80 GList*  GListUnion(GList *l1, GList *l2);
81 GList*  GListIntersect(GList *l1, GList *l2);
82
83 #define GListForeach(cell, l) \
84         for ((cell) = GLIST_HEAD(l); (cell) != NULL; (cell) = GLCELL_NEXT(cell))
85
86 #define GListForeachBack(cell, l) \
87         for ((cell) = GLIST_TAIL(l); (cell) != NULL; (cell) = GLCELL_PREV(cell))
88
89 #define GListForeachCell(cell, initcell) \
90         for ((cell) = (initcell); (cell) != NULL; (cell) = GLCELL_NEXT(cell))
91
92 #define GListForeachCellBack(cell, initcell) \
93         for ((cell) = (initcell); (cell) != NULL; (cell) = GLCELL_PREV(cell))
94
95 #endif