Unformatted text preview:

Generic programming Define software components with type parameters A sorting algorithm has the same structure regardless of the types being sorted Stack primitives have the same semantics regardless of the objects stored on the stack Most common use algorithms on containers updating iteration search C model macros textual substitution C model templates Ada model generics Templates in C template class T class Vector public Vector size t n constructor T operator size t subscript hopefully with checks private a struct with a size and a pointer to an array Vector int V1 100 instantiation Vector int Vdef use default constructor typedef Vector employee Dept named instance Class and value parameters template class T int i class Buffer T v i storage for buffer int sz total capacity int count current contents public Buffer sz i count 0 T read void write T elem Buffer Shape 100 picture instance for a graphics app Template hopes for the best template class T class List struct Link for a list node Link pre doubly linked Link succ T val Link Link p s const T v pre p succ s val v Link head public void print all for Link p head p p p succ cout p val n operator better exist for T Function templates Instantiated implicitly at point of call template class T void sort vector T void testit vector int vi sort vi can also write sort int vi Generics in Ada95 I O for integer types Identical implementation but need separate procedures for strong typing reasons generic type Elem is range any integer type package Integer IO is procedure Put Item Elem A generic Package generic type Elem is private parameter package Stacks is type Stack is private procedure Push X Elem On in out Stack private type Cell type Stack is access Cell linked list representation type Cell is record Val Elem Next Ptr end record end Stacks Instantiations just use as a macro with Stacks procedure Test Stacks is package Int Stack is new Stacks Integer list of integers package Float Stack is new Stacks Float list of floats S1 Int Stack Stack a stack object S2 Float Stack Stack use Int Stack Float Stack ok regular packages begin Push 15 S1 Int Stack Push Push 3 5 Pi S2 end Test Stacks May not want generic to apply to ALL types The generic type declaration specifies the class of types for which an instance of the generic will work type T is private type T is limited private type T is range type T is any type with assignment Non limited any type no required operations any integer type arithmetic operations any discrete type enumeration or type T is digits type T is delta integer any floating point type any fixed point type Within the generic the operations that apply to any type of the class can be used The instantiation must use a specific type of the class A generic function generic type T is range parameter of some integer type type Arr is array Integer range of T parameter is array of those function Sum Array A Arr return T Body identical to non generic version function Sum array A Arr return T is Result T 0 some integer type so literal 0 is legal begin for J in A range loop some array type so attribute is available Result Result A J some integer type so available end loop return Result end Instantiating a generic function type Apple is range 1 2 15 1 type Production is array 1 12 of Apple type Sick Days is range 1 5 type Absences is array 1 52 of Sick Days function Get Crop is new Sum array Apple Production function Lost Work is new Sum array Sick Days Absences generic private types Only available operations are assignment and equality generic type T is private procedure Swap X Y in out T procedure Swap X Y in out T is Temp constant T X begin X Y Y Temp end Swap Subprogram parameters A generic sorting routine should apply to any array whose components are comparable i e for which an ordering predicate exists This class includes more than the numeric types so you have to say what you need function generic type T is private parameter with function X Y T return Boolean parameter type Arr is array Integer range of T parameter procedure Sort A in out Arr Supplying subprogram parameters The actual must have a matching signature not necessarily the same name procedure Sort Up is new Sort Integer procedure Sort Down is new Sort Integer type Employee is record end record function Senior E1 E2 Employee return Boolean matches in signature function Rank is new Sort Employee Senior


View Full Document

NYU CSCI-GA 2110 - Generic programming

Loading Unlocking...
Login

Join to view Generic programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Generic programming and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?