* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / toolbox / list.c
1 /* src/toolbox/list.c - double linked list
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    $Id: list.c 4357 2006-01-22 23:33:38Z twisti $
30
31 */
32
33
34 #include <stdlib.h>
35
36 #include "config.h"
37 #include "toolbox/list.h"
38
39
40 void list_init(list *l, int nodeoffset)
41 {
42         l->first = NULL;
43         l->last = NULL;
44         l->nodeoffset = nodeoffset;
45 }
46
47
48 void list_addlast(list *l, void *element)
49 {
50         listnode *n = (listnode*) (((char*) element) + l->nodeoffset);
51
52         if (l->last) {
53                 n->prev = l->last;
54                 n->next = NULL;
55                 l->last->next = n;
56                 l->last = n;
57
58         } else {
59                 n->prev = NULL;
60                 n->next = NULL;
61                 l->last = n;
62                 l->first = n;
63         }
64 }
65
66
67 void list_addfirst(list *l, void *element)
68 {
69         listnode *n = (listnode*) (((char*) element) + l->nodeoffset);
70
71         if (l->first) {
72                 n->prev = NULL;
73                 n->next = l->first;
74                 l->first->prev = n;
75                 l->first = n;
76
77         } else {
78                 n->prev = NULL;
79                 n->next = NULL;
80                 l->last = n;
81                 l->first = n;
82         }
83 }
84
85
86 void list_remove(list *l, void *element)
87 {
88         listnode *n = (listnode*) (((char*) element) + l->nodeoffset);
89         
90         if (n->next) {
91                 n->next->prev = n->prev;
92
93         } else {
94                 l->last = n->prev;
95         }
96
97         if (n->prev) {
98                 n->prev->next = n->next;
99
100         } else {
101                 l->first = n->next;
102         }
103
104         n->next = NULL;
105         n->prev = NULL;
106 }
107
108  
109 void *list_first(list *l)
110 {
111         if (!l->first)
112                 return NULL;
113
114         return ((char*) l->first) - l->nodeoffset;
115 }
116
117
118 void *list_last(list *l)
119 {
120         if (!l->last)
121                 return NULL;
122
123         return ((char*) l->last) - l->nodeoffset;
124 }
125
126
127 void *list_next(list *l, void *element)
128 {
129         listnode *n;
130
131         n = (listnode*) (((char*) element) + l->nodeoffset);
132
133         if (!n->next)
134                 return NULL;
135
136         return ((char*) n->next) - l->nodeoffset;
137 }
138
139         
140 void *list_prev(list *l, void *element)
141 {
142         listnode *n;
143
144         n = (listnode*) (((char*) element) + l->nodeoffset);
145
146         if (!n->prev)
147                 return NULL;
148
149         return ((char*) n->prev) - l->nodeoffset;
150 }
151
152
153 /*
154  * These are local overrides for various environment variables in Emacs.
155  * Please do not remove this and leave it at the end of the file, where
156  * Emacs will automagically detect them.
157  * ---------------------------------------------------------------------
158  * Local variables:
159  * mode: c
160  * indent-tabs-mode: t
161  * c-basic-offset: 4
162  * tab-width: 4
163  * End:
164  */