pcb 4.1.1
An interactive printed circuit board layout editor.

vector.c

Go to the documentation of this file.
00001 
00040 #ifdef HAVE_CONFIG_H
00041 #include "config.h"
00042 #endif
00043 
00044 #include <assert.h>
00045 #include <stdlib.h>
00046 #ifdef HAVE_STRING_H
00047 #include <string.h>
00048 #endif
00049 
00050 #include "global.h"
00051 #include "vector.h"
00052 
00053 #ifdef HAVE_LIBDMALLOC
00054 #include <dmalloc.h>
00055 #endif
00056 
00057 /* ---------------------------------------------------------------------------
00058  * some local prototypes
00059  */
00060 
00061 /* ---------------------------------------------------------------------------
00062  * some local types
00063  */
00064 struct vector_struct
00065 {
00066   vector_element_t *element;
00067   int size, max;
00068 };
00069 
00070 /* ---------------------------------------------------------------------------
00071  * some local identifiers
00072  */
00073 
00074 /* ---------------------------------------------------------------------------
00075  * functions.
00076  */
00077 
00078 #ifndef NDEBUG
00079 
00082 static int
00083 __vector_is_good (vector_t * vector)
00084 {
00085   return vector && (vector->max == 0 || vector->element) &&
00086     (vector->max >= 0) && (vector->size >= 0) &&
00087     (vector->size <= vector->max) && 1;
00088 }
00089 #endif /* !NDEBUG */
00090 
00094 vector_t *
00095 vector_create ()
00096 {
00097   vector_t *vector;
00098   /* okay, create empty vector */
00099   vector = (vector_t *)calloc (1, sizeof (*vector));
00100   assert (vector);
00101   assert (__vector_is_good (vector));
00102   return vector;
00103 }
00104 
00108 void
00109 vector_destroy (vector_t ** vector)
00110 {
00111   assert (vector && *vector);
00112   assert (__vector_is_good (*vector));
00113   if ((*vector)->element)
00114     free ((*vector)->element);
00115   free (*vector);
00116   *vector = NULL;
00117 }
00118 
00119 /* -- interrogation -- */
00120 int
00121 vector_is_empty (vector_t * vector)
00122 {
00123   assert (__vector_is_good (vector));
00124   return (vector->size == 0);
00125 }
00126 
00127 int
00128 vector_size (vector_t * vector)
00129 {
00130   assert (__vector_is_good (vector));
00131   return (vector->size);
00132 }
00133 
00134 vector_element_t
00135 vector_element (vector_t * vector, int N)
00136 {
00137   assert (__vector_is_good (vector));
00138   assert (N < vector->size);
00139   return vector->element[N];
00140 }
00141 
00145 vector_element_t
00146 vector_element_first (vector_t * vector)
00147 {
00148   assert (__vector_is_good (vector));
00149   assert (vector->size > 0);
00150   return vector_element (vector, 0);
00151 }
00152 
00156 vector_element_t
00157 vector_element_last (vector_t * vector)
00158 {
00159   assert (__vector_is_good (vector));
00160   assert (vector->size > 0);
00161   return vector_element (vector, vector->size - 1);
00162 }
00163 
00164 /* -- mutation -- */
00168 void
00169 vector_append (vector_t * vector, vector_element_t data)
00170 {
00171   vector_insert_many (vector, vector->size, &data, 1);
00172 }
00173 
00177 void
00178 vector_append_many (vector_t * vector, vector_element_t data[], int count)
00179 {
00180   vector_insert_many (vector, vector->size, data, count);
00181 }
00182 
00186 void
00187 vector_append_vector (vector_t * vector, vector_t * other_vector)
00188 {
00189   vector_append_many (vector, other_vector->element, other_vector->size);
00190 }
00191 
00195 void
00196 vector_insert (vector_t * vector, int N, vector_element_t data)
00197 {
00198   vector_insert_many (vector, N, &data, 1);
00199 }
00200 
00204 void
00205 vector_insert_many (vector_t * vector, int N,
00206                     vector_element_t data[], int count)
00207 {
00208   assert (__vector_is_good (vector));
00209   assert (N <= vector->size);
00210   if (count == 0)
00211     return;
00212   assert (data && count > 0);
00213   if (vector->size + count > vector->max)
00214     {
00215       vector->max = MAX (32, MAX (vector->size + count, vector->max * 2));
00216       vector->element = (void **)realloc (vector->element,
00217                                  vector->max * sizeof (*vector->element));
00218     }
00219   memmove (vector->element + N + count, vector->element + N,
00220            (vector->size - N) * sizeof (*vector->element));
00221   memmove (vector->element + N, data, count * sizeof (*data));
00222   vector->size += count;
00223   assert (__vector_is_good (vector));
00224 }
00225 
00229 vector_t *
00230 vector_duplicate (vector_t * orig)
00231 {
00232   vector_t * newone = vector_create();
00233   if (!orig)
00234     return newone;
00235   newone->element = (void **)malloc (orig->max * sizeof (*orig->element));
00236   newone->max = orig->max;
00237   newone->size = orig->size;
00238   memcpy (newone->element, orig->element, orig->size * sizeof (vector_element_t));
00239   assert (__vector_is_good (newone));
00240   return newone;
00241 }
00242 
00246 vector_element_t
00247 vector_remove_last (vector_t * vector)
00248 {
00249   assert (vector->size > 0);
00250   return vector_remove (vector, vector->size - 1);
00251 }
00252 
00256 vector_element_t
00257 vector_remove (vector_t * vector, int N)
00258 {
00259   vector_element_t old;
00260   assert (__vector_is_good (vector));
00261   assert (N < vector->size);
00262   old = vector->element[N];
00263   memmove (vector->element + N, vector->element + N + 1,
00264            (vector->size - (N + 1)) * sizeof (*vector->element));
00265   vector->size--;
00266   assert (__vector_is_good (vector));
00267   return old;
00268 }
00269 
00276 vector_element_t
00277 vector_replace (vector_t * vector, vector_element_t data, int N)
00278 {
00279   vector_element_t old;
00280   assert (__vector_is_good (vector));
00281   assert (N < vector->size);
00282   old = vector->element[N];
00283   vector->element[N] = data;
00284   assert (__vector_is_good (vector));
00285   return old;
00286 }