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

34
1 Lecture 4 Lecture 4 Aggregate Data Types Aggregate Data Types
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    235
  • download

    0

Transcript of 1 Lecture 4 Aggregate Data Types. 2 Aggregates Types defined as aggregates of other types Types...

11

Lecture 4Lecture 4

Aggregate Data Types Aggregate Data Types

22

AggregatesAggregates

Types defined as aggregates of other Types defined as aggregates of other typestypes

At least two typesAt 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 Arrays and lists contain multiple instances of the same typeof the same type Some languages allow heterogeneous arraysSome languages allow heterogeneous arrays

Records (Structures) and Classes contain Records (Structures) and Classes contain named instances of different typesnamed instances of different types

33

Abstract Data TypesAbstract Data Types

An Abstract data type defines a An Abstract data type defines a protocol – or contract – for using the protocol – or contract – for using the type, but does not imply any type, but does not imply any particular implementationparticular implementation

Classes are abstract data typesClasses are abstract data types Arrays can be viewed either as Arrays can be viewed either as

abstract or concrete typesabstract or concrete types

44

Concrete ArraysConcrete Arrays An array is a sequence of elements in An array is a sequence of elements in

memorymemory The shape of an array determined by The shape of an array determined by

a radix vectora radix vector Dimensionality determined by length Dimensionality determined by length

of the radix vectorof the radix vector Array addressing: Array addressing:

A is array (0..2, 0..2) of integerA is array (0..2, 0..2) of integer A[1,2] is at: base + (1 * 3) + 2A[1,2] is at: base + (1 * 3) + 2

55

Array as ADTArray as ADT Abstraction: Indexed storage of elementsAbstraction: Indexed storage of elements

Set or read value of specific elementSet or read value of specific element Iterate over elementsIterate 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 elementsContiguous sequence of elements List of listsList 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?

66

Aggregates and Aggregates and QualificationQualification

Aggregate may be ambiguousAggregate may be ambiguous::

typetype Vector Vector isis arrayarray (1 .. 3) (1 .. 3) ofof Float; Float;

procedure Display (V : vector);procedure Display (V : vector);

typetype Assay Assay isis arrayarray (1 .. 3) (1 .. 3) ofof Float; Float;

procedure Display (A : assay);procedure Display (A : assay);

… …

Display ((1.0, 1.2, 1.5));Display ((1.0, 1.2, 1.5)); -- which? -- which?

ambiguousambiguous

Display (Vector ‘ (1.0, 1.2, 1.5));Display (Vector ‘ (1.0, 1.2, 1.5)); -- OK.-- OK.

77

Ada Array TypesAda Array Types Index types can be of any discrete typeIndex types can be of any discrete type

typetype week week isis arrayarray ( Sunday .. Saturday) ( Sunday .. Saturday) ofof Float); Float);

Component type must be definite, i.e. have bounds:Component type must be definite, i.e. have bounds:

typetype class_list class_list isis arrayarray ( 1 .. 100) ( 1 .. 100) ofof String (1..10); String (1..10); -- OK-- OK

typetype class_list class_list isis arrayarray ( 1 .. 100) ( 1 .. 100) ofof String; String; -- Error-- Error

The subtype constrains all indices or none:The subtype constrains all indices or none:::

typetype Matrix Matrix isis arrayarray

(positive (positive rangerange <>, positive <>, positive rangerange <>) <>) ofof Long_Float; Long_Float;

subtypesubtype Table Table isis Matrix; Matrix;

subtypesubtype Rotation Rotation isis Matrix (1 .. 3, 1 .. 3); Matrix (1 .. 3, 1 .. 3);

arrays are objects with assignment: (unlike C, C++)arrays are objects with assignment: (unlike C, C++)

Table1 := Table2; Table1 := Table2; -- all components assigned-- all components assigned

88

Anonymous Array TypesAnonymous Array Types Grades : Grades : arrayarray (1 .. Num_Students) (1 .. Num_Students) ofof Natural; Natural;

type of Grades has no name: distinct from any other type of Grades has no name: distinct from any other

