list.c
Go to the documentation of this file.
1 #include "list.h"
2 
3 #include "error.h"
4 #include "defines.h"
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
26 void PrivDelEle(Tbuf *node,Tlist *list);
27 
28 
40 void PrivAddElement(void *Info,Tbuf *pos,boolean in_front,Tlist *list);
41 
42 
43 /*Delete the element pointed by node from the list 'list'*/
44 void PrivDelEle(Tbuf *node, /*pointer to the element to be removed from the list*/
45  Tlist *list /*list*/
46  )
47 {
48  if (node->previous!=(Tbuf *)NULL)
49  node->previous->next=node->next;
50 
51  if (node->next!=(Tbuf *)NULL)
52  node->next->previous=node->previous;
53 
54  if (node==list->first)
55  list->first=node->next;
56  if (node==list->last)
57  list->last=node->previous;
58 
59  list->num_ele--;
60 
61  free(node->data);
62  free(node);
63 }
64 
65 /*
66  * Adds a new element (pointed by Info) in the position of the list pointed by pos.
67  * If in_front is TRUE then the new element is inserted in front of that pointed by
68  * pos and if not behind.
69  * If the list is empty, the parameter pos is not used.
70  */
71 void PrivAddElement(void *Info, /*pointer to the data to de added to the list*/
72  Tbuf *pos, /*pointer to the position of the list where the new data is added*/
73  boolean in_front, /*TRUE if info is added in front of the node*/
74  Tlist *list /*list to be extended*/
75  )
76 {
77  Tbuf *node;
78 
79  NEW(node,1,Tbuf);
80 
81  NEW(node->data,list->ele_size,char);
82 
83  memcpy(node->data,Info,list->ele_size);
84 
85  if (list->num_ele==0) /*pos must be NULL*/
86  {
87  node->previous=NULL;
88  node->next=NULL;
89 
90  list->first=node;
91  list->last=node;
92  }
93  else
94  {
95  if (in_front)
96  {
97  node->next=pos;
98  node->previous=pos->previous;
99 
100  if (node->previous!=NULL)
101  node->previous->next=node;
102  pos->previous=node;
103  }
104  else
105  {
106  node->next=pos->next;
107  node->previous=pos;
108 
109  if (node->next!=NULL)
110  node->next->previous=node;
111  pos->next=node;
112  }
113 
114  if ((list->first==pos)&&(in_front))
115  list->first=node;
116 
117  if ((list->last==pos)&&(!in_front))
118  list->last=node;
119  }
120 
121  list->num_ele++;
122 }
123 
124 /**************************************************************************
125  **************************************************************************/
126 
127 /*PUBLIC FUNCTIONS*/
128 
129 /*
130  * Init a list with elements of size ele_size.
131  * This size can be obtained usint sizeof(type)
132  * where type is the type of elements to be stored
133  * in the list.
134  */
135 void InitList(unsigned int ele_size, /*size of the elements of the list*/
136  Tlist *list /*list to be initialized*/
137  )
138 {
139 
140  list->num_ele=0;
141  list->ele_size=ele_size;
142  list->first=(Tbuf *)NULL;
143  list->last=(Tbuf *)NULL;
144 }
145 
146 /*
147  * Deletes a list.
148  * No operation is done on each list element.
149  * The user is in charge of deleting each element of the list using
150  * the apropritate delete function (iterating on the list, getting and
151  * deleting each element and the removing it from the list)
152  */
153 void DeleteList(Tlist *list /*list to be deleted*/
154  )
155 {
156  DeleteAllItems(list);
157 }
158 
159 /*
160  * Deletes all the items of a list list.
161  */
162 void DeleteAllItems(Tlist *list /*list to be deleted*/
163  )
164 {
165  Tbuf *e,*next;
166 
167  e=list->first;
168  while(e!=NULL)
169  {
170  next=e->next;
171  PrivDelEle(e,list);
172  e=next;
173  }
174 }
175 
176 
177 /*
178  * Returns the number of elements of the list
179  */
180 unsigned int ListSize(Tlist *list)
181 {
182  return(list->num_ele);
183 }
184 
185 /*
186  * Returns true if the list is empty
187 */
188 boolean ListEmpty(Tlist *list)
189 {
190  return(list->num_ele==0);
191 }
192 
193 /*
194  * Adds a new element at the head of the list
195  */
196 void AddFirstElement(void *Info, /*pointer to the data to de added to the list*/
197  Tlist *list /*list to be extended*/
198  )
199 {
200  PrivAddElement(Info,list->first,TRUE,list);
201 }
202 
203 /*
204  * Adds a new element at the end of the list
205  */
206 void AddLastElement(void *Info,
207  Tlist *list)
208 {
209  PrivAddElement(Info,list->last,FALSE,list);
210 }
211 
212 void ExtractFirst(void *Info,Tlist *list)
213 {
214  if (list->first!=NULL)
215  {
216  Tbuf *e;
217 
218  memcpy(Info,list->first->data,list->ele_size);
219 
220  e=list->first->next;
221  PrivDelEle(list->first,list);
222  list->first=e;
223  }
224  else
225  Error("Empty list");
226 }
227 
228 void ExtractLast(void *Info,Tlist *list)
229 {
230  if (list->last!=NULL)
231  {
232  Tbuf *e;
233 
234  memcpy(Info,list->last->data,list->ele_size);
235 
236  e=list->last->next;
237  PrivDelEle(list->last,list);
238  list->last=e;
239  }
240  else
241  Error("Empty list");
242 }
243 
244 boolean HasElement(void *Info,boolean (* cmp)(void *,void*),Tlist *list)
245 {
246  Tbuf *current;
247  boolean found;
248 
249  current=list->first;
250 
251  found=FALSE;
252  while((!found)&&(current!=NULL))
253  {
254  found=cmp(Info,current->data);
255  if (!found)
256  current=current->next;
257  }
258  return(found);
259 }
260 
261 void PrintList(Tlist *l)
262 {
263  Tbuf *current;
264  printf("L:[");
265 
266  current=l->first;
267 
268  while(current!=NULL)
269  {
270  printf("%p (%p) ",current,current->data);
271  current=current->next;
272  }
273 
274  printf(" ]\n");
275 }
276 
277 /**************************************************************************/
278 
279 /*Iterators functions*/
280 
281 /*
282  * Starts a new itarator for a given list
283  */
285 {
286  i->current=NULL;
287  i->list=list;
288 }
289 
291 {
292  dst->current=src->current;
293  dst->list=src->list;
294 }
295 
296 /*
297  * Returns a pointer to the data stored at the position of the list pointed by 'i->current'
298  */
300 {
301  return(i->current->data);
302 }
303 
304 /*
305  * Makes a copy of the element pointed by the iterator
306  * and deletes the element from the list
307  */
308 void ExtractCurrent(void *Info,Titerator *i)
309 {
310  if (i->current!=NULL)
311  {
312  Tbuf *e;
313 
314  memcpy(Info,i->current->data,i->list->ele_size);
315 
316  e=i->current->next;
317  PrivDelEle(i->current,i->list);
318  i->current=e;
319  }
320 }
321 
322 /*
323  * Adds a new item in front of the element pointed by i->current
324  */
325 void AddInFrontOfCurrent(void *Info,Titerator *i)
326 {
327  PrivAddElement(Info,i->current,TRUE,i->list);
328 }
329 
330 /*
331  * Adds a new item behind the element pointed by i->current
332  */
333 void AddBehindCurrent(void *Info,Titerator *i)
334 {
335  PrivAddElement(Info,i->current,FALSE,i->list);
336 }
337 
338 /*
339  * Removes the element pointed by i->current from the list
340  */
342 {
343  if (i->current!=NULL)
344  {
345  Tbuf *e;
346 
347  e=i->current->next;
348  PrivDelEle(i->current,i->list);
349  i->current=e;
350  }
351 }
352 
353 /*
354  * Moves the iterator to the behning of the list
355  */
356 void First(Titerator *i)
357 {
358  i->current=i->list->first;
359 }
360 
361 /*
362  * Moves the iterator to the end of the list
363  */
364 void Last(Titerator *i)
365 {
366  i->current=i->list->last;
367 }
368 
369 /*
370  * Moves the iterator forward on the list
371  * Returns FALSE if the movement can not be performed
372  */
373 boolean Advance(Titerator *i)
374 {
375  boolean b;
376 
377  if (i->current!=NULL)
378  {
379  i->current=i->current->next;
380  if (i->current!=NULL)
381  b=TRUE;
382  else
383  b=FALSE;
384  }
385  else
386  b=FALSE;
387 
388  return(b);
389 }
390 
391 /*
392  * Moves the iterator backwards on the list
393  * Returns FALSE if the movement can not be performed
394  */
395 boolean Bacward(Titerator *i)
396 {
397  boolean b;
398 
399  if (i->current!=NULL)
400  {
401  i->current=i->current->previous;
402  if (i->current!=NULL)
403  b=TRUE;
404  else
405  b=FALSE;
406  }
407  else
408  b=FALSE;
409 
410  return(b);
411 }
412 
413 /*
414  * Moves the iterator to the position 'n' of the list
415  * Returns FALSE if the movement can not be performed
416  */
417 boolean MoveTo(unsigned int n,Titerator *i)
418 {
419  unsigned int j;
420  boolean b;
421 
422  if (n>=i->list->num_ele)
423  {
424  i->current=NULL;
425  b=FALSE;
426  }
427  else
428  {
429  i->current=i->list->first;
430  j=0;
431  while(j<n)
432  {
433  i->current=i->current->next;
434  j++;
435  }
436  b=TRUE;
437  }
438 
439  return(b);
440 }
441 
442 /*
443  * Returns TRUE if the iterator points to a non valid element
444  */
445 boolean EndOfList(Titerator *i)
446 {
447  return(i->current==NULL);
448 }
void DeleteAllItems(Tlist *list)
Resets a list.
Definition: list.c:162
void CopyIterator(Titerator *dst, Titerator *src)
Copy constructor.
Definition: list.c:290
void AddFirstElement(void *Info, Tlist *list)
Adds an element at the head of the list.
Definition: list.c:196
boolean EndOfList(Titerator *i)
Checks if an iterator is pointing at the end of the list.
Definition: list.c:445
boolean MoveTo(unsigned int n, Titerator *i)
Moves an iterator to a given position of its associated list.
Definition: list.c:417
#define FALSE
FALSE.
Definition: boolean.h:30
void ExtractCurrent(void *Info, Titerator *i)
Extract the element pointed by the iterator from the list.
Definition: list.c:308
unsigned int ele_size
Definition: list.h:50
unsigned int ListSize(Tlist *list)
Gets the number of elements in the list.
Definition: list.c:180
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
Tbuf * first
Definition: list.h:48
struct Tbuf * previous
Definition: list.h:28
void * data
Definition: list.h:30
void ExtractFirst(void *Info, Tlist *list)
Extracts the first element from the list.
Definition: list.c:212
void First(Titerator *i)
Moves an iterator to the first position of its associated list.
Definition: list.c:356
void DeleteCurrent(Titerator *i)
Destructor.
Definition: list.c:341
#define TRUE
TRUE.
Definition: boolean.h:21
void PrintList(Tlist *l)
Prints a list to stdout.
Definition: list.c:261
void Error(const char *s)
General error function.
Definition: error.c:80
Error and warning functions.
unsigned int num_ele
Definition: list.h:51
void Last(Titerator *i)
Moves an iterator to the last position of its associated list.
Definition: list.c:364
void * GetCurrent(Titerator *i)
Gets the element pointed by the iterator.
Definition: list.c:299
void InitList(unsigned int ele_size, Tlist *list)
Constructor.
Definition: list.c:135
Definitions of constants and macros used in several parts of the cuik library.
A generic list.
Definition: list.h:46
void PrivDelEle(Tbuf *node, Tlist *list)
Private function to delete an element from the list.
Definition: list.c:44
Definition of the Tlist type and the associated functions.
Tbuf * current
Definition: list.h:63
void AddInFrontOfCurrent(void *Info, Titerator *i)
Adds an element to the list in front of the position pointed by the iterator.
Definition: list.c:325
void ExtractLast(void *Info, Tlist *list)
Extracts the last element from the list.
Definition: list.c:228
boolean Bacward(Titerator *i)
Moves an iterator to the previuos position of its associated list.
Definition: list.c:395
void AddLastElement(void *Info, Tlist *list)
Adds an element at the tail of the list.
Definition: list.c:206
void AddBehindCurrent(void *Info, Titerator *i)
Adds an element to the list behind the position pointed by the iterator.
Definition: list.c:333
Tbuf * last
Definition: list.h:49
boolean Advance(Titerator *i)
Moves an iterator to the next position of its associated list.
Definition: list.c:373
void InitIterator(Titerator *i, Tlist *list)
Constructor.
Definition: list.c:284
struct Tbuf * next
Definition: list.h:29
boolean ListEmpty(Tlist *list)
Checks if a list is empty.
Definition: list.c:188
Tlist * list
Definition: list.h:64
A node in a list.
Definition: list.h:26
List iterator.
Definition: list.h:61
boolean HasElement(void *Info, boolean(*cmp)(void *, void *), Tlist *list)
Searches an element in the list.
Definition: list.c:244
void DeleteList(Tlist *list)
Destructor.
Definition: list.c:153
void PrivAddElement(void *Info, Tbuf *pos, boolean in_front, Tlist *list)
Private function to add an element from the list.
Definition: list.c:71