]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
modula2: Add FindIndice to library module gm2-libs/Indexing
authorGaius Mulley <gaiusmod2@gmail.com>
Tue, 1 Oct 2024 14:06:54 +0000 (15:06 +0100)
committerGaius Mulley <gaiusmod2@gmail.com>
Tue, 1 Oct 2024 14:06:54 +0000 (15:06 +0100)
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
gcc/m2/gm2-libs/Indexing.mod

index f7c4676df33a420f853b0b08ccda73bb4e2e083a..0b56043f20a744077aa6dfc47eb8ad654fbd40fa 100644 (file)
@@ -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.
index 08af13484d086e2f2190a2de7c573c0fd981ffab..7bcaf87e278eb97fad74654ea05f7d09bbd93c6d 100644 (file)
@@ -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])
 *)