Tue, 01 Mar 2005 22:57:57 +0100
Use install-sh.
60 | 1 | /* |
90 | 2 | * libtu/ptrlist.c |
60 | 3 | * |
90 | 4 | * Copyright (c) Tuomo Valkonen 1999-2005. |
60 | 5 | * |
6 | * You may distribute and modify this library under the terms of either | |
7 | * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. | |
8 | */ | |
9 | ||
10 | #include "obj.h" | |
90 | 11 | #include "ptrlist.h" |
60 | 12 | #include "types.h" |
13 | #include "dlist.h" | |
14 | #include "misc.h" | |
15 | ||
16 | ||
90 | 17 | static void free_node(PtrList **ptrlist, PtrList *node) |
60 | 18 | { |
90 | 19 | UNLINK_ITEM(*ptrlist, node, next, prev); |
62 | 20 | free(node); |
60 | 21 | } |
22 | ||
23 | ||
90 | 24 | static PtrList *mknode(void *ptr) |
60 | 25 | { |
90 | 26 | PtrList *node; |
62 | 27 | |
90 | 28 | if(ptr==NULL) |
88 | 29 | return NULL; |
62 | 30 | |
90 | 31 | node=ALLOC(PtrList); |
62 | 32 | |
33 | if(node==NULL) | |
34 | return FALSE; | |
35 | ||
90 | 36 | node->ptr=ptr; |
62 | 37 | |
88 | 38 | return node; |
39 | } | |
40 | ||
41 | ||
90 | 42 | static PtrList *ptrlist_find_node(PtrList *ptrlist, void *ptr) |
88 | 43 | { |
90 | 44 | PtrList *node=ptrlist; |
88 | 45 | |
46 | while(node!=NULL){ | |
90 | 47 | if(node->ptr==ptr) |
88 | 48 | break; |
49 | node=node->next; | |
50 | } | |
51 | ||
52 | return node; | |
53 | } | |
54 | ||
55 | ||
90 | 56 | bool ptrlist_insert_last(PtrList **ptrlist, void *ptr) |
88 | 57 | { |
90 | 58 | PtrList *node=mknode(ptr); |
88 | 59 | |
60 | if(node==NULL) | |
61 | return FALSE; | |
62 | ||
90 | 63 | LINK_ITEM_LAST(*ptrlist, node, next, prev); |
88 | 64 | |
65 | return TRUE; | |
66 | } | |
67 | ||
68 | ||
90 | 69 | bool ptrlist_insert_first(PtrList **ptrlist, void *ptr) |
88 | 70 | { |
90 | 71 | PtrList *node=mknode(ptr); |
88 | 72 | |
73 | if(node==NULL) | |
74 | return FALSE; | |
75 | ||
90 | 76 | LINK_ITEM_FIRST(*ptrlist, node, next, prev); |
88 | 77 | |
78 | return TRUE; | |
79 | } | |
80 | ||
81 | ||
90 | 82 | bool ptrlist_reinsert_last(PtrList **ptrlist, void *ptr) |
88 | 83 | { |
90 | 84 | PtrList *node=ptrlist_find_node(*ptrlist, ptr); |
88 | 85 | |
86 | if(node==NULL) | |
87 | return FALSE; | |
88 | ||
90 | 89 | UNLINK_ITEM(*ptrlist, node, next, prev); |
90 | LINK_ITEM_LAST(*ptrlist, node, next, prev); | |
88 | 91 | |
92 | return TRUE; | |
93 | } | |
94 | ||
95 | ||
90 | 96 | bool ptrlist_reinsert_first(PtrList **ptrlist, void *ptr) |
88 | 97 | { |
90 | 98 | PtrList *node=ptrlist_find_node(*ptrlist, ptr); |
88 | 99 | |
100 | if(node==NULL) | |
101 | return FALSE; | |
102 | ||
90 | 103 | UNLINK_ITEM(*ptrlist, node, next, prev); |
104 | LINK_ITEM_FIRST(*ptrlist, node, next, prev); | |
62 | 105 | |
106 | return TRUE; | |
60 | 107 | } |
108 | ||
109 | ||
90 | 110 | void ptrlist_remove(PtrList **ptrlist, void *ptr) |
60 | 111 | { |
90 | 112 | PtrList *node=ptrlist_find_node(*ptrlist, ptr); |
62 | 113 | |
88 | 114 | if(node!=NULL) |
90 | 115 | free_node(ptrlist, node); |
60 | 116 | } |
117 | ||
118 | ||
90 | 119 | void ptrlist_clear(PtrList **ptrlist) |
60 | 120 | { |
90 | 121 | while(*ptrlist!=NULL) |
122 | free_node(ptrlist, *ptrlist); | |
60 | 123 | } |
124 | ||
125 | ||
90 | 126 | PtrListIterTmp ptrlist_iter_tmp=NULL; |
60 | 127 | |
128 | ||
90 | 129 | void ptrlist_iter_init(PtrListIterTmp *state, PtrList *ptrlist) |
60 | 130 | { |
90 | 131 | *state=ptrlist; |
60 | 132 | } |
133 | ||
134 | ||
90 | 135 | void *ptrlist_iter(PtrListIterTmp *state) |
60 | 136 | { |
90 | 137 | void *ptr=NULL; |
62 | 138 | |
88 | 139 | if(*state!=NULL){ |
90 | 140 | ptr=(*state)->ptr; |
88 | 141 | (*state)=(*state)->next; |
142 | } | |
62 | 143 | |
90 | 144 | return ptr; |
60 | 145 | } |
146 | ||
88 | 147 | |
90 | 148 | void ptrlist_iter_rev_init(PtrListIterTmp *state, PtrList *ptrlist) |
88 | 149 | { |
90 | 150 | *state=(ptrlist==NULL ? NULL : ptrlist->prev); |
88 | 151 | } |
152 | ||
153 | ||
90 | 154 | void *ptrlist_iter_rev(PtrListIterTmp *state) |
88 | 155 | { |
90 | 156 | void *ptr=NULL; |
88 | 157 | |
158 | if(*state!=NULL){ | |
90 | 159 | ptr=(*state)->ptr; |
88 | 160 | *state=(*state)->prev; |
161 | if((*state)->next==NULL) | |
162 | *state=NULL; | |
163 | } | |
164 | ||
90 | 165 | return ptr; |
88 | 166 | } |
167 | ||
92
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
168 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
169 | void *ptrlist_take_first(PtrList **ptrlist) |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
170 | { |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
171 | PtrList *node=*ptrlist; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
172 | void *ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
173 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
174 | if(node==NULL) |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
175 | return NULL; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
176 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
177 | ptr=node->ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
178 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
179 | free_node(ptrlist, node); |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
180 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
181 | return ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
182 | } |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
183 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
184 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
185 | void *ptrlist_take_last(PtrList **ptrlist) |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
186 | { |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
187 | PtrList *node=*ptrlist; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
188 | void *ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
189 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
190 | if(node==NULL) |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
191 | return NULL; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
192 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
193 | node=node->prev; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
194 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
195 | ptr=node->ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
196 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
197 | free_node(ptrlist, node); |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
198 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
199 | return ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
200 | } |