From 727e3c59346da4f91284b34b4c18f2e0ba155e53 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 9 Aug 2025 16:03:28 +0200 Subject: Initial commit --- arrays/arrays.gpr | 5 ++ arrays/src/arrays.adb | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 arrays/arrays.gpr create mode 100644 arrays/src/arrays.adb (limited to 'arrays') diff --git a/arrays/arrays.gpr b/arrays/arrays.gpr new file mode 100644 index 0000000..3d7ea37 --- /dev/null +++ b/arrays/arrays.gpr @@ -0,0 +1,5 @@ +project Arrays is + for Source_Dirs use ("src"); + for Object_Dir use "obj"; + for Main use ("arrays.adb"); +end Arrays; diff --git a/arrays/src/arrays.adb b/arrays/src/arrays.adb new file mode 100644 index 0000000..e851fc6 --- /dev/null +++ b/arrays/src/arrays.adb @@ -0,0 +1,127 @@ +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; + +procedure Arrays is + ----------------------------------------------------------------------------- + -- Arrays 101. + -- + -- The index is strongly-typed and can be any discrete type. + -- + -- Iterations over the index type are preferred over iterations over specific + -- index values. + ----------------------------------------------------------------------------- + procedure Test_Array is + type My_Int is range 0 .. 1_000; + type Index is range 1 .. 5; + + type My_Int_Array is array (Index) of My_Int; + + function To_String (A : My_Int_Array) return String is + S : Unbounded_String; + begin + for I in Index loop + S := S & My_Int'Image (A (I)) & " "; + end loop; + return To_String (S); + end To_String; + + Arr : My_Int_Array := (2, 3, 5, 7, 11); + -- This array is not actually empty; its size is determined by the range of + -- its index type, which in this example is 1 .. 5. + Empty_Arr : My_Int_Array; + begin + Put_Line ("Arr = " & To_String (Arr)); + Put_Line ("Arr is " & Integer'Image (Arr'Size) & " bytes"); + + Put_Line ("Empty_Arr = " & To_String (Empty_Arr)); + Put_Line ("Empty_Arr is " & Integer'Image (Empty_Arr'Size) & " bytes"); + end Test_Array; + + ----------------------------------------------------------------------------- + -- Enums as array indices. + ----------------------------------------------------------------------------- + procedure Test_Enum_Array is + type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); + type Day is range 1 .. 31; + + Month_Days : array (Month) of Day := + (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); + begin + for M in Month loop + Put_Line + (Month'Image (M) & " has " & Day'Image (Month_Days (M)) & " days"); + end loop; + end Test_Enum_Array; + + ----------------------------------------------------------------------------- + -- Arrays in ADA are bounds-checked. + ----------------------------------------------------------------------------- + procedure Test_Bounds is + Arr : array (Integer range 1 .. 5) of Integer := (3, 4, 7, 8, 9); + begin + Arr (1) := 17; + Arr (5) := 18; + --Arr (6) := 19; -- Error. + end Test_Bounds; + + ----------------------------------------------------------------------------- + -- Use the Range attribute to iterate over an array with an anonymous range. + ----------------------------------------------------------------------------- + procedure Test_Anonymous_Range is + Arr : array (3 .. 7) of Integer := (5, 8, 3, 5, 3); + begin + for I in Arr'Range loop + Put_Line + ("Index " & Integer'Image (I) & " has value " & + Integer'Image (Arr (I))); + end loop; + end Test_Anonymous_Range; + + ----------------------------------------------------------------------------- + -- Unconstrained arrays. + -- + -- The size/bounds are provided when creating an instance of the array type. + ----------------------------------------------------------------------------- + procedure Test_Unbounded_Array is + type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); + type Days_Arr is array (Integer range <>) of Day; + + Days_Off : Days_Arr := (Sat, Sun); + begin + Put ("Holidays: "); + for D in Days_Off'Range loop + Put (Day'Image (Days_Off (D)) & " "); + end loop; + New_Line; + end Test_Unbounded_Array; + + ----------------------------------------------------------------------------- + -- Bounds are automatically inferred from the initialization value. + ----------------------------------------------------------------------------- + procedure Test_Auto_Bounds is + Arr : array (Natural range <>) of Integer := (2, 3, 4); + begin + for I in Arr'First .. Arr'Last loop + Put_Line ("Arr(" & Integer'Image (I) & ") = " & Integer'Image (Arr (I))); + end loop; + end Test_Auto_Bounds; + + ----------------------------------------------------------------------------- + -- Array slices. + ----------------------------------------------------------------------------- + procedure Test_Slices is + Str : String := "Hello world"; + begin + Str (7 .. 11) := "there"; + Put_Line (Str); + end Test_Slices; + +begin + Test_Array; + Test_Enum_Array; + Test_Bounds; + Test_Anonymous_Range; + Test_Unbounded_Array; + Test_Auto_Bounds; + Test_Slices; +end Arrays; -- cgit v1.2.3