Vlib/list.c

Go to the documentation of this file.
00001 /*
00002 ****************************************************************************
00003 *
00004 * MODULE:       Vector library 
00005 *               
00006 * AUTHOR(S):    Original author CERL, probably Dave Gerdes or Mike Higgins.
00007 *               Update to GRASS 5.7 Radim Blazek and David D. Gray.
00008 *
00009 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00010 *
00011 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012 *
00013 *               This program is free software under the GNU General Public
00014 *               License (>=v2). Read the file COPYING that comes with GRASS
00015 *               for details.
00016 *
00017 *****************************************************************************/
00018 #include <grass/Vect.h>
00019 #include <stdlib.h>
00020 #include <grass/gis.h>
00021 
00022 /* ADD comment what and why a list */
00023 
00024 struct ilist *
00025 Vect_new_list (void)
00026 {
00027   struct ilist *p;
00028 
00029   p = (struct ilist *) malloc (sizeof (struct ilist));
00030 
00031   if (p) {
00032     p->value = NULL;
00033     p->n_values = 0;
00034     p->alloc_values = 0;
00035   }
00036   
00037   return p;
00038 }
00039 
00046 int 
00047 Vect_reset_list (struct ilist *list)
00048 {
00049   list->n_values = 0;
00050 
00051   return 0;
00052 }
00053 
00060 int 
00061 Vect_destroy_list (struct ilist *list)
00062 {
00063   if (list)                     /* probably a moot test */
00064     {
00065       if (list->alloc_values)
00066         {
00067           G_free ((void *) list->value);
00068         }
00069       G_free ((void *) list);
00070     }
00071   list = NULL;
00072 
00073   return 0;
00074 }
00075 
00082 int
00083 Vect_list_append ( struct ilist *list, int val )
00084 {
00085     int i;
00086     size_t size;
00087     
00088     if ( list == NULL ) 
00089         return 1;
00090         
00091     for ( i = 0; i < list->n_values; i++ ) {
00092         if ( val == list->value[i] )
00093             return 0;
00094     }
00095     
00096     if ( list->n_values == list->alloc_values ) {
00097                 size = (list->n_values + 1000) * sizeof(int);
00098         list->value = (int *) G_realloc ( (void *) list->value, size );
00099         list->alloc_values = list->n_values + 1000;
00100     }
00101     
00102     list->value[list->n_values] = val;
00103     list->n_values++;
00104   
00105     return 0;
00106 }
00107 
00114 int
00115 Vect_list_append_list ( struct ilist *alist,  struct ilist *blist )
00116 {
00117     int i;
00118     
00119     if ( alist == NULL || blist == NULL ) 
00120         return 1;
00121         
00122     for ( i = 0; i < blist->n_values; i++ ) 
00123         Vect_list_append ( alist, blist->value[i] );
00124     
00125     return 0;
00126 }
00127 
00134 int
00135 Vect_list_delete ( struct ilist *list, int val )
00136 {
00137     int i, j;
00138     
00139     if ( list == NULL ) 
00140         return 1;
00141         
00142     for ( i = 0; i < list->n_values; i++ ) {
00143         if ( val == list->value[i] ) {
00144             for ( j = i + 1; j < list->n_values; j++ ) 
00145                 list->value[j - 1] = list->value[j];
00146                 
00147             list->n_values--;
00148             return 0;
00149         }
00150     }
00151     
00152     return 0;
00153 }
00154 
00161 int
00162 Vect_list_delete_list ( struct ilist *alist,  struct ilist *blist )
00163 {
00164     int i;
00165     
00166     if ( alist == NULL || blist == NULL ) 
00167         return 1;
00168         
00169     for ( i = 0; i < blist->n_values; i++ ) 
00170         Vect_list_delete ( alist, blist->value[i] );
00171     
00172     return 0;
00173 }
00174 
00181 int
00182 Vect_val_in_list ( struct ilist *list, int val )
00183 {
00184     int i;
00185     
00186     if ( list == NULL ) 
00187         return 0;
00188         
00189     for ( i = 0; i < list->n_values; i++ ) {
00190         if ( val == list->value[i] )
00191             return 1;
00192     }
00193     
00194     return 0;
00195 }

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5