340 likes | 588 Views
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());
E N D
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()); g_copy(A, A + n, L.Begin());
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);
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);
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);
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
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);
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
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());
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
Generic Simple Sort (23) TList<int> L; g_simple_sort(L.Begin(), L.End()); • Running time: O(n2)
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