data

Document Sample
data Powered By Docstoc
					    Programming Languages
Implementation of Data Structures



             Cao Hoaøng Truï
        Khoa Coâng Ngheä Thoâng Tin
        Ñaïi Hoïc Baùch Khoa TP. HCM




                                       1
                  Contents

• Fundamentals
• Elementary data types
• Structured data types
• Subprograms




                             2
                 Fundamentals

• Data objects
• Data types
• Type specification and implementation
• Declaration
• Type checking, compatibility, and conversion
• Assignment and initialisation
                                                 3
                Data Objects
• A run-time grouping of one or more pieces of data
• A container for data values
• Block of storage
• Programmer-defined and system-defined
• Lifetime
• Variables and constants

                                                  4
                 Fundamentals

• Data objects
• Data types
• Type specification and implementation
• Declaration
• Type checking, compatibility, and conversion
• Assignment and initialisation
                                                 5
                 Data Types

• A class of objects having the same properties
• Primitive data types




                                                  6
                 Fundamentals

• Data objects
• Data types
• Type specification and implementation
• Declaration
• Type checking, compatibility, and conversion
• Assignment and initialisation
                                                 7
                     Specification

• Attributes
• Values
• Operations:
    op_name: arg_type x arg_type x …  result_type x result_type x …




                                                                  8
                Examples: Arrays

• Attributes:
      number of dimensions
      subscript range for each dimension
      data types of the components

• Values: valid values for the components
• Operations: subscripting, creating, accessing
  attributes

                                                  9
              Implementation

• Storage representation
• Operation definitions:
      hardware operations
      subprograms
      in-line codes




                               10
                 Fundamentals

• Data objects
• Data types
• Type specification and implementation
• Declaration
• Type checking, compatibility, and conversion
• Assignment and initialisation
                                                 11
                     Declaration

• To provide information about data objects:
        number and type
        name and position
        lifetime
        constant and initialised value




                                               12
                   Declaration

• Data type declaration:
      by name
             var A: integer

      by specification

                var A: array [1..20] of integer




                                                  13
                  Declaration

• Operation declaration:
      argument number, order, data types
      result number, order, data types

         function FOO(X: integer; Y: real) : real;

         FOO: integer x real  real




                                                     14
                    Declaration

• Purposes:
        choice of storage representations
        storage management
        generic operations
        type checking




                                             15
                 Fundamentals

• Data objects
• Data types
• Type specification and implementation
• Declaration
• Type checking, compatibility, and conversion
• Assignment and initialisation
                                                 16
              Type Checking

• Checking if each operation receives the proper
  number of arguments of the proper data types

• Dynamic (run time) vs. static (compile time)
  type checking




                                                 17
                  Type Checking

• Dynamic type checking:
        no declarations required
        types of data objects may change as needed
        programmers have no concerns about data types
        difficult to debug and remove type errors
        extra storage required for keeping type information
        reduction of execution speed




                                                           18
             Type Compatibility
• T1 and T2 are compatible if data objects of type T1
  can occur in the positions of data objects of type
  T2, and vice versa: name or structural equivalence
       type VECT1: array [1..10] of real;
            VECT2: array [1..10] of real;
       var X, Y: VECT1; Z: VECT2;

       Y := X;
       Z := Y;

                                                   19
             Type Compatibility

• Name equivalence:
      no anonymous types allowed
      global type definitions used

      var X: array [1..10] of real;




                                      20
            Type Compatibility

• Structural equivalence:
      difficult to define
      static type checking compromised
      cost to check
      type METERS = integer;
           LITERS = integer;
      var LEN: METERS;
          VOL: LITERS;

          LEN + VOL
                                          21
                Type Conversion

• When types are mismatched:
     type error
     type conversion (coercion)

   var A: integer;        int I;
       B, C: real;        unsigned U;
                          float F;
   C := A + B
                          I = - 1; U = 1; F = U * I;
                          /* I = - 1  65535 */
                                                       22
                 Fundamentals

• Data objects
• Data types
• Type specification and implementation
• Declaration
• Type checking, compatibility, and conversion
• Assignment and initialisation
                                                 23
                    Assignment

• Basic operation for changing values of data
  objects
• Specification:

  := : type1 x type2  void   = : type1 x type2  type3

  Z := X + Y                   Z = X + (Y = W * 2)

                               A=B=C

                                                            24
                  Initialisation

• To set a value in the storage of an data object
• Un-initialised data object
• Explicit and implicit initialisation




                                                25
    Programming Languages
Implementation of Data Structures



             Cao Hoaøng Truï
        Khoa Coâng Ngheä Thoâng Tin
        Ñaïi Hoïc Baùch Khoa TP. HCM




                                       26
                  Contents

• Fundamentals
• Elementary data types
• Structured data types
• Subprograms




                             27
        Elementary Data Types

• Numeric data types
• Enumerations
• Booleans
• Characters




                                28
             Numeric Data Types

