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