summaryrefslogtreecommitdiff
path: root/src/vector.c
blob: b65ee2f38611bbf291990e93b401a3fecd0273a5 (plain) (blame)
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
#include "config.h"

#include <assert.h>
#include <errno.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "logerr.h"
#include "catalog.h"
#include "i18n.h"
#include "math.h"

#include "vector.h"


struct Vector {
	const void **values;
	size_t capacity;
	size_t count;
	const size_t max_capacity;
	const size_t multiplier;
	const size_t value_size;
};

static const size_t
VECTOR_MAX_CAPACITY = SIZE_MAX;

static const size_t
VECTOR_DEFAULT_CAPACITY = 8;

static const size_t
GROWTH_MULTIPLIER = 2;


int
vector_new_with(
	const size_t capacity,
	const size_t max_capacity,
	const size_t multiplier,
	const size_t value_size,
	const struct Vector **const out
) {
	int rc = -1;

	const struct Vector *ret = NULL;
	void *values = NULL;

	assert(capacity > 0);
	if (capacity > max_capacity) {
		logerr(
			_(MSG_ERR_VECTOR_MAX_CAPACITY),
			max_capacity,
			strerror(EOVERFLOW)
		);
		rc = EOVERFLOW;
		goto out;
	}

	const size_t array_bytes_size = capacity * value_size;
	values = malloc(array_bytes_size);
	if (values == NULL) {
		logerr("malloc(): %s", strerror(errno));
		goto out;
	}

	ret = malloc(sizeof(*ret));
	if (ret == NULL) {
		logerr("malloc(): %s", strerror(errno));
		goto out;
	}

	memcpy((void *)ret, &(struct Vector) {
		.values       = values,
		.capacity     = capacity,
		.count        = 0U,
		.max_capacity = max_capacity,
		.multiplier   = multiplier,
		.value_size   = value_size,
	}, sizeof(*ret));
	*out = ret;
	rc = 0;
out:
	if (rc) {
		if (ret != NULL) {
			free((void *)ret);
			ret = NULL;
		}
		if (values != NULL) {
			free((void *)values);
			values = NULL;
		}
	}
	return rc;
}

int
vector_new(const size_t value_size, const struct Vector **const out) {
	return vector_new_with(
		VECTOR_DEFAULT_CAPACITY,
		VECTOR_MAX_CAPACITY,
		GROWTH_MULTIPLIER,
		value_size,
		out
	);
}

void
vector_free(const struct Vector **const v) {
	assert((*v) != NULL);
	free((*v)->values);
	free((void *)*v);
	*v = NULL;
}

size_t
vector_count(const struct Vector *const v) {
	return v->count;
}

static size_t
next_capacity(const struct Vector *const v) {
	size_t ret;
	if (mul_size(v->capacity, v->multiplier, &ret)) {
		return v->max_capacity;
	}

	return ret;
}

static int
next_size(const struct Vector *const v, const size_t capacity, size_t *const out) {
	return mul_size(v->value_size, capacity, out);
}

int
vector_nth(const struct Vector *const v, const size_t idx, const void **const out) {
	int rc = -1;

	const size_t count = vector_count(v);
	if (idx >= count) {
		logerr(_(MSG_ERR_VECTOR_OUT_OF_BOUNDS), idx, count);
		goto out;
	}

	*out = v->values[idx];
	rc = 0;
out:
	return rc;
}

int
vector_push_back(const struct Vector *const v, const void *const value) {
	int rc = -1;

	void *new_values = NULL;
	struct Vector *const v_mut = (struct Vector *const)v;

	const size_t count = vector_count(v);
	if (count == v->capacity) {
		if (count == v->max_capacity) {
			logerr(
				_(MSG_ERR_VECTOR_MAX_CAPACITY),
				v->max_capacity,
				strerror(EOVERFLOW)
			);
			rc = EOVERFLOW;
			goto out;
		}

		const size_t new_capacity = next_capacity(v);
		size_t new_size;
		if (next_size(v, new_capacity, &new_size)) {
			logerr(
				_(MSG_ERR_VECTOR_MAX_CAPACITY),
				v->max_capacity,
				strerror(EOVERFLOW)
			);
			rc = EOVERFLOW;
			goto out;
		}

		new_values = realloc(v->values, new_size);
		if (new_values == NULL) {
			logerr("realloc(): %s", strerror(errno));
			goto out;
		}
		v_mut->capacity = new_capacity;
		v_mut->values   = new_values;
	}

	memcpy(&v->values[v->count], value, v->value_size);
	v_mut->count++;
	rc = 0;
out:
	if (rc) {
		if (new_values != NULL) {
			free(new_values);
			new_values = NULL;
		}
	}
	return rc;
}

int
vector_pop_back(const struct Vector *const v, const void **const out) {
	int rc = -1;

	struct Vector *const v_mut = (struct Vector *const)v;

	const size_t count = vector_count(v);
	if (count == 0) {
		logerr(_(MSG_ERR_VECTOR_UNDERFLOW));
		goto out;
	}

	v_mut->count--;
	*out = v->values[v->count];
	rc = 0;
out:
	return rc;
}

bool
vector_contains(const struct Vector *const v, const void *const value) {
	for (size_t i = 0; i < vector_count(v); i++) {
		if (memcmp(&v->values[i], value, v->value_size) == 0) {
			return true;
		}
	}

	return false;
}