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 "Vect.h"
00019 #include <stdlib.h>
00020 #include "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, size;
00086     
00087     if ( list == NULL ) 
00088         return 1;
00089         
00090     for ( i = 0; i < list->n_values; i++ ) {
00091         if ( val == list->value[i] )
00092             return 0;
00093     }
00094     
00095     if ( list->n_values == list->alloc_values ) {
00096                 size = (list->n_values + 1000) * sizeof(int);
00097         list->value = (int *) G_realloc ( (void *) list->value, size );
00098         list->alloc_values = list->n_values + 1000;
00099     }
00100     
00101     list->value[list->n_values] = val;
00102     list->n_values++;
00103   
00104     return 0;
00105 }
00106 
00113 int
00114 Vect_list_append_list ( struct ilist *alist,  struct ilist *blist )
00115 {
00116     int i;
00117     
00118     if ( alist == NULL || blist == NULL ) 
00119         return 1;
00120         
00121     for ( i = 0; i < blist->n_values; i++ ) 
00122         Vect_list_append ( alist, blist->value[i] );
00123     
00124     return 0;
00125 }
00126 
00133 int
00134 Vect_list_delete ( struct ilist *list, int val )
00135 {
00136     int i, j;
00137     
00138     if ( list == NULL ) 
00139         return 1;
00140         
00141     for ( i = 0; i < list->n_values; i++ ) {
00142         if ( val == list->value[i] ) {
00143             for ( j = i + 1; j < list->n_values; j++ ) 
00144                 list->value[j - 1] = list->value[j];
00145                 
00146             list->n_values--;
00147             return 0;
00148         }
00149     }
00150     
00151     return 0;
00152 }
00153 
00160 int
00161 Vect_list_delete_list ( struct ilist *alist,  struct ilist *blist )
00162 {
00163     int i;
00164     
00165     if ( alist == NULL || blist == NULL ) 
00166         return 1;
00167         
00168     for ( i = 0; i < blist->n_values; i++ ) 
00169         Vect_list_delete ( alist, blist->value[i] );
00170     
00171     return 0;
00172 }
00173 
00180 int
00181 Vect_val_in_list ( struct ilist *list, int val )
00182 {
00183     int i;
00184     
00185     if ( list == NULL ) 
00186         return 0;
00187         
00188     for ( i = 0; i < list->n_values; i++ ) {
00189         if ( val == list->value[i] )
00190             return 1;
00191     }
00192     
00193     return 0;
00194 }

Generated on Mon Jan 1 19:49:16 2007 for GRASS by  doxygen 1.5.1