Sunteți pe pagina 1din 29

Chapter 6

m
o

c
.
rs

Structured Data
Types
Arrays
Records

e
e
in

a
a
F

o
D

E
O

g
n

Definitions
data type

c
.
rs

collection of data objects


a set of predefined operations

m
o

e
e
n
descriptor : collection
of attributes for a
i
g
n
variable
E
O
object : instance
of a user-defined
o
Ddata) type
(abstract
a
a
F

Structured Data Types


Built out of other types

m
o

c
.
usually composed of multiple
s
r
e
elements.
e
n
i
homogeneous : all
elements have the
g
same type En
O
o
heterogeneous
: elements have
D
a
different
types
a
F

Structured Data Types


Arrays

m
o

c
.
rs

aggregate of homogeneous data


elements indexed by its position

e
e
in

Associative arraysg

n
E

unordered collection of key-value pairs

O
o

RecordsD

a
a
heterogeneous
aggregate of data
F

elements indexed by element name

Array Operations
Whole array operations:
assignment
catenation

c
.
rs

m
o

e
e
Elemental operations
same as those
n
i
g
of base type n
E
O
Indexing :omapping
from indexes to
D
elements
a
a
F
array_name
(index_value_list) an
element

Array Design Issues


What types are legal for subscripts?
m
Are subscripting expressionsoin
c
.
element references range
checked?
s
r
e bound?
When are subscriptnranges
e
i
g
When does allocation
take place?
n
E
O
What is the
maximum number of
o
D
subscripts?
a
a
F array objects be initialized?
Can
Are any kind of slices allowed?

Binding Time Choices


Static: compile-time binding of subscript
range and memory
Fixed stack-dynamic: subscript ranges
static, allocated at declaration time (C,
C++)
Stack-dynamic: run-time binding of
subscript range and memory
Fixed heap-dynamic: storage binding is
dynamic but fixed after allocation (Java, C
and C++)
Heap-dynamic: binding of subscript ranges
and storage allocation is dynamic (Perl and

m
o

c
.
rs

e
e
in

g
n

E
O

a
a
F

o
D

Array Initialization
Some language allow initialization at
the time of storage allocation m

o
c
C, C++, Java, C# example .
s
r
e7, 83}
int list [] = {4, 5,
e
Character stringsg
inin
C and C++
n
char name []E= freddie;
O
ArraysD
ofostrings in C and C++
charaa
*names [] = {Bob, Jake,
F
Joe};

Memory for arrays


For 1D arrays, contiguous block of
memory with equal amount of space
m
o
for each element
c
.
s
r
Two approaches for multie
e
dimensional arraysin

g
n

Single block of contiguous memory for


all elements

E
O

o
D

Arrays must be rectangular


Address of array is starting memory
location

a
a
F

Implement as arrays of arrays (Java)


Jagged arrays are possible
Array variable is a pointer (reference)

Implementation of Arrays
Access function maps subscript expressions
to an address in the array
Access function for single-dimensioned
arrays:

m
o

c
.
rs

e
e
in

g
n

address(list[k]) = address (list[lower_bound])

E
O

+ ((k-lower_bound) * element_size)

o
D

Two common ways to organize 2D arrays

a
a
Row major order (by rows) used in most
F

languages
Column major order (by columns) used in
Fortran

Contiguous Array Memory


Row major (by
c
.
s
rows) or column
r
e
major order (by
e
n
i
columns) for 2D g
n
array
E
O
o
Access D
function
asubscript
maps
a
F
expressions to an
address in the
array

m
o

Row-major access formula


Location (a[I,j])
= address of a [row_lb,col_lb]
+ (((I - row_lb) * n) + (j - col_lb)) *element_size

m
o

c
.
rs

e
e
in

g
n

E
O

a
a
F

o
D

Rectangular and Jagged


A rectangularArrays
array is a multi-

dimensioned array in which all m


of
o
c
the rows have the same number
of
.
s
r
elements and all columns
have the
e
e
same number of g
elements
in
n
A jagged matrix
has rows with
E
O
o
varying D
number
of elements

a
a
F

Possible when multi-dimensioned


arrays actually appear as arrays of
arrays

Slices
A slice is some substructure of an
array; it is nothing more than am
o
referencing mechanism
c
.
s
Slices are only useful iner
languages
e
that have array operations
in

g
n

Java allows row slices from 2D arrays


Fortran 95

E
O

o
Integer,
Dimension (10) :: Vector
D
a Dimension (3, 3) :: Mat
Integer,
a
FInteger, Dimension (3, 3) :: Cube
Vector (3:6) is a four element array

Slices Examples in Fortran


95

m
o

c
.
rs

e
e
in

g
n

E
O

a
a
F

o
D

Compile-Time Descriptors

m
o

c
.
rs

e
e
in

g
n

E
O

o
D

a
a
Single-dimensioned
array
F

Multi-dimensional array

Associative Arrays
An associative array is an unordered
m
collection of data elements that
are
o
c
.
indexed by an equal number
of
s
r
values called keys nee

i
g
A hash table has
the same behavior
n
E
O
o
D
Design
Issues:
a
a
1. F
What is the form of references to
elements?
2. Is the size static or dynamic?

Associative Arrays in Perl


Names begin with %; literals are
delimited by parentheses
m

o
c
%hi_temps = ("Mon" => 77,. "Tue"
s
r
=> 79, Wed => 65, e);
e
n
i
Subscripting is done
using braces
g
n
and keys
E
O
o
$hi_temps{"Wed"}
= 83;
D
a
Elements
can be removed with delete
a
F
delete $hi_temps{"Tue"};

Record Types
A possibly heterogeneous aggregate
of data elements
m
o
c
.
Individual elements identified
by field
s
r
e
name
e
n
i
Like a class with no
methods and only
g
n
public data. OE
o
Designaissues:
D

a
What
F is the syntactic form of references
to the field?
Are elliptical references allowed

Definition of Records in Ada


Record structures are indicated in an
m
orthogonal way
o
c
.
type Emp_Rec_Type is rrecord
s
e
e
First: String n
(1..20);
i
g
Mid: Stringn (1..10);
E
Last: String
(1..20);
O
o
D
Hourly_Rate:
Float;
a
a
endFrecord;
Emp_Rec: Emp_Rec_Type;

structs in C
Define a record in C using the struct syntax
struct record {
int var1;
double var2;
}

m
o

c
.
rs

e
e
in

g
n

E
Structs can be O
copied
o
struct record
D
a
a
records
F

r1, r2 // mem for 2

r1.var1 = 1; r1.var2 = 2.3;


r2 = r1; // copy data from r1 into r2

References to Records
Most language use dot notation
Emp_Rec.Name

m
o

c
.
Fully qualified referencesrmust
include
s
e
all record names
e
n
i
g
Elliptical references
allow leaving out
n
record names E
as long as the reference
O
o
is unambiguous,
for
example
in
D
a
COBOL
a
F
FIRST, FIRST OF EMP-NAME, and
FIRST of EMP-REC are elliptical
references to the employees first

Operations on Records
Assignment is very common if the
m
types are identical
o
c
.
s
Ada allows record comparison
r
e
e
Ada records can bein
initialized with
g
aggregate literals
n
E
O
COBOL provides
MOVE
o
CORRESPONDING
aD

a
F a field of the source record to
Copies
the corresponding field in the target
record

Records vs. Arrays


Straight forward and safe design
m
o
c
Use records when collection
of data
.
s
r
values is heterogeneous
e
e
n
i
Access to array elements
is much
g
n
slower than access
E to record fields

O
o
subscripts
are dynamic
D
a
a
field names are static
F

Implementation of Record Type

m
o

Offset address relative to


the beginning of the records
is associated with each field

g
n

e
e
in

E
O

a
a
F

o
D

c
.
rs

Union Types
A type whose elements are allowed to
m
o
store different types at different
times
c
.
s
during execution
r
e
e
Fortran, C, and C++inprovide free

union

g
n

E
O
no language
support for type checking
o
D
a
Type a
checking requires extra element
F
Type indicator called a discriminant
Supported by Ada

Evaluation of Unions
Potentially unsafe construct

m
o

c
.
Do not allow type checkings
r
e unions
e
Java and C# do not n
support
i
g
Reflective of growing
concerns for
n
E
safety in programming
language
O
o
D
a
a
F

Type Equivalence
Consider the problem of two structured
types:
Are two record types compatible if they
are structurally the same but use
different field names?
Are two array types compatible if they
are the same except that the subscripts
are different?
(e.g. [1..10] and [0..9])
Are two enumeration types compatible
if their components are spelled
differently?

m
o

c
.
rs

e
e
in

g
n

E
O

a
a
F

o
D

Two approaches
Name type compatibility : two variables have
compatible types if they are in either the same
declaration or in declarations that use the same
type name

m
o

c
.
rs

e
e
in

Easy to implement but highly restrictive:


Subranges of integer types are not compatible with integer types
Formal parameters must be the same type as their
corresponding actual parameters (Pascal)

g
n

E
O

o
D

Structure type compatibility means that two


variables have compatible types if their types have
identical structures

a
a
F

More flexible, but harder to implement

S-ar putea să vă placă și