Skip to content

Commit badabd2

Browse files
Merge branch 'slides/135-break-050_array_types-into-chapters' into 'master'
Resolve "Break 050_array_types into chapters" Closes #135 See merge request feng/training/material!201
2 parents 34540da + 1f80d65 commit badabd2

13 files changed

+1730
-1671
lines changed

courses/fundamentals_of_ada/050_array_types.rst

Lines changed: 10 additions & 1671 deletions
Large diffs are not rendered by default.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
--------------
6+
Introduction
7+
--------------
8+
9+
* Traditional array concept supported to any dimension
10+
11+
.. code:: Ada
12+
13+
declare
14+
type Hours is digits 6;
15+
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
16+
type Schedule is array (Days) of Hours;
17+
Workdays : Schedule;
18+
begin
19+
...
20+
Workdays (Mon) := 8.5;
21+
22+
-------------
23+
Terminology
24+
-------------
25+
26+
* :dfn:`Index type`
27+
28+
- Specifies the values to be used to access the array components
29+
30+
* :dfn:`Component type`
31+
32+
- Specifies the type of values contained by objects of the array type
33+
- All components are of this same type
34+
35+
.. code:: Ada
36+
37+
type Array_T is array (Index_T) of Component_T;
38+
39+
------------------------------
40+
Array Type Index Constraints
41+
------------------------------
42+
43+
* Must be of an integer or enumeration type
44+
* May be dynamic
45+
* Default to predefined `Integer`
46+
47+
- Same rules as for-loop parameter default type
48+
49+
* Allowed to be null range
50+
51+
- Defines an empty array
52+
- Meaningful when bounds are computed at run-time
53+
54+
* Used to define constrained array types
55+
56+
.. code:: Ada
57+
58+
type Schedule is array (Days range Mon .. Fri) of Float;
59+
type Flags_T is array (-10 .. 10) of Boolean;
60+
61+
* Or to constrain unconstrained array types
62+
63+
.. code:: Ada
64+
65+
subtype Line is String (1 .. 80);
66+
subtype Translation is Matrix (1..3, 1..3);
67+
68+
-------------------------
69+
Run-Time Index Checking
70+
-------------------------
71+
72+
* Array indices are checked at run-time as needed
73+
* Invalid index values result in :ada:`Constraint_Error`
74+
75+
.. code:: Ada
76+
77+
procedure Test is
78+
type Int_Arr is array (1..10) of Integer;
79+
A : Int_Arr;
80+
K : Integer;
81+
begin
82+
A := (others => 0);
83+
K := FOO;
84+
A (K) := 42; -- run-time error if Foo returns < 1 or > 10
85+
Put_Line (A(K)'Image);
86+
end Test;
87+
88+
----------------------
89+
Kinds of Array Types
90+
----------------------
91+
92+
* :dfn:`Constrained` Array Types
93+
94+
- Bounds specified by type declaration
95+
- **All** objects of the type have the same bounds
96+
97+
* :dfn:`Unconstrained` Array Types
98+
99+
- Bounds not constrained by type declaration
100+
- Objects share the type, but not the bounds
101+
- More flexible
102+
103+
.. code:: Ada
104+
105+
type Unconstrained is array (Positive range <>)
106+
of Integer;
107+
108+
U1 : Unconstrained (1 .. 10);
109+
S1 : String (1 .. 50);
110+
S2 : String (35 .. 95);
111+
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
=========================
2+
Constrained Array Types
3+
=========================
4+
5+
-------------------------------------
6+
Constrained Array Type Declarations
7+
-------------------------------------
8+
9+
* Syntax
10+
11+
.. code:: Ada
12+
13+
constrained_array_definition ::=
14+
array index_constraint of subtype_indication
15+
index_constraint ::= (discrete_subtype_definition
16+
{, discrete_subtype_indication})
17+
discrete_subtype_definition ::=
18+
discrete_subtype_indication | range
19+
subtype_indication ::= subtype_mark [constraint]
20+
range ::= range_attribute_reference |
21+
simple_expression .. simple_expression
22+
23+
* Examples
24+
25+
.. code:: Ada
26+
27+
type Full_Week_T is array (Days) of Float;
28+
type Work_Week_T is array (Days range Mon .. Fri) of Float;
29+
type Weekdays is array (Mon .. Fri) of Float;
30+
type Workdays is array (Weekdays'Range) of Float;
31+
32+
----------------------------------
33+
Multiple-Dimensioned Array Types
34+
----------------------------------
35+
36+
.. container:: columns
37+
38+
.. container:: column
39+
40+
* Declared with more than one index definition
41+
42+
- Constrained array types
43+
- Unconstrained array types
44+
45+
* Components accessed by giving value for each index
46+
47+
.. container:: column
48+
49+
.. container:: latex_environment small
50+
51+
.. code:: Ada
52+
53+
type Three_Dimensioned is
54+
array (
55+
Boolean,
56+
12 .. 50,
57+
Character range 'a' .. 'z')
58+
of Integer;
59+
TD : Three_Dimensioned;
60+
...
61+
begin
62+
TD (True, 42, 'b') := 42;
63+
TD (Flag, Count, Char) := 42;
64+
65+
-----------------------------
66+
Tic-Tac-Toe Winners Example
67+
-----------------------------
68+
69+
.. container:: columns
70+
71+
.. container:: column
72+
73+
.. code:: Ada
74+
75+
-- 9 positions on a board
76+
type Move_Number is range 1 .. 9;
77+
-- 8 ways to win
78+
type Winning_Combinations is
79+
range 1 .. 8;
80+
-- need 3 positions to win
81+
type Required_Positions is
82+
range 1 .. 3;
83+
Winning : constant array (
84+
Winning_Combinations,
85+
Required_Positions)
86+
of Move_Number := (1 => (1,2,3),
87+
2 => (1,4,7),
88+
...
89+
90+
.. container:: column
91+
92+
.. list-table::
93+
:width: 55%
94+
95+
* - :superscript:`1` **X**
96+
97+
- :superscript:`2` **X**
98+
- :superscript:`3` **X**
99+
100+
* - :superscript:`4`
101+
102+
- :superscript:`5`
103+
- :superscript:`6`
104+
105+
* - :superscript:`7`
106+
107+
- :superscript:`8`
108+
- :superscript:`9`
109+
110+
* -
111+
112+
-
113+
-
114+
115+
* - :superscript:`1` **X**
116+
117+
- :superscript:`2`
118+
- :superscript:`3`
119+
120+
* - :superscript:`4` **X**
121+
122+
- :superscript:`5`
123+
- :superscript:`6`
124+
125+
* - :superscript:`7` **X**
126+
127+
- :superscript:`8`
128+
- :superscript:`9`
129+
130+
* -
131+
132+
-
133+
-
134+
135+
* - :superscript:`1` **X**
136+
137+
- :superscript:`2`
138+
- :superscript:`3`
139+
140+
* - :superscript:`4`
141+
142+
- :superscript:`5` **X**
143+
- :superscript:`6`
144+
145+
* - :superscript:`7`
146+
147+
- :superscript:`8`
148+
- :superscript:`9` **X**
149+
150+
------
151+
Quiz
152+
------
153+
154+
.. code:: Ada
155+
156+
type Array1_T is array (1 .. 8) of Boolean;
157+
type Array2_T is array (0 .. 7) of Boolean;
158+
X1, Y1 : Array1_T;
159+
X2, Y2 : Array2_T;
160+
161+
.. container:: columns
162+
163+
.. container:: column
164+
165+
Which statement(s) is (are) legal?
166+
167+
A. :answermono:`X1 (1) := Y1 (1);`
168+
B. :answermono:`X1 := Y1;`
169+
C. :answermono:`X1 (1) := X2 (1);`
170+
D. ``X2 := X1;``
171+
172+
.. container:: column
173+
174+
.. container:: animate
175+
176+
Explanations
177+
178+
A. Legal - elements are :ada:`Boolean`
179+
B. Legal - object types match
180+
C. Legal - elements are :ada:`Boolean`
181+
D. Although the sizes are the same and the elements are the same, the type is different
182+

0 commit comments

Comments
 (0)