* Removed all Id tags.
[cacao.git] / src / toolbox / list.h
1 /* src/toolbox/list.h - synchronized linked list
2
3    Copyright (C) 1996-2005, 2006, 2007 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 */
26
27
28 #ifndef _LIST_H
29 #define _LIST_H
30
31 #include "config.h"
32 #include "vm/types.h"
33
34 #include "vm/global.h"
35
36
37 /* ---------------------- interface description -----------------------------
38
39 The list management with this module works like this:
40         
41         - to be used in a list, a structure must have an element of type
42           'listnode'.
43           
44         - there needs to be a structure of type 'list'.
45         
46         - the function list_init(l, nodeoffset) initializes the structure.
47           nodeoffset is the offset of the 'listnode' from the start of the
48           structure in bytes.
49           
50         - The remaining functions provide inserting, removing and searching.
51           
52 This small example aims to demonstrate correct usage:
53
54
55
56         void bsp() {
57                 struct node {
58                         listnode linkage;
59                         int value;
60                         } a,b,c, *el;
61                         
62                 list l;
63                 
64                 a.value = 7;
65                 b.value = 9;
66                 c.value = 11;
67                 
68                 list_init (&l, OFFSET(struct node,linkage) );
69                 list_addlast (&l, a);
70                 list_addlast (&l, b);
71                 list_addlast (&l, c);
72                 
73                 e = list_first (&l);
74                 while (e) {
75                         printf ("Element: %d\n", e->value);
76                         e = list_next (&l,e);
77                         }
78         }
79         
80         
81         The output from this program should be:
82                 7
83                 9
84                 11
85
86
87
88 The reason for the usage of 'nodeoffset' is that this way, the same node can
89 part of different lists (there must be one 'listnode' element for every
90 distinct list).
91
92 */
93
94 /* listnode_t *****************************************************************/
95
96 typedef struct listnode_t listnode_t;
97
98 struct listnode_t {
99         listnode_t *next;
100         listnode_t *prev;
101 };
102
103
104 /* list_t *********************************************************************/
105
106 typedef struct list_t list_t;
107
108 struct list_t {
109 #if defined(ENABLE_THREADS)
110         java_object_t      lock;            /* threads lock object                */
111 #endif
112         listnode_t        *first;
113         listnode_t        *last;
114         s4                 nodeoffset;
115         s4                 size;            /* number of elements in the list     */
116 };
117
118
119 /* function prototypes ********************************************************/
120
121 list_t *list_create(s4 nodeoffset);
122 list_t *list_create_dump(s4 nodeoffset);
123
124 void    list_add_first(list_t *l, void *element);
125 void    list_add_first_unsynced(list_t *l, void *element);
126
127 void    list_add_last(list_t *l, void *element);
128 void    list_add_last_unsynced(list_t *l, void *element);
129
130 void    list_add_before(list_t *l, void *element, void *newelement);
131
132 void    list_remove(list_t *l, void *element);
133 void    list_remove_unsynced(list_t *l, void *element);
134
135 void   *list_first(list_t *l);
136 void   *list_first_unsynced(list_t *l);
137
138 void   *list_last(list_t *l);
139 void   *list_last_unsynced(list_t *l);
140
141 void   *list_next(list_t *l, void *element);
142 void   *list_next_unsynced(list_t *l, void *element);
143
144 void   *list_prev(list_t *l, void *element);
145 void   *list_prev_unsynced(list_t *l, void *element);
146
147 #endif /* _LIST_H */
148
149
150 /*
151  * These are local overrides for various environment variables in Emacs.
152  * Please do not remove this and leave it at the end of the file, where
153  * Emacs will automagically detect them.
154  * ---------------------------------------------------------------------
155  * Local variables:
156  * mode: c
157  * indent-tabs-mode: t
158  * c-basic-offset: 4
159  * tab-width: 4
160  * End:
161  */