@@ -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);
@@ -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).
@@ -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);
}
@@ -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 &&