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

[PATCH v2 3/3] [RFC] Add a sorted-list API for use-cases that require to get the element index.

From
Yann Dirson <ydirson@altern.org>
Date
Nov 6, 2010, 21:00 UTC
Message-ID
<1289077235-3208-4-git-send-email-ydirson@altern.org>
In-Reply-To
<1289077235-3208-1-git-send-email-ydirson@altern.org>

The idea was to replace index_name_pos(), but that would require a much larger API change in callers.

Signed-off-by: Yann Dirson <ydirson@altern.org>
---
 sorted-array.h |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/sorted-array.h b/sorted-array.h
index 03d5d1e..df07687 100644
--- a/sorted-array.h
+++ b/sorted-array.h
@@ -1,7 +1,7 @@
 #define declare_sorted_array(MAYBESTATIC,ELEMTYPE,LIST,CMP,INIT)	\
 MAYBESTATIC ELEMTYPE *LIST;						\
 MAYBESTATIC int LIST##_nr, LIST##_alloc;				\
-MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok)		\
+MAYBESTATIC int locate_##LIST##_idx(void *data, int insert_ok)		\
 {									\
 	int first, last;						\
 									\
@@ -12,7 +12,7 @@ MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok)		\
 		ELEMTYPE *nextelem = &(LIST[next]);			\
 		int cmp = CMP(data, nextelem);				\
 		if (!cmp)						\
-			return nextelem;				\
+			return next;					\
 		if (cmp < 0) {						\
 			last = next;					\
 			continue;					\
@@ -21,7 +21,7 @@ MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok)		\
 	}								\
 	/* not found */							\
 	if (!insert_ok)							\
-		return NULL;						\
+		return -first-1;					\
 	/* insert to make it at "first" */				\
 	if (LIST##_alloc <= LIST##_nr) {				\
 		LIST##_alloc = alloc_nr(LIST##_alloc);			\
@@ -32,5 +32,12 @@ MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok)		\
 		memmove(LIST + first + 1, LIST + first,			\
 			(LIST##_nr - first - 1) * sizeof(*LIST));	\
 	INIT(&LIST[first], data);					\
-	return &(LIST[first]);						\
+	return first;							\
+}									\
+MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok)		\
+{									\
+	int idx = locate_##LIST##_idx(data, insert_ok);			\
+	if (idx < 0)							\
+		return NULL;						\
+	return &(LIST[idx]);						\
 }
-- 
1.7.2.3
Previous: Yann Dirson
Message 4 of 4 in “generalizing sorted-array handling”
  1. generalizing sorted-array handlingYann Dirson, Nov 6, 2010
  2. 1/3 Introduce sorted-array binary-search function.Yann Dirson, Nov 6, 2010
  3. 2/3 Convert diffcore-rename's rename_src to the new sorted-array API.Yann Dirson, Nov 6, 2010
  4. 3/3 [RFC] Add a sorted-list API for use-cases that require to get the element index.Yann Dirson, Nov 6, 2010

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.