* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / toolbox / list.h
1 /* src/toolbox/list.h - 
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28
29    Changes: Christian Thalinger
30
31    $Id: list.h 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #ifndef _LIST_H
37 #define _LIST_H
38
39 typedef struct listnode {           /* structure for list element */
40         struct listnode *next;
41         struct listnode *prev;
42 } listnode;
43
44
45 typedef struct list {               /* structure for list head */
46         listnode *first;
47         listnode *last;
48         int nodeoffset;
49 } list;
50
51
52 /* function prototypes */
53
54 void list_init(list *l, int nodeoffset);
55
56 void list_addlast(list *l, void *element);
57 void list_addfirst(list *l, void *element);
58
59 void list_remove(list *l, void *element);
60  
61 void *list_first(list *l);
62 void *list_last(list *l);
63
64 void *list_next(list *l, void *element);
65 void *list_prev(list *l, void *element);
66
67
68 /*
69 ---------------------- interface description -----------------------------
70
71 The list management with this module works like this:
72         
73         - to be used in a list, a structure must have an element of type
74           'listnode'.
75           
76         - there needs to be a structure of type 'list'.
77         
78         - the function list_init(l, nodeoffset) initializes the structure.
79           nodeoffset is the offset of the 'listnode' from the start of the
80           structure in bytes.
81           
82         - The remaining functions provide inserting, removing and searching.
83           
84 This small example aims to demonstrate correct usage:
85
86
87
88         void bsp() {
89                 struct node {
90                         listnode linkage;
91                         int value;
92                         } a,b,c, *el;
93                         
94                 list l;
95                 
96                 a.value = 7;
97                 b.value = 9;
98                 c.value = 11;
99                 
100                 list_init (&l, OFFSET(struct node,linkage) );
101                 list_addlast (&l, a);
102                 list_addlast (&l, b);
103                 list_addlast (&l, c);
104                 
105                 e = list_first (&l);
106                 while (e) {
107                         printf ("Element: %d\n", e->value);
108                         e = list_next (&l,e);
109                         }
110         }
111         
112         
113         The output from this program should be:
114                 7
115                 9
116                 11
117
118
119
120 The reason for the usage of 'nodeoffset' is that this way, the same node can
121 part of different lists (there must be one 'listnode' element for every
122 distinct list).
123
124 */
125
126 #endif /* _LIST_H */
127
128
129 /*
130  * These are local overrides for various environment variables in Emacs.
131  * Please do not remove this and leave it at the end of the file, where
132  * Emacs will automagically detect them.
133  * ---------------------------------------------------------------------
134  * Local variables:
135  * mode: c
136  * indent-tabs-mode: t
137  * c-basic-offset: 4
138  * tab-width: 4
139  * End:
140  */