This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
vector.h
279 lines (264 loc) · 8.8 KB
/
vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ14
*
* Repository:
* https://github.com/Dasio/IFJ
*
* Team:
* Dávid Mikuš (xmikus15)
* Peter Hostačný (xhosta03)
* Tomáš Kello (xkello00)
* Adam Lučanský (xlucan01)
* Michaela Lukášová (xlukas09)
*/
// #include "stack.h"
#ifndef VECTOR_H
#define VECTOR_H
#include "system.h"
#include "error.h"
/*
____ == type of vector
--------------------------------------
* GenVectorPrototypes(type)
* return pointer for work with structures or values
*
* ____VectorInit(initial_size);
*
* ____VectorAppend(*Vect, Value);
* ____VectorPushMore(*Vect, Number);
* ____VectorPop(*Vect);
* ____VectorPopMore(*Vect, Number);
*
* ____VectorAtSet(*Vect, Index, Value)
* ____VectorAt(*Vect, Index);
*
* ____VectorFirst(*Vect);
* ____VectorLast(*Vect);
*
* ____VectorFree(*Vect);
*
*
* GenVectorPrototypesValues(type)
* return ONLY values if structure is int, double, float...
*
* ____VectorPopValue(*Vect);
* ____VectorFirstValue(*Vect);
* ____VectorLastValue(*Vect);
*
*/
#define VECTOR_DEFAULT_SIZE 8
struct Vector{
/** Allocated elements, including uninitialized */
int64_t capacity;
/** Used elements */
int64_t used;
/** Pointer to beginning of array */
void *array;
};
/** if function failed, NULL poiter (or FALSE) is returned */
#define GenVectorPrototypes(type) \
typedef struct Vector type##Vector; \
\
\
/** Initialized vector with initial_size*/ \
/** if zero, uses default (8) */ \
/** error if malloc failed: return NULL */ \
type##Vector * type##VectorInit(int64_t initial_size); \
\
/** Add Value to end of vector - ERR_Allocation */ \
/** error if realloc failed: return 0 */ \
bool type##VectorAppend(type##Vector *Vect, type Value); \
\
/** Reserve space in Vector */ \
/** error if malloc failed: return 0 */ \
bool type##VectorPushMore(type##Vector *Vect, int64_t); \
\
/** Return pointer to POP Value */ \
/** error if empty: return NULL */ \
type *type##VectorPop(type##Vector *Vect); \
\
/** delete more items */ \
/** error if not enought values: return NULL & set error */ \
type *type##VectorPopMore(type##Vector *Vect, int64_t); \
\
/** delete more items */ \
/** error if not enought values: return NULL */ \
void type##VectorAtSet(type##Vector *Vect, int64_t, type); \
\
/** delete more items */ \
/** error if not enought values: return NULL */ \
type *type##VectorAt(type##Vector *Vect, int64_t); \
\
/** Return pointer to first Value */ \
/** error if empty: return NULL */ \
type *type##VectorFirst(type##Vector *Vect); \
\
/** Return pointer to Last Value */ \
/** error if empty: return NULL */ \
type *type##VectorLast(type##Vector *Vect); \
\
/** Free array from Vector */ \
void type##VectorFree(type##Vector *Vect);
/** Important to chack ERRORs if value is valid */
#define GenVectorPrototypesValues(type) \
\
/** Return index Value */ \
/** error if empty: return 0 + setError */ \
type type##VectorAtValue(type##Vector *Vect, int64_t); \
\
/** Return last Value */ \
/** error if empty: return 0 + setError */ \
type type##VectorLastValue(type##Vector *Vect); \
\
/** Return first Value */ \
/** error if empty: return 0 + setError */ \
type type##VectorFirstValue(type##Vector *Vect); \
\
/** Return POP Value */ \
/** error if empty: return 0 + setError */ \
type type##VectorPopValue(type##Vector *Vect);
/**
* Generates functions for appropriate type.
* Moved to vector.c, used only for generating
* within same module.
* @param type Type for generalization
*
* GenVectorFunctions(int)
* for vector of structure and pointers
* returns ONLY pointers
*
* GenVectorFunctionsValues(int)
* added functions which return directly values
*
*/
#define GenVectorFunctions(type) \
type##Vector * type##VectorInit(int64_t initial_size) { \
if(initial_size == 0) \
initial_size = VECTOR_DEFAULT_SIZE; \
type##Vector *Vect = malloc(sizeof(type##Vector)); \
if (Vect == NULL) \
return NULL; \
Vect->used = 0; \
Vect->capacity = initial_size; \
Vect->array = malloc(Vect->capacity * sizeof(type)); \
if (Vect->array == NULL) \
{ \
free(Vect); \
return NULL; \
} \
return Vect; \
} \
bool type##VectorAppend(type##Vector *Vect, type Value) { \
if(Vect->capacity == Vect->used) \
{ \
Vect->capacity *= 2; \
Vect->array = realloc(Vect->array, Vect->capacity * sizeof(type)); \
if (Vect->array == NULL) \
return 0; \
} \
/* tmp - pointer on last Value */ \
type *tmp; \
tmp = (type*)Vect->array + Vect->used; \
*tmp = Value; \
Vect->used++; \
return 1; \
} \
bool type##VectorPushMore(type##Vector *Vect, int64_t number) { \
while(Vect->capacity < Vect->used+number) \
{ \
Vect->capacity *= 2; \
Vect->array = realloc(Vect->array, Vect->capacity * sizeof(type)); \
if (Vect->array == NULL) return 0; \
} \
memset((void*)((type*)Vect->array + Vect->used), 0, number*sizeof(type)); \
/* Set used values */ \
Vect->used += number; \
return 1; \
} \
type *type##VectorPop(type##Vector *Vect) { \
if(Vect->used < 1) return NULL; \
Vect->used--; /* Mark last value as invalid and return its address*/\
return (type*)Vect->array + Vect->used; \
} \
type *type##VectorPopMore(type##Vector *Vect, int64_t number) { \
if(Vect->used < number) { setError(ERR_OutOfRange) return NULL; } \
Vect->used -= number; /* Mark last X value as invalid and return last address*/ \
return (type*)Vect->array + Vect->used; \
} \
void type##VectorAtSet(type##Vector *Vect, int64_t index, type value) { \
if(index >= Vect->used) \
{ \
if(index >= Vect->capacity) \
{ \
while(index >= Vect->capacity) \
Vect->capacity *= 2; \
Vect->array = realloc(Vect->array, Vect->capacity * sizeof(type)); \
} \
Vect->used = index +1; \
} \
type *tmp; \
tmp = (type*)Vect->array + index; \
*tmp = value; \
} \
type *type##VectorAt(type##Vector *Vect, int64_t index) { \
if(index >= Vect->used) return NULL; \
return (type*)Vect->array + index; \
} \
type *type##VectorFirst(type##Vector *Vect){ \
if(Vect->used == 0) return NULL; \
return Vect->array; \
} \
type *type##VectorLast(type##Vector *Vect) { \
if(Vect->used == 0) return NULL; \
return (type*)Vect->array+ (Vect->used-1); \
} \
void type##VectorFree(type##Vector *Vect) { \
if(Vect == NULL) return; \
if(Vect->array) free(Vect->array); \
free(Vect); \
Vect = NULL; \
}
#define GenVectorFunctionsValues(type) \
type type##VectorPopValue(type##Vector *Vect) { \
if(Vect->used < 1) \
{ \
setError(ERR_OutOfRange); \
return (type)0; \
} \
Vect->used--; /* Mark last value as invalid and return its address*/\
type *r = (type*)Vect->array + Vect->used; \
return *r; \
} \
type type##VectorAtValue(type##Vector *Vect, int64_t index) { \
if(index >= Vect->used) \
{ \
setError(ERR_OutOfRange); \
return (type)0; \
} \
type *r = (type*)Vect->array + index; \
return *r; \
} \
type type##VectorFirstValue(type##Vector *Vect){ \
if(Vect->used == 0) \
{ \
setError(ERR_OutOfRange); \
return (type)0; \
} \
type *r = Vect->array; \
return *r; \
} \
type type##VectorLastValue(type##Vector *Vect) { \
if(Vect->used == 0) \
{ \
setError(ERR_OutOfRange); \
return (type)0; \
} \
type *r = (type*)Vect->array+ (Vect->used-1); \
return *r; \
}
GenVectorPrototypes(int)
GenVectorPrototypesValues(int)
GenVectorPrototypes(int64_t)
GenVectorPrototypesValues(int64_t)
#endif // VECTOR_H