src/vapiLists.c

Ir para a documentação deste ficheiro.
00001 #include <stdio.h>
00002 #include "vapiLists.h"
00003 #include "vapiMacro.h"
00004 
00005 vList *
00006 v_list_new ()
00007 {
00008         vList *List = malloc (sizeof (vList));
00009         *List = NULL;
00010         return List;
00011 }
00012 
00013 void
00014 v_list_append (vList * List, vpointer data)
00015 {
00016 
00017         if (*List == NULL)
00018         {
00019                 *List = malloc (sizeof (vListItem));
00020                 (*List)->data = data;
00021                 (**List).Next = NULL;
00022         }
00023         else
00024         {
00025 
00026                 v_list_append ((vList *) & (**List).Next, data);
00027         }
00028 
00029 }
00030 
00031 int
00032 v_list_count_elements_aux (vList * List, int start)
00033 {
00034         start = start + 1;
00035 
00036         if ((**List).Next != NULL)
00037         {
00038                 start = v_list_count_elements_aux ((vList *) & (**List).
00039                                                    Next, start);
00040         }
00041 
00042         return start;
00043 }
00044 
00045 int
00046 v_list_count_elements (vList * List)
00047 {
00048         if ((*List) == NULL)
00049         {
00050                 return 0;
00051         }
00052         return v_list_count_elements_aux (List, 0);
00053 }
00054 
00055 static void
00056 v_list_delete_item_aux (vList * List, vList * Previous,
00057                         vFreeFunction FreeFunction, int Delete, int Start)
00058 {
00059         vList *Temp;
00060         if (*List == NULL)
00061         {
00062                 return;
00063         }
00064 
00065         if (Start == Delete)
00066 
00067         {
00068 
00069 
00070                 if ((**List).Next != NULL)
00071                 {
00072                         if (Previous == NULL)
00073                         {
00074                                 Temp = (vList *) & (**List).Next;
00075                                 if (FreeFunction != NULL)
00076                                 {
00077                                         FreeFunction ((*List)->data);
00078                                 }
00079                                 (*List)->data = (*Temp)->data;
00080                                 (*List)->Next =
00081                                         (struct vListItem *) (*Temp)->Next;
00082                                 //free(Temp);
00083 
00084                         }
00085                         else
00086                         {
00087 
00088                                 (*Previous)->Next = (*List)->Next;
00089                                 //FreeFunction ((*List)->data);
00090                                 //free ((*List));
00091 
00092 
00093                         }
00094                 }
00095 
00096                 else
00097                 {
00098                         if (FreeFunction != NULL)
00099                         {
00100                                 FreeFunction ((*List)->data);
00101                         }
00102                         free (*List);
00103                         *List = NULL;
00104                 }
00105 
00106                 return;
00107         }
00108         if ((**List).Next != NULL)
00109         {
00110                 Start++;
00111                 v_list_delete_item_aux ((vList *) & (*List)->Next,
00112                                         (vList *) List, FreeFunction, Delete,
00113                                         Start);
00114         }
00115 
00116 
00117 }
00118 
00119 void
00120 v_list_delete_item (vList * List, vFreeFunction FreeFunction, int Delete)
00121 {
00122         v_list_delete_item_aux (List, NULL, FreeFunction, Delete, 0);
00123 }
00124 
00125 static vpointer
00126 v_list_get_aux (vList * List, int Item, int Start)
00127 {
00128 
00129         if (*List == NULL)
00130         {
00131                 return NULL;
00132         }
00133 
00134         if (Start == Item)
00135         {
00136                 return (*List)->data;
00137 
00138         }
00139         if (&(**List).Next != NULL)
00140         {
00141                 Start++;
00142                 return (v_list_get_aux
00143                         ((vList *) & (**List).Next, Item, Start));
00144         }
00145         else
00146         {
00147                 return NULL;
00148         }
00149 }
00150 
00151 vpointer
00152 v_list_get (vList * List, int Item)
00153 {
00154         return (v_list_get_aux (List, Item, 0));
00155 }
00156 
00157 
00158 
00159 static void
00160 v_list_insert_aux (vList * List, vList * Previous, vpointer data,
00161                 int position, int Start)
00162 {
00163         vListItem *NewItem;
00164 
00165 
00166         if (Start == position)
00167         {
00168 
00169                 NewItem = malloc (sizeof (vListItem));
00170                 NewItem->data = data;
00171                 NewItem->Next = (struct vListItem *) (*Previous);
00172                 (*Previous) = NewItem;
00173 
00174                 return;
00175 
00176         }
00177         if (*List == NULL)
00178         {
00179                 return;
00180         }
00181         else
00182         {
00183                 Start++;
00184                 v_list_insert_aux ((vList *) & (**List).Next,
00185                                 List, data, position, Start);
00186         }
00187 
00188 
00189 
00190 }
00191 void
00192 v_list_insert (vList * List, vpointer data, int position)
00193 {
00194         int ListSize;
00195         
00196         ListSize = v_list_count_elements(List);
00197         /*
00198          * Verificação da posição dentros dos limites da lista.
00199          */
00200         if (position <0 || position >=ListSize)
00201         {
00202                 return;
00203         }
00204         /*
00205          * O que se pretende é ir à posição anterior e 
00206          *  adicionar o Operation seguinte. 
00207          */
00208         v_list_insert_aux ((vList *) & (**List).Next, List, data, position, 0);
00209 }
00210 
00211 void
00212 v_list_free (vList * List, vFreeFunction FreeFunction)
00213 {
00214         int Count, i;
00215 
00216         Count = v_list_count_elements (List);
00217 
00218         /*
00219          * Caso existam items, eliminá-los.
00220          */
00221         if (Count > 0)
00222         {
00223                 for (i = Count; i > 0; i--)
00224                 {
00225 
00226                         v_list_delete_item (List, FreeFunction, (i - 1));
00227 
00228                 }
00229         }
00230         free(List);
00231 }
00232 void
00233 v_list_move (vList * List, int source, int destination)
00234 
00235 {
00236         vpointer * Item;
00237         Item = v_list_get(List, source);
00238         
00239         v_list_delete_item (List, NULL, source);
00240         
00241         if (source < destination)
00242         {
00243                 destination--;
00244         }
00245         
00246         v_list_insert(List, Item, destination);
00247         
00248 }

Gerado em Tue Jul 24 10:34:59 2007 para Vapi por  doxygen 1.5.1