-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorgen.c
309 lines (267 loc) · 9.46 KB
/
vectorgen.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "containers.h"
#include "ccl_internal.h"
#include "vectorgen.h"
/* Number of elements by default */
#ifndef DEFAULT_START_SIZE
#define DEFAULT_START_SIZE 20
#endif
static VECTOR_TYPE *SetVTable(VECTOR_TYPE *result);
static int NullPtrError(const char *fnName)
{
char buf[512];
snprintf(buf,sizeof(buf),"iVector.%s",fnName);
return iError.NullPtrError(buf);
}
static int Add(VECTOR_TYPE * l, const DATA_TYPE elem)
{
return iVector.Add((Vector *)l,&elem);
}
static VECTOR_TYPE *GetRange(const VECTOR_TYPE *AL, size_t start,size_t end)
{
VECTOR_TYPE *result=(VECTOR_TYPE *)iVector.GetRange((Vector *)AL,start,end);
return SetVTable(result);
}
static int Contains(const VECTOR_TYPE * l, const DATA_TYPE data,void *ExtraArgs)
{
size_t idx;
return (iVector.IndexOf((Vector *)l, &data, ExtraArgs, &idx) < 0) ? 0 : 1;
}
static VECTOR_TYPE *Copy(const VECTOR_TYPE * l)
{
VECTOR_TYPE *result = (VECTOR_TYPE *)iVector.Copy((Vector *)l);
return SetVTable(result);
}
static void **CopyTo(const VECTOR_TYPE *AL)
{
return iVector.CopyTo((Vector *)AL);
}
static int IndexOf(const VECTOR_TYPE *AL,const DATA_TYPE data,void *ExtraArgs,size_t *result)
{
return iVector.IndexOf((Vector *)AL,&data,ExtraArgs,result);
}
static int InsertAt(VECTOR_TYPE *AL,size_t idx,DATA_TYPE newval)
{
return iVector.InsertAt((Vector *)AL, idx,&newval);;
}
static int Insert(VECTOR_TYPE *AL,DATA_TYPE newval)
{
return iVector.InsertAt((Vector *)AL,0,&newval);
}
static int Erase(VECTOR_TYPE *AL, DATA_TYPE elem)
{
return iVector.Erase((Vector *)AL, &elem);
}
static int EraseAll(VECTOR_TYPE *AL, DATA_TYPE elem)
{
return iVector.EraseAll((Vector *)AL, &elem);;
}
static int PushBack(VECTOR_TYPE *AL,const DATA_TYPE data)
{
return iVector.PushBack((Vector *)AL,&data);
}
static int PopBack(VECTOR_TYPE *AL,DATA_TYPE result)
{
return iVector.PushBack((Vector *)AL,&result);;
}
/*------------------------------------------------------------------------
Procedure: ReplaceAt ID:1
Purpose: Replace the data at the given position with new
data.
Input: The list and the new data
Output: Returns the new data if OK, NULL if error.
Errors: Index error or read only errors are caught
------------------------------------------------------------------------*/
static int ReplaceAt(VECTOR_TYPE *AL,size_t idx,DATA_TYPE newval)
{
return iVector.ReplaceAt((Vector *)AL,idx,&newval);
}
static size_t Sizeof(const VECTOR_TYPE *AL)
{
if (AL == NULL)
return sizeof(VECTOR_TYPE);
return sizeof(VECTOR_TYPE) + (AL->count * sizeof(DATA_TYPE));
}
static int Sort(VECTOR_TYPE *AL)
{
CompareInfo ci;
if (AL == NULL) {
return NullPtrError("Sort");
}
ci.ContainerLeft = AL;
ci.ExtraArgs = NULL;
qsortEx(AL->contents,AL->count,AL->ElementSize,AL->CompareFn,&ci);
return 1;
}
/* ------------------------------------------------------------------------------ */
/* Iterators */
/* ------------------------------------------------------------------------------ */
static int ReplaceWithIterator(Iterator * it, DATA_TYPE data, int direction)
{
struct ITERATOR(DATA_TYPE) *iter = (struct ITERATOR(DATA_TYPE) *)it;
return iter->VectorReplace(it,&data,direction);
}
static Iterator *SetupIteratorVTable(struct ITERATOR(DATA_TYPE) *result)
{
if (result == NULL) return NULL;
result->VectorReplace = result->it.Replace;
result->it.Replace = (int (*)(Iterator * , void * , int ))ReplaceWithIterator;
return &result->it;
}
static Iterator *NewIterator(VECTOR_TYPE * L)
{
return SetupIteratorVTable((struct ITERATOR(DATA_TYPE) *)iVector.NewIterator((Vector *)L));
}
static VECTOR_TYPE *Load(FILE * stream, ReadFunction loadFn, void *arg)
{
VECTOR_TYPE *result = (VECTOR_TYPE *)iVector.Load(stream,loadFn,arg);
return SetVTable(result);
}
static int InitIterator(VECTOR_TYPE * L, void *r)
{
iVector.InitIterator((Vector *)L,r);
SetupIteratorVTable(r);
return 1;
}
static VECTOR_TYPE *SetVTable(VECTOR_TYPE *result)
{
static int Initialized;
INTERFACE(DATA_TYPE) *intface = &INTERFACE_NAME(DATA_TYPE);
result->VTable = intface;
if (Initialized) return result;
Initialized = 1;
intface->Size = (size_t (*)(const VECTOR_TYPE *))iVector.Size;
intface->SetFlags = (unsigned (*)(VECTOR_TYPE * l, unsigned newval))iVector.SetFlags;
intface->GetFlags = (unsigned (*)(const VECTOR_TYPE *))iVector.GetFlags;
intface->GetCapacity = (size_t (*)(const VECTOR_TYPE *))iVector.GetCapacity;
intface->SetCapacity = (int (*)(VECTOR_TYPE *, size_t))iVector.SetCapacity;
intface->Clear = (int (*)(VECTOR_TYPE *))iVector.Clear;
intface->Finalize = (int (*)(VECTOR_TYPE *))iVector.Finalize;
intface->Apply = (int (*)(VECTOR_TYPE *, int (Applyfn) (DATA_TYPE *, void * ), void *))iVector.Apply;
intface->SetErrorFunction = (ErrorFunction (*)(VECTOR_TYPE *, ErrorFunction))iVector.SetErrorFunction;
intface->Resize = (int (*)(VECTOR_TYPE *,size_t))iVector.Resize;
intface->Save = (int (*)(const VECTOR_TYPE *, FILE *, SaveFunction, void *))iVector.Save;
intface->IndexIn = (VECTOR_TYPE *(*)(VECTOR_TYPE *,const Vector *))iVector.IndexIn;
intface->EraseAt = (int (*)(VECTOR_TYPE *AL,size_t idx))iVector.EraseAt;
intface->InsertIn = (int (*)(VECTOR_TYPE *, size_t,VECTOR_TYPE *))iVector.InsertIn;
intface->Append = (int (*)(VECTOR_TYPE *, VECTOR_TYPE *))iVector.Append;
intface->RemoveRange = (int (*)(VECTOR_TYPE *,size_t,size_t))iVector.RemoveRange;
intface->AddRange = (int (*)(VECTOR_TYPE *,size_t,const DATA_TYPE *))iVector.AddRange;
intface->GetAllocator = (const ContainerAllocator * (*)(const VECTOR_TYPE *))iVector.GetAllocator;
intface->Mismatch = (int (*)(VECTOR_TYPE *a1, VECTOR_TYPE *a2,size_t *mismatch))iVector.Mismatch;
intface->SetCompareFunction = (CompareFunction (*)(VECTOR_TYPE *,CompareFunction))iVector.SetCompareFunction;
intface->SelectCopy = (VECTOR_TYPE *(*)(VECTOR_TYPE *,Mask *))iVector.SelectCopy;
intface->CopyElement = (int (*)(const VECTOR_TYPE *,size_t,DATA_TYPE *))iVector.CopyElement;
intface->Reverse = (int (*)(VECTOR_TYPE *))iVector.Reverse;
intface->SetDestructor = (DestructorFunction (*)(VECTOR_TYPE *,DestructorFunction))iVector.SetDestructor;
intface->SearchWithKey = (int (*)(VECTOR_TYPE *,size_t,size_t ,size_t,DATA_TYPE,size_t *))iVector.SearchWithKey;
intface->Select = (int (*)(VECTOR_TYPE *src,const Mask *m))iVector.Select;
intface->GetData = (DATA_TYPE *(*)(const VECTOR_TYPE *))iVector.GetData;
intface->RotateLeft = (int (*)(VECTOR_TYPE *V,size_t n))iVector.RotateLeft;
intface->RotateRight = (int (*)(VECTOR_TYPE *V,size_t n))iVector.RotateRight;
intface->CompareEqual = (Mask *(*)(const VECTOR_TYPE *,const VECTOR_TYPE *,Mask *))iVector.CompareEqual;
intface->GetElement = (DATA_TYPE *(*)(const VECTOR_TYPE *,size_t))iVector.GetElement;
intface->Back = (DATA_TYPE *(*)(const VECTOR_TYPE *))iVector.Back;
intface->Front = (DATA_TYPE *(*)(const VECTOR_TYPE *))iVector.Front;
return result;
}
static VECTOR_TYPE *CreateWithAllocator(size_t siz,const ContainerAllocator *a)
{
VECTOR_TYPE *r = (VECTOR_TYPE *)iVector.CreateWithAllocator(sizeof(DATA_TYPE),siz,a);
if (r == NULL) return NULL;
return SetVTable(r);
}
static VECTOR_TYPE *Create(size_t startsize)
{
return CreateWithAllocator(startsize,CurrentAllocator);
}
static VECTOR_TYPE *InitializeWith(size_t n,const DATA_TYPE *data)
{
VECTOR_TYPE *result = Create(n);
if (result) {
memcpy(result->contents,data,n*sizeof(DATA_TYPE));
result->count = n;
}
return result;
}
static VECTOR_TYPE *Init(VECTOR_TYPE *result,size_t startsize)
{
VECTOR_TYPE *r = (VECTOR_TYPE *)iVector.Init((Vector *)result,sizeof(DATA_TYPE),startsize);
return SetVTable(r);
}
static size_t GetElementSize(const VECTOR_TYPE *AL)
{
return sizeof(DATA_TYPE);
}
static size_t SizeofIterator(const VECTOR_TYPE * l)
{
return sizeof(struct ITERATOR(DATA_TYPE));
}
static Mask *CompareEqualScalar(const VECTOR_TYPE *left,const DATA_TYPE right,Mask *bytearray)
{
return iVector.CompareEqualScalar((Vector *)left,&right,bytearray);
}
INTERFACE(DATA_TYPE) INTERFACE_NAME(DATA_TYPE) = {
NULL, /* Size, */
NULL, /* GetFlags, */
NULL, /* SetFlags, */
NULL, /* Clear, */
Contains,
Erase,
EraseAll,
NULL, /* Finalize, */
NULL, /* Apply, */
NULL, /* Equal, */
Copy,
NULL, /* SetErrorFunction, */
Sizeof,
NewIterator,
InitIterator,
NULL, /* DeleteIterator, */
SizeofIterator,
NULL, /* Save, */
Load,
GetElementSize,
/* Sequential container fields */
Add,
NULL, /* GetElement, */
PushBack,
PopBack,
InsertAt,
NULL, /* EraseAt, */
ReplaceAt,
IndexOf,
/* Vector specific fields */
Insert,
NULL, /* InsertIn, */
NULL, /* IndexIn, */
NULL, /* GetCapacity, */
NULL, /* SetCapacity, */
NULL, /* SetCompareFunction, */
Sort,
Create,
CreateWithAllocator,
Init,
NULL, /* AddRange, */
GetRange,
NULL, /* CopyElement, */
CopyTo,
NULL, /* Reverse, */
NULL, /* Append, */
NULL, /* Mismatch, */
NULL, /* GetAllocator, */
NULL, /* SetDestructor, */
NULL, /* SearchWithKey, */
NULL, /* Select, */
NULL, /* SelectCopy,*/
NULL, /* Resize, */
InitializeWith,
NULL, /* GetData, */
NULL, /* Back, */
NULL, /* Front, */
NULL, /* RemoveRange, */
NULL, /* RotateLeft, */
NULL, /* RotateRight, */
NULL, /* CompareEqual, */
CompareEqualScalar,
NULL, /* Reserve */
};