git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/8] evolve: Implement oid_array_contains_nondestructive

From
Ssxenos@google.com <sxenos@google.com>
Date
Jan 21, 2019, 22:32 UTC
Message-ID
<20190121223216.66659-2-sxenos@google.com>
In-Reply-To
<20190121223216.66659-1-sxenos@google.com>
From: Stefan Xenos <sxenos@gmail.com>

Implement a "contains_nondestructive" 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.
---
 sha1-array.c               | 15 +++++++++++++++
 sha1-array.h               |  2 ++
 t/helper/test-sha1-array.c |  6 ++++++
 t/t0064-sha1-array.sh      | 22 ++++++++++++++++++++++
 4 files changed, 45 insertions(+)
diff --git a/sha1-array.c b/sha1-array.c
index b94e0ec0f5..466937d73d 100644
--- a/sha1-array.c
+++ b/sha1-array.c
@@ -26,6 +26,21 @@ static const unsigned char *sha1_access(size_t index, void *table)
 	return array[index].hash;
 }
 
+int oid_array_contains_nondestructive(const struct oid_array* array,
+	const struct object_id* oid)
+{
+	int i;
+	if (array->sorted) {
+		return sha1_pos(oid->hash, array->oid, array->nr, sha1_access) >= 0;
+	}
+	for (i = 0; i < array->nr; i++) {
+		if (hashcmp(array->oid[i].hash, oid->hash) == 0) {
+			return 1;
+		}
+	}
+	return 0;
+}
+
 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
 {
 	if (!array->sorted)
diff --git a/sha1-array.h b/sha1-array.h
index 232bf95017..a887e7e4eb 100644
--- a/sha1-array.h
+++ b/sha1-array.h
@@ -13,6 +13,8 @@ struct oid_array {
 void oid_array_append(struct oid_array *array, const struct object_id *oid);
 int oid_array_lookup(struct oid_array *array, const struct object_id *oid);
 void oid_array_clear(struct oid_array *array);
+int oid_array_contains_nondestructive(const struct oid_array* array,
+	const struct object_id* oid);
 
 typedef int (*for_each_oid_fn)(const struct object_id *oid,
 			       void *data);
diff --git a/t/helper/test-sha1-array.c b/t/helper/test-sha1-array.c
index ad5e69f9d3..127a4c0b51 100644
--- a/t/helper/test-sha1-array.c
+++ b/t/helper/test-sha1-array.c
@@ -25,10 +25,16 @@ int cmd__sha1_array(int argc, const char **argv)
 			if (get_oid_hex(arg, &oid))
 				die("not a hexadecimal SHA1: %s", arg);
 			printf("%d\n", oid_array_lookup(&array, &oid));
+		} else if (skip_prefix(line.buf, "contains_nondestructive ", &arg)) {
+			if (get_oid_hex(arg, &oid))
+				die("not a hexadecimal SHA1: %s", arg);
+			printf("%d\n", oid_array_contains_nondestructive(&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-sha1-array.sh b/t/t0064-sha1-array.sh
index 5dda570b9a..05512b0461 100755
--- a/t/t0064-sha1-array.sh
+++ b/t/t0064-sha1-array.sh
@@ -32,6 +32,28 @@ test_expect_success 'ordered enumeration with duplicate suppression' '
 	test_cmp expect actual
 '
 
+test_expect_success 'contains_nondestructive finds existing' '
+	echo 1 > expect &&
+	echoid "" 88 44 aa 55 >> expect &&
+	{
+		echoid append 88 44 aa 55 &&
+		echoid contains_nondestructive 55 &&
+		echo for_each
+	} | test-tool sha1-array >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'contains_nondestructive non-existing query' '
+	echo 0 > expect &&
+	echoid "" 88 44 aa 55 >> expect &&
+	{
+		echoid append 88 44 aa 55 &&
+		echoid contains_nondestructive 33 &&
+		echo for_each
+	} | test-tool sha1-array >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'lookup' '
 	{
 		echoid append 88 44 aa 55 &&
-- 
2.20.1.321.g9e740568ce-goog
Previous: sxenos@google.comNext: Stefan Beller
Message 2 of 10 in “technical doc: add a design doc for the evolve command”
  1. 1/8 technical doc: add a design doc for the evolve commandsxenos@google.com, Jan 21, 2019
  2. 2/8 evolve: Implement oid_array_contains_nondestructivesxenos@google.com, Jan 21, 2019
  3. Stefan BellerJan 22, 2019
  4. Stefan XenosJan 27, 2019
  5. 3/8 evlove: Add the metas namespace to ref-filtersxenos@google.com, Jan 21, 2019
  6. 4/8 evolve: Add support for parsing metacommitssxenos@google.com, Jan 21, 2019
  7. 5/8 evolve: Add the change-table structuresxenos@google.com, Jan 21, 2019
  8. 6/8 evolve: Add support for writing metacommitssxenos@google.com, Jan 21, 2019
  9. 7/8 evolve: Implement the git change update commandsxenos@google.com, Jan 21, 2019
  10. 8/8 evolve: Add the git change list commandsxenos@google.com, Jan 21, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.