summaryrefslogtreecommitdiff
path: root/src/libgit2/oidarray.c
blob: 37f67756aa5c8dfb4baed56818ae9fc1a193d48a (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
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "oidarray.h"

#include "git2/oidarray.h"
#include "array.h"

void git_oidarray_dispose(git_oidarray *arr)
{
	git__free(arr->ids);
}

void git_oidarray__from_array(git_oidarray *out, const git_array_oid_t *array)
{
	out->count = array->size;
	out->ids = array->ptr;
}

void git_oidarray__to_array(git_array_oid_t *out, const git_oidarray *array)
{
	out->ptr = array->ids;
	out->size = array->count;
	out->asize = array->count;
}

void git_oidarray__reverse(git_oidarray *arr)
{
	size_t i;
	git_oid tmp;

	for (i = 0; i < arr->count / 2; i++) {
		git_oid_cpy(&tmp, &arr->ids[i]);
		git_oid_cpy(&arr->ids[i], &arr->ids[(arr->count-1)-i]);
		git_oid_cpy(&arr->ids[(arr->count-1)-i], &tmp);
	}
}

int git_oidarray__add(git_array_oid_t *arr, git_oid *id)
{
	git_oid *add, *iter;
	size_t i;

	git_array_foreach(*arr, i, iter) {
		if (git_oid_cmp(iter, id) == 0)
			return 0;
	}

	if ((add = git_array_alloc(*arr)) == NULL)
		return -1;

	git_oid_cpy(add, id);
	return 0;
}

bool git_oidarray__remove(git_array_oid_t *arr, git_oid *id)
{
	bool found = false;
	size_t remain, i;
	git_oid *iter;

	git_array_foreach(*arr, i, iter) {
		if (git_oid_cmp(iter, id) == 0) {
			arr->size--;
			remain = arr->size - i;

			if (remain > 0)
				memmove(&arr->ptr[i], &arr->ptr[i+1], remain * sizeof(git_oid));

			found = true;
			break;
		}
	}

	return found;
}

#ifndef GIT_DEPRECATE_HARD

void git_oidarray_free(git_oidarray *arr)
{
	git_oidarray_dispose(arr);
}

#endif