• Integers
• Subranges
• Floating-point real numbers
• Fixed-point real numbers
• Complex numbers
• Rational numbers
                                  29
                 Integers

                     -32768  32768

                            2 bytes

-65536  65536

                  4 bytes




                                      30
                   Subranges
                      var A: 1..10

• Smaller storage requirements
      fewer bits than a general integer
      software-simulated operations

• Better type checking



                                           31
  Floating-Point Real Numbers

6.7510 = 110.112 = 0.110112 x 23 = 0.110112 x 00112

Sign bit for exponent
      exponent
0 0     0011                     11011

                               mantissa
Sign bit for mantissa


                                                      32
     Fixed-Point Real Numbers

                     6.7510 = 110.112

Sign bit
           integer part            fractional part
0              110                       11




                                                     33
            Complex Numbers

real part




imaginary part




                              34
             Rational Numbers
• To avoid roundoff and truncation
• Pairs of integers of unbounded length

            numerator



            denominator
 Sign bit


                                          35
        Elementary Data Types

• Numeric data types
• Enumerations
• Booleans
• Characters




                                36
          Enumerations

type DEGREE = (bachelor, master, doctor)

                   0        1       2




                                           37
        Elementary Data Types

• Numeric data types
• Enumerations
• Booleans
• Characters




                                38
                     Booleans

                        1 byte


• A particular bit
• Zero/non-zero value




                                 39
        Elementary Data Types

• Numeric data types
• Enumerations
• Booleans
• Characters




                                40
Characters

            1 byte


   2 byte




                     41
                  Contents

• Fundamentals
• Elementary data types
• Structured data types
• Subprograms




                             42
           Structured Data Types
•   Fundamentals
•   Vectors and arrays
•   Records
•   Character strings
•   Pointers
•   Sets
•   Files

                                   43
                  Fundamentals

• Specification
• Implementation
• Type checking




                                 44
                  Specification

• Attributes:
        Number of components
        Type of each component
        Names for selecting components
        Maximum number of components
        Organization of components




                                          45
                   Specification

• Operations:
        Component selection (random/sequential)
        Whole-data-structure operations
        Insertion/deletion
        Creation/destruction




                                                   46
              Implementation
• Storage representation:
            Decriptor       Decriptor
           Component
                            Component




                            Component
           Component

           Sequential        Linked
                                        47
               Implementation

• Selection operation:
      Sequential  base-address-plus-offset
      Linked  pointer following




                                               48
            Implementation
• Storage management:




         object           object




         Garbage         Dangling
                        References
                                     49
              Type Checking
             var A: array [1..10] of real;

• Existence of a selected component:
                         A[I]

• Type of a selected component:
                   A[2].link.item



                                             50
             Structured Data Types
•   Fundamentals
•   Vectors and arrays
•   Records
•   Character strings
• Pointers
• Sets
• Files

                                     51
                  Vectors
Base address a    Vector
                    LB
                           var A: array [1..10] of integer;
                   UB
                 Integer
Component size       E
        A[LB]              loc A[I] = a + D + (I - LB) x E
    A[LB + 1]


                           packed/unpacked storage

         A[UB]
                                                         52
                   Matrix
      var A: array [1..2, -1..1] of real;

                                   1    2
     -1   0    1
                             -1
1
                              0
2
                              1

    row-major order         column-major order
                                                 53
          Multidimensional Arrays
   var A: array [LB1..UB1, LB2..UB2, …, LBn..UBn] of real;

• Row-major order:
   var A: array [LB1..UB1] of
                    array [LB2..UB2, …, LBn..UBn] of real;

• Column-major order:
   var A: array [LBn..UBn] of
                 array [LB1..UB1, …, LBn-1..Ubn-1] of real;

                                                              54
                     Matrix
Base address a    Matrix
                   LB1
                           var A: array [1..2, -1..1] of real;
                   UB1
                   LB2
                   UB2     loc A[I, J] = a + D + (I - LB1) x S
Component size     Real
                                         + (J - LB2) x E
                    E
       A[1, -1]            S = (UB2 - LB2 + 1) x E
       A[1, 0]
       A[1, 1]             loc A[I, J] = a + K + I x S + J x E
       A[2, -1]
                           K = D - LB1 x S - LB2 x E
       A[2, 0]
       A[2, 1]                                              55
             Structured Data Types
•   Fundamentals
•   Vectors and arrays
•   Records
•   Character strings
• Pointers
• Sets
• Files

                                     56
                    Records
• Heterogeneous components
• Symbolic names for components

  var EMPLOYEE: record
                    ID: integer;
                    AGE: integer;
                    DEPT: string;
                    SALARY: real;
                end

                                    57
                   Records
Base address a         22901            ID

                         27             AGE

                        “IT”            DEPT

                      2901.10           SALARY



         loc R.I = a + j=1, I-1(size of R.j)


                    computed during compile time
                                                   58
               Variant Records
