GNU header update.
[cacao.git] / src / toolbox / list.h
1 /* toolbox/list.h - 
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    $Id: list.h 1735 2004-12-07 14:33:27Z twisti $
30
31 */
32
33
34 #ifndef _LIST_H
35 #define _LIST_H
36
37 typedef struct listnode {           /* structure for list element */
38         struct listnode *next;
39         struct listnode *prev;
40 } listnode;
41
42
43 typedef struct list {               /* structure for list head */
44         listnode *first;
45         listnode *last;
46         int nodeoffset;
47 } list;
48
49
50 /* function prototypes */
51
52 void list_init(list *l, int nodeoffset);
53
54 void list_addlast(list *l, void *element);
55 void list_addfirst(list *l, void *element);
56
57 void list_remove(list *l, void *element);
58  
59 void *list_first(list *l);
60 void *list_last(list *l);
61
62 void *list_next(list *l, void *element);
63 void *list_prev(list *l, void *element);
64
65
66 /*
67 ---------------------- interface description -----------------------------
68
69 The list management with this module works like this:
70         
71         - to be used in a list, a structure must have an element of type
72           'listnode'.
73           
74         - there needs to be a structure of type 'list'.
75         
76         - the function list_init(l, nodeoffset) initializes the structure.
77           nodeoffset is the offset of the 'listnode' from the start of the
78           structure in bytes.
79           
80         - The remaining functions provide inserting, removing and searching.
81           
82 This small example aims to demonstrate correct usage:
83
84
85
86         void bsp() {
87                 struct node {
88                         listnode linkage;
89                         int value;
90                         } a,b,c, *el;
91                         
92                 list l;
93                 
94                 a.value = 7;
95                 b.value = 9;
96                 c.value = 11;
97                 
98                 list_init (&l, OFFSET(struct node,linkage) );
99                 list_addlast (&l, a);
100                 list_addlast (&l, b);
101                 list_addlast (&l, c);
102                 
103                 e = list_first (&l);
104                 while (e) {
105                         printf ("Element: %d\n", e->value);
106                         e = list_next (&l,e);
107                         }
108         }
109         
110         
111         The output from this program should be:
112                 7
113                 9
114                 11
115
116
117
118 The reason for the usage of 'nodeoffset' is that this way, the same node can
119 part of different lists (there must be one 'listnode' element for every
120 distinct list).
121
122 */
123
124 #endif /* _LIST_H */
125
126
127 /*
128  * These are local overrides for various environment variables in Emacs.
129  * Please do not remove this and leave it at the end of the file, where
130  * Emacs will automagically detect them.
131  * ---------------------------------------------------------------------
132  * Local variables:
133  * mode: c
134  * indent-tabs-mode: t
135  * c-basic-offset: 4
136  * tab-width: 4
137  * End:
138  */