-
Notifications
You must be signed in to change notification settings - Fork 13
/
intvec.c
169 lines (142 loc) · 3.38 KB
/
intvec.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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "intvec.h"
void
intvec_push(intvec_t *vec, int value)
{
if(vec->count + 1 >= vec->capacity) {
vec->capacity = vec->capacity * 2 + 16;
vec->v = realloc(vec->v, sizeof(vec->v[0]) * vec->capacity);
}
vec->v[vec->count++] = value;
}
void
intvec_reset(intvec_t *vec)
{
vec->count = 0;
vec->capacity = 0;
free(vec->v);
vec->v = NULL;
}
void
intvec_insert(intvec_t *vec, int position, int value)
{
if(position == vec->count)
return intvec_push(vec, value);
if(vec->count + 1 >= vec->capacity) {
vec->capacity = vec->capacity * 2 + 16;
vec->v = realloc(vec->v, sizeof(vec->v[0]) * vec->capacity);
}
memmove(vec->v + position + 1, vec->v + position,
(vec->count - position) * sizeof(vec->v[0]));
vec->v[position] = value;
vec->count++;
}
void
intvec_delete(intvec_t *vec, unsigned int position)
{
assert(position < vec->count);
memmove(vec->v + position, vec->v + position + 1,
(vec->count - position - 1) * sizeof(vec->v[0]));
vec->count--;
}
static int
intvec_search(const intvec_t *vec, int value)
{
int imin = 0;
int imax = vec->count;
while(imin < imax) {
int imid = (imin + imax) >> 1;
if(vec->v[imid] < value)
imin = imid + 1;
else
imax = imid;
}
return imin;
}
int
intvec_insert_sorted(intvec_t *vec, int value)
{
int position = intvec_search(vec, value);
intvec_insert(vec, position, value);
return position;
}
int
intvec_find(const intvec_t *vec, int value)
{
if(vec->count == 0)
return -1;
const int pos = intvec_search(vec, value);
return pos < vec->count && vec->v[pos] == value ? pos : -1;
}
void
intvec_copy(intvec_t *dst, const intvec_t *src)
{
// We trim the capacity down to the actual size here
dst->count = dst->capacity = src->count;
dst->v = malloc(dst->count * sizeof(dst->v[0]));
memcpy(dst->v, src->v, dst->count * sizeof(dst->v[0]));
}
#ifdef TEST
static void
printvec(intvec_t *v)
{
for(int i = 0; i < v->count; i++) {
printf("%d ", v->v[i]);
}
printf("\n");
}
int main(void)
{
intvec_t v = {};
intvec_insert_sorted(&v, 5);
printvec(&v);
intvec_insert_sorted(&v, 3);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 1);
printvec(&v);
intvec_insert_sorted(&v, 11);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 7);
printvec(&v);
intvec_insert_sorted(&v, 1);
printvec(&v);
intvec_insert_sorted(&v, 1);
printvec(&v);
intvec_insert_sorted(&v, 1);
printvec(&v);
intvec_insert_sorted(&v, 1);
printvec(&v);
intvec_insert_sorted(&v, 0);
printvec(&v);
intvec_insert_sorted(&v, 11);
printvec(&v);
intvec_insert_sorted(&v, 12);
printvec(&v);
intvec_insert_sorted(&v, 13);
printvec(&v);
printf("Find %d = %d\n", 0, intvec_find(&v, 0));
printf("Find %d = %d\n", 7, intvec_find(&v, 7));
printf("Find %d = %d\n", 8, intvec_find(&v, 8));
printf("Find %d = %d\n", 12, intvec_find(&v, 12));
printf("Find %d = %d\n", 1200, intvec_find(&v, 1200));
printf("Find %d = %d\n", -120, intvec_find(&v, -120));
intvec_reset(&v);
}
#endif