type PAY_TYPE = (MONTHLY, HOURLY)
var EMPLOYEE: record
                 ID: integer;
                 AGE: integer;
                 DEPT: string;
                 case PAY_CLASS: PAY_TYPE of
                       MONTHLY: (MONTH_RATE: real;
                                  START_DATE: integer);
                       HOURLY: (HOUR_RATE: real;
                                REG_HOURS: integer;
                                OVERTIME: integer)
              end
                                                     59
         Variant Records
        ID    22901    ID

       AGE     27      AGE

      DEPT     “IT”    DEPT

 PAY_CLASS             PAY_CLASS

MONTH_RATE             HOUR_RATE

START_DATE             REG_HOURS

    Unused             OVERTIME


                                   60
            Variant Records

• Dynamic tag checking

• No checking




                              61
             Structured Data Types
•   Fundamentals
•   Vectors and arrays
•   Records
•   Character strings
• Pointers
• Sets
• Files

                                     62
            Character Strings

• Fixed declared length:

                 R   E     L   A

                 T   I     V   I

                 T   Y




                                   63
            Character Strings

• Variable length with declared bound:

               Maximum length
                   Current length

                12   8   E    I

                N    S   T    E

                I    N
                                         64
              Character Strings

• Unbounded length:
 Current length

  8

      E   I       N   S


                          T   E   I   N

                                          65
              Character Strings
• Concatenation
• Relational operations
• Substring selection (by character-position/pattern
  matching)
• Input-output formatting




                                                   66
             Structured Data Types
•   Fundamentals
•   Vectors and arrays
•   Records
•   Character strings
• Pointers
• Sets
• Files

                                     67
                     Pointers
• Referencing data objects of a single type
         type VECT = array [1..20] of real;
         var P: VECT;

• Referencing data objects of any type




                                              68
                          Pointers
        0                               0


    d
                                    d

                 Object                          Object

d                               d




            Absolute address                Relative address
                                                               69
                     Pointers

• Absolute address:
      fast access
      difficult storage management

• Relative address:
      slower access
      simpler storage management



                                      70
                      Pointers
• List:
    type ELEMENT = record
                      HEAD: integer;
                      TAIL: ELEMENT
                   end
    var P: ELEMENT

          P


                                       71
             Structured Data Types
•   Fundamentals
•   Vectors and arrays
•   Records
•   Character strings
• Pointers
• Sets
• Files

                                     72
                      Sets

• Membership test

• Element insertion/deletion
• Union, intersection, difference




                                    73
                               Sets
        var S: set of (mon, tue, wed, thu, fri, sat, sun);

• Bit-string representation:
                     mon   tue wed thu fri    sat   sun

   [mon, wed, fri]    1    0    1     0   1    0     0


  [mon, tue, wed]     1    1    1     0   0    0     0


      [mon, wed]      1    0    1     0   0    0     0
                                                             74
                        Sets

• Hash-coded representation:
      for large domain size
      each set is represented by a storage block
      each element has a hash address in that block




                                                       75
             Structured Data Types
•   Fundamentals
•   Vectors and arrays
•   Records
•   Character strings
• Pointers
• Sets
• Files

                                     76
                     Files

• Represented on a secondary storage device
  (disk/tape)

• Much larger than data structures of other
  types

• Lifetime is longer than that of the program
  creating it

                                                77
                        Files

• Sequential files

• Text files

• Direct-access files

• Indexed sequential files


                                78
                     Files

• Open

• Read

• Write

• End-of-file test


                             79
                  Contents

• Fundamentals
• Elementary data types
• Structured data types
• Subprograms




                             80
                 Subprograms

• Abstract operations:
        name
        arguments
        results
        action


• Data objects


                               81
              Subprograms
function FOO(X: real; Y: integer): real;
   var A: array [1..10] of real;
        N: integer;
   begin
        …
       N := Y + 1;
       X := A[N]*2;
       …
   end;


                                           82
                 Subprograms

• Code segment (static part)

• Activation record (dynamic part):
      parameters
      function results
      local variables




                                      83
               Subprograms
                                       Return point and
  Prologue                             system data
                 FOO
                   X
  Statement
                   Y
  executable
    codes          A



  Epilogue        N

Code segment       Activation record

                                                     84
                  Subprograms

                     Code segment


       1st call    2nd call         N-th call



Activation          Activation          Activation
 record 1            record 2            record N




                                                     85
                        Exercises

• Consider the following piece of program:
  program P;
  var A: 1..10;
      N: integer;
                     Where is type checking?
  begin
  …                  Can it be done statically?
  read(N);
  A := N + 1;
                     Can it be done dynamically and
  …                 how?
  end;


                                                       86
                  Exercises

• Illustrate the storage representation of
  A: array [0..1, 1..2] of integer using the
  column-major order.
  Give the accessing formula for computing the
  location of A[I, J], supposing that the size of
  an integer is 2 bytes.



                                                    87

				
DOCUMENT INFO