24
Generic programming starts with algorithms

Generic programming starts with algorithms

Embed Size (px)

DESCRIPTION

Generic programming starts with algorithms. Minimal requirements: works with maximal family of types. A. Generic algorithm. m. Lift. Remove an unneeded requirement on the type. Lift. Less specialized: works with more than one type. A. 1. Lift. Start here. A. - PowerPoint PPT Presentation

Citation preview

Generic programming starts with algorithms

Lift

Minimal requirements: works with maximal

family of types

Concrete algorithm: requires specific data type

Less specialized: works with more

than one type

A 0

A 1

Lift

Lift

A m

Remove an unneeded

requirement on the type

Generic algorithm

. . .

Start here

Maximal with respect to what?

Lift

Maintain usefulness of the algorithm – make efficiency part of the

requirements

Concrete algorithmA 0

A 1

Lift

Lift

A mGeneric

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 Concrete Algorithm

int max_element(float a[], int first, int last) { if (first == last) return first; int result = first; while (++first != last) if (a[result] < a[first]) result = first; return result;}

A More Useful Interface

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;}

A Linked-List Counterpart

int max_element(float a[], int first, int last) { if (first == last) return first; int result = first; while (++first != last) if (a[result] < a[first]) result = first; return result;}

Back to the Array Version

template <typename E>int max_element(E a[], int first, int last) { if (first == last) return first; int result = first; while (++first != last) if (a[result] < a[first]) result = first; return result;}

Generalize the Element Type

template <typename T>T*max_element(T* first, T* last) { if (first == last) return first; T* result = first; while (++first != last) if (*result < *first) result = first; return result;}

From Array Indexing to Pointers

template <typename ForwardIterator>ForwardIterator max_element(ForwardIterator first, ForwardIterator last){ if (first == last) return first; ForwardIterator result = first; while (++first != last) if (*result < *first) result = first; return result;}

Generalize from Pointers to Iterators

int a[] = {6, 3, 7, 5};

int* ai = max_element(a, a+4);

vector<int> v(a, a+4);vector<int>::iterator vi = max_element(v.begin(), v.end());

list<int> x(a, a+4); list<int>::iterator li = max_element(x.begin(), x.end());

. . .

Use with any Container with appropriate iterators

Forward Container

Concept

max_element

max_element

max_element

373

7

Generic algorithm

on

j kj k

k = ++j*j = 3*k = 7

++i*ii==j

What is a concept?

Concept

Intension Extension

Requirement 1

Requirement 2....Requirement N

Abstraction 1

Abstraction 2...Abstraction K...

Intension Extension

Requirement 1

Requirement 2....Requirement N

Abstraction 1

Abstraction 2...Abstraction K...

An abstraction is in the Extension if and only if it satisfies all of the requirements in the Intension

Example: Polygon Concept

Intension Extension

Closed plane figure

N sides (for any N)

. . .

Example: Container Concept

Intension Extension

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.

. . .

vector<T>, for any Tdeque<T> for any Tlist<T> for any Tslist<T> for any Tset<T> for any Tmap<T> for any T

A

Algorithm A works with every type T in concept C

Definition: an algorithm is generic if it works with every type in a concept

“works”: is correct and efficient

CT…

Container

AssociativeContainer

SortedA. C.

UniqueA. C.

MultipleA. C.

HashedA. C.

UniqueSortedA. C.

MultipleSortedA. C.

UniqueHashedA. C.

MultipleHashedA. C.

SimpleA. C.

PairA. C.

Set Multiset H. Set

H. Multiset

H. Multi-map

H. Map

Map Multimap

Front InsertionSequence

BackInsertionSequence

Random AccessContainer

ForwardContainer

Sequence ReversibleContainer

List

Vector

Deque

Front & BackInsertionSequence

Slist

STL Container Concepts Back

See also http://www.sgi.com/tech/stl

Click on any node in the above concept hierarchy to see the corresponding requirements as specified in the SGI STL Programmer’s Guide

Container

ForwardContainer

Sequence

Front InsertionSequence

BackInsertionSequence

ReversibleContainer

Random AccessContainer

List

Vector

Deque

Front & BackInsertionSequence

Slist

STL Generic Algorithms on Forward Containers

Requires input iterators

Enables generic algorithms copy, for_each, equal, transform, …

Requiresforward iterators

Enables find, merge, fill, replace, generate, remove, unique, rotate, …

Requiresbidirectional iterators

Enables reverse, partition, inplace_merge, …

Requiresrandom access iterators

Enables sort, binary_search, random_shuffle, …

Back

Container

ForwardContainer

AssociativeContainer

STL Concepts

Input Iterator

Output Iterator

Iterator

Forward Iterator

Bidirectional

Iterator

Random Access Iterator

Algorithm Functor Adaptor

Input Algorithm

Output Algorithm

Forward Algorithm

Bidirectional

Algorithm

Random Access Algorithm

Unary Functor

BinaryFunctor

BinaryPredicate

OrderRelation

IteratorAdaptor

Back

BGL Concepts

STL Concepts

Container Iterator Algorithm Functor Adaptor

Graph Iterator

Graph

IncidenceGraph

Adj.Graph

EdgeListGraph …

New Concepts in the Boost Graph Library

See also http://www.boost.org/libs/graph/doc

Graph Algorithms

Visitor

BFS Visitor

DFSVisitor

Uniform CostVisitor

Back

• Standard Template Library (HP, RPI, SGI)

• Matrix Template Library (Indiana U.)

• Boost Graph Library (Indiana U.)

• Parallel Boost Graph Library (D. Gregor [RPI PhD], Indiana U.)

• Boost Array Library (N. Josuttis)

• Boost Multi-Array Library (Indiana U.)

• Boost Basic Linear Algebra Library (J. Walter, M. Koch)

• Boost Property Map Library (Indiana U.)

• Boost Random Number Generator Library (J. Mauer)

• Boost Threads Library (W. Kempf)

• Boost Concept Checking Library (Indiana U.)

• Computational Geometry Algorithms Library (H. Brőnnimann, S. Schirra, R. Veltkamp, …)

Generic Libraries Designed and Developed Viathe Concept-Based Approach