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