870 likes | 1.03k Views
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. Contents. Fundamentals Elementary data types Structured data types Subprograms. Fundamentals. Data objects Data types Type specification and implementation
E N D
Programming LanguagesImplementation of Data Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM
Contents • Fundamentals • Elementary data types • Structured data types • Subprograms
Fundamentals • Data objects • Data types • Type specification and implementation • Declaration • Type checking, compatibility, and conversion • Assignment and initialisation
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
Fundamentals • Data objects • Data types • Type specification and implementation • Declaration • Type checking, compatibility, and conversion • Assignment and initialisation
Data Types • A class of objects having the same properties • Primitive data types
Fundamentals • Data objects • Data types • Type specification and implementation • Declaration • Type checking, compatibility, and conversion • Assignment and initialisation
Specification • Attributes • Values • Operations: op_name: arg_type x arg_type x … result_type x result_type x …
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
Implementation • Storage representation • Operation definitions: • hardware operations • subprograms • in-line codes
Fundamentals • Data objects • Data types • Type specification and implementation • Declaration • Type checking, compatibility, and conversion • Assignment and initialisation
Declaration • To provide information about data objects: • number and type • name and position • lifetime • constant and initialised value
Declaration • Data type declaration: • by name varA:integer • by specification varA:array [1..20]of integer
Declaration • Operation declaration: • argument number, order, data types • result number, order, data types functionFOO(X:integer; Y: real): real; FOO: integer x real real
Declaration • Purposes: • choice of storage representations • storage management • generic operations • type checking
Fundamentals • Data objects • Data types • Type specification and implementation • Declaration • Type checking, compatibility, and conversion • Assignment and initialisation
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
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
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 typeVECT1:array [1..10] of real; VECT2:array [1..10] of real; varX, Y:VECT1; Z:VECT2; Y := X; Z := Y;
Type Compatibility • Name equivalence: • no anonymous types allowed • global type definitions used varX:array [1..10] of real;
Type Compatibility • Structural equivalence: • difficult to define • static type checking compromised • cost to check typeMETERS = integer; LITERS = integer; var LEN:METERS; VOL:LITERS; LEN + VOL
Type Conversion • When types are mismatched: • type error • type conversion (coercion) varA:integer; int I; B, C: real; unsigned U; float F; C := A + B I = - 1; U = 1; F = U * I; /* I = - 1 65535 */
Fundamentals • Data objects • Data types • Type specification and implementation • Declaration • Type checking, compatibility, and conversion • Assignment and initialisation
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
Initialisation • To set a value in the storage of an data object • Un-initialised data object • Explicit and implicit initialisation
Programming LanguagesImplementation of Data Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM
Contents • Fundamentals • Elementary data types • Structured data types • Subprograms
Elementary Data Types • Numeric data types • Enumerations • Booleans • Characters
Numeric Data Types • Integers • Subranges • Floating-point real numbers • Fixed-point real numbers • Complex numbers • Rational numbers
Integers -32768 32768 2 bytes -65536 65536 4 bytes
Subranges • Smaller storage requirements • fewer bits than a general integer • software-simulated operations • Better type checking var A: 1..10
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
Fixed-Point Real Numbers 6.7510 = 110.112 Sign bit integer part fractional part 0 110 11
Complex Numbers real part imaginary part
Rational Numbers • To avoid roundoff and truncation • Pairs of integers of unbounded length numerator denominator Sign bit
Elementary Data Types • Numeric data types • Enumerations • Booleans • Characters
Enumerations type DEGREE = (bachelor, master, doctor) 0 1 2
Elementary Data Types • Numeric data types • Enumerations • Booleans • Characters
Booleans • A particular bit • Zero/non-zero value 1 byte
Elementary Data Types • Numeric data types • Enumerations • Booleans • Characters
Characters 1 byte 2 byte
Contents • Fundamentals • Elementary data types • Structured data types • Subprograms
Structured Data Types • Fundamentals • Vectors and arrays • Records • Character strings • Pointers • Sets • Files
Fundamentals • Specification • Implementation • Type checking
Specification • Attributes: • Number of components • Type of each component • Names for selecting components • Maximum number of components • Organization of components
Specification • Operations: • Component selection (random/sequential) • Whole-data-structure operations • Insertion/deletion • Creation/destruction
Implementation • Storage representation: Decriptor Decriptor Component Component Component Component Sequential Linked
Implementation • Selection operation: • Sequential base-address-plus-offset • Linked pointer following
Implementation • Storage management: object object Dangling References Garbage
Type Checking • Existence of a selected component: A[I] • Type of a selected component: A[2].link.item var A: array [1..10] ofreal;