FreeWRL/FreeX3D  3.0.0
Vector.h
1 /*
2 Vector.h
3 General purpose containers - vector and stack (implemented on top of it)
4 */
5 
6 /****************************************************************************
7  This file is part of the FreeWRL/FreeX3D Distribution.
8 
9  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
10 
11  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
12  it under the terms of the GNU Lesser Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
23 ****************************************************************************/
24 
25 
26 #ifndef VECTOR_H
27 #define VECTOR_H
28 
29 #include <stddef.h>
30 
31 /* ************************************************************************** */
32 /* ******************************** Vector ********************************** */
33 /* ************************************************************************** */
34 
35 /* This is the vector structure. */
36 struct Vector
37 {
38  int n;
39  int allocn;
40  void* data;
41 };
42 
43 /* Constructor/destructor */
44 struct Vector* newVector_(int elSize, int initSize,char *,int);
45 #define newVector(type, initSize) \
46  newVector_((int)sizeof(type), initSize,__FILE__,__LINE__)
47 
48 #if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
49  void deleteVectorDebug_(char *file, int line, int elSize, struct Vector**);
50  #define deleteVector(type, me) deleteVectorDebug_(__FILE__,__LINE__,(int)sizeof(type), &(me))
51 #else
52  void deleteVector_(int elSize, struct Vector**);
53  #define deleteVector(type, me) deleteVector_((int)sizeof(type), ((struct Vector**)(&me)))
54 #endif
55 
56 /* Ensures there's at least one space free. */
57 void vector_ensureSpace_(int, struct Vector*, char *fi, int line);
58 
59 /* Element retrieval. */
60 #define vector_get(type, me, ind) \
61  ((type*)((struct Vector*)me)->data)[ind]
62 
63 /* pointer to element retrieval */
64 #define vector_get_ptr(type, me, ind) \
65  &((type*)((struct Vector*)me)->data)[ind]
66 
67 
68 /* Element set. */
69 #define vector_set(type,me,element,value) \
70  ((type*)((struct Vector *) (me))->data)[element]=value
71 
72 void vector_removeElement(int elSize,struct Vector* myp, int element);
73 #define vector_remove_elem(type,me,element) \
74  vector_removeElement((int)sizeof(type),me,element)
75 
76 /* Size of vector */
77 #define vectorSize(me) \
78  (((struct Vector*)me)->n)
79 
80 /* Back of a vector */
81 #define vector_back(type, me) \
82  vector_get(type, me, vectorSize(me)-1)
83 
84 /* Is the vector empty? */
85 #define vector_empty(me) \
86  (!vectorSize(me))
87 
88 /* Shrink the vector to minimum required space. */
89 void vector_shrink_(int, struct Vector*);
90 #define vector_shrink(type, me) \
91  vector_shrink_((int)sizeof(type), me)
92 
93 /* Push back operation. */
94 #define vector_pushBack(type, me, el) \
95  { \
96  vector_ensureSpace_((int)sizeof(type), me,__FILE__,__LINE__); \
97  ASSERT(((struct Vector*)me)->n<((struct Vector*)me)->allocn); \
98  vector_get(type, me, ((struct Vector*)me)->n)=el; \
99  ++((struct Vector*)me)->n; \
100  }
101 
102 /* Pop back operation */
103 void vector_popBack_(struct Vector*, size_t count);
104 #define vector_popBack(type, me) \
105  { \
106  ASSERT(!vector_empty(me)); \
107  --((struct Vector*)me)->n; \
108  }
109 #define vector_popBackN(type, me, popn) \
110  { \
111  ASSERT(popn<=vectorSize(me)); \
112  ((struct Vector*)me)->n-=popn; \
113  }
114 
115 /* Release and get vector data. */
116 void* vector_releaseData_(int, struct Vector*);
117 #define vector_releaseData(type, me) \
118  vector_releaseData_((int)sizeof(type), me)
119 
120 /* ************************************************************************** */
121 /* ************************************ Stack ******************************* */
122 /* ************************************************************************** */
123 
124 /* A stack is essentially a vector */
125 typedef struct Vector Stack;
126 
127 /* Constructor and destructor */
128 #define newStack(type) \
129  newVector(type, 4)
130 #define deleteStack(type, me) \
131  deleteVector(type, me)
132 
133 /* Push and pop */
134 #define stack_push(type, me, el) \
135  vector_pushBack(type, me, el)
136 #define stack_pop(type, me) \
137  vector_popBack(type, me)
138 
139 /* Top of stack */
140 #define stack_top(type, me) \
141  vector_get(type, me, vectorSize(me)-1)
142 
143 /* Is the stack empty? */
144 #define stack_empty(me) \
145  vector_empty(me)
146 
147 /* tie assert in here to give better failure methodology */
148 /* #define ASSERT(cond) if(!(cond)){fw_assert(__FILE__,__LINE__);} */
149 /* void fw_assert(char *,int); */
150 
151 #endif /* Once-check */
Definition: Vector.h:36