PULSAR  1.0.0
Parallel Ultra Light Systolic Array Runtime
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
icl_list.c
Go to the documentation of this file.
1 
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 #include "icl_list.h"
16 
18 
24 {
25  icl_list_t *node;
26 
27  node = (icl_list_t*)malloc(sizeof(icl_list_t));
28  if (!node)
29  return NULL;
30 
31  node->flink = NULL;
32  node->blink = node;
33  node->data = NULL;
34 
35  return(node);
36 }
37 
39 
50 {
51  icl_list_t *node;
52 
53  if(!head || !pos)
54  return NULL;
55 
56  node = (icl_list_t*)malloc(sizeof(icl_list_t));
57  if (!node)
58  return NULL;
59 
60  node->blink = pos;
61  node->flink = pos->flink;
62  node->data = data;
63 
64  if (pos->flink)
65  pos->flink->blink = node;
66  else
67  head->blink = node; /* node at end of list */
68 
69  pos->flink = node;
70  return node;
71 }
72 
74 
84  icl_list_t *head, icl_list_t *pos, void (*free_function)(void *))
85 {
86  if (!pos || !head)
87  return -1;
88  if (pos == head)
89  return -1;
90 
91  if (free_function && pos->data)
92  (*free_function)(pos->data);
93 
94  pos->blink->flink = pos->flink;
95 
96  if (pos->flink)
97  pos->flink->blink = pos->blink;
98  else
99  head->blink = pos->blink; /* pos at end of list */
100 
101  free(pos);
102  return 0;
103 }
104 
106 
116  icl_list_t *head, void *data, int (*compare)(void*, void*))
117 {
118  icl_list_t *node;
119 
120  if (!head)
121  return NULL;
122 
123  for (node = head->flink; node != NULL; node = node->flink) {
124  if (!node->data)
125  continue;
126  if ((compare && (*compare)(node->data, data) == 0))
127  break;
128  else if (node->data == data)
129  break; /* compare == NULL, then direct comparison of pointers */
130  }
131  return(node);
132 }
133 
135 
146  icl_list_t *head, void *data, int (*compare)(void*, void*))
147 {
148  if (!head || !compare)
149  return NULL;
150 
151  if (head->flink == NULL)
152  return (icl_list_insert(head, head, data));
153 
154  icl_list_t *node = head->flink;
155  if (compare(data, node->data) <= 0)
156  return (icl_list_insert(head, head, data));
157 
158  while (node->flink != NULL && compare(data, node->flink->data) > 0)
159  node = node->flink;
160  return (icl_list_insert(head, node, data));
161 }
162 
164 
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 }
icl_list_t * icl_list_search(icl_list_t *head, void *data, int(*compare)(void *, void *))
Finds a data item in the specified linked list.
Definition: icl_list.c:115
icl_list_t * icl_list_prepend(icl_list_t *head, void *data)
Insert a node at the beginning of this list.
Definition: icl_list.c:312
icl_list_t * icl_list_new()
Create new linked list.
Definition: icl_list.c:23
int icl_list_size(icl_list_t *head)
Get the number of items in this linked list.
Definition: icl_list.c:200
void * data
Definition: icl_list.h:20
int icl_list_destroy(icl_list_t *head, void(*free_function)(void *))
Frees the resources associated with this linked list.
Definition: icl_list.c:173
icl_list_t * icl_list_append(icl_list_t *head, void *data)
Insert a node at the end of this list.
Definition: icl_list.c:326
dependency free linked list
icl_list_t * icl_list_last(icl_list_t *head)
Get the last item in this linked list.
Definition: icl_list.c:237
icl_list_t * icl_list_first(icl_list_t *head)
Get the first item in this linked list.
Definition: icl_list.c:221
icl_list_t * icl_list_prev(icl_list_t *head, icl_list_t *pos)
Get the node preceding the specified node.
Definition: icl_list.c:271
icl_list_t * icl_list_isort(icl_list_t *head, void *data, int(*compare)(void *, void *))
Insert data into a sorted list. Does not support direct comparison of pointers.
Definition: icl_list.c:145
icl_list_t * icl_list_concat(icl_list_t *head1, icl_list_t *head2)
Concatenate two linked lists.
Definition: icl_list.c:290
int icl_list_delete(icl_list_t *head, icl_list_t *pos, void(*free_function)(void *))
Delete the specified node.
Definition: icl_list.c:83
icl_list_t * icl_list_insert(icl_list_t *head, icl_list_t *pos, void *data)
Insert a new node after the specified node.
Definition: icl_list.c:49
struct icl_list_s * blink
Definition: icl_list.h:22
struct icl_list_s * flink
Definition: icl_list.h:21
icl_list_t * icl_list_next(icl_list_t *head, icl_list_t *pos)
Get the node following the specified node.
Definition: icl_list.c:254