array typesarray 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; Ar1 := Ar2; -- Error: different (anonymous) -- Error: different (anonymous)

typestypes..

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!

99

Array AttributesArray Attributes Ada:Ada:

typetype Matrix Matrix isis arrayarray (Positive (Positive rangerange <>, Positive <>, Positive rangerange <>) <>) ofof

Float;Float;

subtypesubtype Rect Rect isis Matrix (1 .. 3, 1 .. 5); Matrix (1 .. 3, 1 .. 5); M3 : Rect;M3 : Rect; M3’First (1) M3’First (1) -- Yields 1-- Yields 1 M3’First M3’First -- same.-- same. Rect’length (2) Rect’length (2) -- Yields 5 (applies to type)-- Yields 5 (applies to type) M3’range (2) M3’range (2) -- equivalent to 1..5-- equivalent to 1..5 String’LengthString’Length -- ERROR unconstrained-- ERROR unconstrained

Java:Java:

int Rect[3,5]; int Rect[3,5];

Rect.lengthRect.length

Arrays are self-describing: size information is built-in Arrays are self-describing: size information is built-in

Unlike C,Unlike C, C++C++

1010

Array InitializationArray Initialization Expression that yields an array valueExpression that yields an array value::

A := (1, 2, 3, 10); A := (1, 2, 3, 10); -- positional-- positional

A := (1, A := (1, othersothers => 0); => 0); -- notation for default. -- notation for default.

A := (1..3 => 1, 4 => -999); A := (1..3 => 1, 4 => -999); -- component -- component

associationsassociations

Default can only be used if bounds are known:Default can only be used if bounds are known:

A : String (1 .. 10) := (A : String (1 .. 10) := (othersothers => ‘?’); => ‘?’); -- OK-- OK

A : String := (A : String := (othersothers => ‘?’); => ‘?’); -- Error: unknown -- Error: unknown

boundsbounds..

1111

Initializers in C++Initializers in C++

Similar notion for declarations:Similar notion for declarations:

int v2[] = {1, 2, 3, 4}; int v2[] = {1, 2, 3, 4}; -- size from initializer-- size from initializer

char v3[2] = {‘a’, ‘z’}; char v3[2] = {‘a’, ‘z’}; -- declared size-- declared size

int v5[10] = {-1}; int v5[10] = {-1}; -- default : other components -- default : other components = 0= 0

char name [] = “Algol” char name [] = “Algol” -- String literals are aggregates-- String literals are aggregates

but no array assignments, so initializer is not an but no array assignments, so initializer is not an expression (mechanism is less orthogonal)expression (mechanism is less orthogonal)

1212

Multidimensional ArraysMultidimensional Arrays Aggregates given in row-major order with Aggregates given in row-major order with

subaggregatessubaggregates::

typetype Square Square isis arrayarray (1 .. 3, 1 .. 3) (1 .. 3, 1 .. 3) ofof Integer; Integer;

Unit : Unit : constantconstant Square := ( (1, 0 ,0), (0, 1, 0), (0, 0, 1)); 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:

typetype vector vector isis arrayarray (1 .. 3) (1 .. 3) ofof Integer; Integer;

typetype V3 V3 isis arrayarray (1 .. 3) (1 .. 3) ofof vector; vector;

-- not convertible to Square-- not convertible to Square

Unlike CUnlike C

int matrix[4][4];int matrix[4][4];

Note that this is consistent with the concrete storage Note that this is consistent with the concrete storage

model for arraysmodel for arrays

1313

String TypesString Types

Abstract Data Type String Abstract Data Type String Sequence of charactersSequence of characters Set of operations: concatenation, substring Set of operations: concatenation, substring

operations, etc.operations, etc. Implemented as Implemented as

pointer to a null terminated array of characters pointer to a null terminated array of characters ( C )( C )

pointer to integer containing the length of the pointer to integer containing the length of the string followed by array of characters (Basic)string followed by array of characters (Basic)

Pointer to array of chars and pointer to end of Pointer to array of chars and pointer to end of array (STL)array (STL)

