Make sha1_array_append take a struct object_id *
[git/git.git] / sha1-array.c
CommitLineData
902bb364
JK
1#include "cache.h"
2#include "sha1-array.h"
3#include "sha1-lookup.h"
4
98a72ddc 5void sha1_array_append(struct sha1_array *array, const struct object_id *oid)
902bb364 6{
ee3051bd 7 ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
98a72ddc 8 oidcpy(&array->oid[array->nr++], oid);
902bb364
JK
9 array->sorted = 0;
10}
11
12static int void_hashcmp(const void *a, const void *b)
13{
ee3051bd 14 return oidcmp(a, b);
902bb364
JK
15}
16
69850be4 17static void sha1_array_sort(struct sha1_array *array)
902bb364 18{
ee3051bd 19 QSORT(array->oid, array->nr, void_hashcmp);
902bb364
JK
20 array->sorted = 1;
21}
22
23static const unsigned char *sha1_access(size_t index, void *table)
24{
ee3051bd 25 struct object_id *array = table;
26 return array[index].hash;
902bb364
JK
27}
28
29int sha1_array_lookup(struct sha1_array *array, const unsigned char *sha1)
30{
31 if (!array->sorted)
32 sha1_array_sort(array);
ee3051bd 33 return sha1_pos(sha1, array->oid, array->nr, sha1_access);
902bb364
JK
34}
35
36void sha1_array_clear(struct sha1_array *array)
37{
ee3051bd 38 free(array->oid);
39 array->oid = NULL;
902bb364
JK
40 array->nr = 0;
41 array->alloc = 0;
42 array->sorted = 0;
43}
cff38a5e 44
16ddcd40 45int sha1_array_for_each_unique(struct sha1_array *array,
cff38a5e
JK
46 for_each_sha1_fn fn,
47 void *data)
48{
49 int i;
50
51 if (!array->sorted)
52 sha1_array_sort(array);
53
54 for (i = 0; i < array->nr; i++) {
16ddcd40 55 int ret;
ee3051bd 56 if (i > 0 && !oidcmp(array->oid + i, array->oid + i - 1))
cff38a5e 57 continue;
ee3051bd 58 ret = fn(array->oid[i].hash, data);
16ddcd40
JK
59 if (ret)
60 return ret;
cff38a5e 61 }
16ddcd40 62 return 0;
cff38a5e 63}