VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/list.c@ 16912

Last change on this file since 16912 was 16912, checked in by vboxsync, 16 years ago

crOpenGL: use custom CRASSERT instead of assert

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1#include "cr_list.h"
2#include "cr_error.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7struct CRListIterator {
8 void *element;
9 CRListIterator *prev;
10 CRListIterator *next;
11};
12
13struct CRList {
14 CRListIterator *head;
15 CRListIterator *tail;
16 unsigned size;
17};
18
19CRList *crAllocList( void )
20{
21 CRList *l = malloc( sizeof( CRList ) );
22 CRASSERT( l );
23
24 l->head = malloc( sizeof( CRListIterator ) );
25 CRASSERT( l->head );
26
27 l->tail = malloc( sizeof( CRListIterator ) );
28 CRASSERT( l->tail );
29
30 l->head->prev = NULL;
31 l->head->next = l->tail;
32
33 l->tail->prev = l->head;
34 l->tail->next = NULL;
35
36 l->size = 0;
37
38 return l;
39}
40
41void crFreeList( CRList *l )
42{
43 CRListIterator *t1;
44
45 CRASSERT( l != NULL );
46 t1 = l->head;
47 while ( t1 != NULL )
48 {
49 CRListIterator *t2 = t1;
50 t1 = t1->next;
51 t2->prev = NULL;
52 t2->next = NULL;
53 t2->element = NULL;
54 free( t2 );
55 }
56 l->size = 0;
57 free( l );
58}
59
60unsigned crListSize( const CRList *l )
61{
62 return l->size;
63}
64
65int crListIsEmpty( const CRList *l )
66{
67 CRASSERT( l != NULL );
68 return l->size == 0;
69}
70
71void crListInsert( CRList *l, CRListIterator *iter, void *elem )
72{
73 CRListIterator *p;
74
75 CRASSERT( l != NULL );
76 CRASSERT( iter != NULL );
77 CRASSERT( iter != l->head );
78
79 p = malloc( sizeof( CRListIterator ) );
80 CRASSERT( p != NULL );
81 p->prev = iter->prev;
82 p->next = iter;
83 p->prev->next = p;
84 iter->prev = p;
85
86 p->element = elem;
87 l->size++;
88}
89
90void crListErase( CRList *l, CRListIterator *iter )
91{
92 CRASSERT( l != NULL );
93 CRASSERT( iter != NULL );
94 CRASSERT( iter != l->head );
95 CRASSERT( iter != l->tail );
96 CRASSERT( l->size > 0 );
97
98 iter->next->prev = iter->prev;
99 iter->prev->next = iter->next;
100
101 iter->prev = NULL;
102 iter->next = NULL;
103 iter->element = NULL;
104 free( iter );
105
106 l->size--;
107}
108
109void crListClear( CRList *l )
110{
111 CRASSERT( l != NULL );
112 while ( !crListIsEmpty( l ) )
113 {
114 crListPopFront( l );
115 }
116}
117
118void crListPushBack( CRList *l, void *elem )
119{
120 CRASSERT( l != NULL );
121 crListInsert( l, l->tail, elem );
122}
123
124void crListPushFront( CRList *l, void *elem )
125{
126 CRASSERT( l != NULL );
127 crListInsert( l, l->head->next, elem );
128}
129
130void crListPopBack( CRList *l )
131{
132 CRASSERT( l != NULL );
133 CRASSERT( l->size > 0 );
134 crListErase( l, l->tail->prev );
135}
136
137void crListPopFront( CRList *l )
138{
139 CRASSERT( l != NULL );
140 CRASSERT( l->size > 0 );
141 crListErase( l, l->head->next );
142}
143
144void *crListFront( CRList *l )
145{
146 CRASSERT( l != NULL );
147 CRASSERT( l->size > 0 );
148 CRASSERT( l->head != NULL );
149 CRASSERT( l->head->next != NULL );
150 return l->head->next->element;
151}
152
153void *crListBack( CRList *l )
154{
155 CRASSERT( l != NULL );
156 CRASSERT( l->size > 0 );
157 CRASSERT( l->tail != NULL );
158 CRASSERT( l->tail->prev != NULL );
159 return l->tail->prev->element;
160}
161
162CRListIterator *crListBegin( CRList *l )
163{
164 CRASSERT( l != NULL );
165 CRASSERT( l->head != NULL );
166 CRASSERT( l->head->next != NULL );
167 return l->head->next;
168}
169
170CRListIterator *crListEnd( CRList *l )
171{
172 CRASSERT( l != NULL );
173 CRASSERT( l->tail != NULL );
174 return l->tail;
175}
176
177CRListIterator *crListNext( CRListIterator *iter )
178{
179 CRASSERT( iter != NULL );
180 CRASSERT( iter->next != NULL );
181 return iter->next;
182}
183
184CRListIterator *crListPrev( CRListIterator *iter )
185{
186 CRASSERT( iter != NULL );
187 CRASSERT( iter->prev != NULL );
188 return iter->prev;
189}
190
191void *crListElement( CRListIterator *iter )
192{
193 CRASSERT( iter != NULL );
194 return iter->element;
195}
196
197CRListIterator *crListFind( CRList *l, void *element, CRListCompareFunc compare )
198{
199 CRListIterator *iter;
200
201 CRASSERT( l != NULL );
202 CRASSERT( compare );
203
204 for ( iter = crListBegin( l ); iter != crListEnd( l ); iter = crListNext( iter ) )
205 {
206 if ( compare( element, iter->element ) == 0 )
207 {
208 return iter;
209 }
210 }
211 return NULL;
212}
213
214void crListApply( CRList *l, CRListApplyFunc apply, void *arg )
215{
216 CRListIterator *iter;
217
218 CRASSERT( l != NULL );
219 for ( iter = crListBegin( l ); iter != crListEnd( l ); iter = crListNext( iter ) )
220 {
221 apply( iter->element, arg );
222 }
223}
224
225#if CR_TESTING_LIST
226
227static void printElement( void *elem, void *arg )
228{
229 char *s = elem;
230 FILE *fp = arg;
231
232 CRASSERT( s != NULL );
233 CRASSERT( fp != NULL );
234 fprintf( fp, "%s ", s );
235}
236
237static void printList( CRList *l )
238{
239 CRASSERT( l != NULL );
240 crListApply( l, printElement, stderr );
241 fprintf( stderr, "\n" );
242}
243
244static int elementCompare( void *a, void *b )
245{
246 return strcmp( a, b );
247}
248
249int main( void )
250{
251 char *names[] = { "a", "b", "c", "d", "e", "f" };
252 CRList *l;
253 CRListIterator *iter;
254 int i, n;
255
256 n = sizeof( names ) / sizeof( names[0] );
257 fprintf( stderr, "n=%d\n", n );
258
259 l = crAllocList( );
260 for ( i = 0; i < n; ++i )
261 {
262 crListPushBack( l, names[i] );
263 }
264 printList( l );
265
266 crListPushFront( l, "x" );
267 printList( l );
268
269 crListPushBack( l, "y" );
270 printList( l );
271
272 crListPopFront( l );
273 printList( l );
274
275 crListPopBack( l );
276 printList( l );
277
278 iter = crListFind( l, "c", elementCompare );
279 CRASSERT( iter != NULL );
280 crListInsert( l, iter, "z" );
281 printList( l );
282
283 iter = crListFind( l, "d", elementCompare );
284 CRASSERT( iter != NULL );
285 crListErase( l, iter );
286 printList( l );
287
288 crListClear( l );
289 printList( l );
290 fprintf( stderr, "size: %d\n", crListSize( l ) );
291 fprintf( stderr, "is empty: %d\n", crListIsEmpty( l ) );
292
293 crListPushBack( l, "w" );
294 crListPushBack( l, "t" );
295 crListPushBack( l, "c" );
296 printList( l );
297
298 fprintf( stderr, "front: %s\n", ( char * ) crListFront( l ) );
299 fprintf( stderr, "back: %s\n", ( char * ) crListBack( l ) );
300 fprintf( stderr, "size: %d\n", crListSize( l ) );
301 fprintf( stderr, "is empty: %d\n", crListIsEmpty( l ) );
302
303 crFreeList( l );
304 return 0;
305}
306
307#endif // CR_TESTING_LIST
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette