Merge branch 'rs/simplify-by-deco-with-deco-refs-exclude'
[git/git.git] / sha1-array.c
CommitLineData
902bb364
JK
1#include "cache.h"
2#include "sha1-array.h"
3#include "sha1-lookup.h"
4
910650d2 5void oid_array_append(struct oid_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
910650d2 17static void oid_array_sort(struct oid_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
910650d2 29int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
902bb364
JK
30{
31 if (!array->sorted)
910650d2 32 oid_array_sort(array);
5d3206d5 33 return sha1_pos(oid->hash, array->oid, array->nr, sha1_access);
902bb364
JK
34}
35
910650d2 36void oid_array_clear(struct oid_array *array)
902bb364 37{
6a83d902 38 FREE_AND_NULL(array->oid);
902bb364
JK
39 array->nr = 0;
40 array->alloc = 0;
41 array->sorted = 0;
42}
cff38a5e 43
5cc044e0
ÆAB
44
45int oid_array_for_each(struct oid_array *array,
46 for_each_oid_fn fn,
47 void *data)
48{
49 int i;
50
51 /* No oid_array_sort() here! See the api-oid-array.txt docs! */
52
53 for (i = 0; i < array->nr; i++) {
54 int ret = fn(array->oid + i, data);
55 if (ret)
56 return ret;
57 }
58 return 0;
59}
60
910650d2 61int oid_array_for_each_unique(struct oid_array *array,
72486729
ÆAB
62 for_each_oid_fn fn,
63 void *data)
cff38a5e
JK
64{
65 int i;
66
67 if (!array->sorted)
910650d2 68 oid_array_sort(array);
cff38a5e
JK
69
70 for (i = 0; i < array->nr; i++) {
16ddcd40 71 int ret;
4a7e27e9 72 if (i > 0 && oideq(array->oid + i, array->oid + i - 1))
cff38a5e 73 continue;
1b7ba794 74 ret = fn(array->oid + i, data);
16ddcd40
JK
75 if (ret)
76 return ret;
cff38a5e 77 }
16ddcd40 78 return 0;
cff38a5e 79}
161b1cf3
SB
80
81void oid_array_filter(struct oid_array *array,
82 for_each_oid_fn want,
83 void *cb_data)
84{
85 unsigned nr = array->nr, src, dst;
86 struct object_id *oids = array->oid;
87
88 for (src = dst = 0; src < nr; src++) {
89 if (want(&oids[src], cb_data)) {
90 if (src != dst)
91 oidcpy(&oids[dst], &oids[src]);
92 dst++;
93 }
94 }
95 array->nr = dst;
96}