a class (java)a class (java)

1414

Variations on Strings: AdaVariations on Strings: AdaStrings are arrays: Strings are arrays:

type type StringString is array is array (positive(positive range range <>) <>) ofof character; character;

typetype Str_Ptr Str_Ptr is accessis access String; String;

Ptr1, Ptr2 : Str_Ptr; -- initially Ptr1, Ptr2 : Str_Ptr; -- initially nullnull

Title : String := “Brave New World” ; -- fixed sizeTitle : String := “Brave New World” ; -- fixed size

Ptr3 : Str_Ptr := Ptr3 : Str_Ptr := newnew String’(“Island”); String’(“Island”);

……

Ptr1 := Ptr3; Ptr1 := Ptr3; -- pointer assignment makes synonyms-- pointer assignment makes synonyms

Ptr1.Ptr1.allall := “what??”; := “what??”; -- array assignment: must be same -- array assignment: must be same

sizesize

Ptr1 := Ptr1 := newnew String (“the genius and the goddess”); String (“the genius and the goddess”);

Title := Ptr1.Title := Ptr1.allall; ; -- run time error: sizes don’t match-- run time error: sizes don’t match

1515

Variations on Strings: C++Variations on Strings: C++

charchar* name1, name2;* name1, name2;charchar title[ ] = “brave new world”; title[ ] = “brave new world”;

// 16 characters: implicit 0 at end// 16 characters: implicit 0 at endcharchar* t = “island”; * t = “island”; // pointer to constant array// pointer to constant arrayname1 = name1 = newnew charchar[16]; [16]; // allocate dynamic storage// allocate dynamic storageconst charconst char* ptr = &title[0]; * ptr = &title[0]; // pointer to local constant // pointer to local constant

arrayarray……whilewhile (*name1++ = *ptr++); (*name1++ = *ptr++); // amusing C idiom// amusing C idiomname1 [0] = ‘B’; name1 [0] = ‘B’; // // titletitle not affected not affectedt [0] = “I”; t [0] = “I”; // illegal: string literal is // illegal: string literal is

constantconstant semantic equivalence: a[k] = * (a + k)semantic equivalence: a[k] = * (a + k)

1616

Variations on Strings: JavaVariations on Strings: Java

Strings are classes, not arrays: need Strings are classes, not arrays: need special notation for indexing and slicing.special notation for indexing and slicing.

String values are constants: need to use String values are constants: need to use arrays of characters to modify strings.arrays of characters to modify strings.

StringString name = “The answer is ”; name = “The answer is ”;……name = name + 42 + “!”); name = name + 42 + “!”); // assign // assign

different valuedifferent value // implicit conversion of integer to string: // implicit conversion of integer to string:

