]>
Commit | Line | Data |
---|---|---|
902bb364 | 1 | #include "cache.h" |
fe299ec5 | 2 | #include "oid-array.h" |
902bb364 JK |
3 | #include "sha1-lookup.h" |
4 | ||
910650d2 | 5 | void oid_array_append(struct oid_array *array, const struct object_id *oid) |
902bb364 | 6 | { |
ee3051bd | 7 | ALLOC_GROW(array->oid, array->nr + 1, array->alloc); |
98a72ddc | 8 | oidcpy(&array->oid[array->nr++], oid); |
902bb364 JK |
9 | array->sorted = 0; |
10 | } | |
11 | ||
12 | static int void_hashcmp(const void *a, const void *b) | |
13 | { | |
ee3051bd | 14 | return oidcmp(a, b); |
902bb364 JK |
15 | } |
16 | ||
d0482b44 | 17 | void oid_array_sort(struct oid_array *array) |
902bb364 | 18 | { |
d0482b44 JK |
19 | if (array->sorted) |
20 | return; | |
ee3051bd | 21 | QSORT(array->oid, array->nr, void_hashcmp); |
902bb364 JK |
22 | array->sorted = 1; |
23 | } | |
24 | ||
25 | static const unsigned char *sha1_access(size_t index, void *table) | |
26 | { | |
ee3051bd | 27 | struct object_id *array = table; |
28 | return array[index].hash; | |
902bb364 JK |
29 | } |
30 | ||
910650d2 | 31 | int oid_array_lookup(struct oid_array *array, const struct object_id *oid) |
902bb364 | 32 | { |
d0482b44 | 33 | oid_array_sort(array); |
7a7d992d | 34 | return hash_pos(oid->hash, array->oid, array->nr, sha1_access); |
902bb364 JK |
35 | } |
36 | ||
910650d2 | 37 | void oid_array_clear(struct oid_array *array) |
902bb364 | 38 | { |
6a83d902 | 39 | FREE_AND_NULL(array->oid); |
902bb364 JK |
40 | array->nr = 0; |
41 | array->alloc = 0; | |
42 | array->sorted = 0; | |
43 | } | |
cff38a5e | 44 | |
5cc044e0 ÆAB |
45 | |
46 | int oid_array_for_each(struct oid_array *array, | |
47 | for_each_oid_fn fn, | |
48 | void *data) | |
49 | { | |
eccce525 | 50 | size_t i; |
5cc044e0 | 51 | |
fe299ec5 | 52 | /* No oid_array_sort() here! See oid-array.h */ |
5cc044e0 ÆAB |
53 | |
54 | for (i = 0; i < array->nr; i++) { | |
55 | int ret = fn(array->oid + i, data); | |
56 | if (ret) | |
57 | return ret; | |
58 | } | |
59 | return 0; | |
60 | } | |
61 | ||
910650d2 | 62 | int oid_array_for_each_unique(struct oid_array *array, |
72486729 ÆAB |
63 | for_each_oid_fn fn, |
64 | void *data) | |
cff38a5e | 65 | { |
eccce525 | 66 | size_t i; |
cff38a5e | 67 | |
d0482b44 | 68 | oid_array_sort(array); |
cff38a5e | 69 | |
12c4b4ce JK |
70 | for (i = 0; i < array->nr; i = oid_array_next_unique(array, i)) { |
71 | int ret = fn(array->oid + i, data); | |
16ddcd40 JK |
72 | if (ret) |
73 | return ret; | |
cff38a5e | 74 | } |
16ddcd40 | 75 | return 0; |
cff38a5e | 76 | } |
161b1cf3 SB |
77 | |
78 | void oid_array_filter(struct oid_array *array, | |
79 | for_each_oid_fn want, | |
80 | void *cb_data) | |
81 | { | |
eccce525 | 82 | size_t nr = array->nr, src, dst; |
161b1cf3 SB |
83 | struct object_id *oids = array->oid; |
84 | ||
85 | for (src = dst = 0; src < nr; src++) { | |
86 | if (want(&oids[src], cb_data)) { | |
87 | if (src != dst) | |
88 | oidcpy(&oids[dst], &oids[src]); | |
89 | dst++; | |
90 | } | |
91 | } | |
92 | array->nr = dst; | |
93 | } |