Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generic Programming in the STL and Beyond David R. Musser Rensselaer Polytechnic Institute.

Similar presentations


Presentation on theme: "Generic Programming in the STL and Beyond David R. Musser Rensselaer Polytechnic Institute."— Presentation transcript:

1 Generic Programming in the STL and Beyond David R. Musser Rensselaer Polytechnic Institute

2 Generic programming starts with algorithms

3 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 A m Remove an unneeded requirement on the type Generic algorithm... Start here

4 Maximal with respect to what?

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

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

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

8 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 data) result = first; return result; }

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

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

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

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

13 int a[] = {6, 3, 7, 5}; int* ai = max_element(a, a+4); vector v(a, a+4); vector ::iterator vi = max_element(v.begin(), v.end()); list x(a, a+4); list ::iterator li = max_element(x.begin(), x.end());...

14 Forward Container Concept max_element 3 73 7 Generic algorithm on jk jk k = ++j *j = 3 *k = 7 ++i *i i==j

15 What is a concept?

16 Concept IntensionExtension Requirement 1 Requirement 2. Requirement N Abstraction 1 Abstraction 2. Abstraction K.

17 Polygon Concept IntensionExtension Closed plane figure N sides...

18 Container Concept IntensionExtension Must be a type whose objects can store other objects (elements) Must have an associated iterator type that can be used to iterate through the elements.... vector, for any T deque for any T list for any T slist for any T set for any T map for any T self_adjusting_bag

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

20 What does it mean to refine a concept?

21 Requirement 1 Requirement 2... Requirement N refine Requirement 1 Requirement 2... Requirement N Requirement N+1 Abstraction 1 Abstraction 2... Abstraction K... Abstraction 1 Abstraction 3... Abstraction 2... Abstraction K... More Requirements, Fewer Abstractions

22 Polygon Concept Closed plane figure N sides refine Closed plane figure N sides N = 3 Triangle Concept...

23 Requirement 1 Requirement 2... Requirement N refine Requirement 1 Requirement 2... Requirement N Requirement N+1 Abstraction 1 Abstraction 2... Abstraction K... Abstraction 1 Abstraction 3... Abstractions that remain are ones that are more specialized.

24 Polygon Concept Closed plane figure N sides refine Closed plane figure N sides Equal side lengths Equilateral Polygon Concept...

25 Container Concept refine Forward Container Concept Reversible Container Concept Random Access Container Concept

26 Forward Container Concept IntensionExtension Container Intension Elements must be arranged in a definite order Iterator type must model Forward Iterator concept... vector for any T deque for any T list for any T slist for any T set for any T map for any T self_adjusting_bag

27 Reversible Container Concept IntensionExtension Forward Container Intension Must have an associated reverse iterator type Iterator and reverse iterator types must model Bidirectional Iterator concept... vector for any T deque for any T list for any T set for any T map for any T slist for any T

28 Random Access Container Concept IntensionExtension Reversible Container Intension Iterator types must model Random Access Iterator concept... vector for any T deque for any T list for any T set for any T map for any T

29 A more refined concept permits better algorithms

30 refine A Algorithm B can be more efficient than A (B doesn’t have to work with as many types as A) B C D

31 For any Polygon P Circumference(P) = sum = 0; for s in sides(P) sum += length(s) For any Equilateral Polygon P Circumference(P) = N * side_length(P)

32 Container refine Forward Container Reversible Container Random Access Container enables generic algorithms copy, equal, transform, … enables find, merge, fill, replace, remove, unique, rotate, max_element, … enables reverse, partition, rotate, inplace_merge, … enables sort, binary_search, rotate random_shuffle, … Requires Input Iterators Forward Iterators Bidirectional Iterators Random Access Iterators

33 Container refine Forward Container Reversible Container Random Access Container enables generic algorithms copy, equal, transform, … enables find, merge, fill, replace, remove, unique, rotate, max_element, … enables reverse, partition, rotate, inplace_merge, … enables sort, binary_search, rotate, random_shuffle, … Requires Input Iterators Forward Iterators Bidirectional Iterators Random Access Iterators

