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

[PATCH 02/10] sha1-array: implement oid_array_readonly_contains

From
CGChris Poucet via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 23, 2022, 18:55 UTC
Message-ID
<84588312c1d4a62ff6c6211e85b4e58ab0563daa.1663959324.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1356.git.1663959324.gitgitgadget@gmail.com>
From: Chris Poucet <poucet@google.com>

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@google.com>
---
 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
Previous: Christophe Poucet via GitGitGadgetNext: Phillip Wood
Message 2 of 66 in “Add the Git Change command”
  1. 00/10 Add the Git Change commandChristophe Poucet via GitGitGadget, Sep 23, 2022
  2. 02/10 sha1-array: implement oid_array_readonly_containsChris Poucet via GitGitGadget, Sep 23, 2022
  3. Phillip WoodSep 26, 2022
  4. 03/10 ref-filter: add the metas namespace to ref-filterChris Poucet via GitGitGadget, Sep 23, 2022
  5. Phillip WoodSep 26, 2022
  6. Chris POct 4, 2022
  7. 04/10 evolve: add support for parsing metacommitsStefan Xenos via GitGitGadget, Sep 23, 2022
  8. Phillip WoodSep 26, 2022
  9. Chris POct 4, 2022
  10. Phillip WoodOct 4, 2022
  11. 01/10 technical doc: add a design doc for the evolve commandStefan Xenos via GitGitGadget, Sep 23, 2022
  12. Jerry ZhangSep 23, 2022
  13. Junio C HamanoSep 28, 2022
  14. Junio C HamanoSep 28, 2022
  15. Phillip WoodSep 29, 2022
  16. Jonathan TanSep 29, 2022
  17. 05/10 evolve: add the change-table structureStefan Xenos via GitGitGadget, Sep 23, 2022
  18. Phillip WoodSep 27, 2022
  19. Ævar Arnfjörð BjarmasonSep 27, 2022
  20. Phillip WoodSep 27, 2022
  21. Ævar Arnfjörð BjarmasonSep 27, 2022
  22. Phillip WoodSep 28, 2022
  23. Ævar Arnfjörð BjarmasonSep 28, 2022
  24. Junio C HamanoSep 28, 2022
  25. Phillip WoodSep 27, 2022
  26. Chris POct 4, 2022
  27. 06/10 evolve: add support for writing metacommitsStefan Xenos via GitGitGadget, Sep 23, 2022
  28. Phillip WoodSep 28, 2022
  29. Chris POct 5, 2022
  30. Phillip WoodOct 5, 2022
  31. 10/10 evolve: add documentation for `git change`Chris Poucet via GitGitGadget, Sep 23, 2022
  32. Phillip WoodSep 25, 2022
  33. 07/10 evolve: implement the git change commandStefan Xenos via GitGitGadget, Sep 23, 2022
  34. Phillip WoodSep 25, 2022
  35. Ævar Arnfjörð BjarmasonSep 26, 2022
  36. Ævar Arnfjörð BjarmasonSep 26, 2022
  37. Chris POct 5, 2022
  38. 09/10 evolve: add delete commandChris Poucet via GitGitGadget, Sep 23, 2022
  39. Ævar Arnfjörð BjarmasonSep 26, 2022
  40. Chris PoucetSep 26, 2022
  41. 08/10 evolve: add the git change list commandStefan Xenos via GitGitGadget, Sep 23, 2022
  42. Phillip WoodSep 25, 2022
  43. Chris POct 4, 2022
  44. Phillip WoodOct 4, 2022
  45. Chris POct 4, 2022
  46. Chris POct 4, 2022
  47. Phillip WoodOct 4, 2022
  48. Phillip WoodOct 4, 2022
  49. 00/10 RFC: Git Evolve / ChangeChristophe Poucet via GitGitGadget, Oct 5, 2022
  50. 03/10 ref-filter: add the metas namespace to ref-filterChris Poucet via GitGitGadget, Oct 5, 2022
  51. 02/10 sha1-array: implement oid_array_readonly_containsChris Poucet via GitGitGadget, Oct 5, 2022
  52. 05/10 evolve: add the change-table structureStefan Xenos via GitGitGadget, Oct 5, 2022
  53. 04/10 evolve: add support for parsing metacommitsStefan Xenos via GitGitGadget, Oct 5, 2022
  54. 06/10 evolve: add support for writing metacommitsStefan Xenos via GitGitGadget, Oct 5, 2022
  55. 01/10 technical doc: add a design doc for the evolve commandStefan Xenos via GitGitGadget, Oct 5, 2022
  56. Chris PoucetOct 5, 2022
  57. Glen ChooOct 6, 2022
  58. Victoria DyeOct 10, 2022
  59. Phillip WoodOct 11, 2022
  60. Victoria DyeOct 11, 2022
  61. Phillip WoodOct 12, 2022
  62. 07/10 evolve: implement the git change commandStefan Xenos via GitGitGadget, Oct 5, 2022
  63. 09/10 evolve: add documentation for `git change`Chris Poucet via GitGitGadget, Oct 5, 2022
  64. 08/10 evolve: add delete commandChris Poucet via GitGitGadget, Oct 5, 2022
  65. 10/10 evolve: add tests for the git-change commandChris Poucet via GitGitGadget, Oct 5, 2022
  66. Phillip WoodOct 10, 2022

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.