From: Chris Poucet <poucet@xxxxxxxxxx> Implement a "readonly_contains" function for oid_array that won't sort the array if it is unsorted. This can be used to test containment in the rare situations where the array order matters. The function has intentionally been given a name that is more cumbersome than the "lookup" function, which is what most callers will will want in most situations. Signed-off-by: Chris Poucet <poucet@xxxxxxxxxx> --- oid-array.c | 12 ++++++++++++ oid-array.h | 7 +++++++ t/helper/test-oid-array.c | 6 ++++++ t/t0064-oid-array.sh | 22 ++++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/oid-array.c b/oid-array.c index 73ba76e9e9a..1e12651d245 100644 --- a/oid-array.c +++ b/oid-array.c @@ -28,6 +28,18 @@ static const struct object_id *oid_access(size_t index, const void *table) return &array[index]; } +int oid_array_readonly_contains(const struct oid_array *array, + const struct object_id* oid) { + int i; + + if (array->sorted) + return oid_pos(oid, array->oid, array->nr, oid_access) >= 0; + for (i = 0; i < array->nr; i++) + if (oideq(&array->oid[i], oid)) + return 1; + return 0; +} + int oid_array_lookup(struct oid_array *array, const struct object_id *oid) { oid_array_sort(array); diff --git a/oid-array.h b/oid-array.h index f60f9af6741..e056eb61fa2 100644 --- a/oid-array.h +++ b/oid-array.h @@ -58,6 +58,13 @@ struct oid_array { #define OID_ARRAY_INIT { 0 } +/** + * Sees whether an array contains an object ID. Optimized for when the array is + * sorted but does not require the array to be sorted. + */ +int oid_array_readonly_contains(const struct oid_array *array, + const struct object_id* oid); + /** * Add an item to the set. The object ID will be placed at the end of the array * (but note that some operations below may lose this ordering). diff --git a/t/helper/test-oid-array.c b/t/helper/test-oid-array.c index d1324d086a2..0dbfc91ca8d 100644 --- a/t/helper/test-oid-array.c +++ b/t/helper/test-oid-array.c @@ -28,10 +28,16 @@ int cmd__oid_array(int argc, const char **argv) if (get_oid_hex(arg, &oid)) die("not a hexadecimal oid: %s", arg); printf("%d\n", oid_array_lookup(&array, &oid)); + } else if (skip_prefix(line.buf, "readonly_contains ", &arg)) { + if (get_oid_hex(arg, &oid)) + die("not a hexadecimal oid: %s", arg); + printf("%d\n", oid_array_readonly_contains(&array, &oid)); } else if (!strcmp(line.buf, "clear")) oid_array_clear(&array); else if (!strcmp(line.buf, "for_each_unique")) oid_array_for_each_unique(&array, print_oid, NULL); + else if (!strcmp(line.buf, "for_each")) + oid_array_for_each(&array, print_oid, NULL); else die("unknown command: %s", line.buf); } diff --git a/t/t0064-oid-array.sh b/t/t0064-oid-array.sh index 88c89e8f48a..aa677af132d 100755 --- a/t/t0064-oid-array.sh +++ b/t/t0064-oid-array.sh @@ -35,6 +35,28 @@ test_expect_success 'ordered enumeration with duplicate suppression' ' test_cmp expect actual ' +test_expect_success 'readonly_contains finds existing' ' + echo 1 >expect && + echoid "" 88 44 aa 55 >>expect && + { + echoid append 88 44 aa 55 && + echoid readonly_contains 55 && + echo for_each + } | test-tool oid-array >actual && + test_cmp expect actual +' + +test_expect_success 'readonly_contains non-existing query' ' + echo 0 >expect && + echoid "" 88 44 aa 55 >>expect && + { + echoid append 88 44 aa 55 && + echoid readonly_contains 33 && + echo for_each + } | test-tool oid-array >actual && + test_cmp expect actual +' + test_expect_success 'lookup' ' { echoid append 88 44 aa 55 && -- gitgitgadget