ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union...

43
ISBN 0-321-19362-8 Chapter 6 Structured Data Types •Array Types •Associative Arrays •Record Types •Union Types

Transcript of ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union...

Page 1: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

ISBN 0-321-19362-8

Chapter 6

Structured Data Types•Array Types•Associative Arrays•Record Types•Union Types

Page 2: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-2

Structured data Types

• A structured data type is defined in terms of other types

• A structured type is usually composed of multiple elements.– In homogeneous types, all elements have the same

type

– In heterogeneous types, elements may have different types.

Page 3: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-3

Arrays

• An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element.

Page 4: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-4

Arrays

• Design Issues:1. What types are legal for subscripts?

2. Are subscripting expressions in element

references range checked?

3. When are subscript ranges bound?

4. When does allocation take place?

5. What is the maximum number of subscripts?

6. Can array objects be initialized?

7. Are any kind of slices allowed?

Page 5: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-5

Arrays

• Indexing is a mapping from indices to elements

map(array_name, index_value_list) an element

• Index Syntax– FORTRAN, PL/I, Ada use parentheses

– Most other languages use brackets

Page 6: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-6

Arrays

• Subscript Types:– FORTRAN, C - integer only

– Pascal - any ordinal type (integer, boolean, char, enum)

– Ada - integer or enum (includes boolean and char)

– Java - integer types only

Page 7: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-7

Array Bindings

• Categories of arrays (based on subscript binding and binding to storage)

1. Static

2. Fixed stack-dynamic

3. Stack-dynamic

4. Heap-dynamic

Page 8: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-8

Static Arrays

• Range of subscripts and storage bindings are static– Both can be done by the compiler

– FORTRAN 77, some arrays in Ada, static arrays in C

– Advantage: execution efficiency (no allocation or deallocation)

Page 9: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-9

Fixed stack dynamic Arrays

• The range of subscripts is statically bound, but storage is bound at elaboration time– e.g. Most Java locals, and C locals that are not static

– Advantage: space efficiency

– Using stack memory means the space can be reused when array lifetime ends

Page 10: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-10

Stack-dynamic Arrays

• Range and storage are dynamic, but fixed from then on for the variable’s lifetime– e.g. Ada declare blocks

declare STUFF : array (1..N) of FLOAT; begin ... end;

– Advantage: flexibility - size need not be known until the array is about to be used

• Fixed Heap-Dynamic arrays are similar but use heap memory.

Page 11: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-11

Heap-dynamic Arrays

• Subscript range and storage bindings are dynamic and not fixed– e.g. (FORTRAN 90)

INTEGER, ALLOCATABLE, ARRAY (:,:) :: MAT

(Declares MAT to be a dynamic 2-dim array)

ALLOCATE (MAT (10,NUMBER_OF_COLS))

(Allocates MAT to have 10 rows and

NUMBER_OF_COLS columns)

DEALLOCATE MAT

(Deallocates MAT’s storage)

Page 12: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-12

Heap-dynamic Arrays (continued)

– In APL, Perl, and JavaScript, arrays grow and shrink as needed

– In C and C++, you can create heap-dynamic arrays using pointers.

– In Java, all arrays are objects (heap-dynamic)

– C# provides both heap-dynamic and fixed-heap dynamic

Page 13: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-13

Array Attributes

• Number of subscripts– FORTRAN I allowed up to three

– FORTRAN 77 allows up to seven

– Others - no limit

• Array Initialization – Usually just a list of values that are put in the array

in the order in which the array elements are stored in memory

Page 14: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-14

Array Initialization

• Examples of array initialization:1. FORTRAN - uses the DATA statement, or put the

values in / ... / on the declaration2. Java, C and C++ - put the values in braces; can let

the compiler count them e.g. int stuff [] = {2, 4, 6, 8};3. For strings (which are treated as arrays in C and

C++), an alternate form of initialization is provided.

Char* names[] = {"Bob", "Mary", "Joe"};

Page 15: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-15

Array Initialization

• Examples of array initialization:3. Ada - positions for the values can be specified

e.g.

SCORE : array (1..14, 1..2) :=

(1 => (24, 10), 2 => (10, 7),

3 =>(12, 30), others => (0, 0));

4. Pascal does not allow array initialization

Page 16: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-16

Array Attributes

• Array Operations1. APL - many, see book (p. 240-241)

2. Ada

– Assignment; RHS can be an aggregate constant or an array name

– Catenation; for all single-dimensioned arrays

– Relational operators (= and /= only)

3. FORTRAN 90

– Intrinsics (subprograms) for a wide variety of array operations (e.g., matrix multiplication, vector dot product)

– Elementals (+) act combine corresponding elements from two arrays

Page 17: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-17

Array Slices

• A slice is some substructure of an array– Nothing more than a referencing mechanism

– A way of designating a part of the array

• Slices are only useful in languages for operations that can be done on a whole array

Page 18: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-18

Array Slices

1. FORTRAN 90

INTEGER MAT (1:4, 1:4)

MAT(1:4, 1) - the first column

