vector.c 6.69 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10
 */

#include "common.h"
#include "vector.h"

11 12
/* In elements, not bytes */
#define MIN_ALLOCSIZE	8
13

14
GIT_INLINE(size_t) compute_new_size(git_vector *v)
15
{
16 17 18 19 20 21
	size_t new_size = v->_alloc_size;

	/* Use a resize factor of 1.5, which is quick to compute using integer
	 * instructions and less than the golden ratio (1.618...) */
	if (new_size < MIN_ALLOCSIZE)
		new_size = MIN_ALLOCSIZE;
Philip Kelley committed
22 23
	else if (new_size <= (SIZE_MAX / 3) * 2)
		new_size += new_size / 2;
24 25 26 27 28
	else
		new_size = SIZE_MAX;

	return new_size;
}
29

30 31 32 33
GIT_INLINE(int) resize_vector(git_vector *v, size_t new_size)
{
	void *new_contents;

34
	new_contents = git__reallocarray(v->contents, new_size, sizeof(void *));
35 36 37 38
	GITERR_CHECK_ALLOC(new_contents);

	v->_alloc_size = new_size;
	v->contents = new_contents;
39

40
	return 0;
41 42
}

43 44 45 46 47 48 49
int git_vector_size_hint(git_vector *v, size_t size_hint)
{
	if (v->_alloc_size >= size_hint)
		return 0;
	return resize_vector(v, size_hint);
}

50
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp)
51
{
52 53
	size_t bytes;

54 55
	assert(v && src);

56
	GITERR_CHECK_ALLOC_MULTIPLY(&bytes, src->length, sizeof(void *));
57

58
	v->_alloc_size = src->length;
59
	v->_cmp = cmp ? cmp : src->_cmp;
60
	v->length = src->length;
61 62 63
	v->flags  = src->flags;
	if (cmp != src->_cmp)
		git_vector_set_sorted(v, 0);
64
	v->contents = git__malloc(bytes);
65 66
	GITERR_CHECK_ALLOC(v->contents);

67
	memcpy(v->contents, src->contents, bytes);
68 69 70 71

	return 0;
}

72 73 74
void git_vector_free(git_vector *v)
{
	assert(v);
75

76
	git__free(v->contents);
77 78 79 80
	v->contents = NULL;

	v->length = 0;
	v->_alloc_size = 0;
81 82
}

83
void git_vector_free_deep(git_vector *v)
Russell Belfer committed
84 85 86 87 88 89 90 91 92 93 94 95 96
{
	size_t i;

	assert(v);

	for (i = 0; i < v->length; ++i) {
		git__free(v->contents[i]);
		v->contents[i] = NULL;
	}

	git_vector_free(v);
}

97
int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp)
98 99 100
{
	assert(v);

101
	v->_alloc_size = 0;
102 103
	v->_cmp = cmp;
	v->length = 0;
104
	v->flags = GIT_VECTOR_SORTED;
105
	v->contents = NULL;
106

107
	return resize_vector(v, max(initial_size, MIN_ALLOCSIZE));
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
void **git_vector_detach(size_t *size, size_t *asize, git_vector *v)
{
	void **data = v->contents;

	if (size)
		*size = v->length;
	if (asize)
		*asize = v->_alloc_size;

	v->_alloc_size = 0;
	v->length   = 0;
	v->contents = NULL;

	return data;
}

126 127 128 129
int git_vector_insert(git_vector *v, void *element)
{
	assert(v);

130
	if (v->length >= v->_alloc_size &&
131
		resize_vector(v, compute_new_size(v)) < 0)
132
		return -1;
133 134

	v->contents[v->length++] = element;
135 136

	git_vector_set_sorted(v, v->length <= 1);
137

138
	return 0;
139 140
}

141 142
int git_vector_insert_sorted(
	git_vector *v, void *element, int (*on_dup)(void **old, void *new))
143
{
144
	int result;
145 146 147 148
	size_t pos;

	assert(v && v->_cmp);

149
	if (!git_vector_is_sorted(v))
150 151
		git_vector_sort(v);

152
	if (v->length >= v->_alloc_size &&
153
		resize_vector(v, compute_new_size(v)) < 0)
154
		return -1;
155

156 157
	/* If we find the element and have a duplicate handler callback,
	 * invoke it.  If it returns non-zero, then cancel insert, otherwise
158 159
	 * proceed with normal insert.
	 */
160 161
	if (!git__bsearch(v->contents, v->length, element, v->_cmp, &pos) &&
		on_dup && (result = on_dup(&v->contents[pos], element)) < 0)
162
		return result;
163 164

	/* shift elements to the right */
165
	if (pos < v->length)
166 167 168 169 170
		memmove(v->contents + pos + 1, v->contents + pos,
		        (v->length - pos) * sizeof(void *));

	v->contents[pos] = element;
	v->length++;
171

172
	return 0;
173 174
}

175 176 177 178
void git_vector_sort(git_vector *v)
{
	assert(v);

179
	if (git_vector_is_sorted(v) || !v->_cmp)
180 181
		return;

182 183
	if (v->length > 1)
		git__tsort(v->contents, v->length, v->_cmp);
184
	git_vector_set_sorted(v, 1);
185 186
}

187
int git_vector_bsearch2(
188
	size_t *at_pos,
189 190 191
	git_vector *v,
	git_vector_cmp key_lookup,
	const void *key)
192
{
193 194
	assert(v && key && key_lookup);

195
	/* need comparison function to sort the vector */
196 197
	if (!v->_cmp)
		return -1;
198

199 200
	git_vector_sort(v);

201
	return git__bsearch(v->contents, v->length, key, key_lookup, at_pos);
202 203
}

204
int git_vector_search2(
205
	size_t *at_pos, const git_vector *v, git_vector_cmp key_lookup, const void *key)
206
{
207
	size_t i;
208 209 210 211

	assert(v && key && key_lookup);

	for (i = 0; i < v->length; ++i) {
212 213 214 215 216 217
		if (key_lookup(key, v->contents[i]) == 0) {
			if (at_pos)
				*at_pos = i;

			return 0;
		}
218 219
	}

220
	return GIT_ENOTFOUND;
221 222
}

223
static int strict_comparison(const void *a, const void *b)
224
{
Vicent Marti committed
225
	return (a == b) ? 0 : -1;
226
}
227

228
int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry)
229
{
230
	return git_vector_search2(at_pos, v, v->_cmp ? v->_cmp : strict_comparison, entry);
231 232
}

