Generic Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Generic Algorithms

Description:

Deque_iterator = g_max_element(D.Begin(), D.End(), gt) ... class smartmakeuppercase { public: int count; smartmakeuppercase() : count(0) ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 34
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Generic Algorithms


1
Generic Algorithms
  • Andy Wang
  • Data Structures, Algorithms, and Generic
    Programming

2
Generic Copy
  • template ltclass I, class Jgt
  • 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 ltclass I, typename Tgt
  • 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 ltclass Igt
  • I g_max_element(I begin, I end)
  • I max(begin)
  • while (begin ! end)
  • if (max lt 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 ltclass I, class Pgt
  • 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 ltsometypegt 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 ltclass I, class Pgt
  • F g_for_each(I begin, I end, F f)
  • while (begin ! end)
  • f(begin)
  • return f
  • TListltchargt 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 ltlt smuc1.count smuc0.count // number
    letters converted

9
Testing Generic Algorithms
  • typedef char element_type
  • TListltelement_typegt L
  • TVectorltelement_typegt V
  • TListltelement_typegtIterator L_iterator
  • TVectorltelement_typegtIterator V_iterator
  • g_copy(L.Begin(), L.End(), V.Begin())

10
Generic Simple Sort
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
11
Generic Simple Sort (2)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
12
Generic Simple Sort (3)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
13
Generic Simple Sort (4)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
14
Generic Simple Sort (5)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
15
Generic Simple Sort (6)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
16
Generic Simple Sort (7)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
17
Generic Simple Sort (8)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
18
Generic Simple Sort (9)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
19
Generic Simple Sort (10)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
20
Generic Simple Sort (11)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 8 2 7 3
21
Generic Simple Sort (12)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 8 7 3
22
Generic Simple Sort (13)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 8 7 3
23
Generic Simple Sort (14)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 8 7 3
24
Generic Simple Sort (15)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 8 7 3
25
Generic Simple Sort (16)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 8 7 3
26
Generic Simple Sort (17)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 8 7 3
27
Generic Simple Sort (18)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 3 7 8
28
Generic Simple Sort (19)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 3 7 8
29
Generic Simple Sort (20)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 3 7 8
30
Generic Simple Sort (21)
  • template ltclass Tgt
  • 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 lt k)
  • k j
  • swap(i, k)

1 2 3 7 8
31
Generic Simple Sort (22)
  • template ltclass Tgt
  • 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 lt 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)
  • TListltintgt 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
Write a Comment
User Comments (0)
About PowerShow.com