diff options
| author | 3gg <3gg@shellblade.net> | 2025-08-09 16:03:28 +0200 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-08-09 16:03:28 +0200 |
| commit | 727e3c59346da4f91284b34b4c18f2e0ba155e53 (patch) | |
| tree | 807dccd5cba3c6bae2f8d0c9910157e306c6da5b /arrays | |
Diffstat (limited to 'arrays')
| -rw-r--r-- | arrays/arrays.gpr | 5 | ||||
| -rw-r--r-- | arrays/src/arrays.adb | 127 |
2 files changed, 132 insertions, 0 deletions
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 @@ | |||
| 1 | project Arrays is | ||
| 2 | for Source_Dirs use ("src"); | ||
| 3 | for Object_Dir use "obj"; | ||
| 4 | for Main use ("arrays.adb"); | ||
| 5 | 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 @@ | |||
| 1 | with Ada.Text_IO; use Ada.Text_IO; | ||
| 2 | with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; | ||
| 3 | |||
| 4 | procedure Arrays is | ||
| 5 | ----------------------------------------------------------------------------- | ||
| 6 | -- Arrays 101. | ||
| 7 | -- | ||
| 8 | -- The index is strongly-typed and can be any discrete type. | ||
| 9 | -- | ||
| 10 | -- Iterations over the index type are preferred over iterations over specific | ||
| 11 | -- index values. | ||
| 12 | ----------------------------------------------------------------------------- | ||
| 13 | procedure Test_Array is | ||
| 14 | type My_Int is range 0 .. 1_000; | ||
| 15 | type Index is range 1 .. 5; | ||
| 16 | |||
| 17 | type My_Int_Array is array (Index) of My_Int; | ||
| 18 | |||
| 19 | function To_String (A : My_Int_Array) return String is | ||
| 20 | S : Unbounded_String; | ||
| 21 | begin | ||
| 22 | for I in Index loop | ||
| 23 | S := S & My_Int'Image (A (I)) & " "; | ||
| 24 | end loop; | ||
| 25 | return To_String (S); | ||
| 26 | end To_String; | ||
| 27 | |||
| 28 | Arr : My_Int_Array := (2, 3, 5, 7, 11); | ||
| 29 | -- This array is not actually empty; its size is determined by the range of | ||
| 30 | -- its index type, which in this example is 1 .. 5. | ||
| 31 | Empty_Arr : My_Int_Array; | ||
| 32 | begin | ||
| 33 | Put_Line ("Arr = " & To_String (Arr)); | ||
| 34 | Put_Line ("Arr is " & Integer'Image (Arr'Size) & " bytes"); | ||
| 35 | |||
| 36 | Put_Line ("Empty_Arr = " & To_String (Empty_Arr)); | ||
| 37 | Put_Line ("Empty_Arr is " & Integer'Image (Empty_Arr'Size) & " bytes"); | ||
| 38 | end Test_Array; | ||
| 39 | |||
| 40 | ----------------------------------------------------------------------------- | ||
| 41 | -- Enums as array indices. | ||
| 42 | ----------------------------------------------------------------------------- | ||
| 43 | procedure Test_Enum_Array is | ||
| 44 | type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); | ||
| 45 | type Day is range 1 .. 31; | ||
| 46 | |||
| 47 | Month_Days : array (Month) of Day := | ||
| 48 | (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); | ||
| 49 | begin | ||
| 50 | for M in Month loop | ||
| 51 | Put_Line | ||
| 52 | (Month'Image (M) & " has " & Day'Image (Month_Days (M)) & " days"); | ||
| 53 | end loop; | ||
| 54 | end Test_Enum_Array; | ||
| 55 | |||
| 56 | ----------------------------------------------------------------------------- | ||
| 57 | -- Arrays in ADA are bounds-checked. | ||
| 58 | ----------------------------------------------------------------------------- | ||
| 59 | procedure Test_Bounds is | ||
| 60 | Arr : array (Integer range 1 .. 5) of Integer := (3, 4, 7, 8, 9); | ||
| 61 | begin | ||
| 62 | Arr (1) := 17; | ||
| 63 | Arr (5) := 18; | ||
| 64 | --Arr (6) := 19; -- Error. | ||
| 65 | end Test_Bounds; | ||
| 66 | |||
| 67 | ----------------------------------------------------------------------------- | ||
| 68 | -- Use the Range attribute to iterate over an array with an anonymous range. | ||
| 69 | ----------------------------------------------------------------------------- | ||
| 70 | procedure Test_Anonymous_Range is | ||
| 71 | Arr : array (3 .. 7) of Integer := (5, 8, 3, 5, 3); | ||
| 72 | begin | ||
| 73 | for I in Arr'Range loop | ||
| 74 | Put_Line | ||
| 75 | ("Index " & Integer'Image (I) & " has value " & | ||
| 76 | Integer'Image (Arr (I))); | ||
| 77 | end loop; | ||
| 78 | end Test_Anonymous_Range; | ||
| 79 | |||
| 80 | ----------------------------------------------------------------------------- | ||
| 81 | -- Unconstrained arrays. | ||
| 82 | -- | ||
| 83 | -- The size/bounds are provided when creating an instance of the array type. | ||
| 84 | ----------------------------------------------------------------------------- | ||
| 85 | procedure Test_Unbounded_Array is | ||
| 86 | type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); | ||
| 87 | type Days_Arr is array (Integer range <>) of Day; | ||
| 88 | |||
| 89 | Days_Off : Days_Arr := (Sat, Sun); | ||
| 90 | begin | ||
| 91 | Put ("Holidays: "); | ||
| 92 | for D in Days_Off'Range loop | ||
| 93 | Put (Day'Image (Days_Off (D)) & " "); | ||
| 94 | end loop; | ||
| 95 | New_Line; | ||
| 96 | end Test_Unbounded_Array; | ||
| 97 | |||
| 98 | ----------------------------------------------------------------------------- | ||
| 99 | -- Bounds are automatically inferred from the initialization value. | ||
| 100 | ----------------------------------------------------------------------------- | ||
| 101 | procedure Test_Auto_Bounds is | ||
| 102 | Arr : array (Natural range <>) of Integer := (2, 3, 4); | ||
| 103 | begin | ||
| 104 | for I in Arr'First .. Arr'Last loop | ||
| 105 | Put_Line ("Arr(" & Integer'Image (I) & ") = " & Integer'Image (Arr (I))); | ||
| 106 | end loop; | ||
| 107 | end Test_Auto_Bounds; | ||
| 108 | |||
| 109 | ----------------------------------------------------------------------------- | ||
| 110 | -- Array slices. | ||
| 111 | ----------------------------------------------------------------------------- | ||
| 112 | procedure Test_Slices is | ||
| 113 | Str : String := "Hello world"; | ||
| 114 | begin | ||
| 115 | Str (7 .. 11) := "there"; | ||
| 116 | Put_Line (Str); | ||
| 117 | end Test_Slices; | ||
| 118 | |||
| 119 | begin | ||
| 120 | Test_Array; | ||
| 121 | Test_Enum_Array; | ||
| 122 | Test_Bounds; | ||
| 123 | Test_Anonymous_Range; | ||
| 124 | Test_Unbounded_Array; | ||
| 125 | Test_Auto_Bounds; | ||
| 126 | Test_Slices; | ||
| 127 | end Arrays; | ||
