objlist.c

Fri, 25 Mar 2005 17:48:19 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 25 Mar 2005 17:48:19 +0100
changeset 99
46ee75d9eba9
parent 92
55fcdff5bcea
child 101
50525dab6c8e
permissions
-rw-r--r--

Added plain dlist reverse forall.

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 *
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2005.
60
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
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
17 static ObjList *reuse_first(ObjList **objlist)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
18 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
19 ObjList *node=*objlist;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
20
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
21 if(node!=NULL && node->watch.obj==NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
22 UNLINK_ITEM(*objlist, node, next, prev);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
23 return node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
24 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
25
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
26 return NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
27 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
28
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
29
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
30 static ObjList *reuse_last(ObjList **objlist)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
31 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
32 ObjList *node=*objlist;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
33
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
34 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
35 return NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
36
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
37 node=node->prev;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
38
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
39 if(node!=NULL && node->watch.obj==NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
40 UNLINK_ITEM(*objlist, node, next, prev);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
41 return node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
42 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
43
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
44 return NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
45 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
46
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
47
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
48 static ObjList *reuse(ObjList **objlist)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
49 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
50 ObjList *first=reuse_first(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
51 ObjList *last=reuse_first(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
52
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
53 if(first==NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
54 return last;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
55 }else{
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
56 if(last!=NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
57 free(last);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
58 return first;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
59 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
60 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
61
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
62
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
63 static void optimise(ObjList **objlist)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
64 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
65 ObjList *first=reuse_first(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
66 ObjList *last=reuse_first(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
67
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
68 if(first!=NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
69 free(first);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
70 if(last!=NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
71 free(last);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
72 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
73
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
74
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
75 void watch_handler(Watch *watch, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
76 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
77 ObjList *node=(ObjList*)watch;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
78
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
79 assert(node->prev!=NULL);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
80
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
81 if(node->next==NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
82 /* Last item - can't free */
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
83 }else if(node->prev->next==NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
84 /* First item - can't free cheaply */
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
85 }else{
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
86 ObjList *tmp=node->prev;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
87 node->next->prev=node->prev;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
88 tmp->next=node->next;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
89 free(node);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
90 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
91 }
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
92
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
93
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
94 static void free_node(ObjList **objlist, ObjList *node)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
95 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
96 watch_reset(&(node->watch));
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
97 UNLINK_ITEM(*objlist, node, next, prev);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
98 free(node);
60
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
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
102 static ObjList *mknode(void *obj)
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
103 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
104 ObjList *node;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
105
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
106 if(obj==NULL)
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
107 return NULL;
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
108
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
109 node=ALLOC(ObjList);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
110
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
111 if(node==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
112 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
113
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
114 watch_init(&(node->watch));
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
115
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
116 if(!watch_setup(&(node->watch), obj, watch_handler)){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
117 free(node);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
118 return NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
119 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
120
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
121 return node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
122 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
123
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
124
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
125 static ObjList *objlist_find_node(ObjList *objlist, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
126 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
127 ObjList *node=objlist;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
128
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
129 while(node!=NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
130 if(node->watch.obj==obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
131 break;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
132 node=node->next;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
133 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
134
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
135 return node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
136 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
137
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
138
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
139 bool objlist_insert_last(ObjList **objlist, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
140 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
141 ObjList *node=reuse(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
142
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
143 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
144 node=mknode(obj);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
145
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
146 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
147 return FALSE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
148
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
149 LINK_ITEM_LAST(*objlist, node, next, prev);
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
150
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
151 return TRUE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
152 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
153
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
154
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
155 bool objlist_insert_first(ObjList **objlist, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
156 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
157 ObjList *node=reuse(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
158
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
159 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
160 node=mknode(obj);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
161
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
162 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
163 return FALSE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
164
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
165 LINK_ITEM_FIRST(*objlist, node, next, prev);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
166
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
167 return TRUE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
168 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
169
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
170
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
171 bool objlist_reinsert_last(ObjList **objlist, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
172 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
173 ObjList *node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
174
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
175 optimise(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
176
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
177 node=objlist_find_node(*objlist, obj);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
178
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
179 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
180 return FALSE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
181
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
182 UNLINK_ITEM(*objlist, node, next, prev);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
183 LINK_ITEM_LAST(*objlist, node, next, prev);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
184
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
185 return TRUE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
186 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
187
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
188
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
189 bool objlist_reinsert_first(ObjList **objlist, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
190 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
191 ObjList *node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
192
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
193 optimise(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
194
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
195 node=objlist_find_node(*objlist, obj);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
196
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
197 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
198 return FALSE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
199
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
200 UNLINK_ITEM(*objlist, node, next, prev);
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
201 LINK_ITEM_FIRST(*objlist, node, next, prev);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
202
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
203 return TRUE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
204 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
205
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
206
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
207 void objlist_remove(ObjList **objlist, Obj *obj)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
208 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
209 ObjList *node=objlist_find_node(*objlist, obj);
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
210
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
211 if(node!=NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
212 free_node(objlist, node);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
213
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
214 optimise(objlist);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
215 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
216
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
217
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
218 void objlist_clear(ObjList **objlist)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
219 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
220 while(*objlist!=NULL)
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
221 free_node(objlist, *objlist);
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
222 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
223
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
224
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
225 ObjListIterTmp objlist_iter_tmp=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
226
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
227
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
228 void objlist_iter_init(ObjListIterTmp *state, ObjList *objlist)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
229 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
230 *state=objlist;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
231 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
232
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
233
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
234 Obj *objlist_iter(ObjListIterTmp *state)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
235 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
236 Obj *obj=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
237
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
238 while(obj==NULL && *state!=NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
239 obj=(*state)->watch.obj;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
240 (*state)=(*state)->next;
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
241 }
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
242
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
243 return obj;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
244 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
245
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
246
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
247 void objlist_iter_rev_init(ObjListIterTmp *state, ObjList *objlist)
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
248 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
249 *state=(objlist==NULL ? NULL : objlist->prev);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
250 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
251
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
252
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
253 Obj *objlist_iter_rev(ObjListIterTmp *state)
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
254 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
255 Obj *obj=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
256
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
257 while(obj==NULL && *state!=NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
258 obj=(*state)->watch.obj;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
259 *state=(*state)->prev;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
260 if((*state)->next==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
261 *state=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
262 }
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
263
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
264 return obj;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
265 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
266
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
267
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
268 bool objlist_empty(ObjList *objlist)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
269 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
270 ObjListIterTmp tmp;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
271 Obj *obj;
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
272
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
273 FOR_ALL_ON_OBJLIST(Obj*, obj, objlist, tmp){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
274 return FALSE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
275 }
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
276
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
277 return TRUE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
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 }

mercurial