233
int git_vector_remove(git_vector *v, size_t idx)
234
{
235
	size_t shift_count;
236 237 238

	assert(v);

239
	if (idx >= v->length)
240
		return GIT_ENOTFOUND;
241

242 243 244 245 246
	shift_count = v->length - idx - 1;

	if (shift_count)
		memmove(&v->contents[idx], &v->contents[idx + 1],
			shift_count * sizeof(void *));
247 248

	v->length--;
249
	return 0;
250 251
}

252
void git_vector_pop(git_vector *v)
253
{
254 255
	if (v->length > 0)
		v->length--;
256 257
}

258
void git_vector_uniq(git_vector *v, void  (*git_free_cb)(void *))
259 260
{
	git_vector_cmp cmp;
261
	size_t i, j;
262 263 264 265 266 267 268 269

	if (v->length <= 1)
		return;

	git_vector_sort(v);
	cmp = v->_cmp ? v->_cmp : strict_comparison;

	for (i = 0, j = 1 ; j < v->length; ++j)
270 271 272 273
		if (!cmp(v->contents[i], v->contents[j])) {
			if (git_free_cb)
				git_free_cb(v->contents[i]);

274
			v->contents[i] = v->contents[j];
275
		} else
276 277 278 279 280
			v->contents[++i] = v->contents[j];

	v->length -= j - i - 1;
}

281
void git_vector_remove_matching(
282 283 284
	git_vector *v,
	int (*match)(const git_vector *v, size_t idx, void *payload),
	void *payload)
Edward Thomson committed
285
{
286
	size_t i, j;
Edward Thomson committed
287 288 289 290

	for (i = 0, j = 0; j < v->length; ++j) {
		v->contents[i] = v->contents[j];

291
		if (!match(v, i, payload))
Edward Thomson committed
292 293 294 295 296 297
			i++;
	}

	v->length = i;
}

298 299 300 301
void git_vector_clear(git_vector *v)
{
	assert(v);
	v->length = 0;
302
	git_vector_set_sorted(v, 1);
303 304
}

305 306 307 308
void git_vector_swap(git_vector *a, git_vector *b)
{
	git_vector t;

309
	assert(a && b);
310

311 312 313 314 315
	if (a != b) {
		memcpy(&t, a, sizeof(t));
		memcpy(a, b, sizeof(t));
		memcpy(b, &t, sizeof(t));
	}
316
}
317 318 319

int git_vector_resize_to(git_vector *v, size_t new_length)
{
320 321 322
	if (new_length > v->_alloc_size &&
		resize_vector(v, new_length) < 0)
		return -1;
323

324 325 326
	if (new_length > v->length)
		memset(&v->contents[v->length], 0,
			sizeof(void *) * (new_length - v->length));
327 328 329 330 331 332 333 334

	v->length = new_length;

	return 0;
}

int git_vector_set(void **old, git_vector *v, size_t position, void *value)
{
335 336 337 338
	if (position + 1 > v->length) {
		if (git_vector_resize_to(v, position + 1) < 0)
			return -1;
	}
339 340 341 342 343 344 345 346

	if (old != NULL)
		*old = v->contents[position];

	v->contents[position] = value;

	return 0;
}
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

int git_vector_verify_sorted(const git_vector *v)
{
	size_t i;

	if (!git_vector_is_sorted(v))
		return -1;

	for (i = 1; i < v->length; ++i) {
		if (v->_cmp(v->contents[i - 1], v->contents[i]) > 0)
			return -1;
	}

	return 0;
}