xref: /AOO41X/main/vcl/source/fontsubset/list.cxx (revision 9f62ea84a806e17e6f2bbff75724a7257a0eb5d9)
1*9f62ea84SAndrew Rist /**************************************************************
2c754d7fcSMichael Stahl  *
3*9f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9f62ea84SAndrew Rist  * distributed with this work for additional information
6*9f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
9*9f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10c754d7fcSMichael Stahl  *
11*9f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12c754d7fcSMichael Stahl  *
13*9f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9f62ea84SAndrew Rist  * software distributed under the License is distributed on an
15*9f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9f62ea84SAndrew Rist  * specific language governing permissions and limitations
18*9f62ea84SAndrew Rist  * under the License.
19c754d7fcSMichael Stahl  *
20*9f62ea84SAndrew Rist  *************************************************************/
21*9f62ea84SAndrew Rist 
22*9f62ea84SAndrew Rist 
23c754d7fcSMichael Stahl 
24c754d7fcSMichael Stahl /*[]---------------------------------------------------[]*/
25c754d7fcSMichael Stahl /*|                                                     |*/
26c754d7fcSMichael Stahl /*|     list.c - bidirectional list class               |*/
27c754d7fcSMichael Stahl /*|                                                     |*/
28c754d7fcSMichael Stahl /*|                                                     |*/
29c754d7fcSMichael Stahl /*|  Author: Alexander Gelfenbain                       |*/
30c754d7fcSMichael Stahl /*[]---------------------------------------------------[]*/
31c754d7fcSMichael Stahl 
32c754d7fcSMichael Stahl #include <rtl/alloc.h>
33c754d7fcSMichael Stahl 
34c754d7fcSMichael Stahl #if OSL_DEBUG_LEVEL == 0
35c754d7fcSMichael Stahl #  ifndef NDEBUG
36c754d7fcSMichael Stahl #    define NDEBUG
37c754d7fcSMichael Stahl #  endif
38c754d7fcSMichael Stahl #endif
39c754d7fcSMichael Stahl 
40c754d7fcSMichael Stahl #include <assert.h>
41c754d7fcSMichael Stahl 
42c754d7fcSMichael Stahl /* #define TEST */
43c754d7fcSMichael Stahl #include "list.h"
44c754d7fcSMichael Stahl 
45c754d7fcSMichael Stahl /*- private data types */
46c754d7fcSMichael Stahl typedef struct _lnode {
47c754d7fcSMichael Stahl     struct _lnode *next;
48c754d7fcSMichael Stahl     struct _lnode *prev;
49c754d7fcSMichael Stahl 
50c754d7fcSMichael Stahl     void *value;
51c754d7fcSMichael Stahl 
52c754d7fcSMichael Stahl } lnode;
53c754d7fcSMichael Stahl 
54c754d7fcSMichael Stahl struct _list {
55c754d7fcSMichael Stahl     lnode *head, *tail, *cptr;
56c754d7fcSMichael Stahl     size_t aCount;
57c754d7fcSMichael Stahl     list_destructor eDtor;
58c754d7fcSMichael Stahl };
59c754d7fcSMichael Stahl 
60c754d7fcSMichael Stahl /*- private methods */
61c754d7fcSMichael Stahl 
newNode(void * el)62c754d7fcSMichael Stahl static lnode *newNode(void *el)
63c754d7fcSMichael Stahl {
64c754d7fcSMichael Stahl     lnode *ptr = (lnode*)rtl_allocateMemory(sizeof(lnode));
65c754d7fcSMichael Stahl     assert(ptr != 0);
66c754d7fcSMichael Stahl 
67c754d7fcSMichael Stahl     ptr->value = el;
68c754d7fcSMichael Stahl 
69c754d7fcSMichael Stahl     return ptr;
70c754d7fcSMichael Stahl }
71c754d7fcSMichael Stahl 
appendPrim(list pThis,void * el)72c754d7fcSMichael Stahl static lnode *appendPrim(list pThis, void *el)
73c754d7fcSMichael Stahl {
74c754d7fcSMichael Stahl     lnode *ptr = newNode(el);
75c754d7fcSMichael Stahl     lnode **flink, *blink;
76c754d7fcSMichael Stahl 
77c754d7fcSMichael Stahl     if (pThis->tail != 0) {
78c754d7fcSMichael Stahl         flink = &(pThis->tail->next);
79c754d7fcSMichael Stahl         blink = pThis->tail;
80c754d7fcSMichael Stahl     } else {
81c754d7fcSMichael Stahl         flink = &pThis->head;
82c754d7fcSMichael Stahl         blink = 0;
83c754d7fcSMichael Stahl         pThis->cptr = ptr;                         /*- list was empty - set current to pThis element */
84c754d7fcSMichael Stahl     }
85c754d7fcSMichael Stahl 
86c754d7fcSMichael Stahl     *flink  = ptr;
87c754d7fcSMichael Stahl     pThis->tail = ptr;
88c754d7fcSMichael Stahl 
89c754d7fcSMichael Stahl     ptr->prev = blink;
90c754d7fcSMichael Stahl     ptr->next = 0;
91c754d7fcSMichael Stahl 
92c754d7fcSMichael Stahl     pThis->aCount++;
93c754d7fcSMichael Stahl     return ptr;
94c754d7fcSMichael Stahl }
95c754d7fcSMichael Stahl #ifdef TEST
prependPrim(list pThis,void * el)96c754d7fcSMichael Stahl static lnode *prependPrim(list pThis, void *el)
97c754d7fcSMichael Stahl {
98c754d7fcSMichael Stahl     lnode *ptr = newNode(el);
99c754d7fcSMichael Stahl     lnode *flink, **blink;
100c754d7fcSMichael Stahl 
101c754d7fcSMichael Stahl     if (pThis->head != 0) {
102c754d7fcSMichael Stahl         blink = &(pThis->head->prev);
103c754d7fcSMichael Stahl         flink = pThis->head;
104c754d7fcSMichael Stahl     } else {
105c754d7fcSMichael Stahl         blink = &pThis->tail;
106c754d7fcSMichael Stahl         flink = 0;
107c754d7fcSMichael Stahl         pThis->cptr  = ptr;                        /*- list was empty - set current to pThis element */
108c754d7fcSMichael Stahl     }
109c754d7fcSMichael Stahl 
110c754d7fcSMichael Stahl     *blink = ptr;
111c754d7fcSMichael Stahl     pThis->head   = ptr;
112c754d7fcSMichael Stahl 
113c754d7fcSMichael Stahl     ptr->next = flink;
114c754d7fcSMichael Stahl     ptr->prev = 0;
115c754d7fcSMichael Stahl 
116c754d7fcSMichael Stahl     pThis->aCount++;
117c754d7fcSMichael Stahl     return ptr;
118c754d7fcSMichael Stahl }
119c754d7fcSMichael Stahl #endif
120c754d7fcSMichael Stahl 
121c754d7fcSMichael Stahl /*- public methods  */
listNewEmpty(void)122c754d7fcSMichael Stahl list listNewEmpty(void)                           /*- default ctor */
123c754d7fcSMichael Stahl {
124c754d7fcSMichael Stahl     list pThis = (list)rtl_allocateMemory(sizeof(struct _list));
125c754d7fcSMichael Stahl     assert(pThis != 0);
126c754d7fcSMichael Stahl 
127c754d7fcSMichael Stahl     pThis->aCount = 0;
128c754d7fcSMichael Stahl     pThis->eDtor = 0;
129c754d7fcSMichael Stahl     pThis->head = pThis->tail = pThis->cptr = 0;
130c754d7fcSMichael Stahl 
131c754d7fcSMichael Stahl     return pThis;
132c754d7fcSMichael Stahl }
133c754d7fcSMichael Stahl 
134c754d7fcSMichael Stahl #ifdef TEST
listNewCopy(list l)135c754d7fcSMichael Stahl list listNewCopy(list l)                          /*- copy ctor */
136c754d7fcSMichael Stahl {
137c754d7fcSMichael Stahl     lnode *ptr, *c;
138c754d7fcSMichael Stahl     list pThis;
139c754d7fcSMichael Stahl     assert(l != 0);
140c754d7fcSMichael Stahl 
141c754d7fcSMichael Stahl     pThis = rtl_allocateMemory(sizeof(struct _list));
142c754d7fcSMichael Stahl     assert(pThis != 0);
143c754d7fcSMichael Stahl 
144c754d7fcSMichael Stahl     ptr = l->head;
145c754d7fcSMichael Stahl 
146c754d7fcSMichael Stahl     pThis->aCount = 0;
147c754d7fcSMichael Stahl     pThis->eDtor = 0;
148c754d7fcSMichael Stahl     pThis->head = pThis->tail = pThis->cptr = 0;
149c754d7fcSMichael Stahl 
150c754d7fcSMichael Stahl     while (ptr) {
151c754d7fcSMichael Stahl         c = appendPrim(pThis, ptr->value);
152c754d7fcSMichael Stahl         if (ptr == l->cptr) pThis->cptr = c;
153c754d7fcSMichael Stahl         ptr = ptr->next;
154c754d7fcSMichael Stahl     }
155c754d7fcSMichael Stahl 
156c754d7fcSMichael Stahl     return pThis;
157c754d7fcSMichael Stahl }
158c754d7fcSMichael Stahl #endif
159c754d7fcSMichael Stahl 
listDispose(list pThis)160c754d7fcSMichael Stahl void listDispose(list pThis)                       /*- dtor */
161c754d7fcSMichael Stahl {
162c754d7fcSMichael Stahl     assert(pThis != 0);
163c754d7fcSMichael Stahl     listClear(pThis);
164c754d7fcSMichael Stahl     rtl_freeMemory(pThis);
165c754d7fcSMichael Stahl }
166c754d7fcSMichael Stahl 
listSetElementDtor(list pThis,list_destructor f)167c754d7fcSMichael Stahl void listSetElementDtor(list pThis, list_destructor f)
168c754d7fcSMichael Stahl {
169c754d7fcSMichael Stahl     assert(pThis != 0);
170c754d7fcSMichael Stahl     pThis->eDtor = f;
171c754d7fcSMichael Stahl }
172c754d7fcSMichael Stahl 
173c754d7fcSMichael Stahl /* calling this function on an empty list is a run-time error */
listCurrent(list pThis)174c754d7fcSMichael Stahl void *listCurrent(list pThis)
175c754d7fcSMichael Stahl {
176c754d7fcSMichael Stahl     assert(pThis != 0);
177c754d7fcSMichael Stahl     assert(pThis->cptr != 0);
178c754d7fcSMichael Stahl     return pThis->cptr->value;
179c754d7fcSMichael Stahl }
180c754d7fcSMichael Stahl 
listCount(list pThis)181c754d7fcSMichael Stahl int   listCount(list pThis)
182c754d7fcSMichael Stahl {
183c754d7fcSMichael Stahl     assert(pThis != 0);
184c754d7fcSMichael Stahl     return pThis->aCount;
185c754d7fcSMichael Stahl }
186c754d7fcSMichael Stahl 
listIsEmpty(list pThis)187c754d7fcSMichael Stahl int   listIsEmpty(list pThis)
188c754d7fcSMichael Stahl {
189c754d7fcSMichael Stahl     assert(pThis != 0);
190c754d7fcSMichael Stahl     return pThis->aCount == 0;
191c754d7fcSMichael Stahl }
192c754d7fcSMichael Stahl 
193c754d7fcSMichael Stahl 
194c754d7fcSMichael Stahl #ifdef TEST
listAtFirst(list pThis)195c754d7fcSMichael Stahl int   listAtFirst(list pThis)
196c754d7fcSMichael Stahl {
197c754d7fcSMichael Stahl     assert(pThis != 0);
198c754d7fcSMichael Stahl     return pThis->cptr == pThis->head;
199c754d7fcSMichael Stahl }
200c754d7fcSMichael Stahl 
listAtLast(list pThis)201c754d7fcSMichael Stahl int   listAtLast(list pThis)
202c754d7fcSMichael Stahl {
203c754d7fcSMichael Stahl     assert(pThis != 0);
204c754d7fcSMichael Stahl     return pThis->cptr == pThis->tail;
205c754d7fcSMichael Stahl }
206c754d7fcSMichael Stahl 
listPosition(list pThis)207c754d7fcSMichael Stahl int   listPosition(list pThis)
208c754d7fcSMichael Stahl {
209c754d7fcSMichael Stahl     int res = 0;
210c754d7fcSMichael Stahl     lnode *ptr;
211c754d7fcSMichael Stahl     assert(pThis != 0);
212c754d7fcSMichael Stahl 
213c754d7fcSMichael Stahl     ptr = pThis->head;
214c754d7fcSMichael Stahl 
215c754d7fcSMichael Stahl     while (ptr != pThis->cptr) {
216c754d7fcSMichael Stahl         ptr = ptr->next;
217c754d7fcSMichael Stahl         res++;
218c754d7fcSMichael Stahl     }
219c754d7fcSMichael Stahl 
220c754d7fcSMichael Stahl     return res;
221c754d7fcSMichael Stahl }
222c754d7fcSMichael Stahl #endif
listFind(list pThis,void * el)223c754d7fcSMichael Stahl int    listFind(list pThis, void *el)
224c754d7fcSMichael Stahl {
225c754d7fcSMichael Stahl     lnode *ptr;
226c754d7fcSMichael Stahl     assert(pThis != 0);
227c754d7fcSMichael Stahl 
228c754d7fcSMichael Stahl     ptr = pThis->head;
229c754d7fcSMichael Stahl 
230c754d7fcSMichael Stahl     while (ptr) {
231c754d7fcSMichael Stahl         if (ptr->value == el) {
232c754d7fcSMichael Stahl             pThis->cptr = ptr;
233c754d7fcSMichael Stahl             return 1;
234c754d7fcSMichael Stahl         }
235c754d7fcSMichael Stahl         ptr = ptr->next;
236c754d7fcSMichael Stahl     }
237c754d7fcSMichael Stahl 
238c754d7fcSMichael Stahl     return 0;
239c754d7fcSMichael Stahl }
240c754d7fcSMichael Stahl 
listNext(list pThis)241c754d7fcSMichael Stahl int    listNext(list pThis)
242c754d7fcSMichael Stahl {
243c754d7fcSMichael Stahl     return listSkipForward(pThis, 1);
244c754d7fcSMichael Stahl }
245c754d7fcSMichael Stahl 
listSkipForward(list pThis,int n)246c754d7fcSMichael Stahl int    listSkipForward(list pThis, int n)
247c754d7fcSMichael Stahl {
248c754d7fcSMichael Stahl     int m = 0;
249c754d7fcSMichael Stahl     assert(pThis != 0);
250c754d7fcSMichael Stahl 
251c754d7fcSMichael Stahl     if (pThis->cptr == 0) return 0;
252c754d7fcSMichael Stahl 
253c754d7fcSMichael Stahl     while (n != 0) {
254c754d7fcSMichael Stahl         if (pThis->cptr->next == 0) break;
255c754d7fcSMichael Stahl         pThis->cptr = pThis->cptr->next;
256c754d7fcSMichael Stahl         n--;
257c754d7fcSMichael Stahl         m++;
258c754d7fcSMichael Stahl     }
259c754d7fcSMichael Stahl     return m;
260c754d7fcSMichael Stahl }
261c754d7fcSMichael Stahl 
listToFirst(list pThis)262c754d7fcSMichael Stahl int    listToFirst(list pThis)
263c754d7fcSMichael Stahl {
264c754d7fcSMichael Stahl     assert(pThis != 0);
265c754d7fcSMichael Stahl 
266c754d7fcSMichael Stahl     if (pThis->cptr != pThis->head) {
267c754d7fcSMichael Stahl         pThis->cptr = pThis->head;
268c754d7fcSMichael Stahl         return 1;
269c754d7fcSMichael Stahl     }
270c754d7fcSMichael Stahl     return 0;
271c754d7fcSMichael Stahl }
272c754d7fcSMichael Stahl 
listToLast(list pThis)273c754d7fcSMichael Stahl int    listToLast(list pThis)
274c754d7fcSMichael Stahl {
275c754d7fcSMichael Stahl     assert(pThis != 0);
276c754d7fcSMichael Stahl 
277c754d7fcSMichael Stahl     if (pThis->cptr != pThis->tail) {
278c754d7fcSMichael Stahl         pThis->cptr = pThis->tail;
279c754d7fcSMichael Stahl         return 1;
280c754d7fcSMichael Stahl     }
281c754d7fcSMichael Stahl     return 0;
282c754d7fcSMichael Stahl }
283c754d7fcSMichael Stahl 
listPositionAt(list pThis,int n)284c754d7fcSMichael Stahl int    listPositionAt(list pThis, int n)                     /*- returns the actual position number */
285c754d7fcSMichael Stahl {
286c754d7fcSMichael Stahl     int m = 0;
287c754d7fcSMichael Stahl     assert(pThis != 0);
288c754d7fcSMichael Stahl 
289c754d7fcSMichael Stahl     pThis->cptr = pThis->head;
290c754d7fcSMichael Stahl     while (n != 0) {
291c754d7fcSMichael Stahl         if (pThis->cptr->next == 0) break;
292c754d7fcSMichael Stahl         pThis->cptr = pThis->cptr->next;
293c754d7fcSMichael Stahl         n--;
294c754d7fcSMichael Stahl         m++;
295c754d7fcSMichael Stahl     }
296c754d7fcSMichael Stahl     return m;
297c754d7fcSMichael Stahl }
298c754d7fcSMichael Stahl 
listAppend(list pThis,void * el)299c754d7fcSMichael Stahl list   listAppend(list pThis, void *el)
300c754d7fcSMichael Stahl {
301c754d7fcSMichael Stahl     assert(pThis != 0);
302c754d7fcSMichael Stahl 
303c754d7fcSMichael Stahl     appendPrim(pThis, el);
304c754d7fcSMichael Stahl     return pThis;
305c754d7fcSMichael Stahl }
306c754d7fcSMichael Stahl #ifdef TEST
listPrepend(list pThis,void * el)307c754d7fcSMichael Stahl list   listPrepend(list pThis, void *el)
308c754d7fcSMichael Stahl {
309c754d7fcSMichael Stahl     assert(pThis != 0);
310c754d7fcSMichael Stahl 
311c754d7fcSMichael Stahl     prependPrim(pThis, el);
312c754d7fcSMichael Stahl     return pThis;
313c754d7fcSMichael Stahl }
314c754d7fcSMichael Stahl 
listInsertAfter(list pThis,void * el)315c754d7fcSMichael Stahl list   listInsertAfter(list pThis, void *el)
316c754d7fcSMichael Stahl {
317c754d7fcSMichael Stahl     lnode *ptr;
318c754d7fcSMichael Stahl     assert(pThis != 0);
319c754d7fcSMichael Stahl 
320c754d7fcSMichael Stahl     if (pThis->cptr == 0) return listAppend(pThis, el);
321c754d7fcSMichael Stahl 
322c754d7fcSMichael Stahl     ptr = newNode(el);
323c754d7fcSMichael Stahl 
324c754d7fcSMichael Stahl     ptr->prev  = pThis->cptr;
325c754d7fcSMichael Stahl     ptr->next  = pThis->cptr->next;
326c754d7fcSMichael Stahl     pThis->cptr->next = ptr;
327c754d7fcSMichael Stahl 
328c754d7fcSMichael Stahl     if (ptr->next != 0) {
329c754d7fcSMichael Stahl         ptr->next->prev = ptr;
330c754d7fcSMichael Stahl     } else {
331c754d7fcSMichael Stahl         pThis->tail = ptr;
332c754d7fcSMichael Stahl     }
333c754d7fcSMichael Stahl     pThis->aCount++;
334c754d7fcSMichael Stahl     return pThis;
335c754d7fcSMichael Stahl }
336c754d7fcSMichael Stahl 
listInsertBefore(list pThis,void * el)337c754d7fcSMichael Stahl list   listInsertBefore(list pThis, void *el)
338c754d7fcSMichael Stahl {
339c754d7fcSMichael Stahl     lnode *ptr;
340c754d7fcSMichael Stahl     assert(pThis != 0);
341c754d7fcSMichael Stahl 
342c754d7fcSMichael Stahl     if (pThis->cptr == 0) return listAppend(pThis, el);
343c754d7fcSMichael Stahl 
344c754d7fcSMichael Stahl     ptr = newNode(el);
345c754d7fcSMichael Stahl 
346c754d7fcSMichael Stahl     ptr->prev  = pThis->cptr->prev;
347c754d7fcSMichael Stahl     ptr->next  = pThis->cptr;
348c754d7fcSMichael Stahl     pThis->cptr->prev = ptr;
349c754d7fcSMichael Stahl 
350c754d7fcSMichael Stahl     if (ptr->prev != 0) {
351c754d7fcSMichael Stahl         ptr->prev->next = ptr;
352c754d7fcSMichael Stahl     } else {
353c754d7fcSMichael Stahl         pThis->head = ptr;
354c754d7fcSMichael Stahl     }
355c754d7fcSMichael Stahl     pThis->aCount++;
356c754d7fcSMichael Stahl     return pThis;
357c754d7fcSMichael Stahl }
358c754d7fcSMichael Stahl #endif
listRemove(list pThis)359c754d7fcSMichael Stahl list   listRemove(list pThis)
360c754d7fcSMichael Stahl {
361c754d7fcSMichael Stahl     lnode *ptr = 0;
362c754d7fcSMichael Stahl     if (pThis->cptr == 0) return pThis;
363c754d7fcSMichael Stahl 
364c754d7fcSMichael Stahl     if (pThis->cptr->next != 0) {
365c754d7fcSMichael Stahl         ptr  = pThis->cptr->next;
366c754d7fcSMichael Stahl         pThis->cptr->next->prev = pThis->cptr->prev;
367c754d7fcSMichael Stahl     } else {
368c754d7fcSMichael Stahl         pThis->tail = pThis->cptr->prev;
369c754d7fcSMichael Stahl     }
370c754d7fcSMichael Stahl 
371c754d7fcSMichael Stahl     if (pThis->cptr->prev != 0) {
372c754d7fcSMichael Stahl         if (ptr == 0) ptr = pThis->cptr->prev;
373c754d7fcSMichael Stahl         pThis->cptr->prev->next = pThis->cptr->next;
374c754d7fcSMichael Stahl     } else {
375c754d7fcSMichael Stahl         pThis->head = pThis->cptr->next;
376c754d7fcSMichael Stahl     }
377c754d7fcSMichael Stahl 
378c754d7fcSMichael Stahl     if (pThis->eDtor) pThis->eDtor(pThis->cptr->value);        /* call the dtor callback */
379c754d7fcSMichael Stahl 
380c754d7fcSMichael Stahl     rtl_freeMemory(pThis->cptr);
381c754d7fcSMichael Stahl     pThis->aCount--;
382c754d7fcSMichael Stahl     pThis->cptr = ptr;
383c754d7fcSMichael Stahl     return pThis;
384c754d7fcSMichael Stahl }
385c754d7fcSMichael Stahl 
listClear(list pThis)386c754d7fcSMichael Stahl list   listClear(list pThis)
387c754d7fcSMichael Stahl {
388c754d7fcSMichael Stahl     lnode *node = pThis->head, *ptr;
389c754d7fcSMichael Stahl 
390c754d7fcSMichael Stahl     while (node) {
391c754d7fcSMichael Stahl         ptr = node->next;
392c754d7fcSMichael Stahl         if (pThis->eDtor) pThis->eDtor(node->value);           /* call the dtor callback */
393c754d7fcSMichael Stahl         rtl_freeMemory(node);
394c754d7fcSMichael Stahl         pThis->aCount--;
395c754d7fcSMichael Stahl         node = ptr;
396c754d7fcSMichael Stahl     }
397c754d7fcSMichael Stahl 
398c754d7fcSMichael Stahl     pThis->head = pThis->tail = pThis->cptr = 0;
399c754d7fcSMichael Stahl     assert(pThis->aCount == 0);
400c754d7fcSMichael Stahl     return pThis;
401c754d7fcSMichael Stahl }
402c754d7fcSMichael Stahl 
403c754d7fcSMichael Stahl #ifdef TEST
404c754d7fcSMichael Stahl 
listForAll(list pThis,void (* f)(void *))405c754d7fcSMichael Stahl void   listForAll(list pThis, void (*f)(void *))
406c754d7fcSMichael Stahl {
407c754d7fcSMichael Stahl     lnode *ptr = pThis->head;
408c754d7fcSMichael Stahl     while (ptr) {
409c754d7fcSMichael Stahl         f(ptr->value);
410c754d7fcSMichael Stahl         ptr = ptr->next;
411c754d7fcSMichael Stahl     }
412c754d7fcSMichael Stahl }
413c754d7fcSMichael Stahl 
414c754d7fcSMichael Stahl 
415c754d7fcSMichael Stahl #include <stdio.h>
416c754d7fcSMichael Stahl 
printlist(list l)417c754d7fcSMichael Stahl void printlist(list l)
418c754d7fcSMichael Stahl {
419c754d7fcSMichael Stahl     int saved;
420c754d7fcSMichael Stahl     assert(l != 0);
421c754d7fcSMichael Stahl     saved = listPosition(l);
422c754d7fcSMichael Stahl 
423c754d7fcSMichael Stahl     printf("[ ");
424c754d7fcSMichael Stahl 
425c754d7fcSMichael Stahl     if (!listIsEmpty(l)) {
426c754d7fcSMichael Stahl         listToFirst(l);
427c754d7fcSMichael Stahl         do {
428c754d7fcSMichael Stahl             printf("%d ", (int) listCurrent(l));
429c754d7fcSMichael Stahl         } while (listNext(l));
430c754d7fcSMichael Stahl     }
431c754d7fcSMichael Stahl 
432c754d7fcSMichael Stahl     printf("]\n");
433c754d7fcSMichael Stahl 
434c754d7fcSMichael Stahl     listPositionAt(l, saved);
435c754d7fcSMichael Stahl }
436c754d7fcSMichael Stahl 
printstringlist(list l)437c754d7fcSMichael Stahl void printstringlist(list l)
438c754d7fcSMichael Stahl {
439c754d7fcSMichael Stahl     int saved;
440c754d7fcSMichael Stahl     assert(l != 0);
441c754d7fcSMichael Stahl     saved = listPosition(l);
442c754d7fcSMichael Stahl 
443c754d7fcSMichael Stahl     printf("[ ");
444c754d7fcSMichael Stahl 
445c754d7fcSMichael Stahl     if (!listIsEmpty(l)) {
446c754d7fcSMichael Stahl         listToFirst(l);
447c754d7fcSMichael Stahl         do {
448c754d7fcSMichael Stahl             printf("'%s' ", (char *) listCurrent(l));
449c754d7fcSMichael Stahl         } while (listNext(l));
450c754d7fcSMichael Stahl     }
451c754d7fcSMichael Stahl 
452c754d7fcSMichael Stahl     printf("]\n");
453c754d7fcSMichael Stahl 
454c754d7fcSMichael Stahl     listPositionAt(l, saved);
455c754d7fcSMichael Stahl }
456c754d7fcSMichael Stahl 
printstat(list l)457c754d7fcSMichael Stahl void printstat(list l)
458c754d7fcSMichael Stahl {
459c754d7fcSMichael Stahl     printf("count: %d, position: %d, isEmpty: %d, atFirst: %d, atLast: %d.\n",
460c754d7fcSMichael Stahl            listCount(l), listPosition(l), listIsEmpty(l), listAtFirst(l), listAtLast(l));
461c754d7fcSMichael Stahl }
462c754d7fcSMichael Stahl 
allfunc(void * e)463c754d7fcSMichael Stahl void allfunc(void *e)
464c754d7fcSMichael Stahl {
465c754d7fcSMichael Stahl     printf("%d ", e);
466c754d7fcSMichael Stahl }
467c754d7fcSMichael Stahl 
edtor(void * ptr)468c754d7fcSMichael Stahl void edtor(void *ptr)
469c754d7fcSMichael Stahl {
470c754d7fcSMichael Stahl     printf("element dtor: 0x%08x\n", ptr);
471c754d7fcSMichael Stahl     rtl_freeMemory(ptr);
472c754d7fcSMichael Stahl }
473c754d7fcSMichael Stahl 
main()474c754d7fcSMichael Stahl int main()
475c754d7fcSMichael Stahl {
476c754d7fcSMichael Stahl     list l1, l2;
477c754d7fcSMichael Stahl     char *ptr;
478c754d7fcSMichael Stahl     int i;
479c754d7fcSMichael Stahl 
480c754d7fcSMichael Stahl     l1 = listNewEmpty();
481c754d7fcSMichael Stahl     printstat(l1);
482c754d7fcSMichael Stahl 
483c754d7fcSMichael Stahl     listAppend(l1, 1);
484c754d7fcSMichael Stahl     printstat(l1);
485c754d7fcSMichael Stahl 
486c754d7fcSMichael Stahl     listAppend(l1, 2);
487c754d7fcSMichael Stahl     printstat(l1);
488c754d7fcSMichael Stahl 
489c754d7fcSMichael Stahl     listAppend(l1, 3);
490c754d7fcSMichael Stahl     printstat(l1);
491c754d7fcSMichael Stahl 
492c754d7fcSMichael Stahl     printlist(l1);
493c754d7fcSMichael Stahl 
494c754d7fcSMichael Stahl     listToFirst(l1);
495c754d7fcSMichael Stahl     listInsertBefore(l1, -5);
496c754d7fcSMichael Stahl     printlist(l1);
497c754d7fcSMichael Stahl 
498c754d7fcSMichael Stahl     l2 = listNewCopy(l1);
499c754d7fcSMichael Stahl     printlist(l2);
500c754d7fcSMichael Stahl 
501c754d7fcSMichael Stahl     listForAll(l2, allfunc);
502c754d7fcSMichael Stahl     printf("\n");
503c754d7fcSMichael Stahl 
504c754d7fcSMichael Stahl     listClear(l1);
505c754d7fcSMichael Stahl     listSetElementDtor(l1, edtor);
506c754d7fcSMichael Stahl 
507c754d7fcSMichael Stahl     for(i=0; i<10; i++) {
508c754d7fcSMichael Stahl         ptr = rtl_allocateMemory(20);
509c754d7fcSMichael Stahl         snprintf(ptr, 20, "element # %d", i);
510c754d7fcSMichael Stahl         listAppend(l1, ptr);
511c754d7fcSMichael Stahl     }
512c754d7fcSMichael Stahl 
513c754d7fcSMichael Stahl     printstringlist(l1);
514c754d7fcSMichael Stahl 
515c754d7fcSMichael Stahl 
516c754d7fcSMichael Stahl     listDispose(l1);
517c754d7fcSMichael Stahl     listDispose(l2);
518c754d7fcSMichael Stahl 
519c754d7fcSMichael Stahl 
520c754d7fcSMichael Stahl     return 0;
521c754d7fcSMichael Stahl }
522c754d7fcSMichael Stahl #endif
523c754d7fcSMichael Stahl 
524c754d7fcSMichael Stahl 
525