MAT(2, 1:4) - the second row

Page 19: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-19

Example Slices in FORTRAN 90

Page 20: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-20

Array Slices

2. Ada - single-dimensioned arrays onlyLIST(4..10)

3. Java has something like slices for multi-dimensioned arrays

int [][]array =array[1] gets the second row

Page 21: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-21

Memory for arrays

• For 1D arrays, have a contiguous block of memory with equal amount of space for each element

• Two approaches for multi-dimensional arrays– Single block of contiguous memory for all

elements• Arrays must be rectangular• Address of array is starting memory location

– Implement as arrays of arrays (Java)• Jagged arrays are possible• Array variable is a pointer (reference)

Page 22: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-22

Contiguous Array Memory

• Row major (by rows) or column major order (by columns) for 2D array

• Access function maps subscript expressions to an address in the array

Page 23: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-23

Locating an Element

Page 24: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-24

Array Descriptors

Single-dimensioned array Multi-dimensional array

Page 25: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-25

Associative Arrays

• An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys– A hash table has the same behavior

• Design Issues:1. What is the form of references to elements?

2. Is the size static or dynamic?

Page 26: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-26

Associative Arrays

• Structure and Operations in Perl– Names begin with %– Literals are delimited by parentheses

e.g.,

%hi_temps = ("Monday" => 77,

"Tuesday" => 79,…);– Subscripting is done using braces and keys

e.g.,

$hi_temps{"Wednesday"} = 83;– Elements can be removed with delete

e.g.,

delete $hi_temps{"Tuesday"};

Page 27: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-27

Records

• A record is a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names

• Design Issues:1. What is the form of references?

2. What unit operations are defined?

Page 28: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-28

Records

• Record Definition Syntax– COBOL uses level numbers to show nested

records; others use recursive definition

• Record Field References1. COBOL

field_name OF record_name_1 OF ... OF record_name_n

2. Others (dot notation)

record_name_1.record_name_2. ... record_name_n.field_name

Page 29: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-29

Records

• Fully qualified references must include all record names

• Elliptical references allow leaving out record names as long as the reference is unambiguous (Cobol only)

• Pascal provides a with clause to abbreviate references

Page 30: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-30

Record Descriptors

A compile-time descriptor for a record

Page 31: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-31

Record Operations

1. Assignment

– Pascal, Ada, and C allow it if the types are identical

– In Ada, the RHS can be an aggregate constant

2. Initialization

– Allowed in Ada, using an aggregate constant

Page 32: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-32

Record Operations (continued)

3. Comparison

– In Ada, = and /=; one operand can be an aggregate constant

4. MOVE CORRESPONDING– In COBOL - it moves all fields in the source

record to fields with the same names in the destination record

Page 33: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-33

Comparing Records and Arrays

1. Access to array elements is much slower than access to record fields, because subscripts are dynamic (field names are static)

2. Dynamic subscripts could be used with record field access, but it would disallow type checking and it would be much slower

Page 34: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-34

Unions

• A union is a type whose variables are allowed to store different type values at different times during execution

• Design Issues for unions:1. What kind of type checking, if any, must be done?

2. Should unions be integrated with records?

Page 35: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-35

Union Examples

1. FORTRAN - with EQUIVALENCEEQUIVALENCE (A, B, C, D), (X(1), Y(1))– No type checking

2. Pascal - both discriminated and nondiscriminated unions

e.g. type intreal =

record tagg : Boolean of

true : (blint : integer);

false : (blreal : real);

end; – Problem with Pascal’s design: type checking is ineffective

Page 36: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-36

Unions

A discriminated union of three shape variables

Page 37: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-37

Pascal Unions

• Pascal’s unions cannot be type checked effectively:a. User can create inconsistent unions (because the tag can be

individually assigned)

var blurb : intreal;

x : real;

blurb.tagg := true; { it is an integer }

blurb.blint := 47; { ok }

blurb.tagg := false; { it is a real }

x := blurb.blreal; { assigns an integer to real }

Page 38: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-38

Pascal Unions

b. The tag is optional!

– Now, only the declaration and the second and last assignments are required to cause trouble

Page 39: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-39

Ada Unions

• Ada has discriminated unionsReasons they are safer than Pascal:

a. Tag must be present

b. It is impossible for the user to create an inconsistent union (because tag cannot be assigned by itself--All assignments to the union must include the tag value, because they are aggregate values)

Page 40: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-40

Unions in C, C++ and Java

• C and C++ have free unions (no tags)– Not part of their records

– No type checking of references

• Java has neither records nor unions

Page 41: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-41

Evaluation

• Unions are unsafe in most languages– not Ada

• Not really necessary in most systems today – Cheap memory

– Virtual memory

Page 42: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-42

Sets

• A set is a type whose variables can store unordered collections of distinct values from some ordinal type

• Design Issue:– What is the maximum number of elements in any

set base type?

Page 43: ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-43

Other Structured Types

• Some languages provide lists– LISP

• Classes are structured types with methods attached to them (more later)