]>
Commit | Line | Data |
---|---|---|
1 | #include "git-compat-util.h" | |
2 | #include "oid-array.h" | |
3 | #include "hash-lookup.h" | |
4 | ||
5 | void oid_array_append(struct oid_array *array, const struct object_id *oid) | |
6 | { | |
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; | |
12 | } | |
13 | ||
14 | static int void_hashcmp(const void *va, const void *vb) | |
15 | { | |
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; | |
23 | } | |
24 | ||
25 | void oid_array_sort(struct oid_array *array) | |
26 | { | |
27 | if (array->sorted) | |
28 | return; | |
29 | QSORT(array->oid, array->nr, void_hashcmp); | |
30 | array->sorted = 1; | |
31 | } | |
32 | ||
33 | static const struct object_id *oid_access(size_t index, const void *table) | |
34 | { | |
35 | const struct object_id *array = table; | |
36 | return &array[index]; | |
37 | } | |
38 | ||
39 | int oid_array_lookup(struct oid_array *array, const struct object_id *oid) | |
40 | { | |
41 | oid_array_sort(array); | |
42 | return oid_pos(oid, array->oid, array->nr, oid_access); | |
43 | } | |
44 | ||
45 | void oid_array_clear(struct oid_array *array) | |
46 | { | |
47 | FREE_AND_NULL(array->oid); | |
48 | array->nr = 0; | |
49 | array->alloc = 0; | |
50 | array->sorted = 0; | |
51 | } | |
52 | ||
53 | ||
54 | int oid_array_for_each(struct oid_array *array, | |
55 | for_each_oid_fn fn, | |
56 | void *data) | |
57 | { | |
58 | size_t i; | |
59 | ||
60 | /* No oid_array_sort() here! See oid-array.h */ | |
61 | ||
62 | for (i = 0; i < array->nr; i++) { | |
63 | int ret = fn(array->oid + i, data); | |
64 | if (ret) | |
65 | return ret; | |
66 | } | |
67 | return 0; | |
68 | } | |
69 | ||
70 | int oid_array_for_each_unique(struct oid_array *array, | |
71 | for_each_oid_fn fn, | |
72 | void *data) | |
73 | { | |
74 | size_t i; | |
75 | ||
76 | oid_array_sort(array); | |
77 | ||
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; | |
82 | } | |
83 | return 0; | |
84 | } | |
85 | ||
86 | void oid_array_filter(struct oid_array *array, | |
87 | for_each_oid_fn want, | |
88 | void *cb_data) | |
89 | { | |
90 | size_t nr = array->nr, src, dst; | |
91 | struct object_id *oids = array->oid; | |
92 | ||
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++; | |
98 | } | |
99 | } | |
100 | array->nr = dst; | |
101 | } |