Generic Programming Starts With: Algorithms
Generic Programming Starts With: Algorithms
algorithms
Minimal requirements:
works with maximal
family of types
Am
Generic
algorithm
Lift
...
Remove an
unneeded
requirement
on the type
Start
here
Lift
A1
Lift
A0
Less specialized:
works with more
than one type
Concrete algorithm:
requires specific data type
Maximalwithrespecttowhat?
Maintain usefulness of
the algorithm make
efficiency part of the
requirements
Am
Generic
algorithm
Lift
...
Lift
A1
Lift
A0
Concrete algorithm
A Concrete Algorithm
float
max_element(float a[],
int N)
{
if (N == 0)
throw MaxElementEmptyArrayError;
int first = 0; float max = a[0];
while (++first < N)
if (max < a[first])
max = a[first];
return max;
}
A Linked-List Counterpart
float_list_node*
max_element(float_list_node* first,
float_list_node* last)
{
if (first == last)
return first;
float_list_node* result = first;
while (first->next != last)
if (result->data < first->data)
result = first;
return result;
}
Generic
max_element algorithm
on
k = ++j
*j = 3
*k = 7
j
3 7
max_element
++i
*i
i==j
Forward
Container
Concept
max_element
Whatisaconcept?
Intension
Extension
Requirement 1
Abstraction 1
Requirement 2
.
.
.
.
Requirement N
Abstraction 2
.
.
.
Abstraction K
.
.
.
Concept
Intension
Extension
Requirement 1
Abstraction 1
Requirement 2
.
.
.
.
Requirement N
Abstraction 2
.
.
.
Abstraction K
.
.
.
Intension
Extension
...
Example: Polygon Concept
Intension
Must be a type
whose objects can
store other objects
(elements)
Must have an
associated iterator
type that can be
used to traverse
through the
elements.
Extension
vector<T>, for any T
deque<T> for any T
list<T> for any T
slist<T> for any T
set<T> for any T
map<T> for any T
...
AlgorithmAworkswith
everytypeTinconceptC
Definition:analgorithmisgenericif
itworkswitheverytypeinaconcept
works:iscorrectandefficient
Container
Associative
Container
Forward
Container
Sequence
Front
Insertion
Sequence
Reversible
Container
Back
Insertion
Sequence
Front
& Back
Insertion
Sequence
Random
Access
Container
Vector
Simple
A. C.
Set
Multiset
Sorted
A. C.
Unique
A. C.
Multiple
A. C.
Hashed
A. C.
Unique
Sorted
A. C.
Multiple
Sorted
A. C.
Unique
Hashed
A. C.
Multiple
Hashed
A. C.
Map
Multimap
H.
Set
Slist
List
Deque
H.
Multiset
H.
Map
Pair
A. C.
H.
Multimap
STL Generic
Algorithms
on Forward
Containers
Requires input
iterators
Requires
forward iterators
Container
Forward
Container
Requires
bidirectional
iterators
Sequence
Front
Insertion
Sequence
Back
Insertion
Sequence
Front
& Back
Insertion
Sequence
Reversible
Container
Requires
Random
random Access
access
Container
iterators
Vector
Slist
List
Deque
Enables sort,
binary_search,
random_shuffle,
STL Concepts
Container
Forward
Container
Associative
Container
Iterator
Input
Iterator
Output
Iterator
Algorithm
Input
Algorithm
Output
Algorithm
Forward
Iterator
Forward
Algorithm
Bidirectional
Bidirectional
Iterator
Algorithm
Random
Access
Iterator
Random
Access
Algorithm
Functor
Unary
Functor
Binary
Functor
Binary
Predicate
Order
Relation
Adaptor
Iterator
Adaptor
BGL Concepts
STL Concepts
Container
Graph
Incidence Adj.
Graph
Graph
Iterator
Graph
Iterator
EdgeList
Graph
Algorithm
Graph
Algorithms
Functor
Adaptor
Visitor
BFS
Visitor
DFS
Visitor
Uniform
Cost
Visitor