Array TypesAnonymous Array TypesArray AttributesArray AggregatesInitializers in C++Aggregates and QualificationMultidimensional ArraysOperations on One_Dimensional ArraysConcatenation and SlicingA discrete range specifies a sliceRecordsVariantsVariants are type safeDiscriminant checkingVariants and classesFree UnionsDiscriminated unions and dynamic typingAccess Types and pointersDynamic data structuresIncomplete declarations in C++Pointers and dereferencingThree models for arraysVariations on Strings: AdaVariations on Strings: C++Variations on Strings: JavaPointers and safetyDangling referencesDeallocationArray TypesIndex types can be of any discrete typeComponent type must be definite, i.e. have bounds: type class_list is array ( 1 .. 100) of String (1..10); -- OK type class_list is array ( 1 .. 100) of String; -- ErrorThe subtype constrains all indices or none:: type Matrix is array (positive range <>, positive range <>) of Long_Float; subtype Table is Matrix; subtype Rotation is Matrix (1 .. 3, 1 .. 3);arrays are objects with assignment: (unlike C, C++) Table1 := Table2; -- all components assignedAnonymous Array Types Grades : array (1 .. Num_Students) of Natural;type of Grades has no name: distinct from any other array types. Ar1: array (1 .. 10) of Boolean; Ar2 : array (1 .. 10) of Boolean; … Ar1 := Ar2; -- Error: different (anonymous) types.If a type is a useful abstraction, it deserves to have a name!Array Attributes type Matrix is array (Positive range <>, Positive range <>) of Float; subtype Rect is Matrix (1 .. 3, 1 .. 5); M3 : Rect; M3’First (1) -- Yields 1 M3’First -- same. Rect’length (2) -- Yields 5 (applies to type) M3’range (2) -- equivalent to 1..5 String’Length -- ERROR: unconstrainedArrays are self-describing: size information is built-inArray AggregatesExpression that yields an array value: A := (1, 2, 3, 10); -- positional A := (1, others => 0); -- notation for default. A := (1..3 => 1, 4 => -999); -- component associationsDefault can only be used if bounds are known: A : String (1 .. 10) := (others => ‘?’); -- OK A : String := (others => ‘?’); -- Error: unknown bounds.Initializers in C++•Similar notion for declarations:int v2[] = {1, 2, 3, 4}; -- size from initializerchar v3[2] = {‘a’, ‘z’}; -- declared sizeint v5[10] = {-1}; -- default : other components = 0char name [] = “Algol” -- String literals are aggregates•but no array assignments, so initializer is not an expression (mechanism is less orthogonal)Aggregates and QualificationAggregate may be ambiguous: type Vector is array (1 .. 3) of Float; procedure Display (V : vector); type Assay is array (1 .. 3) of Float; procedure Display (A : assay); … Display ((1.0, 1.2, 1.5)); -- which? ambiguous Display (Vector ‘ (1.0, 1.2, 1.5)); -- OK.Multidimensional ArraysAggregates given in row-major order with subaggregates: type Square is array (1 .. 3, 1 .. 3) of Integer; Unit : constant Square := ( (1, 0 ,0), (0, 1, 0), (0, 0, 1));A two-dimensional array is NOT array of arrays: type vector is array (1 .. 3) of Integer; type V3 is array (1 .. 3) of vector; -- not convertible to SquareOperations on One_Dimensional ArraysBoolean operations extend pointwise: type Set is array (1 .. Card) of Boolean; S1, S2, S3 : Set; … S3 := S1 and S2; -- Set Intersectionlexicographic comparisons on arrays of discrete types: S1 := (T, T, T); S2 := (T, T, F); .. S2 < S1 -- yields TrueConcatenation and SlicingBoth operations yield the base type: type Table is array (1..10) of Integer; T1, T2 : Table; … T1 & T2 -- What type?Declaration equivalent to: type Anon is array (integer range <>) of Integer; subtype Table is Anon (1 .. 10); T1 & T2 , T1 (X .. Y) are of type AnonA discrete range specifies a slicesubtype Sub is Positive range 2 .. 4;Label : String (1..10) := “transcends” ;… Label (2 .. 4) -- Yields “ran” Label (Integer range 2 .. 4) -- Same Label (Sub) -- DittoRecordstype city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean;end record;struct city { -- C, C++ char* name; char* country; int population bool capital }Variants•Need to treat group of related representations as a single type:type figure_kind is (Circle, Square, Line);type Figure (Kind : Figure_kind) is record Color : color_type; -- defined elsewhere Visible : Boolean; case Kind is when Line => Length : Integer; Orientation: Float; Start : Point; -- defined elsewhere when square => Lower_Left, Upper_Right : Point; when circle => Radius : Integer; Center : Point; end case;end record;Variants are type safeC1 : Figure (Circle); -- discriminant provides constraintS1 : Figure (Square);…C1. Radius := 15;if S1.Lower_Left = C1.Center then..function Area (F : Figure) return Float is -- applies to any figure, i.e. subtypebegin case F.Kind is when Circle => return Pi * Radius ** 2; ..Discriminant checkingC : Figure (Circle);L : Figure (Line);F : Figure; -- illegal, don’t know which kindP1, P2 := Point;…C := (Circle, Red, False, 10, P1); -- record aggregateif C.Orientation then -- illegal, circles have no orientationC := L; -- illegal, different kinds C.Kind := Square; -- Illegal, discriminant is constantDiscriminant is visible constant component of objectThere is a way of specifying a figure that can change kindsVariants and classes•Discriminated types and classes have similar functionalities•Discriminated types can be allocated statically•Run-time code uses less indirection•Compiler can enforce consistent use of discriminants•Adding new variants is disruptive–must modify every case statement•Variant programming: one procedure at a time•Class programming : one class at a timeFree
View Full Document