From e6c4873789ae1ed1b0e7c0787b3a53f12251d6fd Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 6 Oct 2021 17:30:12 +0000 Subject: [PATCH] added the Find function in h3-arrays.adb --- lib2/h3-arrays.adb | 26 +++++++++++++++++++++++++- lib2/h3-arrays.ads | 3 +-- lib2/hello.adb | 10 +++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib2/h3-arrays.adb b/lib2/h3-arrays.adb index 4e2f4c3..9d737e4 100644 --- a/lib2/h3-arrays.adb +++ b/lib2/h3-arrays.adb @@ -320,8 +320,32 @@ package body H3.Arrays is return System_Size'First; end Find; - --function Find (Obj: in Elastic_Array; V: In Item_Array; From_Pos: in System_Index; Find_Dir: in Direction := DIRECTION_FORWARD); + function Find (Obj: in Elastic_Array; V: In Item_Array; Start_Pos: in System_Index; Find_Dir: in Direction := DIRECTION_FORWARD) return System_Size is + End_Pos: System_Size; + begin + if Get_Length(Obj) > 0 and then V'Length > 0 and then V'Length <= Get_Length(Obj) then + End_Pos := Get_Last_Index(Obj) - V'Length + 1; + + if Find_Dir = DIRECTION_FORWARD then + for i in Start_Pos .. End_Pos loop + if Obj.Buffer.Slot(i .. i + V'Length - 1) = V then + return i; + end if; + end loop; + else + if Start_Pos < End_Pos then + End_Pos := Start_Pos; + end if; + for i in reverse Get_First_Index(Obj) .. End_Pos loop + if Obj.Buffer.Slot(i .. i + V'Length - 1) = V then + return i; + end if; + end loop; + end if; + end if; + return System_Size'First; + end Find; function "=" (Obj: in Elastic_Array; Obj2: in Elastic_Array) return Standard.Boolean is begin diff --git a/lib2/h3-arrays.ads b/lib2/h3-arrays.ads index f65aa6e..09decde 100644 --- a/lib2/h3-arrays.ads +++ b/lib2/h3-arrays.ads @@ -62,9 +62,8 @@ package H3.Arrays is procedure Delete (Obj: in out Elastic_Array; From_Pos: in System_Index; To_Pos: in System_Size); - function Find (Obj: in Elastic_Array; V: In Item_Type; Start_Pos: in System_Index; Find_Dir: in Direction := DIRECTION_FORWARD) return System_Size; - --function Find (Obj: in Elastic_Array; V: In Item_Array; Start_Pos: in System_Index; Find_Dir: in Direction := DIRECTION_FORWARD); + function Find (Obj: in Elastic_Array; V: In Item_Array; Start_Pos: in System_Index; Find_Dir: in Direction := DIRECTION_FORWARD) return System_Size; function "=" (Obj: in Elastic_Array; Obj2: in Elastic_Array) return Standard.Boolean; function "=" (Obj: in Elastic_Array; Obj2: in Item_Array) return Standard.Boolean; diff --git a/lib2/hello.adb b/lib2/hello.adb index 47310db..142b1d6 100644 --- a/lib2/hello.adb +++ b/lib2/hello.adb @@ -16,7 +16,7 @@ use type H3.System_Size; procedure hello is package S is new H3.Strings(Standard.Wide_Character, Wide_Character'Val(0)); - package S_I is new H3.Arrays(Integer, 1, 16#FF#); + --type Global_Pool is new System.Storage_Pools.Root_Storage_Pool with null record; P1: aliased System.Pool_Global.Unbounded_No_Reclaim_Pool; @@ -423,6 +423,7 @@ begin declare + package S_I is new H3.Arrays(Integer, 1, 16#FF#); t1: S_I.Elastic_Array; begin S_I.Append (t1, 20, 5); @@ -438,6 +439,13 @@ begin Ada.Text_IO.Put_Line (t1.Find(30, t1.Get_Last_Index, S_I.DIRECTION_BACKWARD)'Img); Ada.Text_IO.Put_Line (t1.Find(30, t1.Get_First_Index)'Img); Ada.Text_IO.Put_Line (t1.Find(90, t1.Get_First_Index)'Img); + Ada.Text_IO.Put_Line (t1.Find(90, t1.Get_First_Index)'Img); + + Ada.Text_IO.Put_Line (t1.Find((30, 30, 30, 30), t1.Get_First_Index)'Img); + Ada.Text_IO.Put_Line (t1.Find((20, 20), t1.Get_First_Index, S_I.DIRECTION_FORWARD)'Img); + Ada.Text_IO.Put_Line (t1.Find((20, 20), t1.Get_Last_Index, S_I.DIRECTION_BACKWARD)'Img); + Ada.Text_IO.Put_Line (t1.Find((30, 20, 20), t1.Get_First_Index, S_I.DIRECTION_FORWARD)'Img); + Ada.Text_IO.Put_Line (t1.Find((30, 20, 20), t1.Get_Last_Index, S_I.DIRECTION_BACKWARD)'Img); end; end;