-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
121 lines (109 loc) · 3.31 KB
/
Copy pathvector.c
File metadata and controls
121 lines (109 loc) · 3.31 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
#include "vector.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void VectorNew(vector *v, int elemSize, VectorFreeFunction freeFn, int initialAllocation)
{
assert(elemSize > 0);
v->elemSize = elemSize;
v->logLength = 0;
v->allocLength = initialAllocation;
v->elems = malloc(initialAllocation * elemSize);
v->freefn = freeFn;
assert(v->elems != NULL);
}
void VectorDispose(vector *v)
{
int i;
v->logLength = 0;
if(v->freefn !=NULL) {
for(i = 0; i < VectorLength(v); i++) {
v->freefn(VectorNth(v, i));
}
} else {
free(v->elems);
}
}
int VectorLength(const vector *v)
{
return v->logLength;
}
void *VectorNth(const vector *v, int position)
{
assert(position >= 0 && position < VectorLength(v));
return (char *)v->elems + position * v->elemSize;
}
void VectorReplace(vector *v, const void *elemAddr, int position)
{
assert(position >= 0 && position < VectorLength(v));
if(v->freefn != NULL) {
v->freefn(VectorNth(v, position));
}
memcpy(VectorNth(v, position), elemAddr, v->elemSize);
}
void VectorInsert(vector *v, const void *elemAddr, int position)
{
assert(position >= 0 && position <= VectorLength(v));
if(VectorLength(v) == v->allocLength) {
v->allocLength *= 2;
v->elems = realloc(v->elems, v->allocLength);
}
void *dest = (char *)v->elems + (position + 1) * v->elemSize;
void *src = (char *)v->elems + position * v->elemSize;
memmove(dest, src, (VectorLength(v) - position) * v->elemSize);
memcpy(src, elemAddr, v->elemSize);
v->logLength++;
}
void VectorAppend(vector *v, const void *elemAddr)
{
void *destAddr;
if(VectorLength(v) == v->allocLength) {
v->allocLength *= 2;
v->elems = realloc(v->elems, v->allocLength * v->elemSize);
assert(v->elems != NULL);
}
destAddr = (char *)v->elems + VectorLength(v) * v->elemSize;
memcpy(destAddr, elemAddr, v->elemSize);
v->logLength++;
}
void VectorDelete(vector *v, int position)
{
assert(position >= 0 && position < VectorLength(v));
void *dest = (char *)v->elems + position * v->elemSize;
void *src = (char *)v->elems + (position + 1) * v->elemSize;
memmove(dest, src, (VectorLength(v) - position - 1) * v->elemSize);
v->logLength--;
}
void VectorSort(vector *v, VectorCompareFunction compare)
{
assert(compare != NULL);
qsort(v->elems, VectorLength(v), v->elemSize, compare);
}
void VectorMap(vector *v, VectorMapFunction mapFn, void *auxData)
{
int i;
assert(mapFn != NULL);
for(i=0; i < VectorLength(v); i++) {
mapFn(VectorNth(v, i), auxData);
}
}
static const int kNotFound = -1;
int VectorSearch(const vector *v, const void *key, VectorCompareFunction searchFn, int startIndex, bool isSorted)
{
int i;
assert(startIndex >= 0 && startIndex < VectorLength(v));
assert(searchFn != NULL);
if(isSorted) {
void *bres = bsearch(key, v->elems, VectorLength(v), v->elemSize, searchFn);
if(bres == NULL) return kNotFound;
return ((char *) bres - (char *) v->elems) / v->elemSize;
} else {
for(i = startIndex; i < VectorLength(v); i++) {
if(searchFn(key, VectorNth(v, i)) == 0) {
return i;
}
}
return kNotFound;
}
}