Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 4 Aggregate Data Types. 2 Aggregates Types defined as aggregates of other types Types defined as aggregates of other types At least two types.

Similar presentations


Presentation on theme: "1 Lecture 4 Aggregate Data Types. 2 Aggregates Types defined as aggregates of other types Types defined as aggregates of other types At least two types."— Presentation transcript:

1 1 Lecture 4 Aggregate Data Types

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

3 3 Abstract Data Types An Abstract data type defines a protocol – or contract – for using the type, but does not imply any particular implementation 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 Classes are abstract data types Arrays can be viewed either as abstract or concrete types Arrays can be viewed either as abstract or concrete types

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

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

6 6 Aggregates and Qualification Aggregates and Qualification Aggregate may be ambiguous: Aggregate may be ambiguous: type Vector is array (1.. 3) of Float; type Vector is array (1.. 3) of Float; procedure Display (V : vector); procedure Display (V : vector); type Assay is array (1.. 3) of Float; type Assay is array (1.. 3) of Float; procedure Display (A : assay); procedure Display (A : assay); … … Display ((1.0, 1.2, 1.5)); -- which? ambiguous Display ((1.0, 1.2, 1.5)); -- which? ambiguous Display (Vector ‘ (1.0, 1.2, 1.5)); -- OK. Display (Vector ‘ (1.0, 1.2, 1.5)); -- OK.

7 7 Ada Array Types Index types can be of any discrete type Index types can be of any discrete type type week is array ( Sunday.. Saturday) of Float); Component type must be definite, i.e. have bounds: Component 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 (1..10); -- OK type class_list is array ( 1.. 100) of String; -- Error type class_list is array ( 1.. 100) of String; -- Error The subtype constrains all indices or none:: The subtype constrains all indices or none:: type Matrix is array type Matrix is array (positive range <>, positive range <>) of Long_Float; (positive range <>, positive range <>) of Long_Float; subtype Table is Matrix; subtype Table is Matrix; subtype Rotation is Matrix (1.. 3, 1.. 3); subtype Rotation is Matrix (1.. 3, 1.. 3); arrays are objects with assignment: (unlike C, C++) arrays are objects with assignment: (unlike C, C++) Table1 := Table2; -- all components assigned Table1 := Table2; -- all components assigned

8 8 Anonymous Array Types Grades : array (1.. Num_Students) of Natural; Grades : array (1.. Num_Students) of Natural; type of Grades has no name: distinct from any other array types. type of Grades has no name: distinct from any other array types. Ar1: array (1.. 10) of Boolean; Ar1: array (1.. 10) of Boolean; Ar2 : array (1.. 10) of Boolean; Ar2 : array (1.. 10) of Boolean; … … Ar1 := Ar2; -- Error: different (anonymous) types. Ar1 := Ar2; -- Error: different (anonymous) types. If a type is a useful abstraction, it deserves to have a name! If a type is a useful abstraction, it deserves to have a name!

9 9 Array Attributes Array Attributes Ada: Ada: type Matrix is array (Positive range <>, Positive range <>) of Float; type Matrix is array (Positive range <>, Positive range <>) of Float; subtype Rect is Matrix (1.. 3, 1.. 5); subtype Rect is Matrix (1.. 3, 1.. 5); M3 : Rect; M3 : Rect; M3’First (1) -- Yields 1 M3’First (1) -- Yields 1 M3’First -- same. M3’First -- same. Rect’length (2) -- Yields 5 (applies to type) Rect’length (2) -- Yields 5 (applies to type) M3’range (2) -- equivalent to 1..5 M3’range (2) -- equivalent to 1..5 String’Length -- ERROR unconstrained String’Length -- ERROR unconstrained Java: int Rect[3,5]; Java: int Rect[3,5];Rect.length Arrays are self-describing: size information is built-in Unlike C, C++ Unlike C, C++

10 10 Array Initialization Array Initialization Expression that yields an array value: Expression that yields an array value: A := (1, 2, 3, 10); -- positional A := (1, 2, 3, 10); -- positional A := (1, others => 0); -- notation for default. A := (1, others => 0); -- notation for default. A := (1..3 => 1, 4 => -999); -- component associations A := (1..3 => 1, 4 => -999); -- component associations Default can only be used if bounds are known: Default can only be used if bounds are known: A : String (1.. 10) := (others => ‘?’); -- OK A : String (1.. 10) := (others => ‘?’); -- OK A : String := (others => ‘?’); -- Error: unknown bounds. A : String := (others => ‘?’); -- Error: unknown bounds.

