1 / 34

Lecture 4 Aggregate Data Types

Lecture 4 Aggregate Data Types. Aggregates. Types defined as aggregates of other types At least two types Type of the aggregate (array or record) Type(s) of the elements Arrays and lists contain multiple instances of the same type Some languages allow heterogeneous arrays

Télécharger la présentation

Lecture 4 Aggregate Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 Aggregate Data Types

  2. Aggregates • Types defined as aggregates of other types • At least two types • Type of the aggregate (array or record) • Type(s) of the elements • Arrays and lists contain multiple instances of the same type • Some languages allow heterogeneous arrays • Records (Structures) and Classes contain named instances of different types

  3. Abstract Data Types • An Abstract data type defines a protocol – or contract – for using the type, but does not imply any particular implementation • Classes are abstract data types • Arrays can be viewed either as abstract or concrete types

  4. Concrete Arrays • An array is a sequence of elements in memory • The shape of an array determined by a radix vector • Dimensionality determined by length of the radix vector • Array addressing: • A is array (0..2, 0..2) of integer • A[1,2] is at: base + (1 * 3) + 2

  5. Array as ADT • Abstraction: Indexed storage of elements • Set or read value of specific element • Iterate over elements • Allow dynamic resize or reshape? • Provide built-in range checking? • Implementation Options: • Contiguous sequence of elements • List of lists • Tree (with sub-tree counts) • Hash table keyed by indices • Why would one want to do that?

  6. Aggregates and Qualification • Aggregate may be ambiguous: • type Vector isarray (1 .. 3) of Float; • procedure Display (V : vector); • type Assay isarray (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.

  7. Ada Array Types • Index types can be of any discrete type type week isarray ( Sunday .. Saturday) of Float); • Component type must be definite, i.e. have bounds: • type class_list isarray ( 1 .. 100) of String (1..10); -- OK • type class_list isarray ( 1 .. 100) of String; -- Error • The subtype constrains all indices or none:: • type Matrix isarray • (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 assigned

  8. Anonymous 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!

  9. Array Attributes Ada: type Matrix isarray (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 unconstrained • Java: int Rect[3,5]; Rect.length Arrays are self-describing: size information is built-in • Unlike C,C++

  10. Array Initialization • Expression 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 associations • Default can only be used if bounds are known: • A : String (1 .. 10) := (others => ‘?’); -- OK • A : String := (others => ‘?’); -- Error: unknown bounds.

  11. Initializers in C++ • Similar notion for declarations: int v2[] = {1, 2, 3, 4}; -- size from initializer char v3[2] = {‘a’, ‘z’}; -- declared size int v5[10] = {-1}; -- default : other components = 0 char name [] = “Algol” -- String literals are aggregates • but no array assignments, so initializer is not an expression (mechanism is less orthogonal)

  12. Multidimensional Arrays • Aggregates given in row-major order with subaggregates: • type Square isarray (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 isarray (1 .. 3) of Integer; • type V3 isarray (1 .. 3) of vector; • -- not convertible to Square • Unlike C • int matrix[4][4]; • Note that this is consistent with the concrete storage model for arrays

  13. String Types • Abstract Data Type String • Sequence of characters • Set of operations: concatenation, substring operations, etc. • Implemented as • pointer to a null terminated array of characters ( C ) • pointer to integer containing the length of the string followed by array of characters (Basic) • Pointer to array of chars and pointer to end of array (STL) • a class (java)

  14. Variations on Strings: Ada Strings are arrays: type String is array (positive range <>) of character; type Str_Ptr is access String; Ptr1, Ptr2 : Str_Ptr; -- initially null Title : String := “Brave New World” ; -- fixed size Ptr3 : Str_Ptr := new String’(“Island”); … Ptr1 := Ptr3; -- pointer assignment makes synonyms Ptr1.all := “what??”; -- array assignment: must be same size Ptr1 := new String (“the genius and the goddess”); Title := Ptr1.all; -- run time error: sizes don’t match

  15. Variations on Strings: C++ char* name1, name2; char title[ ] = “brave new world”; // 16 characters: implicit 0 at end char* t = “island”; // pointer to constant array name1 = newchar[16]; // allocate dynamic storage const char* ptr = &title[0]; // pointer to local constant array … while (*name1++ = *ptr++); // amusing C idiom name1 [0] = ‘B’; // title not affected t [0] = “I”; // illegal: string literal is constant semantic equivalence: a[k] = * (a + k)

  16. Variations on Strings: Java • Strings are classes, not arrays: need special notation for indexing and slicing. • String values are constants: need to use arrays of characters to modify strings. String name = “The answer is ”; … name = name + 42 + “!”); // assign different value // implicit conversion of integer to string: “The answer is 42!” if (name.StringAt (0) == ‘T’ ) { // true

  17. Record types • Abstraction: collection of named fields • Get or set value of a specific field • Field names in structure scope • Implementation: • Offsets to field positions • Alignment & padding

  18. Records type 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 }

  19. Discriminated unions and dynamic typing • In dynamically-typed languages, only values have types, not names. S = 13.45 # a floating-point number … S = [1, 2, 3, 4] # a list • Run-time values are described by discriminated unions. Discriminant denotes type of value. S = X + Y # arithmetic or concatenation • The Variant type in BASIC has the same property. • The Tag in a class object functions like a discriminant

  20. Access Types and pointers • Related (but distinct) notions: • a value that denotes a memory location • a dynamic name that can designate different objects • a mechanism to separate stack and heap allocation type ptr is access integer; -- Ada: named type typedef ptr int*; -- C, C++ • A value of type (access T) designates a value of type T

  21. Dynamic data structures type Cell; -- an incomplete type type Ptr is access Cell; -- an access to it type Cell is record -- its full declaration value : Integer; next, prev : Ptr; end record; List: Ptr := new Cell ‘(10, null, null); … -- a list is just a pointer to its first element List.next := new Cell ‘(15, null, null); List.next.prev := List;

  22. Incomplete declarations in C++ struct cell { int Value; cell* prev; //OK to mention name before end of declaration cell* next; }; struct List; // incomplete declaration struct Link { Link* succ; List* member_of; }; // a pointer to incomplete type struct List { // full definition of the type Link* head: // with mutual references };

  23. Pointers and dereferencing • Need notation to distinguish pointer from designated object, Ptr • Ada: access types are automatically dereferenced • C: int *Ptr; Ptr, *Ptr, &x • C++: reference types are automatically dereferenced • in Java: no notion of pointer, everything (except primitive types) is a reference • For pointers to composite values, dereference can be implicit: • in Ada : C1.Value equivalent to C1.all.Value • in C++ : distinguish C1.Value and C1 -> Value • in both : pointers to arrays are indexable: arr_ptr (5), arr_ptr[5]

  24. Pointers and safety • Pointers create aliases: accessing the value through one name affects the retrieval through the other: int* tab1, tab2; … tab1 = newint [10]; // allocate tab2 = tab1; // share delete (tab1); // discard storage tab2 [5] = .. // error, tab2 does not denote anything

  25. Dangling references • If we can point to local storage, we can create a reference to an undefined value: int* f ( ) { // returns a pointer to an integer int local; // variable on stack frame of f … return local&; // reference to local entity }; int x = f ( ); … x + 1 ... // stack may have been overwritten

  26. Deallocation • Manual deallocation is potentially dangerous, because not all current references to an object may be visible. System is safer if storage reclamation is automated. • Manual solution: make deallocation more explicit: procedure free is new Ada.Unchecked_Deallocation (String, Ptr); • Semi-automatic solution (Ada, C++): destructors, controlled types • Automatic Solution (Java, ML): garbage collector

  27. APL

  28. APL • Developed by Kenneth Iverson in the early 1960’s • Tool for mathematicians • Tool for thought • Way of thinking • Very high level language for matrix manipulation • Widely used by actuaries in Insurance • Use restricted by special character set including greek letters and other symbols

  29. Typing and Scope • Dynamic Scope • Two Types – Numbers and Characters • Automatic conversion between floating point and integer • Strings are character vectors • Boolean values are 0 and 1 • Type associated with Values, not names • Tagged types • Run-time checking

  30. Examples • PRINT MEAN, MAX AND MIN •  STAT X • NX • (+/X)N • /X • /X •  • REMOVE LEADING CHARACTERS FROM A STRING •  R  C REMC S • R(\SC)/S • 

  31. Syntax • Simple syntax • Right to left evaluation • infix operators and functions • modifiers (verbs and adverbs) • Modifiers are operators that modify the operation of other operators • Can be parsed with only 3 states (Zaks) • Expression Language • No selection or looping statements • Only goto • Scalar operators automatically extend to matrices • Loops are unusual in APL

  32. Operations on numbers Monadic • ,  -- grade up/down X X [X] returns indices of elements in sorted order • ,  -- ceiling/floor 3.4 = 4 Dyadic • ,  -- max, min x  y returns maximum of x or y 2  3 = 3

  33. Operations on Arrays •  -- interval n returns a vector of integers from origin to n 4 = 1 2 3 4 •  -- size 0 1 2 3 = 4 • Dyadic -- shape reshapes an array 2 20 1 2 3 creates a 2 x 2 array •  -- Transpose Rotates an array along the major diagonal •  -- Domino Does matrix inversion and division

  34. Operators on Operators • .+ -- outer product 1 2 .+ 3 4 4 5 5 6 • +. -- inner product 1 2 +. 3 4 – matrix multiplication 7 14 • +/ -- reduction +/2 3 4 = 9 equivalent to 2 + 3 + 4 • +\ -- scan +\2 3 4 = 2 5 9 like reduction, but with intermediate results ^\ 0 0 1 0 1 = 0 0 1 1 1 -- turns on all bits after first 1 • Any dyadic operator can be used for + or 

More Related