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