aboutsummaryrefslogtreecommitdiff
path: root/collections/vector/vector.c
blob: a7e0845a8c4ccdfd42546b0fca16e0d5c75c3948 (plain)
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
#include "vector.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define START_SIZE 64;

struct vector {
	void **base;
	int end, limit;
};

void vec_debug_print(root)
vec *root;
{
	int i;

	fprintf(stderr, "VEC[base: %p, end: %p, limit:%p]:\n\t ",
			root->base, root->end, root->limit);
	for (i=0; i < root->end; i++){
		fprintf(stderr, "[%p]", vec_index(root,i));
	}
	fprintf(stderr, "\n");
}

vec* vec_new()
{
	vec *root;

	root = malloc(sizeof(vec));
	assert(root);

	root->limit = START_SIZE;
	root->base = malloc(root->limit * sizeof(void*));
	assert(root->base);

	return root;
}

vec* vec_with_capacity(n)
int n;
{
	vec *root;

	root = malloc(sizeof(vec));
	assert(root);

	root->limit = n;
	root->base = malloc(root->limit * sizeof(void*));
	assert(root->base);

	return root;
}

int vec_size(root)
vec *root;
{
	if (!root) {
		return -1;
	}
	return root->end;
}

int vec_capacity(root)
vec *root;
{
	if (!root) {
		return -1;
	}
	
	return root->limit;
}

void vec_resize(root)
vec *root;
{
	if (!root)
		return;

	root->base = reallocarray(root->base, root->limit * 2, sizeof(void*));
	assert(root->base);
}

vec* vec_cp(root)
vec *root;
{
	vec *copy;

	if (!root)
		return NULL;

	copy = vec_with_capacity(root->limit);

	copy->base = memcpy(copy->base, root->base,
			vec_size(root) * sizeof(void*));
	assert(copy->base);

	copy->end = root->end;
	copy->limit = root->limit;

	return copy;
}

void vec_print(root, to_string)
vec *root;
char* to_string(void*);
{
	int i;

	printf("[");
	for(i = 0; i < root->end; i++) {
		printf("%s", to_string(vec_index(root, i)));
	}
	printf("\b]\n");

}

void vec_push(root, item)
vec *root;
void *item;
{
	if (!root) {
		return;
	}

	if (root->end >= root->limit) {
		vec_resize(root);
	}

	root->base[root->end++] = item;
}

void* vec_pop(root)
vec *root;
{
	if (!root || root->end == 0) {
		return NULL;
	}

	return root->base[--root->end];
}

void* vec_back(root)
vec *root;
{
	if (!root || root->end == 0) {
		return NULL;
	}

	return root->base[root->end - 1];
}

void vec_set(root, index, item)
vec *root;
int index;
void *item;
{
	if (!root || index >= root->end || index < 0)
		return;

	root->base[index] = item;
}

void* vec_index(root, index)
vec *root;
int index;
{
	if (!root || index >= root->end || index < 0) {
		return NULL;
	}

	return root->base[index];
}

void vec_swap(root, i, j)
vec *root;
int i,j;
{
	void *tmp;

	if (!root || i >= root->end || i < 0 || j >= root->end || j < 0)
		return;

	tmp = root->base[i];
	root->base[i] = root->base[j];
	root->base[j] = tmp;

	return;
}

void vec_clear(root)
vec *root;
{
	int i;

	for (i = 0; i < root->end; i++) {
		free(vec_index(root, i));
	}

	root->end = 0;
}

void vec_free(root)
vec *root;
{
	free(root->base);
	root->base = NULL;

	free(root);
}