VirtualBox

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

Last change on this file since 69498 was 63199, checked in by vboxsync, 8 years ago

GuestHost/OpenGL: warnings (gcc).

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