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