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