-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrayMacros.h
121 lines (103 loc) · 2.43 KB
/
arrayMacros.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
/*
Dynamic array manager
with garbage collection
*/
typedef struct arrayHeader {
int size; /* Total size of array */
int blck; /* Amount to allocate when need be */
int maxId; /* Index of last used slot in array */
int avlId; /* First available slot in array */
void *vecPtr; /* Points to actual array */
} arrayHeader;
#include "allocMacros.h"
/*
Initialize the header without allocating
*/
#define arrayInit(Header,block) { \
(Header).size= 0; \
(Header).blck= (block); \
(Header).maxId= -1; \
(Header).avlId= 0; \
(Header).vecPtr= 0; \
}
/*
Given an index id,
return the corresponding structure variable
*/
#define arrayGet(Header,type,id) \
((type *)(Header).vecPtr)[(id)]
/*
Put data into array at given id location
Allocate or Reallocate space if necessary
id must be less or equal to header's size
*/
#define arrayPut(Header,id,data,type) { \
if ( ((id)==(Header).size) ) { \
int new_size; \
new_size= (Header).size + (Header).blck; \
if ((id)==0) { \
myCalloc((Header).vecPtr,type,new_size,sizeof(type)); \
} \
else { \
myRealloc((Header).vecPtr,type,new_size*sizeof(type)); \
} \
(Header).size= new_size; \
} \
((type *)(Header).vecPtr)[(id)]= (data); \
if ( (id)>(Header).maxId ) { \
(Header).maxId= (id); \
} \
}
/*
Put data into next available spot
Allocate or Reallocate space if necessary
member is an int member of the structure type
id indicates where (in array) the data was put
*/
#define arrayPutNext(Header,data,type,member,id) { \
int new_id; \
id= (Header).avlId; \
if ( id==((Header).maxId)+1 ) { \
((Header).avlId)++; \
} \
else { \
new_id= -(((type *)(Header).vecPtr)[(id)].member)-1; \
(Header).avlId= new_id; \
} \
arrayPut(Header,id,data,type); \
}
/*
Get current max index
*/
#define arrayMaxId(Header) \
(Header).maxId
/*
Delete an indexed entry in the array
member is an int member of the structure type
*/
#define arrayDel(Header,id,type,member) { \
((type *)(Header).vecPtr)[(id)].member= \
-((Header).avlId+1); \
(Header).avlId= (id); \
}
/*
Delete all space occupied by the array
and reinitialize header fields
*/
#define arrayDelAll(Header) { \
if ( (Header).vecPtr != 0 ) { \
myFree( (Header).vecPtr ); \
(Header).vecPtr= 0; \
} \
(Header).size= 0; \
(Header).maxId= -1; \
(Header).avlId= 0; \
}
/*
Reset the array
without deleting the vector
*/
#define arrayReset(Header) { \
(Header).maxId = -1; \
(Header).avlId= 0; \
}