 Download Presentation Data Structures Data Structures - PowerPoint PPT Presentation

Download Presentation Data Structures
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. Data Structures Arrays

2. 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.

3. Arrays: Stack Representation

4. Arrays: Array Descriptor Array Descriptor in Stack

5. Arrays: L-Value (L-Address) • Let us assume that the 1st element of the array “A” begins at location a. L-Value(A[ N ]) = a + ( N – LB ) * E L-Value(A[ N ]) = a – LB * E + N * E K = a – LB * E and is a constant value. L-Value(A[ N ]) = K + N * E When E = 1 and LB = 0, then: L-Value(A[ N ]) = a + N • B: Array[ 3 … 10 ] of INT a = 100 L-Value(B[ 4 ]) = 100 – 3 + 4 = 101 LB = 3 E = 1 LB ≤ N ≤ UB

6. Arrays: Virtual Origin (VO) • So far we know: L-Value(A[ N ]) = (a – LB * E) + ( N * E ) • And for A[ 0 ]: L-Value(A[ 0 ]) = (a – LB * E) + ( 0 * E ) • Sometimes the lower bound of the array could be greater than zero. This address is called the virtual origin.

7. Arrays: Finding the Virtual Origin • On creation of vector storage, allocate “N” ( UB – LB + 1) components of the vector of size E and the descriptor of size “D”. • Compute the Virtual Origin:VO = a – LB * E Example: VO = 100 – 3 * 1 = 97 • On accessing an array component (assuming LB £ N £ UB):L-Value(A[ N ]) = VO + N * E

8. Arrays: Finding the Virtual Origin • Example: A[ 3 … N] Assuming: a = 100 E = 1 VO = 97 (See previous slide, step 2) L-Value(A[ 4 ]) = VO + N * E = 97 + 4 * 1 = 101

9. Ordering: Example: M = 3, 4, 7, 6, 2, 5, 1, 3, 8 Row major order: M: Column major order: M: Arrays: Multidimensional Arrays

10. Arrays: Multidimensional Arrays • Storage • Location of A[ N , J ] is given by: L-Value( A[ N , J ] ) = a + ( N – LB1 ) * S + ( J – LB2 ) * E a = Base Address S = Length of a Row ( UB2 – LB2 + 1) * E VO = a – LB1 * S – LB2 * E • Example: L-Value( A[ N , J ]) = VO + N * S + J * E L-Value( A[ 2 , 3 ]) = ( 2 * row length) + 3

11. 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 * 1 VO = a – LB1 * S – LB2 * E = a – 4 Assuming that a = 100: VO = 96 L-Value( A[ 2 , 2 ] ) = VO + N * 3 + 2 * 1 = 96 + 2 * 3 + 2 * 1 = 96 + 6 + 2 = 104

12. Data Structures Dynamic Arrays

13. Dynamic Arrays A[ ] ≡ A[ ] ≡ A[ ][ ] [ ] ≡ Dynamic Arrays x

14. Dynamic Arrays (Continued) New int  int finalMark – allocating space in memory (a way to access that memory location) finalMark[ ] = new int  int marks[ ][ ] = new int  finalMark pointer arithmetic

15. DESCRIPTOR DESCRIPTOR DESCRIPTOR ARRAY DESCRIPTION y x AR Dynamic Arrays (Continued) List  List  List  List  1000i x 8 P 1000 1008 1016 P + K ≡ &List[K] (pointer arithmetic) y x AR

16. Data Structures Records

17. 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

18. ID AGE SALARY DEPT Record Example 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

19. L-Address (value) (R.I.) L-Address (R.I.) = α + ∑ (size of R.J.) We need a record descriptor I = 1 Field J J = 1 α R.I. Field

20. α RECORD NAME TYPE OFFSET NAME TYPE OFFSET NAME TYPE OFFSET Record Description TYPE { RECORD During Execution Time NAME FIELD 1 TYPE OFFSET OFFSET { NAME L-value (R.I) = α + KI TYPE FIELD 2 OFFSET . . . NAME { TYPE FIELD N OFFSET A ID AGE DEPT SALARY

21. Record Description (Continued) β A[I] LB – Lower Bound UB – Upper Bound A[I].ID α RECORD DESCRIPTOR ARRAY DESCRIPTOR { Y X DESCRIPTOR AR UB LB (offset is the same for each element) → use one record descriptor (displacement)