Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,146 +6,34 @@ Constrained Array Types
Constrained Array Type Declarations
-------------------------------------

* Syntax
Syntax (simplified)

.. code:: Ada

constrained_array_definition ::=
array index_constraint of subtype_indication
index_constraint ::= (discrete_subtype_definition
{, discrete_subtype_indication})
discrete_subtype_definition ::=
discrete_subtype_indication | range
subtype_indication ::= subtype_mark [constraint]
range ::= range_attribute_reference |
simple_expression .. simple_expression

* Examples
.. container:: latex_environment footnotesize

.. code:: Ada

type Full_Week_T is array (Days) of Float;
type Work_Week_T is array (Days range Mon .. Fri) of Float;
type Weekdays is array (Mon .. Fri) of Float;
type Workdays is array (Weekdays'Range) of Float;

----------------------------------
Multiple-Dimensioned Array Types
----------------------------------

.. container:: columns

.. container:: column

* Declared with more than one index definition

- Constrained array types
- Unconstrained array types

* Components accessed by giving value for each index

.. container:: column

.. container:: latex_environment small

.. code:: Ada

type Three_Dimensioned is
array (
Boolean,
12 .. 50,
Character range 'a' .. 'z')
of Integer;
TD : Three_Dimensioned;
...
begin
TD (True, 42, 'b') := 42;
TD (Flag, Count, Char) := 42;

-----------------------------
Tic-Tac-Toe Winners Example
-----------------------------

.. container:: columns

.. container:: column

.. code:: Ada

-- 9 positions on a board
type Move_Number is range 1 .. 9;
-- 8 ways to win
type Winning_Combinations is
range 1 .. 8;
-- need 3 positions to win
type Required_Positions is
range 1 .. 3;
Winning : constant array (
Winning_Combinations,
Required_Positions)
of Move_Number := (1 => (1,2,3),
2 => (1,4,7),
...

.. container:: column

.. list-table::
:width: 55%

* - :superscript:`1` **X**

- :superscript:`2` **X**
- :superscript:`3` **X**

* - :superscript:`4`

- :superscript:`5`
- :superscript:`6`

* - :superscript:`7`
type <typename> is array (<index constraint>) of <constrained type>;

- :superscript:`8`
- :superscript:`9`
where

* -
.. container:: latex_environment quote

-
-
**typename** - identifier

* - :superscript:`1` **X**
**index constraint** - discrete range or type

- :superscript:`2`
- :superscript:`3`
**constrained type** - type with size known at compile time

* - :superscript:`4` **X**
Examples

- :superscript:`5`
- :superscript:`6`
.. container:: latex_environment footnotesize

* - :superscript:`7` **X**

- :superscript:`8`
- :superscript:`9`

* -

-
-

* - :superscript:`1` **X**

- :superscript:`2`
- :superscript:`3`

* - :superscript:`4`

- :superscript:`5` **X**
- :superscript:`6`

* - :superscript:`7`
.. code:: Ada

- :superscript:`8`
- :superscript:`9` **X**
type Integer_Array_T is array (1 .. 3) of Integer;
type Boolean_Array_T is array (Boolean) of Integer;
type Character_Array_T is array (character range 'a' .. 'z') of Boolean;
type Copycat_T is array (Boolean_Array_T'Range) of Integer;

------
Quiz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Supplying Index Constraints for Objects
.. code:: Ada

Weekend : Schedule := (Sat => 4.0, Sun => 0.0);
-- (Note this is an array aggregate, explained later)

- Further type definitions (shown later)
- Actual parameter to subprogram (shown later)
Expand Down Expand Up @@ -232,77 +233,27 @@ Quiz

.. code:: Ada

type Array_T is array (Integer range <>) of Integer;
subtype Array1_T is Array_T (1 .. 4);
subtype Array2_T is Array_T (0 .. 3);
X : Array_T := (1, 2, 3, 4);
Y : Array1_T := (1, 2, 3, 4);
Z : Array2_T := (1, 2, 3, 4);
type Bit_T is range 0 .. 1;
type Bit_Array_T is array (Positive range <>) of Bit_T;

.. container:: columns

.. container:: column

Which statement(s) is (are) legal?
Which declaration(s) is (are) legal?

A. ``X (1) := Y (1);``
B. :answermono:`Y (1) := Z (1);`
C. :answermono:`Y := X;`
D. :answermono:`Z := X;`
A. ``AAA : Array_T (0..99);``
B. :answermono:`BBB : Array_T (1..32);`
C. :answermono:`CCC : Array_T (17..16);`
D. ``DDD : Array_T;``

.. container:: column

.. container:: animate

Explanations

A. :ada:`Array_T` starts at :ada:`Integer'First` not :ada:`1`
B. OK, both in range
C. OK, same type and size
D. OK, same type and size

------
Quiz
------

.. code:: Ada

type My_Array is array (Boolean range <>) of Boolean;

O : My_Array (False .. False) := (others => True);

What is the value of :ada:`O (True)`?

A. :ada:`False`
B. :ada:`True`
C. None: Compilation error
D. :answer:`None: Run-time error`

.. container:: animate

:ada:`True` is not a valid index for :ada:`O`.

NB: GNAT will emit a warning by default.

------
Quiz
------

.. code:: Ada

type My_Array is array (Positive range <>) of Boolean;

O : My_Array (0 .. -1) := (others => True);

What is the value of :ada:`O'Length`?

A. 1
B. :answer:`0`
C. None: Compilation error
D. None: Run-time error

.. container:: animate

When the upper bound is less than the lower bound, this is an empty array.
For empty arrays, the index can be out of range for the index type.

A. :ada:`Array_T` index is :ada:`Positive` which starts at 1
B. OK, indices are in range
C. OK, indicates a zero-length array
D. Object must be constrained
36 changes: 25 additions & 11 deletions courses/fundamentals_of_ada/050_array_types/05-operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@ Object-Level Operations

* Conversions

.. code:: Ada

C := Foo (B);

- Component types must be the same type
- Index types must be the same or convertible
- Dimensionality must be the same
- Bounds must be compatible (not necessarily equal)

.. code:: Ada

declare
type Index1_T is range 1 .. 2;
type Index2_T is range 101 .. 102;
type Array1_T is array (Index1_T) of Integer;
type Array2_T is array (Index2_T) of Integer;
type Array3_T is array (Boolean) of Integer;

One : Array1_T;
Two : Array2_T;
Three : Array3_T;

begin

One := Array1_T (Two); -- OK
Two := Array2_T (Three); -- Illegal (indices not convertible)

-------------------------------
Extra Object-Level Operations
-------------------------------
Expand Down Expand Up @@ -142,23 +156,23 @@ Quiz

type Index_T is range 1 .. 10;
type OneD_T is array (Index_T) of Boolean;
type ThreeD_T is array (Index_T, Index_T, Index_T) of OneD_T;
A : ThreeD_T;
type TwoD_T is array (Index_T) of OneD_T;
A : TwoD_T;
B : OneD_T;

Which statement(s) is (are) legal?

A. :answermono:`B(1) := A(1,2,3)(1) or A(4,3,2)(1);`
B. :answermono:`B := A(2,3,4) and A(4,3,2);`
C. ``A(1,2,3..4) := A(2,3,4..5);``
A. :answermono:`B(1) := A(1,2) or A(4,3);`
B. :answermono:`B := A(2) and A(4);`
C. ``A(1..2)(4) := A(5..6)(8);``
D. :answermono:`B(3..4) := B(4..5)`

.. container:: animate

Explanations

A. All three objects are just Boolean values
A. All objects are just Boolean values
B. An element of :ada:`A` is the same type as :ada:`B`
C. No slicing of multi-dimensional arrays
C. Slice must be of outermost array
D. Slicing allowed on single-dimension arrays

Loading