GPtutorial Screen
GPtutorial Screen
In this PDF screen version of the slides, links to other supplementary slides are colored red.
These additional slides are in html format, generated from Microsoft Powerpoint format;
they have not been converted into PDF for direct incorporation in this document since they
contain a few animations of diagrams. The best way to use these links depends on whether
you view this PDF file in a Web browser or download it and view it in Adobe Acrobat.
• If you are using a Web browser, you can left-click the links as usual and remain within
the same browser; however, when you are finished with a set of supplementary slides,
the Back button on the browser will inconveniently take you back to the beginning
of the PDF file. Thus you may instead want to right-click the link, select “Copy
Link Location” from the pop-up menu, and paste it into the Location pane of a
separate browser window. Then when you finish with a set of supplementary slides
you can bring up original browser window and continue the main slides from the
point where you left off.
• If you are using Adobe Acrobat, when you encounter the links to supplementary
slides you should not simply left-click on them, as Acrobat will try (unsuccessfully)
to try to display them with its own very limited Web-browsing capability. Instead,
right-click and select “Open Weblink in Browser” from the pop-up menu to begin
viewing the supplementary slides in a separate Web browser window. When finished
with those supplementary slides, bring up the Acrobat window again to continue
with the main slides.
In case your browser has difficulty displaying the animations in the html format, you may
prefer to download and view the original Powerpoint versions of the supplementary slides
from
http://www.cs.rpi.edu/~musser/gp/GPtutorial.
Contents
1 Generic Programming:
First Definition,
and STL Examples 8
1.13 Adaptors . . . . . . . . . . . . . . . . . . . . 31
2 Generic Programming:
A Broader Definition 38
++ Increment
++ Increment ==, ==,
& Compare,
& Reference Reference
Compare, Generic
= Assign -- Decrement
= Assign -- Decrement Parameter
P
* Dereference +, -, < Random Access
+, -, < Random Access
Five of the Six Major STL Component Categories (Concepts)
Container Iterators Generic Function Adaptors
Classes Algorithms Objects
vector less stack push
sort pop
T insert T
erase C top
istream istream_ T
iterator greater reverse
merge
T
ostream ostream_ T iterator
iterator
– Classes, inheritance
– Automatically invoked constructors and destructors
– Not used: late-binding with virtual functions
1.5. Container example:
application of STL map
class TerminalManager {
map<int, Terminal *> terminalMap;
public:
Status AddTerminal(int terminalId, int type) {
Status status;
if (terminalMap.count(terminalId) == 0) {
Terminal* pTerm = new Terminal(terminalId, type);
terminalMap[terminalId] = pTerm;
status = SUCCESS;
} else
status = FAILURE;
return status;
}
Status RemoveTerminal(int terminalId) {
Status status;
if (terminalMap.count(terminalId) == 1) {
Terminal* pTerm = terminalMap[terminalId];
terminalMap.erase(terminalId);
delete pTerm;
status = SUCCESS;
} else
status = FAILURE;
return status;
}
Terminal* FindTerminal(int terminalId) {
Terminal* pTerm;
if (terminalMap.count(terminalId) == 1) {
pTerm = terminalMap[terminalId];
} else
pTerm = NULL;
return pTerm;
}
void HandleMessage(const Message* pMsg) {
int terminalId = pMsg->GetTerminalId();
Terminal* pTerm = FindTerminal(terminalId);
if (pTerm)
pTerm->HandleMessage(pMsg);
}
}; // end of class TerminalManager
int main()
{
TerminalManager TH;
TH.AddTerminal(700000013, 1);
TH.AddTerminal(700000017, 2);
TH.AddTerminal(700000007, 1);
// ...
Terminal* t = TH.FindTerminal(700000017);
// ...
TH.RemoveTerminal(700000017);
// ...
}
1.6. Generic algorithm example:
application of generic merge
#include <cassert>
#include <list>
#include <deque>
#include <algorithm> // For merge
using namespace std;
“Pointers on steroids”
++ Increment
++ Increment ==, ==,
& Compare,
& Reference Reference
Compare, Generic
= Assign -- Decrement
= Assign -- Decrement Parameter
P
* Dereference +, -, < Random Access
+, -, < Random Access
1.8. Another generic algorithm
example: accumulate
#include <iostream>
#include <vector>
#include <cassert>
#include <numeric> // for accumulate
using namespace std;
int main()
{
int x[5] = {2, 3, 5, 7, 11};
int main()
{
int x[5] = {2, 3, 5, 7, 11};
// Initialize vector1 to x[0] through x[4]:
vector<int> vector1(&x[0], &x[5]);
int product = accumulate(vector1.begin(), vector1.end(),
1, mult);
assert (product == 2310);
}
1.12.2. Another way to define the mult function
object
struct multiplies {
int operator() const (int x, int y) { return x * y; }
} mult;
istream istream_ T
iterator greater reverse
merge
T
ostream ostream_ T iterator
iterator
vector<float>::iterator i;
cout.precision(10);
for (i = vector1.begin(); i != vector1.end(); ++i)
cout << *i << " ";
cout << endl;
float sum = accumulate(vector1.begin(), vector1.end(),
(float)0.0);
cout << "Sum accumulated from left = " << sum << endl;
Output:
Values to be added:
1 4.470348358e-08 2.980232239e-08 1.490116119e-08 1.490116119e
Sum accumulated from left = 1
Sum accumulated from right = 1.000000119
1.15. TerminalManager generalized and
rewritten as a container adaptor
1
Developed by Andrew Lumsdaine’s research group (formerly at
the University of Notre Dame, now at Indiana University).
4. New Ways of Specifying and
Using Performance Requirements
Prototype:
– value comparisons
– value assignments
– iterator operations
– “distance-type” operations
4.4. Algorithm concepts
for setting performance standards
• Going further . . .
8. How Does Generic Programming
Relate to Aspect Oriented Pro-
gramming?
• Recall the definition
Generic Programming = Programming with Concepts.
Or, as Alex Stepanov has said, “The goal of Generic
Programming is to provide a systematic classification of
useful software components . . . Generic Programming is
fundamentally a study of algorithms, data structures and
other software components with particular emphasis on
their systematic organization.”