objlist.c

Wed, 23 Feb 2005 19:05:01 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 23 Feb 2005 19:05:01 +0100
changeset 87
95553f8ea540
parent 62
aae5facf9fc5
child 91
817f90f58aec
permissions
-rw-r--r--

Added dlist iteration macros.

60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
1 /*
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
2 * libtu/objlist.c
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
3 *
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2004.
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
5 *
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
6 * You may distribute and modify this library under the terms of either
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
8 */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
9
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
10 #include "obj.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
11 #include "types.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
12 #include "objlist.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
13 #include "dlist.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
14 #include "misc.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
15
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
16
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
17 static ObjList *iter_next=NULL;
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
18
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
19
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
20 static void free_node(ObjList **objlist, ObjList *node)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
21 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
22 UNLINK_ITEM(*objlist, node, next, prev);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
23 free(node);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
24 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
25
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
26
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
27 void watch_handler(Watch *watch, Obj *obj)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
28 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
29 ObjList *node=(ObjList*)watch;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
30 ObjList **list=node->list;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
31
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
32 if(iter_next==node)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
33 iter_next=node->next;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
34
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
35 free_node(list, node);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
36 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
37
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
38
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
39
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
40 bool objlist_insert(ObjList **objlist, Obj *obj)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
41 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
42 ObjList *node;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
43
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
44 if(obj==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
45 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
46
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
47 node=ALLOC(ObjList);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
48
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
49 if(node==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
50 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
51
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
52 watch_init(&(node->watch));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
53 watch_setup(&(node->watch), obj, watch_handler);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
54 node->list=objlist;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
55
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
56 LINK_ITEM_FIRST(*objlist, node, next, prev);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
57
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
58 return TRUE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
59 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
61
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
62 void objlist_remove(ObjList **objlist, Obj *obj)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
63 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
64 ObjList *node=*objlist;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
65
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
66 while(node!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
67 if(node->watch.obj==obj){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
68 watch_reset(&(node->watch));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
69 free_node(objlist, node);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
70 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
71 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
72 node=node->next;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
73 }
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
74 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
75
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
76
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
77 void objlist_clear(ObjList **objlist)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
78 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
79 while(*objlist!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
80 watch_reset(&((*objlist)->watch));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
81 free_node(objlist, *objlist);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
82 }
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
83 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
84
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
85
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
86 /* Warning: not thread-safe */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
87
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
88
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
89 Obj *objlist_init_iter(ObjList *objlist)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
90 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
91 if(objlist==NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
92 iter_next=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
93 return NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
94 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
95
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
96 iter_next=objlist->next;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
97 return objlist->watch.obj;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
98 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
99
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
100
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
101 Obj *objlist_iter()
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
102 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
103 ObjList *ret;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
104
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
105 if(iter_next==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
106 return NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
107
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
108 ret=iter_next;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
109 iter_next=iter_next->next;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
110
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
111 return ret->watch.obj;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
112 }

mercurial