environment: move object database functions into object layer
[alt-git.git] / oid-array.c
blob9cac9743955d8ff1353a1523de86df20ce09a5b9
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "oid-array.h"
5 #include "hash-lookup.h"
7 void oid_array_append(struct oid_array *array, const struct object_id *oid)
9 ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
10 oidcpy(&array->oid[array->nr++], oid);
11 if (!oid->algo)
12 oid_set_algo(&array->oid[array->nr - 1], the_hash_algo);
13 array->sorted = 0;
16 static int void_hashcmp(const void *va, const void *vb)
18 const struct object_id *a = va, *b = vb;
19 int ret;
20 if (a->algo == b->algo)
21 ret = oidcmp(a, b);
22 else
23 ret = a->algo > b->algo ? 1 : -1;
24 return ret;
27 void oid_array_sort(struct oid_array *array)
29 if (array->sorted)
30 return;
31 QSORT(array->oid, array->nr, void_hashcmp);
32 array->sorted = 1;
35 static const struct object_id *oid_access(size_t index, const void *table)
37 const struct object_id *array = table;
38 return &array[index];
41 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
43 oid_array_sort(array);
44 return oid_pos(oid, array->oid, array->nr, oid_access);
47 void oid_array_clear(struct oid_array *array)
49 FREE_AND_NULL(array->oid);
50 array->nr = 0;
51 array->alloc = 0;
52 array->sorted = 0;
56 int oid_array_for_each(struct oid_array *array,
57 for_each_oid_fn fn,
58 void *data)
60 size_t i;
62 /* No oid_array_sort() here! See oid-array.h */
64 for (i = 0; i < array->nr; i++) {
65 int ret = fn(array->oid + i, data);
66 if (ret)
67 return ret;
69 return 0;
72 int oid_array_for_each_unique(struct oid_array *array,
73 for_each_oid_fn fn,
74 void *data)
76 size_t i;
78 oid_array_sort(array);
80 for (i = 0; i < array->nr; i = oid_array_next_unique(array, i)) {
81 int ret = fn(array->oid + i, data);
82 if (ret)
83 return ret;
85 return 0;
88 void oid_array_filter(struct oid_array *array,
89 for_each_oid_fn want,
90 void *cb_data)
92 size_t nr = array->nr, src, dst;
93 struct object_id *oids = array->oid;
95 for (src = dst = 0; src < nr; src++) {
96 if (want(&oids[src], cb_data)) {
97 if (src != dst)
98 oidcpy(&oids[dst], &oids[src]);
99 dst++;
102 array->nr = dst;