-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
379 lines (321 loc) · 10.4 KB
/
Copy pathvector.c
File metadata and controls
379 lines (321 loc) · 10.4 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#ifndef NATIVE
/* Non-native options */
#pragma WIDE
#pragma XMEM
#pragma NOMAP
#pragma NOOLDCALLS
#pragma LARGESYM
#pragma SYMBOLS
#pragma INSPECT
#pragma HIGHPIN /* tfr */
#pragma SAVEABEND
#pragma NOCHECK /* (NOCHECK to test performance) */
#endif
/*
COPYRIGHT GRAVIC, INC. 1995 - 2007
All Rights Reserved.
*** CONFIDENTIAL AND PROPRIETARY PROPERTY OF GRAVIC, INC. ***
Usage of this source code is provided to customers solely under the terms
of a valid license agreement from Gravic, Inc.. ALL OTHER USERS ARE STRICTLY
FORBIDDEN AND MAY CONSTITUTE THEFT OF TRADE SECRETS OR OTHER SERIOUS CRIME,
AND WILL BE PROSECUTED TO THE FULLEST EXTENT OF THE LAW!
NOTICE: This, and all copies of this software and documentation,
must be returned to Gravic, Inc. or destroyed upon termination of
the valid license. If you find a copy outside of the terms
of a valid license agreement, please notify our office and immediately return
copy to Gravic, Inc. or destroy!
Gravic, Inc. will a provide a $5000 reward for the arrest and conviction of
anyone stealing this software. Report all occurrences to:
President
Gravic, Inc.
301 Lindenwood Dr.
Malvern, PA 19355-1758 USA
(610) 647-6250
All copies of this software in all such forms, and derivatives thereof,
must carry this entire notice.
Any documentation about software incorporating or using such forms must
contain these notices and any advertising about such software must
acknowledge that the software contains elements which are the property
of Gravic, Inc., used under license.
The name and trademarks of Gravic, Inc. may not be otherwise used
without the expressed, written permission of Gravic, Inc..
EXCLUSION OF WARRANTY AND LIMITATION OF LIABILITY:
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. TO THE MAXIMUM
EXTENT PERMITTED BY LAW, IN NO EVENT SHALL GRAVIC, INC. ITS OFFICERS,
DIRECTORS, EMPLOYEES, ADVISORS, SUPPLIERS, OR LICENSORS BE LIABLE FOR ANY
DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, CONSEQUENTIAL OR
SPECIAL DAMAGES OR DAMAGES FOR LOSS OF PROFITS, INTERRUPTION OF BUSINESS,
LOSS OR RECONSTRUCTION OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS)
ARISING OUT OF OR IN CONNECTION WITH USE OF OR INABILITY TO USE THE
SOFTWARE, EVEN IF GRAVIC, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES. Some jurisdictions do not permit the exclusion or limitation
for certain damages, so the above may not apply to you.
All questions about this notice may be made to the above address.
*/
/**
* @file W:\nsb\trunk\dev\shadowbase\Type101\vector.c
*
* Implementation for a growable vector.
*
* @author John Hoffmann
*/
#ifdef _lint
#define LINT_LEVEL 1
#include "include/lint_er.h"
#endif
#ifdef NATIVE
#include "include/vector.h"
#else
#include "vectorh"
#endif
#include <stdlib.h>
static VECTOR_CMP_FUNC compare_function;
/** For non-native compiling only, this function wraps the realloc
* function so that it can be passed to vector_create.
*/
void *vector_nn_realloc(void *ptr, size_t size)
{
return realloc(ptr, size);
}
/** For non-native compiling only, this function wraps the free
* function so that it can be passed to vector_create.
*/
void vector_nn_free(void *ptr)
{
free(ptr);
}
/** Compare function wrapper. The sort and bsearch functions pass in pointers
* to the things that are being compared. In this case, the things that are being
* compared are void * elements, so we are actually getting a void **ptr. This needs
* to be dereferenced for the user's compare function.
*/
static int compare(const void *ptr1, const void *ptr2)
{
const void **ptr1ptr = (const void **)ptr1;
const void **ptr2ptr = (const void **)ptr2;
return compare_function(*ptr1ptr, *ptr2ptr);
}
static void reserve(VECTOR *v, size_t new_size)
{
while (v->allocated < new_size)
{
v->elements = v->alloc_func(v->elements, new_size * sizeof(void *));
v->allocated = new_size;
}
}
VECTOR *vector_create(size_t initial_size,
size_t alloc_incr,
VECTOR_ALLOC_FUNC alloc_func,
VECTOR_FREE_FUNC free_func )
{
VECTOR *new_vector;
if (alloc_func != NULL)
new_vector = alloc_func(NULL, sizeof(VECTOR));
else
new_vector = malloc(sizeof(VECTOR));
if (new_vector != NULL)
{
new_vector->alloc_incr = alloc_incr;
new_vector->allocated = 0;
new_vector->size = 0;
new_vector->elements = NULL;
if (alloc_func != NULL)
new_vector->alloc_func = alloc_func;
else
new_vector->alloc_func = REALLOC;
new_vector->free_func = free_func;
reserve(new_vector, initial_size);
}
return new_vector;
}
size_t vector_size(const VECTOR *v)
{
return v->size;
}
void *vector_get(VECTOR *v, size_t index)
{
if (index < v->size)
return v->elements[index];
else
return NULL;
} /*lint !e818 Pointer parameter 'v' (line 125) could be declared as pointing to const (why? return ptr is not const) */
/** Sets the element at the index passed in to the ptr. If the free function
* was specified, the existing pointer at the element will be freed and NULL will
* be returned. Otherwise, the existing pointer will be returned and it is the
* caller's responsibility to free the memory. If the index is beyond the end of the vector,
* the vector will be expanded to include the elements, all pointers between
* the previous end of the vector and the new pointer will be set to NULL.
*/
void *vector_set(VECTOR *v, /*!< the vector */
void *new_element, /*!< the new element */
size_t index /*!< the index */
)
{
size_t i;
void *ptr;
if (index >= v->size)
{
i = v->size;
reserve(v, index+1);
for (;i<=index;++i)
v->elements[i] = NULL;
v->size = index+1;
}
ptr = v->elements[index];
v->elements[index] = new_element;
if ( (ptr != NULL) && (v->free_func != NULL))
{
v->free_func(ptr);
return NULL;
}
return ptr;
}
void *vector_back(VECTOR *v)
{
if (v->size > 0)
return v->elements[v->size-1];
else
return NULL;
} /*lint !e818 Pointer parameter 'v' (line 125) could be declared as pointing to const (why? return ptr is not const) */
void vector_pop_back(VECTOR *v)
{
if (v->size > 0)
{
v->size--;
if (v->free_func != NULL)
v->free_func(v->elements[v->size]);
}
}
void vector_insert(VECTOR *v, void *element, size_t index)
{
if (v->size==v->allocated)
reserve(v, v->allocated+v->alloc_incr);
if (index >= v->size)
{
v->elements[v->size] = element;
}
else
{
memmove(&v->elements[index+1], &v->elements[index], (v->size-index)*sizeof(void *));
v->elements[index] = element;
}
v->size++;
}
void vector_push_back(VECTOR *v, void *element)
{
if (v->size==v->allocated)
reserve(v, v->allocated+v->alloc_incr);
v->elements[v->size] = element;
v->size++;
}
void vector_remove(VECTOR *v, size_t index)
{
if (index < v->size)
{
size_t num_to_move = (v->size - index) - 1;
if (v->free_func != NULL)
v->free_func(v->elements[index]);
if (num_to_move > 0)
{
memmove(&v->elements[index], &v->elements[index+1], num_to_move * sizeof(void *));
}
v->size -= 1;
}
}
void vector_destroy(VECTOR *v)
{
vector_empty(v);
v->alloc_func(v->elements, 0);
v->alloc_func(v, 0);
}
void vector_sort(VECTOR *v, VECTOR_CMP_FUNC compare_func)
{
if (v->size > 1)
{
compare_function = compare_func;
qsort(v->elements, v->size, sizeof(void *), compare);
}
} /*lint !e818 Pointer parameter 'v' (line 125) could be declared as pointing to const (why? contents are reordered) */
void *vector_search(VECTOR *v, void *key, VECTOR_CMP_FUNC compare_func)
{
void *result;
compare_function = compare_func;
result = bsearch(&key, /* needs to be converted to a void ** */
v->elements,
v->size,
sizeof(void *),
compare);
if (result != NULL)
{
/* result is really a pointer to a void pointer */
void **vpp = (void **)result;
return *vpp;
}
else
return NULL;
} /*lint !e818 Pointer parameter 'v' (line 125) could be declared as pointing to const (why? return ptr is not const) */
void *vector_scan(VECTOR *v, const void *key, size_t *index, VECTOR_CMP_FUNC compare_func)
{
return vector_scan_from(v, key, 0, index, compare_func);
}
void *vector_scan_from(VECTOR *v, const void *key, size_t start_index, size_t *index, VECTOR_CMP_FUNC compare_func)
{
size_t i;
for (i=start_index; i<v->size; ++i)
{
if (compare_func(key, v->elements[i])==0)
{
if (index != NULL)
*index = i;
return v->elements[i];
}
}
if (index != NULL)
*index = v->size;
return NULL;
} /*lint !e818 pointer parameter 'v' (line 275) could be declared as pointing to const */
/*note - causes error in non-native compiles. */
void vector_empty(VECTOR *v)
{
if ((v->free_func != NULL) && (v->size > 0))
{
size_t i;
for (i=0; i< v->size; i++)
v->free_func(v->elements[i]);
}
v->size = 0;
}
void vector_reserve(VECTOR *v, size_t new_alloc_size)
{
if (v->allocated < new_alloc_size)
{
reserve(v, new_alloc_size);
}
}
void vector_push_copy(VECTOR *v, const void *element_to_copy, size_t element_size)
{
void *ptr = NULL;
ptr = v->alloc_func(ptr, element_size);
memcpy(ptr, element_to_copy, element_size);
vector_push_back(v, ptr);
}
void vector_insert_copy(VECTOR *v, const void *element_to_copy,
size_t element_size, size_t insert_at)
{
void *ptr = NULL;
ptr = v->alloc_func(ptr, element_size);
memcpy(ptr, element_to_copy, element_size);
vector_insert(v, ptr, insert_at);
}
void* vector_set_copy(VECTOR *v,
const void *element_to_copy,
size_t element_size,
size_t set_at)
{
void *ptr = NULL;
ptr = v->alloc_func(ptr, element_size);
memcpy(ptr, element_to_copy, element_size);
return vector_set(v, ptr, set_at);
}