Documente Academic
Documente Profesional
Documente Cultură
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
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
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
m
o
c
.
rs
e
e
in
Associative arraysg
n
E
O
o
RecordsD
a
a
heterogeneous
aggregate of data
F
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
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};
g
n
E
O
o
D
a
a
F
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
E
O
+ ((k-lower_bound) * element_size)
o
D
a
a
Row major order (by rows) used in most
F
languages
Column major order (by columns) used in
Fortran
m
o
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
a
a
F
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
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
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?
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
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
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
O
o
subscripts
are dynamic
D
a
a
field names are static
F
m
o
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
g
n
E
O
o
D
a
a
F