summaryrefslogtreecommitdiff
path: root/arrays/src/arrays.adb
blob: e851fc65a6134850e2cedd920bbc54b67b262454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;