1 / 33

Generic Algorithms

Generic Algorithms. Andy Wang Data Structures, Algorithms, and Generic Programming. Generic Copy. template <class I, class J> void g_copy(I src_begin, I src_end, J dest_begin) { while (src_begin != src_end) { *dest_begin++ = *src_begin++; } } g_copy(L.Begin(), L.End(), V.Begin());

Mercy
Download Presentation

Generic 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. Generic Algorithms Andy Wang Data Structures, Algorithms, and Generic Programming

  2. Generic Copy template <class I, class J> void g_copy(I src_begin, I src_end, J dest_begin) { while (src_begin != src_end) { *dest_begin++ = *src_begin++; } } g_copy(L.Begin(), L.End(), V.Begin()); g_copy(A, A + n, L.Begin());

  3. Generic Find • Sequential search template <class I, typename T> I g_find(I begin, I end, const T& t) { for (; begin != end; ++begin) { if (t == *begin) { return begin; } } return end; } List_iterator = g_find(L.Begin(), L.End(), t); Vector_iterator = g_find(V.Begin(), V.End(), t); Array_iterator = g_find(A, A + size, t);

  4. Generic Max template <class I> I g_max_element(I begin, I end) { I max(begin); while (++begin != end) { if (*max < *begin) { max = begin; } } return max; } List_iterator = g_max_element(L.Begin, L.End()); Deque_iterator = g_max_element(D.Begin, D.End()); Array_iterator = g_max_element(A, A + size);

  5. Generic Algorithms and Predicate Objects template <class I, class P> I g_max_element(I begin, I end, const P& p) { I max(begin); while (++begin != end) { if (p(*max, *begin)) { max = begin; } } return max; } TGreaterThan <sometype> gt; Vector_iterator = g_max_element(V.Begin(), V.End(), gt); Deque_iterator = g_max_element(D.Begin(), D.End(), gt); A_iterator = g_max_element(A, A + size, gt);

  6. Generic Algorithms and Predicate Objects (2) • Function classes may be passed to generic algorithm as template parameter • Commonly used to pass predicate such as LessThan • Also used for other function classes

  7. Generic Algorithms and Predicate Objects (3) template <class I, class P> F g_for_each(I begin, I end, F f) { while (begin != end) { f(*begin++); } return f; } TList<char> L; makeuppercase muc; g_for_each(L.Begin(), L.End(), muc);

  8. Generic for_each class smartmakeuppercase { public: int count; smartmakeuppercase() : count(0) { } void operator() (char& x) { char y = x; x = toupper(x); if (y != x) ++count; } }; smartmakeuppercase smuc0, smuc1; smuc1 = g_for_each(L.Begin(), L.End(), smuc0); cout << smuc1.count – smuc0.count; // number letters converted

  9. Testing Generic Algorithms typedef char element_type; TList<element_type> L; TVector<element_type> V; TList<element_type>::Iterator L_iterator; TVector<element_type>::Iterator V_iterator; g_copy(L.Begin(), L.End(), V.Begin());

  10. begin end i k j Generic Simple Sort template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  11. begin end i k j Generic Simple Sort (2) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  12. begin end i k j Generic Simple Sort (3) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  13. begin end i k j Generic Simple Sort (4) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  14. begin end i k j Generic Simple Sort (5) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  15. begin end i k j Generic Simple Sort (6) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  16. begin end i k j Generic Simple Sort (7) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  17. begin end i k j Generic Simple Sort (8) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  18. begin end i k j Generic Simple Sort (9) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  19. begin end i k j Generic Simple Sort (10) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  20. begin end i k j Generic Simple Sort (11) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  21. begin end i k j Generic Simple Sort (12) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  22. begin end i k j Generic Simple Sort (13) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  23. begin end i k j Generic Simple Sort (14) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  24. begin end i k j Generic Simple Sort (15) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  25. begin end i k j Generic Simple Sort (16) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  26. begin end i k j Generic Simple Sort (17) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  27. begin end i k j Generic Simple Sort (18) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  28. begin end i k j Generic Simple Sort (19) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  29. begin end i k j Generic Simple Sort (20) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  30. begin end i k j Generic Simple Sort (21) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { k = i; for (j = i; j != end; ++j) { if (*j < *k) { k = j; } } swap(*i, *k); } }

  31. Generic Simple Sort (22) template <class T> void g_simple_sort(I begin, I end) { I i, j, k; for (i = begin; i != end; ++i) { // for each element k = i; // find the smallest element between i and end for (j = i; j != end; ++j) { if (*j < *k) { // store the index of the smallest // element to k k = j; } } // swap ith element with k swap(*i, *k); } }

  32. Generic Simple Sort (23) TList<int> L; g_simple_sort(L.Begin(), L.End()); • Running time: O(n2)

  33. Announcements • For this week, the Th 2-3 office hour is moved to Fri 11-12 • On Monday (10/20) and Wednesday (10/22), the class will meet in MCH 201 • Assignment 3 is due next Monday

More Related