Initial revision
[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 "list.h"
20
21
22 void list_init (list *l, int nodeoffset)
23 {
24         l->first = NULL;
25         l->last = NULL;
26         l->nodeoffset = nodeoffset;
27 }
28
29 void list_addlast (list *l, void *element)
30 {
31         listnode *n = (listnode*) ( ((char*) element) + l->nodeoffset );
32
33         if (l->last) {
34                 n->prev = l->last;
35                 n->next = NULL;
36                 l->last->next = n;
37                 l->last = n;
38                 }
39         else {
40                 n->prev = NULL;
41                 n->next = NULL;
42                 l->last = n;
43                 l->first = n;
44                 }
45 }
46
47 void list_addfirst (list *l, void *element)
48 {
49         listnode *n = (listnode*) ( ((char*) element) + l->nodeoffset );
50
51         if (l->first) {
52                 n->prev = NULL;
53                 n->next = l->first;
54                 l->first->prev = n;
55                 l->first = n;
56                 }
57         else {
58                 n->prev = NULL;
59                 n->next = NULL;
60                 l->last = n;
61                 l->first = n;
62                 }
63 }
64
65
66 void list_remove (list *l, void *element)
67 {
68         listnode *n = (listnode*) ( ((char*) element) + l->nodeoffset );
69         
70         if (n->next) n->next->prev = n->prev;
71                 else l->last = n->prev;
72         if (n->prev) n->prev->next = n->next;
73                 else l->first = n->next;
74 }
75
76  
77 void *list_first (list *l)
78 {
79         if (!l->first) return NULL;
80         return ((char*) l->first) - l->nodeoffset;
81 }
82
83
84 void *list_last (list *l)
85 {
86         if (!l->last) return NULL;
87         return ((char*) l->last) - l->nodeoffset;
88 }
89
90
91 void *list_next (list *l, void *element)
92 {
93         listnode *n;
94         n = (listnode*) ( ((char*) element) + l->nodeoffset );
95         if (!n->next) return NULL;
96         return ((char*) n->next) - l->nodeoffset;
97 }
98
99         
100 void *list_prev (list *l, void *element)
101 {
102         listnode *n;
103         n = (listnode*) ( ((char*) element) + l->nodeoffset );
104         if (!n->prev) return NULL;
105         return ((char*) n->prev) - l->nodeoffset;
106 }
107