refs: introduce missing functions that accept a `struct ref_store`
[git.git] / oid-array.c
blob1f36651754edc47f733bc51bbecd716deee31ed4
1 #include "git-compat-util.h"
2 #include "oid-array.h"
3 #include "hash-lookup.h"
5 void oid_array_append(struct oid_array *array, const struct object_id *oid)
7 ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
8 oidcpy(&array->oid[array->nr++], oid);
9 if (!oid->algo)
10 oid_set_algo(&array->oid[array->nr - 1], the_hash_algo);
11 array->sorted = 0;
14 static int void_hashcmp(const void *va, const void *vb)
16 const struct object_id *a = va, *b = vb;
17 int ret;
18 if (a->algo == b->algo)
19 ret = oidcmp(a, b);
20 else
21 ret = a->algo > b->algo ? 1 : -1;
22 return ret;
25 void oid_array_sort(struct oid_array *array)
27 if (array->sorted)
28 return;
29 QSORT(array->oid, array->nr, void_hashcmp);
30 array->sorted = 1;
33 static const struct object_id *oid_access(size_t index, const void *table)
35 const struct object_id *array = table;
36 return &array[index];
39 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
41 oid_array_sort(array);
42 return oid_pos(oid, array->oid, array->nr, oid_access);
45 void oid_array_clear(struct oid_array *array)
47 FREE_AND_NULL(array->oid);
48 array->nr = 0;
49 array->alloc = 0;
50 array->sorted = 0;
54 int oid_array_for_each(struct oid_array *array,
55 for_each_oid_fn fn,
56 void *data)
58 size_t i;
60 /* No oid_array_sort() here! See oid-array.h */
62 for (i = 0; i < array->nr; i++) {
63 int ret = fn(array->oid + i, data);
64 if (ret)
65 return ret;
67 return 0;
70 int oid_array_for_each_unique(struct oid_array *array,
71 for_each_oid_fn fn,
72 void *data)
74 size_t i;
76 oid_array_sort(array);
78 for (i = 0; i < array->nr; i = oid_array_next_unique(array, i)) {
79 int ret = fn(array->oid + i, data);
80 if (ret)
81 return ret;
83 return 0;
86 void oid_array_filter(struct oid_array *array,
87 for_each_oid_fn want,
88 void *cb_data)
90 size_t nr = array->nr, src, dst;
91 struct object_id *oids = array->oid;
93 for (src = dst = 0; src < nr; src++) {
94 if (want(&oids[src], cb_data)) {
95 if (src != dst)
96 oidcpy(&oids[dst], &oids[src]);
97 dst++;
100 array->nr = dst;