alist.c

Go to the documentation of this file.
00001 /* Eliot                                                                     */
00002 /* Copyright (C) 2005  Antoine Fraboulet                                     */
00003 /*                                                                           */
00004 /* This file is part of Eliot.                                               */
00005 /*                                                                           */
00006 /* Eliot is free software; you can redistribute it and/or modify             */
00007 /* it under the terms of the GNU General Public License as published by      */
00008 /* the Free Software Foundation; either version 2 of the License, or         */
00009 /* (at your option) any later version.                                       */
00010 /*                                                                           */
00011 /* Eliot is distributed in the hope that it will be useful,                  */
00012 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
00013 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
00014 /* GNU General Public License for more details.                              */
00015 /*                                                                           */
00016 /* You should have received a copy of the GNU General Public License         */
00017 /* along with this program; if not, write to the Free Software               */
00018 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00019 
00020 /**
00021  *  \file   alist.c
00022  *  \brief  List type used by automaton
00023  *  \author Antoine Fraboulet
00024  *  \date   2005
00025  */
00026 
00027 #include <stdlib.h>
00028 #include "alist.h"
00029 
00030 
00031 struct alist_elt_t {
00032   void* info;
00033   alist_elt next;
00034 };
00035 
00036 struct alist_t {
00037   int size;
00038   void (*delete_function)(void*);
00039   alist_elt start;
00040 };
00041 
00042 
00043 void*
00044 alist_elt_get_value(alist_elt e)
00045 {
00046   return e->info;
00047 }
00048 
00049 alist_elt
00050 alist_elt_create(void* info)
00051 {
00052   alist_elt e;
00053   e = (alist_elt)malloc(sizeof(struct alist_elt_t));
00054   e->info = info;
00055   e->next = NULL;
00056   return e;
00057 }
00058 
00059 /* ************************************************** */
00060 /* ************************************************** */
00061 /* ************************************************** */
00062 
00063 alist
00064 alist_create()
00065 {
00066   alist l;
00067   l                  = (alist)malloc(sizeof(struct alist_t));
00068   l->size            = 0;
00069   l->start           = NULL;
00070   l->delete_function = NULL;
00071   return l;
00072 }
00073 
00074 alist
00075 alist_clone(alist l)
00076 {
00077   alist t;
00078   alist_elt ptr;
00079   t = alist_create();
00080   for(ptr = alist_get_first(l); ptr ; ptr = alist_get_next(l,ptr))
00081     {
00082       alist_add(t,alist_elt_get_value(ptr));
00083     }
00084   return t;
00085 }
00086 
00087 void
00088 alist_set_delete (alist l, void (*f)(void*))
00089 {
00090   l->delete_function = f;
00091 }
00092 
00093 static void
00094 alist_delete_rec(alist_elt e, void (*delete_function)(void*))
00095 {
00096   if (e != NULL)
00097     {
00098       alist_delete_rec(e->next, delete_function);
00099       if (delete_function)
00100         delete_function(e->info);
00101       e->info = NULL;
00102       free(e);
00103     }
00104 }
00105 
00106 void
00107 alist_delete(alist l)
00108 {
00109   alist_delete_rec(l->start,l->delete_function);
00110   free(l);
00111 }
00112 
00113 void
00114 alist_add(alist l, void* value)
00115 {
00116   alist_elt e;
00117   e = alist_elt_create(value);
00118   e->next = l->start;
00119   l->start = e;
00120   l->size ++;
00121 }
00122 
00123 int
00124 alist_is_in(alist l, void* e)
00125 {
00126   alist_elt ptr;
00127   for(ptr = alist_get_first(l); ptr; ptr = alist_get_next(l,ptr))
00128     if (alist_elt_get_value(ptr) == e)
00129       return 1;
00130   return 0;
00131 }
00132 
00133 int
00134 alist_equal(alist id1, alist id2)
00135 {
00136   alist_elt e1;
00137 
00138   if (alist_get_size(id1) != alist_get_size(id2))
00139     return 0;
00140 
00141   for(e1 = alist_get_first(id1) ; e1 ; e1 = alist_get_next(id1,e1))
00142     {
00143       if (! alist_is_in(id2, alist_elt_get_value(e1)))
00144         return 0;
00145     }
00146 
00147   return 1;
00148 }
00149 
00150 void
00151 alist_insert(alist dst, alist src)
00152 {
00153   alist_elt ptr;
00154   for(ptr = alist_get_first(src); ptr ; ptr = alist_get_next(src,ptr))
00155     {
00156       void *e = alist_elt_get_value(ptr);
00157       if (! alist_is_in(dst,e))
00158         alist_add(dst,e);
00159     }
00160 }
00161 
00162 alist_elt
00163 alist_get_first(alist l)
00164 {
00165   return l->start;
00166 }
00167 
00168 alist_elt
00169 alist_get_next(alist l, alist_elt e)
00170 {
00171   return e->next;
00172 }
00173 
00174 void*
00175 alist_pop_first_value(alist l)
00176 {
00177   void* p = NULL;
00178   alist_elt e = l->start;
00179   if (e)
00180     {
00181       l->start = e->next;
00182       e->next  = NULL;
00183       p = e->info;
00184       l->size --;
00185       alist_delete_rec(e,l->delete_function);
00186     }
00187   return p;
00188 }
00189 
00190 int
00191 alist_get_size(alist l)
00192 {
00193   return l->size;
00194 }
00195 
00196 int
00197 alist_is_empty(alist l)
00198 {
00199   return l->size == 0;
00200 }

Generated on Thu Dec 29 02:01:14 2005 for Eliot by  doxygen 1.4.5