Data Structures
description
Transcript of Data Structures
Data Structures
Arrays
Arrays
• An array is a homogeneous aggregate of data elements in which an individual element is identified by its position in the aggregate relative to the first element.
A [ ][ ][ ][ G ][ … ][ ][ ] A[G] -> Access the Gth element of the array.
• The operation that selects a component from the array is called scripting or indexing.
Array_Name[ Subscript_Value ]
• Selection operation can be though as a mapping from the array name and a set of subscript values to an element of the aggregate.
Arrays: Stack Representation
Arrays: Array Descriptor
Array Descriptor in Stack
Arrays: L-Value (L-Address)
• Let us assume that the 1st element of the array “A” begins at location .
L-Value(A[ N ]) = + ( N – LB ) * EL-Value(A[ N ]) = – LB * E + N * E K = – LB * E and is a constant value.L-Value(A[ N ]) = K + N * EWhen E = 1 and LB = 0, then:L-Value(A[ N ]) = + N
• B: Array[ 3 … 10 ] of INT
= 100 L-Value(B[ 4 ]) = 100 – 3 + 4 = 101LB = 3 E = 1LB ≤ N ≤ UB
Arrays: Virtual Origin (VO)
• So far we know:
L-Value(A[ N ]) = ( – LB * E) + ( N * E )
• And for A[ 0 ]:
L-Value(A[ 0 ]) = ( – LB * E) + ( 0 * E )
• Sometimes the lower bound of the array could be greater than zero. This address is called the virtual origin.
Arrays: Finding the Virtual Origin
1. On creation of vector storage, allocate “N” ( UB – LB + 1) components of the vector of size E and the descriptor of size “D”.
2. Compute the Virtual Origin:VO = – LB * E
Example: VO = 100 – 3 * 1 = 97
3. On accessing an array component (assuming LB N UB):L-Value(A[ N ]) = VO + N * E
Arrays: Finding the Virtual Origin
• Example:
A[ 3 … N]
Assuming: = 100
E = 1VO = 97 (See previous slide, step 2)
L-Value(A[ 4 ]) = VO + N * E
= 97 + 4 * 1
= 101
Arrays: Multidimensional Arrays
• Ordering:Example: M = 3, 4, 7, 6, 2, 5, 1, 3, 8
• Row major order:
M:
• Column major order:
M:
3 4 7
6 2 5
1 3 8
3 6 1
4 2 3
7 5 8
Arrays: Multidimensional Arrays
• Storage• Location of A[ N , J ] is given by:
L-Value( A[ N , J ] ) = + ( N – LB1 ) * S + ( J – LB2 ) * E
= Base Address
S = Length of a Row ( UB2 – LB2 + 1) * E
VO = – LB1 * S – LB2 * E
• Example:L-Value( A[ N , J ]) = VO + N * S + J * E
L-Value( A[ 2 , 3 ]) = ( 2 * row length) + 3
Arrays: Multidimensional Arrays
• Slices– Slices of a 2 dimensional array (matrix) are treated as an array.
• L-Value( A[ N , J ] ) = VO + N * 3 + J * 1VO = – LB1 * S – LB2 * E = – 4Assuming that a = 100:VO = 96L-Value( A[ 2 , 2 ] ) = VO + N * 3 + 2 * 1
= 96 + 2 * 3 + 2 * 1= 96 + 6 + 2= 104
DESCRIPTOR
VO – 4
LB1 1
UB1 4
MULTIPLIER 3
LB2 1
UB2 3
MULTIPLIER 1
Data Structures
Dynamic Arrays
Dynamic Arrays
A[ ] ≡
A[ ] ≡
A[ ][ ] [ ] ≡
x
Dynamic Arrays
New int [100]
int finalMark – allocating space in memory
(a way to access that memory location)
finalMark[ ] = new int [100]
int marks[ ][ ] = new int [3][2]
Dynamic Arrays (Continued)
finalMark
pointer arithmetic
Dynamic Arrays (Continued)
List [3]
List [2]List [1]List [0]
1000 1008 1016P
P + K ≡ &List[K] (pointer arithmetic)
AR
xy
AR
x
y
ARRAY DESCRIPTION
DESCRIPTOR
DESCRIPTOR
DESCRIPTOR
1000i x 8
Data Structures
Records
• A record is a heterogeneous data structure composed of a fixed number of components of different types
• The record elements are referenced by names (identifiers)• Records were introduced by COBOL
Example: 01 Employee – Name
02 Employee – Name
05 First Name Picture is x(20)
05 Middle Name Picture is x(10)
05 Last Name Picture is x(20)
02 Hourly Rate Picture is 99v99
Name Hourly Rate
First Middle Last
Let us assume we have a record structure called employee with 4 fields
(ID, AGE, SALARY, DEPT)
To address a component of the array, the selector operator “.” is used.
Example: Employee.ID;
Employee.DEPT;
int int int int
ID
AGE
SALARY
DEPT
Record Example
L-Address (value) (R.I.)
L-Address (R.I.) = α + ∑ (size of R.J.)
We need a record descriptor
I = 1
J = 1
Field J
R.I. Field
α
Record DescriptionRECORD
NAME
TYPE
.
.
.
OFFSET
TYPE
NAME
TYPE
OFFSET
NAME
TYPE
OFFSET
During Execution Time{{
{
FIELD 1
FIELD 2
FIELD N
RECORD
NAME
TYPE
OFFSET
NAME
TYPE
OFFSET
NAME
TYPE
OFFSET
α
L-value (R.I) = α + KI
OFFSET
A
ID AGE DEPT SALARY
Record Description (Continued)
ARRAY DESCRIPTOR
Y
X
UB – Upper Bound
LB – Lower Bound
RECORD DESCRIPTOR
β
A[I].ID
AR
α
A[I]
DESCRIPTOR
UB LB
{
(offset is the same for each element) → use one record descriptor
(displacement)