PULSAR  2.0.0
Parallel Ultra-Light Systolic Array Runtime
 All Data Structures Files Functions Typedefs Enumerations Macros Groups
icl_list.c
Go to the documentation of this file.
1 
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include "icl_list.h"
15 
17 
23 {
24  icl_list_t *node;
25 
26  node = (icl_list_t*)malloc(sizeof(icl_list_t));
27  if (!node)
28  return NULL;
29 
30  node->flink = NULL;
31  node->blink = node;
32  node->data = NULL;
33 
34  return(node);
35 }
36 
38 
48 {
49  icl_list_t *node;
50 
51  if(!head || !pos)
52  return NULL;
53 
54  node = (icl_list_t*)malloc(sizeof(icl_list_t));
55  if (!node)
56  return NULL;
57 
58  node->blink = pos;
59  node->flink = pos->flink;
60  node->data = data;
61 
62  if (pos->flink)
63  pos->flink->blink = node;
64  else
65  head->blink = node; // The node at end of the list.
66 
67  pos->flink = node;
68  return node;
69 }
70 
72 
83  icl_list_t *head, icl_list_t *pos, void (*free_function)(void *))
84 {
85  if (!pos || !head)
86  return -1;
87  if (pos == head)
88  return -1;
89 
90  if (free_function && pos->data)
91  (*free_function)(pos->data);
92 
93  pos->blink->flink = pos->flink;
94 
95  if (pos->flink)
96  pos->flink->blink = pos->blink;
97  else
98  head->blink = pos->blink; // Position at the end of the list.
99 
100  free(pos);
101  return 0;
102 }
103 
105 
115  icl_list_t *head, void *data, int (*compare)(void*, void*))
116 {
117  icl_list_t *node;
118 
119  if (!head)
120  return NULL;
121 
122  for (node = head->flink; node != NULL; node = node->flink) {
123  if (!node->data)
124  continue;
125  if ((compare && (*compare)(node->data, data) == 0))
126  break;
127  else if (node->data == data)
128  break; // compare == NULL, then direct comparison of pointers.
129  }
130  return(node);
131 }
132 
134 
145  icl_list_t *head, void *data, int (*compare)(void*, void*))
146 {
147  if (!head || !compare)
148  return NULL;
149 
150  if (head->flink == NULL)
151  return (icl_list_insert(head, head, data));
152 
153  icl_list_t *node = head->flink;
154  if (compare(data, node->data) <= 0)
155  return (icl_list_insert(head, head, data));
156 
157  while (node->flink != NULL && compare(data, node->flink->data) > 0)
158  node = node->flink;
159  return (icl_list_insert(head, node, data));
160 }
161 
163 
172 int
173 icl_list_destroy(icl_list_t *head, void (*free_function)(void*))
174 {
175  icl_list_t *node, *tmpnode;
176 
177  if (!head)
178  return -1;
179 
180  for (node = head; node != NULL;) {
181  tmpnode = node->flink;
182 
183  if (free_function != NULL && node->data != NULL)
184  (*free_function)(node->data);
185 
186  free(node);
187  node = tmpnode;
188  }
189  return 0;
190 }
191 
193 
201 {
202  int size = 0;
203 
204  if (!head)
205  return -1;
206 
207  while ((head=head->flink))
208  size++;
209 
210  return size;
211 }
212 
214 
222 {
223  if (head)
224  return head->flink;
225 
226  return NULL;
227 }
228 
230 
238 {
239  if (head)
240  return head->blink;
241 
242  return NULL;
243 }
244 
246 
255 {
256  if(pos)
257  return pos->flink;
258 
259  return NULL;
260 }
261 
263 
272 {
273  if (pos && pos->blink != NULL && pos != head &&
274  pos->blink != head && pos->blink != pos)
275  return pos->blink;
276 
277  return NULL;
278 }
279 
281 
291 {
292  if (!head1 || !head2 || !head1->blink || !head2->flink)
293  return NULL;
294 
295  head1->blink->flink = head2->flink;
296  head2->flink->blink = head1->blink;
297  head1->blink = head2->blink;
298 
299  free(head2);
300  return(head1);
301 }
302 
304 
313 {
314  return (icl_list_insert(head, head, data));
315 }
316 
318 
327 {
328  return (icl_list_insert(head, head->blink, data));
329 }