ce57bc6a004045e85d75e3f3ee95a3369d9b2e0d
[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 1621 2004-11-30 13:06:55Z twisti $
31
32 */
33
34
35 #include <stdlib.h>
36
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  */