11 11 Initializers in C++ Similar notion for declarations: 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) but no array assignments, so initializer is not an expression (mechanism is less orthogonal)

12 12 Multidimensional Arrays Multidimensional Arrays Aggregates given in row-major order with subaggregates: Aggregates given in row-major order with subaggregates: type Square is array (1.. 3, 1.. 3) of Integer; type Square is array (1.. 3, 1.. 3) of Integer; Unit : constant Square := ( (1, 0,0), (0, 1, 0), (0, 0, 1)); Unit : constant Square := ( (1, 0,0), (0, 1, 0), (0, 0, 1)); A two-dimensional array is NOT array of arrays: A two-dimensional array is NOT array of arrays: type vector is array (1.. 3) of Integer; type vector is array (1.. 3) of Integer; type V3 is array (1.. 3) of vector; type V3 is array (1.. 3) of vector; -- not convertible to Square -- not convertible to Square Unlike C Unlike C int matrix[4][4]; int matrix[4][4]; Note that this is consistent with the concrete storage model for arrays Note that this is consistent with the concrete storage model for arrays

13 13 String Types Abstract Data Type String Abstract Data Type String Sequence of characters Sequence of characters Set of operations: concatenation, substring operations, etc. Set of operations: concatenation, substring operations, etc. Implemented as Implemented as pointer to a null terminated array of characters ( C ) 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 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) Pointer to array of chars and pointer to end of array (STL) a class (java) a class (java)

14 14 Variations on Strings: Ada Strings are arrays: type String is array (positive range <>) of character; 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 15 Variations on Strings: C++ char* name1, name2; char title[ ] = “brave new world”; // 16 characters: implicit 0 at end // 16 characters: implicit 0 at end char* t = “island”; // pointer to constant array name1 = new char[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) semantic equivalence: a[k] = * (a + k)

16 16 Variations on Strings: Java Strings are classes, not arrays: need special notation for indexing and slicing. 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 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: // implicit conversion of integer to string: “The answer is 42!” “The answer is 42!” if (name.StringAt (0) == ‘T’ ) { // true

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

18 18 Records type city is record -- Ada Name: String (1..10); Name: String (1..10); Country : String (1..20); Country : String (1..20); Population: integer; Population: integer; Capital : Boolean; Capital : Boolean; end record; struct city { -- C, C++ char* name; char* name; char* country; char* country; int population int population bool capital } bool capital }

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

20 20 Access Types and pointers Related (but distinct) notions: Related (but distinct) notions: a value that denotes a memory location a value that denotes a memory location a dynamic name that can designate different objects a dynamic name that can designate different objects a mechanism to separate stack and heap allocation 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 A value of type (access T) designates a value of type T

21 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; value : Integer; next, prev : Ptr; next, prev : Ptr; end record; List: Ptr := new Cell ‘(10, null, null); List: Ptr := new Cell ‘(10, null, null); … -- a list is just a pointer to its first element … -- a list is just a pointer to its first element List.next := new Cell ‘(15, null, null); List.next := new Cell ‘(15, null, null); List.next.prev := List; List.next.prev := List;

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

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

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

25 25 Dangling references If we can point to local storage, we can create a reference to an undefined value: 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 int local; // variable on stack frame of f … return local&; // reference to local entity return local&; // reference to local entity }; }; int x = f ( ); … x + 1... // stack may have been overwritten

26 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 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: Manual solution: make deallocation more explicit: procedure free is new Ada.Unchecked_Deallocation (String, Ptr); procedure free is new Ada.Unchecked_Deallocation (String, Ptr); Semi-automatic solution (Ada, C++): destructors, controlled types Semi-automatic solution (Ada, C++): destructors, controlled types Automatic Solution (Java, ML): garbage collector Automatic Solution (Java, ML): garbage collector

27 27 APL

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

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

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

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

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

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

34 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 +/ -- reduction +/2 3 4 = 9 equivalent to 2 + 3 + 4 +\ -- scan +\2 3 4 = 2 5 9 +\ -- 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 


Download ppt "1 Lecture 4 Aggregate Data Types. 2 Aggregates Types defined as aggregates of other types Types defined as aggregates of other types At least two types."

Similar presentations


Ads by Google