Data Types
References to Records
-Record field references 1. COBOL field_name OF record_name_1 OF ... OF record_name_n 2. Others (dot notation) record_name_1.record_name_2. ... record_name_n.field_name -Fully qualified references must include all record names -Elliptical references allow leaving out record names as long as the reference is unambiguous, for example in COBOL FIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the employee's first name
Heterogeneous Arrays
A heterogeneous array is one in which the elements need not be of the same type Supported by Python
Arrays Operations
Ada allows array assignment but also catenation Python's array assignments, but they are only reference changes. Python also supports array catenation and element membership operations
Arrays Index (Subscript) Types
Ada: integer or enumeration (includes Boolean and char) Java: integer types only Index range checking - C++ do not specify range checking - Java, C# specify range checking - In Ada, the default is to require range checking, but it can be turned off
Subrange Evaluation
Aid to readability Make it clear to the readers that variables of subrange can store only certain range of values Reliability Assigning a value to a subrange variable that is outside the specified range is detected as an error
Evaluation of Enumerated Type
Aid to readability, e.g., no need to code a color as a number Aid to reliability, e.g., compiler can check: operations (don't allow colors to be added) No enumeration variable can be assigned a value outside its defined range Ada, C#, and Java 5.0 provide better support for enumeration than C++ because enumeration type variables in these languages are not coerced into integer types
Character String Type Evaluation
Aid to writability As a primitive type with static length, they are inexpensive to provide--why not have them? Dynamic length is nice, but is it worth the expense?
Array Types
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.
Subrange Types
An ordered contiguous subsequence of an ordinal type Example: 12..18 is a subrange of integer type
User-Defined Ordinal Types
An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers
Operations on Records
Assignment is very common if the types are identical Ada allows record comparison Ada records can be initialized with aggregate literals
Character String Type in Languages
C++ Not primitive Use char arrays and a library of functions that provide operations Python Primitive type with assignment and several operations Java Primitive via the String class
Subscript Binding in languages
C++ arrays that include static modifier are static C++ arrays without static modifier are fixed stack-dynamic C++ provide fixed heap-dynamic arrays C# includes a second array class ArrayList that provides fixed heap-dynamic Python support heap-dynamic arrays
Discriminated vs. Free Unions
C++ provide union constructs in which there is no language support for type checking; the union in these languages is called free union Type checking of unions require that each union include a type indicator called a discriminant Supported by Ada
Rectangular and Jagged Arrays support
C, C++, and Java support jagged arrays Ada, and C# support rectangular arrays (C# also supports jagged arrays)
Array Initialization in languages
C-based languages int list [] = {1, 3, 5, 7} char *names [] = {″Mike″, ″Fred″, ″Mary Lou″}; Ada List : array (1..5) of Integer := (1 => 17, 3 => 34, others => 0); Python List comprehensions list = [x ** 2 for x in range(12) if x % 3 == 0] puts [0, 9, 36, 81] in list
Implementation of User-Defined Ordinal Types
Enumeration types are implemented as integers Subrange types are implemented like the parent types with code inserted (by the compiler) to restrict assignments to subrange variables
Evaluation of Unions
Free unions are unsafe Do not allow type checking Java and C# do not support unions Reflective of growing concerns for safety in programming language Ada's descriminated unions are safe
Array Indexing
Indexing (or subscripting) is a mapping from indices to elements: array_name (index_value_list) --> an element Index Syntax Ada use parentheses Ada explicitly uses parentheses to show uniformity between array references and function calls because both are mappings Most other languages use brackets
Enumeration Types Design issues
Is an enumeration constant allowed to appear in more than one type definition, and if so, how is the type of an occurrence of that constant checked? Are enumeration values coerced to integer? Any other type coerced to an enumeration type?
Character String Types Design issues
Is it a primitive type or just a special kind of array? Should the length of strings be static or dynamic?
Enumeration Types
Ordinal Type All possible values, which are named constants, are provided in the definition C# example enum days {mon, tue, wed, thu, fri, sat, sun};
Floating Point
Primitive Data Type real numbers as approximations for scientific use support at least two floating-point types float and double Usually like the hardware IEEE Floating-Point Standard 754
Decimal
Primitive Data Type C# For business (money) Store a fixed number of decimal digits, in coded form (BCD) Advantage: accuracy Disadvantages: limited range, wastes memory
Complex
Primitive Data Type Python value consists of two floats, the real and the imaginary in Python (7 + 3j) where 7 = real 3 = imaginary
Character
Primitive Data Type Stored as numeric code common coding: ASCII Includes characters from most natural languages Originally used in Java C# also support Unicode
Integer
Primitive Data Type exact reflection of the hardware so the mapping is trivial as many as eight different integer types Java's signed integer sizes: byte, short, int, long
Boolean
Primitive Data Type simple Range : "true" and "false" Could be implemented as bits, but often as bytes Advantage: readability
Slice Example
Python vector = [2, 4, 6, 8, 10, 12, 14, 16] mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] vector (3:6) is a three-element array mat[0][0:2] is the first and second element of the first row of mat
Records Evaluation
Records are used when collection of data values is heterogeneous Access to array elements is much slower than access to record fields, because subscripts are dynamic (field names are static) Dynamic subscripts could be used with record field access, but it would disallow type checking and it would be much slower
Unions Types Design issues
Should type checking be required? Should unions be embedded in records?
Array Initialization
Some language allow initialization at the time of storage allocation C++, Java, C# example int list [] = {4, 5, 7, 83} Character strings in C++ char name [] = ″freddie″; Arrays of strings in C++ char *names [] = {″Bob″, ″Jake″, ″Joe″]; Java initialization of String objects String[] names = {″Bob″, ″Jake″, ″Joe″};
Subscript Binding and Array Categories
Static Fixed stack-dynamic Stack-dynamic Fixed heap-dynamic Heap-dynamic
Character String Implementation
Static length: compile-time descriptor Limited dynamic length: may need a run-time descriptor for length (but not in C and C++) Dynamic length: need run-time descriptor; allocation/deallocation is the biggest implementation problem
Character String Length Options
Static: Java's String class Limited Dynamic Length: C++ a special character is used to indicate the end of a string's characters, rather than maintaining the length Dynamic (no maximum): Perl, JavaScript Ada supports all three string length options
Character String Types Operations
Typical operations: Assignment and copying Comparison (=, >, etc.) Catenation Substring reference Pattern matching
Tuple in python
Used in Python to allow functions to return multiple values Python Closely related to its lists, but immutable Create with a tuple literal myTuple = (3, 5.8, ′apple′) Referenced with subscripts (begin at 1) Catenation with + and deleted with del
Character String Types
Values are sequences of characters
Associative Arrays Design issues
What is the form of references to elements? - Is the size static or dynamic? Built-in type in Python
Record Types Design issues
What is the syntactic form of references to the field? Are elliptical references allowed
data type design issue
What operations are defined and how are they specified?
Array Design Issues
What types are legal for subscripts? Are subscripting expressions in element references range checked? When are subscript ranges bound? When does allocation take place? Are ragged or rectangular multidimensional arrays allowed, or both? What is the maximum number of subscripts? Can array objects be initialized? Are any kind of slices supported?
data type
a collection of data objects and a set of predefined operations on those objects
Tuple Types
a data type that is similar to a record, except that the elements are not named
Jagged Array
a matrix has rows with varying number of elements
Record Types
a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names
Primitive Data Types
all programming languages provide them not defined in terms of other data types reflections of the hardware or require only a little non-hardware support their implementation
object
an instance of a user-defined (abstract data) type
Associative Arrays
an unordered collection of data elements that are indexed by an equal number of values called keys User-defined keys must be stored
Heap-dynamic Subscript Binding
binding of subscript ranges and storage allocation is dynamic and can change any number of times Advantage: flexibility (arrays can grow or shrink during program execution)
Examples of primitive ordinal types in Java
integer char boolean
Rectangular array
is a multi-dimensioned array in which all of the rows have the same number of elements and all columns have the same number of elements
Unions Types
is a type whose variables are allowed to store different type values at different times during execution
Fixed heap-dynamic Subscript Binding
similar to fixed stack-dynamic: storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack)
Slices
some substructure of an array; nothing more than a referencing mechanism
Stack-dynamic Subscript Binding
subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time) Advantage: flexibility (the size of an array need not be known until the array is to be used)
Static Subscript Binding
subscript ranges are statically bound and storage allocation is static (before run-time) Advantage: efficiency (no dynamic allocation)
Fixed stack-dynamic Subscript Binding
subscript ranges are statically bound, but the allocation is done at declaration time Advantage: space efficiency
descriptor
the collection of the attributes of a variable
Subrange Types Ada example
type Days is (mon, tue, wed, thu, fri, sat, sun); subtype Weekdays is Days range mon..fri; subtype Index is Integer range 1..100; Day1: Days; Day2: Weekday; Day2 := Day1;
Records in Ada
type Emp_Rec_Type is record First: String (1..20); Mid: String (1..10); Last: String (1..20); Hourly_Rate: Float; end record; Emp_Rec: Emp_Rec_Type;
Ada Union Types
type Shape is (Circle, Triangle, Rectangle); type Colors is (Red, Green, Blue); type Figure (Form: Shape) is record Filled: Boolean; Color: Colors; case Form is when Circle => Diameter: Float; when Triangle => Leftside, Rightside: Integer; Angle: Float; when Rectangle => Side1, Side2: Integer; end case; end record;