Fri, 25 Mar 2005 17:48:19 +0100
Added plain dlist reverse forall.
| 60 | 1 | /* |
| 2 | * libtu/objlist.c | |
| 3 | * | |
| 91 | 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" | |
| 11 | #include "types.h" | |
| 12 | #include "objlist.h" | |
| 13 | #include "dlist.h" | |
| 14 | #include "misc.h" | |
| 15 | ||
| 16 | ||
| 91 | 17 | static ObjList *reuse_first(ObjList **objlist) |
| 18 | { | |
| 19 | ObjList *node=*objlist; | |
| 20 | ||
| 21 | if(node!=NULL && node->watch.obj==NULL){ | |
| 22 | UNLINK_ITEM(*objlist, node, next, prev); | |
| 23 | return node; | |
| 24 | } | |
| 25 | ||
| 26 | return NULL; | |
| 27 | } | |
| 28 | ||
| 29 | ||
| 30 | static ObjList *reuse_last(ObjList **objlist) | |
| 31 | { | |
| 32 | ObjList *node=*objlist; | |
| 33 | ||
| 34 | if(node==NULL) | |
| 35 | return NULL; | |
| 36 | ||
| 37 | node=node->prev; | |
| 38 | ||
| 39 | if(node!=NULL && node->watch.obj==NULL){ | |
| 40 | UNLINK_ITEM(*objlist, node, next, prev); | |
| 41 | return node; | |
| 42 | } | |
| 43 | ||
| 44 | return NULL; | |
| 45 | } | |
| 46 | ||
| 47 | ||
| 48 | static ObjList *reuse(ObjList **objlist) | |
| 49 | { | |
| 50 | ObjList *first=reuse_first(objlist); | |
| 51 | ObjList *last=reuse_first(objlist); | |
| 52 | ||
| 53 | if(first==NULL){ | |
| 54 | return last; | |
| 55 | }else{ | |
| 56 | if(last!=NULL) | |
| 57 | free(last); | |
| 58 | return first; | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | ||
| 63 | static void optimise(ObjList **objlist) | |
| 64 | { | |
| 65 | ObjList *first=reuse_first(objlist); | |
| 66 | ObjList *last=reuse_first(objlist); | |
| 67 | ||
| 68 | if(first!=NULL) | |
| 69 | free(first); | |
| 70 | if(last!=NULL) | |
| 71 | free(last); | |
| 72 | } | |
| 73 | ||
| 74 | ||
| 75 | void watch_handler(Watch *watch, Obj *obj) | |
| 76 | { | |
| 77 | ObjList *node=(ObjList*)watch; | |
| 78 | ||
| 79 | assert(node->prev!=NULL); | |
| 80 | ||
| 81 | if(node->next==NULL){ | |
| 82 | /* Last item - can't free */ | |
| 83 | }else if(node->prev->next==NULL){ | |
| 84 | /* First item - can't free cheaply */ | |
| 85 | }else{ | |
| 86 | ObjList *tmp=node->prev; | |
| 87 | node->next->prev=node->prev; | |
| 88 | tmp->next=node->next; | |
| 89 | free(node); | |
| 90 | } | |
| 91 | } | |
| 60 | 92 | |
| 93 | ||
| 94 | static void free_node(ObjList **objlist, ObjList *node) | |
| 95 | { | |
| 91 | 96 | watch_reset(&(node->watch)); |
| 62 | 97 | UNLINK_ITEM(*objlist, node, next, prev); |
| 98 | free(node); | |
| 60 | 99 | } |
| 100 | ||
| 101 | ||
| 91 | 102 | static ObjList *mknode(void *obj) |
| 60 | 103 | { |
| 62 | 104 | ObjList *node; |
| 105 | ||
| 106 | if(obj==NULL) | |
| 91 | 107 | return NULL; |
| 62 | 108 | |
| 109 | node=ALLOC(ObjList); | |
| 110 | ||
| 111 | if(node==NULL) | |
| 112 | return FALSE; | |
| 113 | ||
| 114 | watch_init(&(node->watch)); | |
| 91 | 115 | |
| 116 | if(!watch_setup(&(node->watch), obj, watch_handler)){ | |
| 117 | free(node); | |
| 118 | return NULL; | |
| 119 | } | |
| 120 | ||
| 121 | return node; | |
| 122 | } | |
| 123 | ||
| 124 | ||
| 125 | static ObjList *objlist_find_node(ObjList *objlist, Obj *obj) | |
| 126 | { | |
| 127 | ObjList *node=objlist; | |
| 128 | ||
| 129 | while(node!=NULL){ | |
| 130 | if(node->watch.obj==obj) | |
| 131 | break; | |
| 132 | node=node->next; | |
| 133 | } | |
| 134 | ||
| 135 | return node; | |
| 136 | } | |
| 137 | ||
| 138 | ||
| 139 | bool objlist_insert_last(ObjList **objlist, Obj *obj) | |
| 140 | { | |
| 141 | ObjList *node=reuse(objlist); | |
| 142 | ||
| 143 | if(node==NULL) | |
| 144 | node=mknode(obj); | |
| 145 | ||
| 146 | if(node==NULL) | |
| 147 | return FALSE; | |
| 148 | ||
| 149 | LINK_ITEM_LAST(*objlist, node, next, prev); | |
| 62 | 150 | |
| 91 | 151 | return TRUE; |
| 152 | } | |
| 153 | ||
| 154 | ||
| 155 | bool objlist_insert_first(ObjList **objlist, Obj *obj) | |
| 156 | { | |
| 157 | ObjList *node=reuse(objlist); | |
| 158 | ||
| 159 | if(node==NULL) | |
| 160 | node=mknode(obj); | |
| 161 | ||
| 162 | if(node==NULL) | |
| 163 | return FALSE; | |
| 164 | ||
| 165 | LINK_ITEM_FIRST(*objlist, node, next, prev); | |
| 166 | ||
| 167 | return TRUE; | |
| 168 | } | |
| 169 | ||
| 170 | ||
| 171 | bool objlist_reinsert_last(ObjList **objlist, Obj *obj) | |
| 172 | { | |
| 173 | ObjList *node; | |
| 174 | ||
| 175 | optimise(objlist); | |
| 176 | ||
| 177 | node=objlist_find_node(*objlist, obj); | |
| 178 | ||
| 179 | if(node==NULL) | |
| 180 | return FALSE; | |
| 181 | ||
| 182 | UNLINK_ITEM(*objlist, node, next, prev); | |
| 183 | LINK_ITEM_LAST(*objlist, node, next, prev); | |
| 184 | ||
| 185 | return TRUE; | |
| 186 | } | |
| 187 | ||
| 188 | ||
| 189 | bool objlist_reinsert_first(ObjList **objlist, Obj *obj) | |
| 190 | { | |
| 191 | ObjList *node; | |
| 192 | ||
| 193 | optimise(objlist); | |
| 194 | ||
| 195 | node=objlist_find_node(*objlist, obj); | |
| 196 | ||
| 197 | if(node==NULL) | |
| 198 | return FALSE; | |
| 199 | ||
| 200 | UNLINK_ITEM(*objlist, node, next, prev); | |
| 62 | 201 | LINK_ITEM_FIRST(*objlist, node, next, prev); |
| 202 | ||
| 203 | return TRUE; | |
| 60 | 204 | } |
| 205 | ||
| 206 | ||
| 207 | void objlist_remove(ObjList **objlist, Obj *obj) | |
| 208 | { | |
| 91 | 209 | ObjList *node=objlist_find_node(*objlist, obj); |
| 62 | 210 | |
| 91 | 211 | if(node!=NULL) |
| 212 | free_node(objlist, node); | |
| 213 | ||
| 214 | optimise(objlist); | |
| 60 | 215 | } |
| 216 | ||
| 217 | ||
| 218 | void objlist_clear(ObjList **objlist) | |
| 219 | { | |
| 91 | 220 | while(*objlist!=NULL) |
| 62 | 221 | free_node(objlist, *objlist); |
| 91 | 222 | } |
| 223 | ||
| 224 | ||
| 225 | ObjListIterTmp objlist_iter_tmp=NULL; | |
| 226 | ||
| 227 | ||
| 228 | void objlist_iter_init(ObjListIterTmp *state, ObjList *objlist) | |
| 229 | { | |
| 230 | *state=objlist; | |
| 231 | } | |
| 232 | ||
| 233 | ||
| 234 | Obj *objlist_iter(ObjListIterTmp *state) | |
| 235 | { | |
| 236 | Obj *obj=NULL; | |
| 237 | ||
| 238 | while(obj==NULL && *state!=NULL){ | |
| 239 | obj=(*state)->watch.obj; | |
| 240 | (*state)=(*state)->next; | |
| 62 | 241 | } |
| 91 | 242 | |
| 243 | return obj; | |
| 60 | 244 | } |
| 245 | ||
| 246 | ||
| 91 | 247 | void objlist_iter_rev_init(ObjListIterTmp *state, ObjList *objlist) |
| 60 | 248 | { |
| 91 | 249 | *state=(objlist==NULL ? NULL : objlist->prev); |
| 60 | 250 | } |
| 251 | ||
| 252 | ||
| 91 | 253 | Obj *objlist_iter_rev(ObjListIterTmp *state) |
| 60 | 254 | { |
| 91 | 255 | Obj *obj=NULL; |
| 256 | ||
| 257 | while(obj==NULL && *state!=NULL){ | |
| 258 | obj=(*state)->watch.obj; | |
| 259 | *state=(*state)->prev; | |
| 260 | if((*state)->next==NULL) | |
| 261 | *state=NULL; | |
| 262 | } | |
| 62 | 263 | |
| 91 | 264 | return obj; |
| 265 | } | |
| 266 | ||
| 267 | ||
| 268 | bool objlist_empty(ObjList *objlist) | |
| 269 | { | |
| 270 | ObjListIterTmp tmp; | |
| 271 | Obj *obj; | |
| 62 | 272 | |
| 91 | 273 | FOR_ALL_ON_OBJLIST(Obj*, obj, objlist, tmp){ |
| 274 | return FALSE; | |
| 275 | } | |
| 62 | 276 | |
| 91 | 277 | return TRUE; |
| 60 | 278 | } |
|
92
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
279 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
280 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
281 | Obj *objlist_take_first(ObjList **objlist) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
282 | { |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
283 | ObjList *node; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
284 | Obj*obj; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
285 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
286 | optimise(objlist); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
287 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
288 | node=*objlist; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
289 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
290 | if(node==NULL) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
291 | return NULL; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
292 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
293 | obj=node->watch.obj; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
294 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
295 | assert(obj!=NULL); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
296 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
297 | free_node(objlist, node); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
298 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
299 | return obj; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
300 | } |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
301 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
302 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
303 | Obj *objlist_take_last(ObjList **objlist) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
304 | { |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
305 | ObjList *node; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
306 | Obj*obj; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
307 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
308 | optimise(objlist); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
309 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
310 | node=*objlist; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
311 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
312 | if(node==NULL) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
313 | return NULL; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
314 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
315 | node=node->prev; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
316 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
317 | obj=node->watch.obj; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
318 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
319 | assert(obj!=NULL); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
320 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
321 | free_node(objlist, node); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
322 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
323 | return obj; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
324 | } |