34 Container Forward Container Reversible Container Random Access Container Sequence Front Insertion Sequence Back Insertion Sequence Vector Deque Slist List …

35 Concept C IntensionExtension A models C  A is in Extension of C A  A satisfies Intension of C (A makes R, …, R true) R, …, R N1 1N...

36 Output Iterator Forward Iterator Bidirectional Iterator Random Access Iterator Vector iterator Input Iterator Deque iterator Slist iterator List iterator Istream iterator Ostream iterator

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

38 template Iterator max_element(Iterator first, Iterator last) { if (first == last) return first; Iterator result = first; while (++first != last) if (*result < *first) result = first; return result; }

39 int a[] = {6, 3, 7, 5}; int* ai = max_element(a, a+4); vector v(a, a+4); vector ::iterator vi = max_element(v.begin(), v.end()); list x(a, a+4); list ::iterator li = max_element(x.begin(), x.end());

40 int a[] = {6, 3, 7, 5}; assert int* models Forward Iterator; int* ai = max_element(a, a+4); vector v(a, a+4); assert vector ::iterator models Forward Iterator; vector ::iterator vi = max_element(v.begin(), v.end()); list x(a, a+4); assert list ::iterator models Forward Iterator; list ::iterator li = max_element(x.begin(), x.end());

41 template Assume that T satisfies Intension of C when checking uses of T in the algorithm. We need to use both the syntactic and the semantic properties listed in the intension. assert T models C; Check that T satisfies Intension of C. Should check that both the syntactic and the semantic requirements are satisfied.

42 What’s this about semantic requirements?

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

44 template ForwardIterator max_element(ForwardIterator first, ForwardIterator last, StrictWeakOrder compare){ if (first == last) return first; ForwardIterator result = first; while (++first != last) if (compare(*result,*first)) result = first; return result; }

45 Strict Weak Order: assumed in all STL sorting-related algorithms

46 <:T  T  bool Transitive[E/<] E(x,y)  not(x<y) & not(y<x) Binary Relation for all x: not(x < x) Irreflexive for all x, y, z: x < y & y < z implies x < z Transitive Strict Partial Order Strict Weak Order Functor

47 Irreflexive Transitive Transitive[E/<] E(x,y)  not(x<y) & not(y<x) Strict Weak Order for all x, y: x < y | y < x | x = y Trichotomy Total Order Sufficient for all STL sorting-related generic algorithms Not needed!

48 vector v; // Put some elements in v:...... // Sort v into descending order: sort(v.begin(), v.end(), greater_equal ()); // Why doesn’t the above call // terminate?

49 vector v; // Put some elements in v:...... // Sort v into descending order: sort(v.begin(), v.end(), greater ()); // Now we get here - that’s better!

50 vector v; // Put some elements in v:... // Sort v into descending order: assert greater models Strict Weak Order; sort(v.begin(), v.end(), greater ());

51 ContainerIteratorAlgorithmFunctorAdaptorAllocator STL Concepts

52 ContainerIteratorAlgorithmFunctorAdaptorAllocator Boost Graph Library Concepts Graph Visitor BFS Visitor DFS Visitor Uniform Cost Visitor See http://www.boost.org

53 Graph Algorithm Graph Algorithm + Visitor = More Specialized Algorithm Visitor BFS Visitor DFS Visitor Uniform Cost Visitor Algorithm DFS + topological_sort_visitor = Topological Sort + cycle_detection_visitor = Cycle Detector

54 Let’s summarize

55 Lift Concrete algorithms A 0 A 1 Lift A m Generic algorithms. Concept Taxonomy Useful Data and Algorithm Abstractions – a Generic Library C requires... template <typename T models C>... assert T models C Making Concepts First Class Constructs... for formal checking of syntactic and semantic properties and using them in software engineering Algorithms, Concepts & Challenges


Download ppt "Generic Programming in the STL and Beyond David R. Musser Rensselaer Polytechnic Institute."

Similar presentations


Ads by Google