]> git.ipfire.org Git - thirdparty/git.git/blob - oid-array.c
rebase: allow overriding the maximal length of the generated labels
[thirdparty/git.git] / oid-array.c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "oid-array.h"
4 #include "hash-lookup.h"
5
6 void oid_array_append(struct oid_array *array, const struct object_id *oid)
7 {
8 ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
9 oidcpy(&array->oid[array->nr++], oid);
10 array->sorted = 0;
11 }
12
13 static int void_hashcmp(const void *a, const void *b)
14 {
15 return oidcmp(a, b);
16 }
17
18 void oid_array_sort(struct oid_array *array)
19 {
20 if (array->sorted)
21 return;
22 QSORT(array->oid, array->nr, void_hashcmp);
23 array->sorted = 1;
24 }
25
26 static const struct object_id *oid_access(size_t index, const void *table)
27 {
28 const struct object_id *array = table;
29 return &array[index];
30 }
31
32 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
33 {
34 oid_array_sort(array);
35 return oid_pos(oid, array->oid, array->nr, oid_access);
36 }
37
38 void oid_array_clear(struct oid_array *array)
39 {
40 FREE_AND_NULL(array->oid);
41 array->nr = 0;
42 array->alloc = 0;
43 array->sorted = 0;
44 }
45
46
47 int oid_array_for_each(struct oid_array *array,
48 for_each_oid_fn fn,
49 void *data)
50 {
51 size_t i;
52
53 /* No oid_array_sort() here! See oid-array.h */
54
55 for (i = 0; i < array->nr; i++) {
56 int ret = fn(array->oid + i, data);
57 if (ret)
58 return ret;
59 }
60 return 0;
61 }
62
63 int oid_array_for_each_unique(struct oid_array *array,
64 for_each_oid_fn fn,
65 void *data)
66 {
67 size_t i;
68
69 oid_array_sort(array);
70
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;
75 }
76 return 0;
77 }
78
79 void oid_array_filter(struct oid_array *array,
80 for_each_oid_fn want,
81 void *cb_data)
82 {
83 size_t nr = array->nr, src, dst;
84 struct object_id *oids = array->oid;
85
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++;
91 }
92 }
93 array->nr = dst;
94 }