vector.c 7.92 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
 */

#include "common.h"
#include "vector.h"
10
#include "integer.h"
11

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

15
GIT_INLINE(size_t) compute_new_size(git_vector *v)
16
{
17 18 19 20 21 22
	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
23 24
	else if (new_size <= (SIZE_MAX / 3) * 2)
		new_size += new_size / 2;
25 26 27 28 29
	else
		new_size = SIZE_MAX;

	return new_size;
}
30

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

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

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

41
	return 0;
42 43
}

44 45 46 47 48 49 50
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);
}

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

55 56
	assert(v && src);

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

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

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

	return 0;
}

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

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

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

84
void git_vector_free_deep(git_vector *v)
Russell Belfer committed
85 86 87 88 89 90 91 92 93 94 95 96 97
{
	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);
}

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

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

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

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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;
}

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

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

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

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

139
	return 0;
140 141
}

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

	assert(v && v->_cmp);

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

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

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

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

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

173
	return 0;
174 175
}

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

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

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

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

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

200 201
	git_vector_sort(v);

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

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

	assert(v && key && key_lookup);

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

			return 0;
		}
219 220
	}

221
	return GIT_ENOTFOUND;
222 223
}

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

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

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

	assert(v);

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

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

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

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

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

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

	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)
271 272 273 274
		if (!cmp(v->contents[i], v->contents[j])) {
			if (git_free_cb)
				git_free_cb(v->contents[i]);

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

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

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

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

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

	v->length = i;
}

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

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

310
	assert(a && b);
311

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

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

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

	v->length = new_length;

	return 0;
}

334
int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len)
335
{
336
	size_t new_length;
337

338
	assert(insert_len > 0 && idx <= v->length);
339

340
	GITERR_CHECK_ALLOC_ADD(&new_length, v->length, insert_len);
341 342

	if (new_length > v->_alloc_size && resize_vector(v, new_length) < 0)
343 344
		return -1;

345
	memmove(&v->contents[idx + insert_len], &v->contents[idx],
346
		sizeof(void *) * (v->length - idx));
347
	memset(&v->contents[idx], 0, sizeof(void *) * insert_len);
348 349 350 351 352

	v->length = new_length;
	return 0;
}

353
int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len)
354
{
355
	size_t new_length = v->length - remove_len;
356 357
	size_t end_idx = 0;
	
358
	assert(remove_len > 0);
359

360
	if (git__add_sizet_overflow(&end_idx, idx, remove_len))
361
		assert(0);
362

363
	assert(end_idx <= v->length);
364

365
	if (end_idx < v->length)
366
		memmove(&v->contents[idx], &v->contents[end_idx],
367
			sizeof(void *) * (v->length - end_idx));
368

369
	memset(&v->contents[new_length], 0, sizeof(void *) * remove_len);
370 371 372 373 374

	v->length = new_length;
	return 0;
}

375 376
int git_vector_set(void **old, git_vector *v, size_t position, void *value)
{
377 378 379 380
	if (position + 1 > v->length) {
		if (git_vector_resize_to(v, position + 1) < 0)
			return -1;
	}
381 382 383 384 385 386 387 388

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

	v->contents[position] = value;

	return 0;
}
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

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;
}
404 405 406 407 408

void git_vector_reverse(git_vector *v)
{
	size_t a, b;

409 410 411
	if (v->length == 0)
		return;

412 413 414 415 416 417 418 419 420 421 422
	a = 0;
	b = v->length - 1;

	while (a < b) {
		void *tmp = v->contents[a];
		v->contents[a] = v->contents[b];
		v->contents[b] = tmp;
		a++;
		b--;
	}
}