vector.c
Go to the documentation of this file.
1 
2 #include "vector.h"
3 
4 #include "defines.h"
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
18 /**************************************************************************/
19 inline boolean CmpID(void *a,void *b)
20 {
21  return(*((unsigned int *)a)==*((unsigned int *)b));
22 }
23 
24 inline void CopyID(void *a,void *b)
25 {
26  (*((unsigned int *)a)=*((unsigned int *)b));
27 }
28 
29 inline void DeleteID(void *a)
30 {
31 }
32 
33 /**************************************************************************/
34 inline boolean CmpDouble(void *a,void *b)
35 {
36  return(*((double *)a)==*((double *)b));
37 }
38 
39 inline void CopyDouble(void *a,void *b)
40 {
41  (*((double *)a)=*((double *)b));
42 }
43 
44 inline void DeleteDouble(void *a)
45 {
46 }
47 
48 /**************************************************************************/
49 inline void NewDoublePair(double f,double s,TDoublePair *p)
50 {
51  p->f=f;
52  p->s=s;
53 }
54 
55 inline double FirstInPair(TDoublePair *p)
56 {
57  return(p->f);
58 }
59 
60 inline double SecondInPair(TDoublePair *p)
61 {
62  return(p->s);
63 }
64 
65 inline boolean CmpDoublePair(void *a,void *b)
66 {
67  return((((TDoublePair *)a)->f==((TDoublePair *)b)->f)&&
68  (((TDoublePair *)a)->s==((TDoublePair *)b)->s));
69 }
70 
71 inline void CopyDoublePair(void *a,void *b)
72 {
73  ((TDoublePair *)a)->f=((TDoublePair *)b)->f;
74  ((TDoublePair *)a)->s=((TDoublePair *)b)->s;
75 }
76 
77 inline void DeleteDoublePair(void *a)
78 {
79 }
80 
81 /**************************************************************************/
82 inline boolean CmpVoidPtr(void *a,void *b)
83 {
84  return(*((void **)a)==*((void **)b));
85 }
86 
87 inline void CopyVoidPtr(void *a,void *b)
88 {
89  (*((void **)a)=*((void **)b));
90 }
91 
92 inline void DeleteVoidPtr(void *a)
93 {
94  /*The data actually pointed by the pointer stored in the vector
95  should be deleted by the caller*/
96 }
97 
98 /**************************************************************************/
99 
100 void InitVector(unsigned int ele_size,void (* Copy)(void *,void*),void (* Delete)(void *),unsigned int max_ele,Tvector *vector)
101 {
102  unsigned int i;
103 
104  vector->max_ele=max_ele;
105  vector->num_ele=0;
106  vector->ele_size=ele_size;
107  if (ele_size==0)
108  Error("The element size can not be zero in InitVector");
109  vector->Delete=Delete;
110  if (Copy==NULL)
111  Error("The copy operator can not be null in InitVector");
112  vector->Copy=Copy;
113 
114  NEW(vector->elements,vector->max_ele,void *);
115 
116  for(i=0;i<vector->max_ele;i++)
117  vector->elements[i]=NULL;
118 }
119 
120 void ResetVector(Tvector *vector)
121 {
122  unsigned int i;
123 
124  for(i=0;i<vector->num_ele;i++)
125  {
126  if (vector->elements[i]!=NULL)
127  {
128  if (vector->Delete!=NULL)
129  vector->Delete(vector->elements[i]);
130  free(vector->elements[i]);
131  vector->elements[i]=NULL;
132  }
133  }
134 
135  vector->num_ele=0;
136 }
137 
138 void CopyVector(Tvector *v_dst,Tvector *v_src)
139 {
140  unsigned int i;
141 
142  v_dst->max_ele=v_src->max_ele;
143  v_dst->num_ele=v_src->num_ele;
144  v_dst->ele_size=v_src->ele_size;
145  v_dst->Delete=v_src->Delete;
146  v_dst->Copy=v_src->Copy;
147 
148  NEW(v_dst->elements,v_dst->max_ele,void *);
149 
150  for(i=0;i<v_dst->max_ele;i++)
151  {
152  if (v_src->elements[i]==NULL)
153  v_dst->elements[i]=NULL;
154  else
155  {
156  NEW(v_dst->elements[i],v_dst->ele_size,char);
157  v_dst->Copy(v_dst->elements[i],v_src->elements[i]);
158  }
159  }
160 }
161 
162 unsigned int VectorFirstFreePos(Tvector *vector)
163 {
164  unsigned int k;
165 
166  k=0;
167  while((k<vector->num_ele)&&(vector->elements[k]!=NULL))
168  k++;
169 
170  return(k);
171 }
172 
173 unsigned int VectorSize(Tvector *vector)
174 {
175  return(vector->num_ele);
176 }
177 
178 unsigned int VectorMaxSize(Tvector *vector)
179 {
180  return(vector->max_ele);
181 }
182 
183 boolean VectorEmpty(Tvector *vector)
184 {
185  return(vector->num_ele==0);
186 }
187 
188 boolean ElementInVector(void *e,boolean (* cmp)(void *,void*),Tvector *vector)
189 {
190  return(FindPos(e,cmp,vector)!=((unsigned int)(-1)));
191 }
192 
193 unsigned int FindPos(void *e,boolean (* cmp)(void *,void*),Tvector *vector)
194 {
195  boolean found;
196  unsigned int i;
197 
198  found=FALSE;
199  i=0;
200  while ((!found)&&(i<vector->num_ele))
201  {
202  if (vector->elements[i]!=NULL)
203  {
204  if (cmp(e,vector->elements[i]))
205  found=TRUE;
206  else
207  i++;
208  }
209  }
210  if (found)
211  return(i);
212  else
213  return((unsigned int)(-1));
214 }
215 
216 unsigned int NewVectorElement(void *e,Tvector *vector)
217 {
218  unsigned int k;
219 
220  k=vector->num_ele;
221 
222  SetVectorElement(vector->num_ele,e,vector);
223 
224  return(k);
225 }
226 
227 unsigned int NewFreeVectorElement(void *e,Tvector *vector)
228 {
229  unsigned int k;
230 
231  k=VectorFirstFreePos(vector);
232 
233  SetVectorElement(k,e,vector);
234 
235  return(k);
236 }
237 
238 void SetVectorElement(unsigned int i,void *e,Tvector *vector)
239 {
240  if (i>=vector->num_ele)
241  {
242  vector->num_ele=i+1;
243  if (i>=vector->max_ele)
244  {
245  unsigned int k,j;
246 
247  k=vector->max_ele;
248  vector->max_ele=i*2;
249  MEM_EXPAND(vector->elements,vector->max_ele,void *);
250 
251  for(j=k;j<vector->max_ele;j++)
252  vector->elements[j]=NULL;
253  }
254  }
255 
256  if (vector->elements[i]==NULL) /* If element 'i' is still empty */
257  {
258  NEW(vector->elements[i],vector->ele_size,char); /* reserve memory */
259  }
260  else
261  {
262  /* We have some info in the vector we have to destroy before copying the new info */
263  if (vector->Delete!=NULL)
264  vector->Delete(vector->elements[i]);
265  }
266 
267  vector->Copy((void *)(vector->elements[i]),e); /* copy the user data in the reserved memory */
268 }
269 
270 void *GetVectorElement(unsigned int i,Tvector *vector)
271 {
272  if (i<vector->num_ele)
273  return(vector->elements[i]);
274  else
275  return(NULL);
276 }
277 
278 void *GetLastElement(Tvector *vector)
279 {
280  if (vector->num_ele==0)
281  return(NULL);
282  else
283  return(vector->elements[vector->num_ele-1]);
284 }
285 
286 void RemoveVectorElement(unsigned int i,Tvector *vector)
287 {
288  if ((i<vector->num_ele)&&(vector->elements[i]!=NULL))
289  {
290  if (vector->Delete!=NULL)
291  vector->Delete(vector->elements[i]);
292  free(vector->elements[i]);
293  vector->elements[i]=NULL;
294  if (i==vector->num_ele-1)
295  vector->num_ele--;
296  }
297 }
298 
299 void SwapVectorElements(unsigned int i,unsigned int j,Tvector *vector)
300 {
301  void *vi,*vj;
302 
303  if ((i<vector->num_ele)&&(j<vector->num_ele))
304  {
305  vi=vector->elements[i];
306  vj=vector->elements[j];
307  if ((vi!=NULL)&&(vj!=NULL))
308  {
309  vector->elements[i]=vj;
310  vector->elements[j]=vi;
311  }
312  else
313  Error("Swapping non-exiting elements in a vector.");
314  }
315  else
316  Error("Swapping non-exiting elements in a vector.");
317 }
318 
319 void ReverseVector(Tvector *vector)
320 {
321  int i,j;
322  void *vi,*vj;
323 
324  for(i=0,j=vector->num_ele-1;i<j;i++,j--)
325  {
326  vi=vector->elements[i];
327  vj=vector->elements[j];
328  vector->elements[i]=vj;
329  vector->elements[j]=vi;
330  }
331 }
332 
333 void ConcatVectors(Tvector *vector1,Tvector *vector)
334 {
335  unsigned int i;
336 
337  for(i=0;i<vector1->num_ele;i++)
338  NewVectorElement(vector1->elements[i],vector);
339 }
340 
341 
342 void ExtractVectorElement(unsigned int i,void *e,Tvector *vector)
343 {
344  void *vi;
345 
346  if (i<vector->num_ele)
347  {
348 
349  vi=(void *)(vector->elements[i]);
350  if (vi!=NULL)
351  {
352  vector->Copy(e,vi);
353  RemoveVectorElement(i,vector);
354  }
355  else
356  Error("Extracting a non-exiting elements in a vector.");
357  }
358  else
359  Error("Extracting a non-exiting elements in a vector.");
360 
361 }
362 
363 void MoveVectorElement(unsigned int i,unsigned int ni,Tvector *vector)
364 {
365  void *vi,*vn;
366 
367  if ((i<vector->num_ele)&&(ni<vector->num_ele))
368  {
369  if (i!=ni)
370  {
371  vi=(void *)(vector->elements[i]);
372  if (vi!=NULL)
373  {
374  vn=(void *)(vector->elements[ni]);
375  if (vn!=NULL)
376  RemoveVectorElement(ni,vector);
377 
378  vector->elements[ni]=vi;
379  vector->elements[i]=NULL;
380  }
381  else
382  Error("Moving a vector existing vector element.");
383  }
384  }
385  else
386  Error("Moving a vector element from/to a non existing position.");
387 }
388 
389 void DeleteVector(void *vector)
390 {
391  ResetVector((Tvector *)vector);
392  free(((Tvector *)vector)->elements);
393 }
unsigned int ele_size
Definition: vector.h:230
unsigned int NewFreeVectorElement(void *e, Tvector *vector)
Adds an element to the vector.
Definition: vector.c:227
void ResetVector(Tvector *vector)
Resets a vector.
Definition: vector.c:120
A generic vector.
Definition: vector.h:228
boolean CmpVoidPtr(void *a, void *b)
Comparison operator for void pointers.
Definition: vector.c:82
void(* Delete)(void *)
Definition: vector.h:234
void DeleteVoidPtr(void *a)
Destructor for void pointers.
Definition: vector.c:92
void CopyVoidPtr(void *a, void *b)
Copy constructor for void pointers.
Definition: vector.c:87
void MoveVectorElement(unsigned int i, unsigned int ni, Tvector *vector)
Moves a vector element from one position to another.
Definition: vector.c:363
boolean CmpID(void *a, void *b)
Comparison operator for identifiers.
Definition: vector.c:19
void * GetLastElement(Tvector *vector)
Returns a pointer to the last element of a vector.
Definition: vector.c:278
void CopyDoublePair(void *a, void *b)
Copy constructor for pairs of doubles.
Definition: vector.c:71
boolean VectorEmpty(Tvector *vector)
Checks if a vector is empty.
Definition: vector.c:183
void ReverseVector(Tvector *vector)
Reverses a vector.
Definition: vector.c:319
void DeleteDouble(void *a)
Destructor for doubles.
Definition: vector.c:44
#define FALSE
FALSE.
Definition: boolean.h:30
void DeleteDoublePair(void *a)
Destructor for pairs of doubles.
Definition: vector.c:77
void SwapVectorElements(unsigned int i, unsigned int j, Tvector *vector)
Swaps two elements in a vector.
Definition: vector.c:299
double FirstInPair(TDoublePair *p)
The first element of a pair.
Definition: vector.c:55
#define TRUE
TRUE.
Definition: boolean.h:21
void CopyID(void *a, void *b)
Copy constructor for identifiers.
Definition: vector.c:24
void Error(const char *s)
General error function.
Definition: error.c:108
Definitions of constants and macros used in several parts of the cuik library.
void CopyVector(Tvector *v_dst, Tvector *v_src)
Copy constructor.
Definition: vector.c:138
boolean ElementInVector(void *e, boolean(*cmp)(void *, void *), Tvector *vector)
Search for an element in a vector.
Definition: vector.c:188
double SecondInPair(TDoublePair *p)
The second element of a pair.
Definition: vector.c:60
void(* Copy)(void *, void *)
Definition: vector.h:233
void InitVector(unsigned int ele_size, void(*Copy)(void *, void *), void(*Delete)(void *), unsigned int max_ele, Tvector *vector)
Constructor.
Definition: vector.c:100
unsigned int max_ele
Definition: vector.h:232
void DeleteID(void *a)
Destructor for identifiers.
Definition: vector.c:29
void ** elements
Definition: vector.h:229
void ExtractVectorElement(unsigned int i, void *e, Tvector *vector)
Extracts an element from a vector.
Definition: vector.c:342
unsigned int VectorFirstFreePos(Tvector *vector)
Finds a free position in the vector.
Definition: vector.c:162
void SetVectorElement(unsigned int i, void *e, Tvector *vector)
Adds an element to the vector in a given position.
Definition: vector.c:238
unsigned int NewVectorElement(void *e, Tvector *vector)
Adds an element to the vector.
Definition: vector.c:216
void ConcatVectors(Tvector *vector1, Tvector *vector)
Concatenates two vectors.
Definition: vector.c:333
void DeleteVector(void *vector)
Destructor.
Definition: vector.c:389
Definition of the Tvector type and the associated functions.
double s
Definition: vector.h:116
unsigned int VectorMaxSize(Tvector *vector)
Gets the maximum number of elements in a vector.
Definition: vector.c:178
unsigned int num_ele
Definition: vector.h:231
void CopyDouble(void *a, void *b)
Copy constructor for doubles.
Definition: vector.c:39
void NewDoublePair(double f, double s, TDoublePair *p)
Constructor.
Definition: vector.c:49
#define MEM_EXPAND(_var, _n, _type)
Expands a previously allocated memory space.
Definition: defines.h:272
boolean CmpDouble(void *a, void *b)
Comparison operator for doubles.
Definition: vector.c:34
double f
Definition: vector.h:115
unsigned int FindPos(void *e, boolean(*cmp)(void *, void *), Tvector *vector)
Search for an element in a vector.
Definition: vector.c:193
A pair of dubles.
Definition: vector.h:114
boolean CmpDoublePair(void *a, void *b)
Comparison operator for paris of doubles.
Definition: vector.c:65
void * GetVectorElement(unsigned int i, Tvector *vector)
Returns a pointer to a vector element.
Definition: vector.c:270
unsigned int VectorSize(Tvector *vector)
Gets the number of elements in a vector.
Definition: vector.c:173
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:253
void RemoveVectorElement(unsigned int i, Tvector *vector)
Removes an element from the vector.
Definition: vector.c:286