Cours 5: Types tableaux.
Objectif: Types non contraints, sous-types contraints. Objet tableau. Vecteur.
Paramètre tableau. Tranche, affectation et comparaison globales.
Type String.
Exemples: plus petit, rotation droite, palindrome, conversion Ascii vers entier, conversion entier vers Ascii.
Extrait du manuel de référence (RM95):
type_declaration ::= full_type_declaration
full_type_declaration ::=
type defining_identifier [known_discriminant_part] is type_definition;
type_definition ::= array_type_definition
array_type_definition ::=
unconstrained_array_definition | constrained_array_definition
unconstrained_array_definition ::=
array(index_subtype_definition {, index_subtype_definition}) of component_definition
index_subtype_definition ::= subtype_mark range <>
constrained_array_definition ::=
array (discrete_subtype_definition {, discrete_subtype_definition}) of component_definition
discrete_subtype_definition ::= discrete_subtype_indication | range
component_definition ::= [aliased] subtype_indication
object_declaration ::=
defining_identifier_list : [aliased] [constant] array_type_definition [:= expression];
defining_identifier_list ::=
defining_identifier {, defining_identifier}
Un type tableau non contraint est un type tableau dont les indices sont typés mais leur intervalle
de valeur est non fixé (intervalle <>; lire boîte).
Un sous-type tableau contraint a un intervalle de variation fixe pour chacun de ses indices.
Un objet tableau doit être contraint.
Un paramètre formel tableau d'un sous-programme peut être contraint ou pas.
Un paramètre formel tableau non contraint peut être associé à n'importe quel objet tableau conforme: même nombre d'indices, dont les types sont ceux du paramètre formel, même type
d'éléments.
Un vecteur est un tableau à une dimension (un indice).
Une matrice est un tableau à deux dimensions (deux indices).
On dénomme le Ième élément d'un vecteur V par la notation indicée V(I).
M(I, J) est l'élément en ligne I, colonne J de la matrice M.
L'expression T'Att(N) est l'attribut Att appliqué au Nème indice du tableau T.
L'expression T'Att est l'attribut Att appliqué au premier indice du tableau T.
Le type String est prédéfini.
"ceci" est une constante String.
type String is array(Positive range <>) of Character;
Extrait du manuel de référence (RM95):
Examples
Examples of type declarations with unconstrained array definitions:
type Vector is array(Integer range <>) of Real;
type Matrix is array(Integer range <>, Integer range <>) of Real;
type Bit_Vector is array(Integer range <>) of Boolean;
type Roman is array(Positive range <>) of Roman_Digit;
Examples of type declarations with constrained array definitions:
type Table is array(1 .. 10) of Integer;
type Schedule is array(Day) of Boolean;
type Line is array(1 .. Max_Line_Size) of Character;
Examples of object declarations with array type definitions:
Grid : array(1 .. 80, 1 .. 100) of Boolean;
Mix : array(Color range Red .. Green) of Boolean;
Page : array(Positive range <>) of Line := -- an array of arrays
(1 | 50 => Line'(1 | Line'Last => '+', others => '-'),
2 .. 49 => Line'(1 | Line'Last => '|', others => ' '));
-- Page is constrained by its initial value to (1..50)
Exemples de sous-programmes employant des vecteurs:
function Plus_Petit(P: Indice) return Indice is
-- calcule l'indice Min du plus petit element d'un vecteur global T(P..T'Last)
Min: Integer:= P;
begin
for I in Indice'Succ(P)..T'Last loop
if T(I)<T(Min) then Min:= I; end if;
end loop;
return Min;
end Plus_Petit;
type Table is array(Indice range <>) of Element;
procedure Rotation_Droite(T: in out Table) is
X: Element;
begin
X:=T(T'Last);
for I in reverse Indice'Succ(T'First)..T'Last loop
T(I):= T(Indice'Pred(I));
end loop;
T(T'First):= X;
end Rotation_Droite;
type Texte is array(Indice range <>) of Element;
function Palindrome(T: Texte) return Boolean is
begin
for I in T'First .. (T'Last+T'First-1)/2 loop
if T(I)/=T(T'Last+T'First-I) then return False; end if;
end loop;
return True;
end Palindrome;
Une tranche est un vecteur d'éléments consécutifs d'une dimension d'un tableau.
e.g. un sous-vecteur d'un vecteur; e.g. un morceau d'une ligne de matrice (mais pas un morceau à cheval sur deux lignes ni un morceau de colonne).
Dans une affectation, le terme de gauche peut être un tableaux ou une tranche.
Dans une expression, les termes peuvent être des tableaux ou des tranches.
Pour les opérations booléennes (and, or, xor, and then, or else, not), les arguments peuvent être des tranches d'éléments booléens.
Pour les opérations de comparaisons (=, /=, <, <=, >, >=), les arguments peuvent être des tableaux multidimensionnels d'éléments de type discret.
L'opérateur & (concaténation) s'applique à des vecteurs ou tranches (on peut concaténer un élément à une tranche, une tranche à un élément ou deux tranches).
Pour une opération binaire, les deux arguments doivent être des tableaux ou tranches compatibles (même nombre d'éléments).
Pour une affectation, l'expression à droite du signe d'affectation doit être compatible avec la destination à gauche.
Extrait du manuel de référence (RM95):
Stars(1 .. 15) -- a slice of 15 characters
Page(10 .. 10 + Size) -- a slice of 1 + Size components
Page(L)(A .. B) -- a slice of the array Page(L)
Stars(1 .. 0) -- a null slice
My_Schedule(Weekday) -- bounds given by subtype
Filter(1 .. 10) and Filter(15 .. 24)
"" < "A" and "A" < "Aa" -- True
"Aa" < "B" and "A" < "A " -- True
"A" & "BCD" -- concatenation of two string literals
'A' & "BCD" -- concatenation of a character literal and a string literal
'A' & 'A' -- concatenation of two character literals
Exemples de sous-programmes employant des chaînes:
function A_to_I(S: String) return Natural is
--convertit une chaine en un entier
E: Natural:= 0;
begin
for I in S'Length loop
E:= E*10 + Character'Pos(S(I))
end loop;
return E;
end A_to_I;
procedure I_to_A(E: Natural; S: out String) is
--convertit un entier en une chaine
R: Natural;
begin
if E=0 then
S:= "0";
else
S:= "";
loop
R:= E rem 10;
S:= Character'Val(R) & S;
E:= E/10;
exit when E=0;
end loop;
end if;
end I_to_A;