Docsity

Array Accessing and Storage in CMSC430 Spring 2007 - Prof. Marvin V. Zelkowitz, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC430 Spring 2007 1
Array accessing
•An array is an ordered sequence of identical objects.
•The ordering is determined by a scalar data object (usually
integer or enumeration data). This value is called the subscript or
index, and written as A[I] for array A and subscript I.
•Multidimensional arrays have more than one subscript. A 2-
dimensional array can be modeled as the boxes on a rectangular
grid.
•The L-value for array element A[I,J]is given by the accessing
formula on the next slide
CMSC430 Spring 2007 2
Array storage
pf3
pf4
pf5

Partial preview of the text

Download Array Accessing and Storage in CMSC430 Spring 2007 - Prof. Marvin V. Zelkowitz and more Study notes Computer Science in PDF only on Docsity!

CMSC430 Spring 2007 (^1)

Array accessing

  • An array is an ordered sequence of identical objects.
  • The ordering is determined by a scalar data object (usually integer or enumeration data). This value is called the subscript or index, and written as A[I] for array A and subscript I.
  • Multidimensional arrays have more than one subscript. A 2- dimensional array can be modeled as the boxes on a rectangular grid.
  • The L-value for array element A[I,J]is given by the accessing formula on the next slide

CMSC430 Spring 2007 (^2)

Array storage

CMSC430 Spring 2007 (^3)

Array accessing (continued)

Rewriting access equation: L-value(A[I,J]) = α - d1L1 - d2L2 +Id1 + Jd Set I = 0; J= 0; L-value(A[0,0]) = α - d1L1 - d2L2 +0d1 + 0d L-value(A[0,0]) = α - d1L1 - d2L2, which is a constant.

  • Call this constant the virtual origin (VO); It represents the address of the 0th element of the array. L-value(A[I,J]) = VO +Id1 + Jd
  • To access an array element, typically use a dope vector:

CMSC430 Spring 2007 (^4)

Array accessing summary

To create arrays:

  1. Allocate total storage beginning at α: (U2-L2+1)(U1-L1+1)eltsize
  2. d2 = eltsize
  3. d1 = (U2-L2+1)*d
  4. VO = α - L1d1 - L2d
  5. To access A[I,J]: Lvalue(A[I,J]) = VO + Id1 + Jd This works for 1, 2 or more dimensions.
  • May not require runtime dope vector if all values are known at compile time. (e.g., in Pascal, d1, d2, and VO can be computed by compiler.)
  • Next slide: Storage representation for 2-dimensional array.

CMSC430 Spring 2007 (^7)

Slices

CMSC430 Spring 2007 (^8)

Array slices

Given array : A[L1:U1, L2:U2]: Give d1, d2, and VO for vector:

Dope vector A[I,] = B[L2:U2] VO = L-value(A[I,L2]) - d2L M1 = eltsize = d

Dope vector A[,J] = B[L1:U1] VO = L-value(A[L1,J]) - d1L M1 = rowsize = d

Create new dope vector that accesses original data

CMSC430 Spring 2007 (^9)

More on slices

  • Diagonal slices:

VO = L-value(A[L1,L2])

  • d1L1 - d2L M1 = d1 + d
  • Other possibilities:

CMSC430 Spring 2007 (^10)

Associative arrays

Access information by name without having a predefined ordering or enumeration:

  • Example: Names and grades for students in a class: NAME[I] = name of Ith student GRADE[I] = Grade for Ith student

Associative array: Use Name as index: CLASS[name] will be grade.

Problem: Do not know enumeration before obtaining data so dope vector method of accessing will not work.

Implemented in Perl and in SNOBOL4 (as a table)

CMSC430 Spring 2007 (^13)

Variant records

type PayType=(Salaried, Hourly); var Employee:record ID: integer; Dept: array[1..3] of char; Age: integer; case PayClass: PayType of Salaried:(MonthlyRate:real; StartDate:integer); Hourly:(HourRate:real; Reg:integer; Overtime:integer) end

CMSC430 Spring 2007 (^14)

Variant records (continued)

Tagged union type - Pascal variant records type whichtype = (inttype, realtype, chartype); type uniontype = record case V: whichtype of inttype: (X: integer); realtype: (Y: real); chartype: (Z: char4); Assumes string of length 4 end But can still subvert tagging: var P: uniontype P.V = inttype; P.X = 142; P.V = chartype; What is P.V value now?