Sun, 21 Jan 2007 21:02:55 +0100
Added stringstore_ref
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 | ||
101
50525dab6c8e
*list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents:
92
diff
changeset
|
110 | bool 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); |
101
50525dab6c8e
*list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents:
92
diff
changeset
|
116 | |
50525dab6c8e
*list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents:
92
diff
changeset
|
117 | return (node!=NULL); |
60 | 118 | } |
119 | ||
120 | ||
90 | 121 | void ptrlist_clear(PtrList **ptrlist) |
60 | 122 | { |
90 | 123 | while(*ptrlist!=NULL) |
124 | free_node(ptrlist, *ptrlist); | |
60 | 125 | } |
126 | ||
127 | ||
90 | 128 | PtrListIterTmp ptrlist_iter_tmp=NULL; |
60 | 129 | |
130 | ||
90 | 131 | void ptrlist_iter_init(PtrListIterTmp *state, PtrList *ptrlist) |
60 | 132 | { |
90 | 133 | *state=ptrlist; |
60 | 134 | } |
135 | ||
136 | ||
90 | 137 | void *ptrlist_iter(PtrListIterTmp *state) |
60 | 138 | { |
90 | 139 | void *ptr=NULL; |
62 | 140 | |
88 | 141 | if(*state!=NULL){ |
90 | 142 | ptr=(*state)->ptr; |
88 | 143 | (*state)=(*state)->next; |
144 | } | |
62 | 145 | |
90 | 146 | return ptr; |
60 | 147 | } |
148 | ||
88 | 149 | |
90 | 150 | void ptrlist_iter_rev_init(PtrListIterTmp *state, PtrList *ptrlist) |
88 | 151 | { |
90 | 152 | *state=(ptrlist==NULL ? NULL : ptrlist->prev); |
88 | 153 | } |
154 | ||
155 | ||
90 | 156 | void *ptrlist_iter_rev(PtrListIterTmp *state) |
88 | 157 | { |
90 | 158 | void *ptr=NULL; |
88 | 159 | |
160 | if(*state!=NULL){ | |
90 | 161 | ptr=(*state)->ptr; |
88 | 162 | *state=(*state)->prev; |
163 | if((*state)->next==NULL) | |
164 | *state=NULL; | |
165 | } | |
166 | ||
90 | 167 | return ptr; |
88 | 168 | } |
169 | ||
92
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 | 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
|
172 | { |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
173 | PtrList *node=*ptrlist; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
174 | void *ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
175 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
176 | if(node==NULL) |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
177 | return NULL; |
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 | ptr=node->ptr; |
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 | 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
|
182 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
183 | return ptr; |
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 | |
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 | 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
|
188 | { |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
189 | PtrList *node=*ptrlist; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
190 | void *ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
191 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
192 | if(node==NULL) |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
193 | return NULL; |
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 | node=node->prev; |
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 | ptr=node->ptr; |
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 | 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
|
200 | |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
201 | return ptr; |
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
202 | } |