Fixed an exception handling bug
[cacao.git] / toolbox / list.c
1 /************************** toolbox/list.c *************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Not documented, see chain.h.
8
9         Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
10
11         Last Change: 1996/10/03
12
13 *******************************************************************************/
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <assert.h>
18
19 #include "global.h"
20 #include "list.h"
21
22
23 void list_init (list *l, int nodeoffset)
24 {
25         l->first = NULL;
26         l->last = NULL;
27         l->nodeoffset = nodeoffset;
28 }
29
30 void list_addlast (list *l, void *element)
31 {
32         listnode *n = (listnode*) ( ((char*) element) + l->nodeoffset );
33
34         if (l->last) {
35                 n->prev = l->last;
36                 n->next = NULL;
37                 l->last->next = n;
38                 l->last = n;
39                 }
40         else {
41                 n->prev = NULL;
42                 n->next = NULL;
43                 l->last = n;
44                 l->first = n;
45                 }
46 }
47
48 void list_addfirst (list *l, void *element)
49 {
50         listnode *n = (listnode*) ( ((char*) element) + l->nodeoffset );
51
52         if (l->first) {
53                 n->prev = NULL;
54                 n->next = l->first;
55                 l->first->prev = n;
56                 l->first = 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_remove (list *l, void *element)
68 {
69         listnode *n = (listnode*) ( ((char*) element) + l->nodeoffset );
70         
71         if (n->next) n->next->prev = n->prev;
72                 else l->last = n->prev;
73         if (n->prev) n->prev->next = n->next;
74                 else l->first = n->next;
75 }
76
77  
78 void *list_first (list *l)
79 {
80         if (!l->first) return NULL;
81         return ((char*) l->first) - l->nodeoffset;
82 }
83
84
85 void *list_last (list *l)
86 {
87         if (!l->last) return NULL;
88         return ((char*) l->last) - l->nodeoffset;
89 }
90
91
92 void *list_next (list *l, void *element)
93 {
94         listnode *n;
95         n = (listnode*) ( ((char*) element) + l->nodeoffset );
96         if (!n->next) return NULL;
97         return ((char*) n->next) - l->nodeoffset;
98 }
99
100         
101 void *list_prev (list *l, void *element)
102 {
103         listnode *n;
104         n = (listnode*) ( ((char*) element) + l->nodeoffset );
105         if (!n->prev) return NULL;
106         return ((char*) n->prev) - l->nodeoffset;
107 }
108