Melhorado o structures
[pspdecompiler.git] / lists.c
blob62d5264ddb4dd373ed05a57789683fb8559f6879
1 /**
2 * Author: Humberto Naves (hsnaves@gmail.com)
3 */
5 #include <stdlib.h>
7 #include "lists.h"
8 #include "alloc.h"
9 #include "utils.h"
11 struct _element {
12 struct _listpool *const pool;
14 struct _list *lst;
15 struct _element *next;
16 struct _element *prev;
18 void *value;
21 struct _list {
22 struct _listpool *const pool;
23 struct _element *head;
24 struct _element *tail;
25 int size;
28 struct _listpool {
29 fixedpool lstpool;
30 fixedpool elmpool;
33 listpool listpool_create (size_t numelms, size_t numlsts)
35 listpool result = (listpool) xmalloc (sizeof (struct _listpool));
36 result->elmpool = fixedpool_create (sizeof (struct _element), numelms, 0);
37 result->lstpool = fixedpool_create (sizeof (struct _list), numlsts, 0);
38 return result;
41 void listpool_destroy (listpool pool)
43 fixedpool_destroy (pool->lstpool, NULL, NULL);
44 fixedpool_destroy (pool->elmpool, NULL, NULL);
45 free (pool);
49 list list_alloc (listpool pool)
51 list l;
52 listpool *ptr;
53 l = fixedpool_alloc (pool->lstpool);
55 ptr = (listpool *) &l->pool;
56 *ptr = pool;
57 l->head = l->tail = NULL;
58 l->size = 0;
59 return l;
62 void list_free (list l)
64 listpool pool;
65 list_reset (l);
66 pool = l->pool;
67 fixedpool_free (pool->lstpool, l);
70 void list_reset (list l)
72 element el, ne;
73 for (el = l->head; el; el = ne) {
74 ne = el->next;
75 element_free (el);
77 l->head = l->tail = NULL;
78 l->size = 0;
81 int list_size (list l)
83 return l->size;
86 element list_head (list l)
88 return l->head;
91 void *list_headvalue (list l)
93 if (list_head (l))
94 return element_getvalue (list_head (l));
95 return NULL;
98 element list_tail (list l)
100 return l->tail;
103 void *list_tailvalue (list l)
105 if (list_tail (l))
106 return element_getvalue (list_tail (l));
107 return NULL;
110 element list_inserthead (list l, void *val)
112 element el = element_alloc (l->pool, val);
113 if (l->size == 0) {
114 el->lst = l;
115 l->size++;
116 l->head = l->tail = el;
117 } else {
118 element_insertbefore (l->head, el);
120 return el;
123 element list_inserttail (list l, void *val)
125 element el = element_alloc (l->pool, val);
126 if (l->size == 0) {
127 el->lst = l;
128 l->size++;
129 l->head = l->tail = el;
130 } else {
131 element_insertafter (l->tail, el);
133 return el;
136 void *list_removehead (list l)
138 element el;
139 el = list_head (l);
140 if (!el) return NULL;
141 return element_free (el);
144 void *list_removetail (list l)
146 element el;
147 el = list_tail (l);
148 if (!el) return NULL;
149 return element_free (el);
154 element element_alloc (listpool pool, void *val)
156 element el;
157 listpool *ptr;
158 el = fixedpool_alloc (pool->elmpool);
160 ptr = (listpool *) &el->pool;
161 *ptr = pool;
162 el->prev = NULL;
163 el->next = NULL;
164 el->value = val;
165 el->lst = NULL;
167 return el;
170 void element_remove (element el)
172 list l = el->lst;
173 if (l) l->size--;
175 if (!el->next) {
176 if (l) l->tail = el->prev;
177 } else {
178 el->next->prev = el->prev;
181 if (!el->prev) {
182 if (l) l->head = el->next;
183 } else {
184 el->prev->next = el->next;
186 el->next = el->prev = NULL;
187 el->lst = NULL;
190 void *element_free (element el)
192 listpool pool;
193 void *val;
194 val = element_getvalue (el);
195 pool = el->pool;
196 element_remove (el);
197 fixedpool_free (pool->elmpool, el);
198 return val;
201 void *element_getvalue (element el)
203 return el->value;
206 void element_setvalue (element el, void *val)
208 el->value = val;
211 element element_next (element el)
213 return el->next;
216 element element_previous (element el)
218 return el->prev;
222 void element_insertbefore (element el, element inserted)
224 inserted->lst = el->lst;
225 inserted->prev = el->prev;
226 inserted->next = el;
227 if (el->prev) el->prev->next = inserted;
228 el->prev = inserted;
230 if (inserted->lst) {
231 inserted->lst->size++;
232 if (!inserted->prev) inserted->lst->head = inserted;
236 void element_insertafter (element el, element inserted)
238 inserted->lst = el->lst;
239 inserted->next = el->next;
240 inserted->prev = el;
241 if (el->next) el->next->prev = inserted;
242 el->next = inserted;
244 if (inserted->lst) {
245 inserted->lst->size++;
246 if (!inserted->next) inserted->lst->tail = inserted;