1 / 24

Generic programming starts with algorithms

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.

Download Presentation

Generic programming starts with algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Genericprogramming starts with algorithms

  2. 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 Concrete algorithm: requires specific data type 0

  3. Maximal with respect to what?

  4. Maintain usefulness of the algorithm – make efficiency part of the requirements A Generic algorithm m Lift . . . Lift A 1 Lift A Concrete algorithm 0

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

  6. A More Useful Interface 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;}

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

  8. Back to the Array Version 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;}

  9. Generalize the Element Type 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;}

  10. From Array Indexing to Pointers 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;}

  11. Generalize from Pointers to Iterators 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;}

  12. Use with any Container with appropriate 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());. . .

  13. Generic algorithm on max_element ++i Forward Container Concept *i i==j k = ++j*j = 3*k = 7 j k j k 3 7 3 7 max_element max_element

  14. What is a concept?

  15. Intension Extension Abstraction 1 Abstraction 2 . . . Abstraction K . . . Requirement 1 Requirement 2 . . . . Requirement N Concept

  16. Intension Extension Abstraction 1 Abstraction 2 . . . Abstraction K . . . Requirement 1 Requirement 2 . . . . Requirement N An abstraction is in the Extension if and only if it satisfies all of the requirements in the Intension

  17. Intension Extension Closed plane figure N sides (for any N) . . . Example: Polygon Concept

  18. 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 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 . . . Example: Container Concept

  19. T … A C 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

  20. AssociativeContainer ForwardContainer Sequence ReversibleContainer SortedA. C. UniqueA. C. MultipleA. C. HashedA. C. Front InsertionSequence BackInsertionSequence UniqueSortedA. C. MultipleSortedA. C. UniqueHashedA. C. MultipleHashedA. C. Random AccessContainer SimpleA. C. PairA. C. Front & BackInsertionSequence Set Multiset Map Multimap H. Set H. Multiset H. Map H. Multi-map Vector Slist Click on any node in the above concept hierarchy to see the corresponding requirements as specified in the SGI STL Programmer’s Guide List Deque See also http://www.sgi.com/tech/stl STL Container Concepts Container Back

  21. STL Generic Algorithms on Forward Containers Requires input iterators Enables generic algorithms copy, for_each, equal, transform, … Back Container Requiresforward iterators … ForwardContainer Enables find, merge, fill, replace, generate, remove, unique, rotate, … Requiresbidirectional iterators Enables reverse, partition, inplace_merge, … Sequence ReversibleContainer Front InsertionSequence BackInsertionSequence Requiresrandom access iterators Enables sort, binary_search, random_shuffle, … Random AccessContainer Front & BackInsertionSequence Vector Slist List Deque

  22. Back STL Concepts Container Iterator Algorithm Functor Adaptor ForwardContainer AssociativeContainer Input Iterator Output Iterator Input Algorithm Output Algorithm Unary Functor BinaryFunctor Iterator Adaptor Forward Iterator Forward Algorithm BinaryPredicate Bidirectional Iterator Bidirectional Algorithm OrderRelation Random Access Iterator Random Access Algorithm

  23. New Concepts in the Boost Graph Library BGL Concepts Graph Iterator Graph Algorithms Visitor Graph Adj.Graph EdgeListGraph Incidence Graph … BFS Visitor DFSVisitor Uniform CostVisitor … Back STL Concepts Container Iterator Algorithm Functor Adaptor See also http://www.boost.org/libs/graph/doc

  24. Generic Libraries Designed and Developed Viathe Concept-Based Approach • 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, …)

More Related