From 8273e31adfa1ba5f0722eb37bcc8aeca8718a472 Mon Sep 17 00:00:00 2001
From: Gaius Mulley <gaiusmod2@gmail.com>
Date: Tue, 1 Oct 2024 15:06:54 +0100
Subject: [PATCH] modula2: Add FindIndice to library module gm2-libs/Indexing

This patch introduces the procedure function FindIndice to library
module Indexing.

gcc/m2/ChangeLog:

	* gm2-libs/Indexing.def (FindIndice): New procedure
	function.
	* gm2-libs/Indexing.mod (FindIndice): Implement new
	procedure function.

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
---
 gcc/m2/gm2-libs/Indexing.def |  8 ++++++++
 gcc/m2/gm2-libs/Indexing.mod | 28 ++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/gcc/m2/gm2-libs/Indexing.def b/gcc/m2/gm2-libs/Indexing.def
index f7c4676df33a..0b56043f20a7 100644
--- a/gcc/m2/gm2-libs/Indexing.def
+++ b/gcc/m2/gm2-libs/Indexing.def
@@ -144,4 +144,12 @@ PROCEDURE ForeachIndiceInIndexDo (i: Index; p: IndexProcedure) ;
 PROCEDURE IsEmpty (i: Index) : BOOLEAN ;
 
 
+(*
+   FindIndice - returns the indice containing a.
+                It returns zero if a is not found in array i.
+*)
+
+PROCEDURE FindIndice (i: Index; a: ADDRESS) : CARDINAL ;
+
+
 END Indexing.
diff --git a/gcc/m2/gm2-libs/Indexing.mod b/gcc/m2/gm2-libs/Indexing.mod
index 08af13484d08..7bcaf87e278e 100644
--- a/gcc/m2/gm2-libs/Indexing.mod
+++ b/gcc/m2/gm2-libs/Indexing.mod
@@ -342,6 +342,34 @@ BEGIN
 END IncludeIndiceIntoIndex ;
 
 
+(*
+   FindIndice - returns the indice containing a.
+                It returns zero if a is not found in array i.
+*)
+
+PROCEDURE FindIndice (i: Index; a: ADDRESS) : CARDINAL ;
+VAR
+   j: CARDINAL ;
+   p: PtrToAddress ;
+   b: PtrToByte ;
+BEGIN
+   WITH i^ DO
+      j := Low ;
+      b := ArrayStart ;
+      WHILE j <= High DO
+         p := VAL (PtrToAddress, b) ;
+         INC (b, TSIZE (ADDRESS)) ;
+         IF p^ = a
+         THEN
+            RETURN j
+         END ;
+         INC (j)
+      END
+   END ;
+   RETURN 0
+END FindIndice ;
+
+
 (*
    ForeachIndiceInIndexDo - for each j indice of i, call procedure p(i[j])
 *)
-- 
GitLab