objlist.c

Mon, 07 May 2012 15:25:18 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 07 May 2012 15:25:18 +0200
changeset 115
f60f9a415d1b
parent 111
7d1dccdd7215
permissions
-rw-r--r--

-DCF_NO_GETTEXT option to system.mk

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
111
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
139 bool objlist_contains(ObjList *objlist, Obj *obj)
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
140 {
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
141 return (objlist_find_node(objlist, obj)!=NULL);
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
142 }
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
143
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
144
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
145 bool objlist_insert_last(ObjList **objlist, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
146 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
147 ObjList *node=reuse(objlist);
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 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
150 node=mknode(obj);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
151
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
152 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
153 return FALSE;
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 LINK_ITEM_LAST(*objlist, node, next, prev);
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
156
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
157 return TRUE;
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
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
160
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
161 bool objlist_insert_first(ObjList **objlist, Obj *obj)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
162 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
163 ObjList *node=reuse(objlist);
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 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
166 node=mknode(obj);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
167
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
168 if(node==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
169 return FALSE;
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 LINK_ITEM_FIRST(*objlist, node, next, prev);
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 return TRUE;
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
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 bool objlist_reinsert_last(ObjList **objlist, Obj *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 ObjList *node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
180
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
181 optimise(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
182
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
183 node=objlist_find_node(*objlist, obj);
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 if(node==NULL)
111
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
186 return objlist_insert_last(objlist, obj);
91
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 UNLINK_ITEM(*objlist, node, next, prev);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
189 LINK_ITEM_LAST(*objlist, node, next, prev);
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 return TRUE;
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
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 bool objlist_reinsert_first(ObjList **objlist, Obj *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 ObjList *node;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
198
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
199 optimise(objlist);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
200
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
201 node=objlist_find_node(*objlist, obj);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
202
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
203 if(node==NULL)
111
7d1dccdd7215 Some list code improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
204 return objlist_insert_first(objlist, obj);
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
205
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
206 UNLINK_ITEM(*objlist, node, next, prev);
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
207 LINK_ITEM_FIRST(*objlist, node, next, prev);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
208
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
209 return TRUE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
210 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
211
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
212
101
50525dab6c8e *list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents: 92
diff changeset
213 bool objlist_remove(ObjList **objlist, Obj *obj)
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
214 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
215 ObjList *node=objlist_find_node(*objlist, obj);
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
216
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
217 if(node!=NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
218 free_node(objlist, node);
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
219
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
220 optimise(objlist);
101
50525dab6c8e *list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents: 92
diff changeset
221
50525dab6c8e *list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents: 92
diff changeset
222 return (node!=NULL);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
223 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
224
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
225
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
226 void objlist_clear(ObjList **objlist)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
227 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
228 while(*objlist!=NULL)
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
229 free_node(objlist, *objlist);
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
230 }
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 ObjListIterTmp objlist_iter_tmp=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
234
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 void objlist_iter_init(ObjListIterTmp *state, ObjList *objlist)
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 *state=objlist;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
239 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
240
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
241
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
242 Obj *objlist_iter(ObjListIterTmp *state)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
243 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
244 Obj *obj=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
245
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
246 while(obj==NULL && *state!=NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
247 obj=(*state)->watch.obj;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
248 (*state)=(*state)->next;
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
249 }
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
250
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
251 return obj;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
252 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
253
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
254
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
255 void objlist_iter_rev_init(ObjListIterTmp *state, ObjList *objlist)
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
256 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
257 *state=(objlist==NULL ? NULL : objlist->prev);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
258 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
259
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
260
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
261 Obj *objlist_iter_rev(ObjListIterTmp *state)
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
262 {
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
263 Obj *obj=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
264
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
265 while(obj==NULL && *state!=NULL){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
266 obj=(*state)->watch.obj;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
267 *state=(*state)->prev;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
268 if((*state)->next==NULL)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
269 *state=NULL;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
270 }
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
271
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
272 return obj;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
273 }
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
274
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
275
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
276 bool objlist_empty(ObjList *objlist)
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
277 {
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
278 ObjListIterTmp tmp;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
279 Obj *obj;
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
280
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
281 FOR_ALL_ON_OBJLIST(Obj*, obj, objlist, tmp){
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
282 return FALSE;
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
283 }
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
284
91
817f90f58aec ObjList changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
285 return TRUE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
286 }
92
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
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
289 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
290 {
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
291 ObjList *node;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
292 Obj*obj;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
293
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
294 optimise(objlist);
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
295
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
296 node=*objlist;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
297
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
298 if(node==NULL)
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
299 return NULL;
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 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
302
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
303 assert(obj!=NULL);
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 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
306
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
307 return obj;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
308 }
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
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
311 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
312 {
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
313 ObjList *node;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
314 Obj*obj;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
315
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
316 optimise(objlist);
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
317
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
318 node=*objlist;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
319
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
320 if(node==NULL)
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
321 return NULL;
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 node=node->prev;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
324
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
325 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
326
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
327 assert(obj!=NULL);
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
328
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
329 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
330
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
331 return obj;
55fcdff5bcea Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
332 }

mercurial