“ “The answer is 42!”The answer is 42!”if if (name.StringAt (0) == ‘T’ ) { (name.StringAt (0) == ‘T’ ) { // true// true

1717

Record typesRecord types

Abstraction: collection of named Abstraction: collection of named fieldsfields Get or set value of a specific fieldGet or set value of a specific field Field names in structure scopeField names in structure scope

Implementation:Implementation: Offsets to field positionsOffsets to field positions Alignment & paddingAlignment & padding

1818

RecordsRecords

typetype city city is record is record -- Ada-- 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 recordend record;;

structstruct city { city { -- C, C++-- C, C++

charchar* name;* name;

charchar* country;* country;

intint population population

boolbool capital } capital }

1919

Discriminated unions and Discriminated unions and dynamic typingdynamic typing

In dynamically-typed languages, only values have In dynamically-typed languages, only values have types, not names.types, not names.

S = 13.45 S = 13.45 # a floating-point number# a floating-point number … … S = [1, 2, 3, 4] S = [1, 2, 3, 4] # a list# a list

Run-time values are described by discriminated Run-time values are described by discriminated

unions. Discriminant denotes type of value.unions. Discriminant denotes type of value. S = X + Y S = X + Y # arithmetic or concatenation# 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 The Tag in a class object functions like a

discriminantdiscriminant

2020

Access Types and pointersAccess Types and pointers

Related (but distinct) notions:Related (but distinct) notions: a value that denotes a memory locationa value that denotes a memory location a dynamic name that can designate different a dynamic name that can designate different

objectsobjects a mechanism to separate stack and heap allocationa mechanism to separate stack and heap allocation

typetype ptr ptr is accessis access integer; integer; -- Ada: named type-- Ada: named type

typedeftypedef ptr ptr intint*; *; -- C, C++-- C, C++

A value of type (access T) designates a value A value of type (access T) designates a value of type Tof type T

2121

Dynamic data structuresDynamic data structures

typetype Cell; Cell; -- an incomplete type-- an incomplete typetypetype Ptr Ptr is accessis access Cell; Cell; -- an access to it-- an access to ittypetype Cell Cell is record is record -- its full declaration-- its full declaration

value : Integer;value : Integer; next, prev : Ptr;next, prev : Ptr;

end recordend record;; List: Ptr := List: Ptr := newnew Cell ‘(10, Cell ‘(10, nullnull, , nullnull); ); … … -- a list is just a pointer to its first element-- a list is just a pointer to its first element List.next := List.next := newnew Cell ‘(15, Cell ‘(15, nullnull, , nullnull);); List.next.prev := List;List.next.prev := List;

2222

Incomplete declarations in Incomplete declarations in C++C++

structstruct cell { cell { intint Value; Value; cell* prev; cell* prev; //// OK to mention name before end of OK to mention name before end of

declarationdeclaration cell* next; }; cell* next; }; structstruct List; List; // incomplete declaration// incomplete declaration

structstruct Link { Link { Link* succ;Link* succ; List* member_of; }; List* member_of; }; // a pointer to incomplete type// a pointer to incomplete type

structstruct List { List { // full definition of the type// full definition of the type

Link* head: Link* head: // with mutual references// with mutual references

};};

2323

Pointers and dereferencingPointers and dereferencing Need notation to distinguish pointer from designated Need notation to distinguish pointer from designated

object, Ptrobject, Ptr Ada: access types are automatically dereferencedAda: access types are automatically dereferenced C: int *Ptr; Ptr, *Ptr, &xC: int *Ptr; Ptr, *Ptr, &x C++: reference types are automatically C++: reference types are automatically

dereferenced dereferenced in Java: no notion of pointer, everything (except in Java: no notion of pointer, everything (except

primitive types) is a referenceprimitive types) is a reference For pointers to composite values, dereference can For pointers to composite values, dereference can

be implicit:be implicit: in Ada : C1.Value in Ada : C1.Value equivalent toequivalent to C1. C1.allall.Value.Value in C++ : distinguish C1.Value and C1 -> Valuein C++ : distinguish C1.Value and C1 -> Value in both : pointers to arrays are indexable: arr_ptr in both : pointers to arrays are indexable: arr_ptr

(5), arr_ptr[5](5), arr_ptr[5]

2424

Pointers and safetyPointers and safety

Pointers create aliases: accessing the Pointers create aliases: accessing the value through one name affects the value through one name affects the retrieval through the other:retrieval through the other:

int* tab1, tab2;int* tab1, tab2;……tab1 = tab1 = newnew intint [10]; [10]; // allocate// allocatetab2 = tab1; tab2 = tab1; // share// sharedeletedelete (tab1); (tab1); // discard storage// discard storagetab2 [5] = .. tab2 [5] = .. // error, tab2 does not denote // error, tab2 does not denote

anythinganything

2525

Dangling referencesDangling references

If we can point to local storage, we can create a If we can point to local storage, we can create a reference to an undefined value:reference to an undefined value:

intint* f ( ) { * f ( ) { // returns a pointer to an // returns a pointer to an integerinteger

intint local; local; // variable on stack frame // variable on stack frame of fof f

…… returnreturn local&; local&; // reference to local entity// reference to local entity };};

intint x = f ( ); x = f ( );… … x + 1 ... x + 1 ... // stack may have been // stack may have been

overwrittenoverwritten

2626

DeallocationDeallocation

Manual deallocation is potentially dangerous, Manual deallocation is potentially dangerous, because not all current references to an object because not all current references to an object may be visible. System is safer if storage may be visible. System is safer if storage reclamation is automated.reclamation is automated.

Manual solutionManual solution: make deallocation more : make deallocation more explicit:explicit:

procedureprocedure free free is newis new Ada.Unchecked_Deallocation (String, Ptr); Ada.Unchecked_Deallocation (String, Ptr); Semi-automatic solutionSemi-automatic solution (Ada, C++): (Ada, C++):

destructors, controlled typesdestructors, controlled types Automatic SolutionAutomatic Solution (Java, ML): garbage (Java, ML): garbage

collectorcollector

2727

APLAPL

2828

APLAPL Developed by Kenneth Iverson in the Developed by Kenneth Iverson in the

early 1960’searly 1960’s Tool for mathematicians Tool for mathematicians

Tool for thoughtTool for thought Way of thinkingWay of thinking Very high level language for matrix Very high level language for matrix

manipulationmanipulation Widely used by actuaries in Insurance Widely used by actuaries in Insurance Use restricted by special character set Use restricted by special character set

including greek letters and other including greek letters and other symbolssymbols

2929

Typing and ScopeTyping and Scope

Dynamic ScopeDynamic Scope Two Types – Numbers and CharactersTwo Types – Numbers and Characters

Automatic conversion between floating point Automatic conversion between floating point and integerand integer

Strings are character vectorsStrings are character vectors Boolean values are 0 and 1Boolean values are 0 and 1

Type associated with Values, not namesType associated with Values, not names Tagged typesTagged types Run-time checkingRun-time checking

3030

ExamplesExamples

3131

SyntaxSyntax Simple syntaxSimple syntax

Right to left evaluationRight to left evaluation infix operators and functionsinfix operators and functions modifiers (verbs and adverbs)modifiers (verbs and adverbs)

Modifiers are operators that modify the operation of other Modifiers are operators that modify the operation of other operatorsoperators

Can be parsed with only 3 states (Zaks)Can be parsed with only 3 states (Zaks) Expression LanguageExpression Language

No selection or looping statementsNo selection or looping statements Only gotoOnly goto

Scalar operators automatically extend to Scalar operators automatically extend to matricesmatrices Loops are unusual in APLLoops are unusual in APL

3232

Operations on numbersOperations on numbers

MonadicMonadic , , -- grade up/down -- grade up/down

XX X [X [X] returns indices of elements in sorted orderX] returns indices of elements in sorted order , , -- ceiling/floor -- ceiling/floor

3.4 = 43.4 = 4

DyadicDyadic , , -- max, min -- max, min

x x y returns maximum of x or y 2 y returns maximum of x or y 2 3 = 3 3 = 3

3333

Operations on ArraysOperations on Arrays -- interval-- interval

n returns a vector of integers from origin to nn returns a vector of integers from origin to n 4 = 1 2 3 44 = 1 2 3 4 -- size-- size 0 1 2 3 = 40 1 2 3 = 4 DyadicDyadic

-- shape -- shape reshapes an array reshapes an array 2 22 20 1 2 3 creates a 2 x 2 array 0 1 2 3 creates a 2 x 2 array -- Transpose-- Transpose

Rotates an array along the major diagonalRotates an array along the major diagonal -- Domino-- Domino

Does matrix inversion and divisionDoes matrix inversion and division

3434

Operators on OperatorsOperators 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 + 4equivalent to 2 + 3 + 4 +\ -- scan +\2 3 4 = 2 5 9+\ -- scan +\2 3 4 = 2 5 9

like reduction, but with intermediate results like reduction, but with intermediate results ^\ 0 0 1 0 1 = 0 0 1 1 1 -- turns on all bits after ^\ 0 0 1 0 1 = 0 0 1 1 1 -- turns on all bits after first 1 first 1

Any dyadic operator can be used for + or