Merge branch 'jk/log-follow-with-non-literal-pathspec'
[alt-git.git] / oid-array.c
blobe8228c777b1619d6bd1baaac0e24f14124d3e4d3
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "oid-array.h"
4 #include "hash-lookup.h"
6 void oid_array_append(struct oid_array *array, const struct object_id *oid)
8 ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
9 oidcpy(&array->oid[array->nr++], oid);
10 array->sorted = 0;
13 static int void_hashcmp(const void *a, const void *b)
15 return oidcmp(a, b);
18 void oid_array_sort(struct oid_array *array)
20 if (array->sorted)
21 return;
22 QSORT(array->oid, array->nr, void_hashcmp);
23 array->sorted = 1;
26 static const struct object_id *oid_access(size_t index, const void *table)
28 const struct object_id *array = table;
29 return &array[index];
32 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
34 oid_array_sort(array);
35 return oid_pos(oid, array->oid, array->nr, oid_access);
38 void oid_array_clear(struct oid_array *array)
40 FREE_AND_NULL(array->oid);
41 array->nr = 0;
42 array->alloc = 0;
43 array->sorted = 0;
47 int oid_array_for_each(struct oid_array *array,
48 for_each_oid_fn fn,
49 void *data)
51 size_t i;
53 /* No oid_array_sort() here! See oid-array.h */
55 for (i = 0; i < array->nr; i++) {
56 int ret = fn(array->oid + i, data);
57 if (ret)
58 return ret;
60 return 0;
63 int oid_array_for_each_unique(struct oid_array *array,
64 for_each_oid_fn fn,
65 void *data)
67 size_t i;
69 oid_array_sort(array);
71 for (i = 0; i < array->nr; i = oid_array_next_unique(array, i)) {
72 int ret = fn(array->oid + i, data);
73 if (ret)
74 return ret;
76 return 0;
79 void oid_array_filter(struct oid_array *array,
80 for_each_oid_fn want,
81 void *cb_data)
83 size_t nr = array->nr, src, dst;
84 struct object_id *oids = array->oid;
86 for (src = dst = 0; src < nr; src++) {
87 if (want(&oids[src], cb_data)) {
88 if (src != dst)
89 oidcpy(&oids[dst], &oids[src]);
90 dst++;
93 array->nr = dst;