hash lock
[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 684 2003-12-02 16:50:17Z twisti $
31
32 */
33
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <assert.h>
38
39 #include "global.h"
40 #include "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
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  */