\documentstyle[cprog]{simslide}
\uppercorners{Data Structures in C++}{\thepage}
\lowercorners{Vectors}{Chapter 8}
\begin{document}

\slide{}

{\Huge \bf
\begin{center}
$\;$ \\ $\;$ \\
Data Structures \\
in C++ \\ $\;$ \\
Chapter 8 \\ $\;$ \\
Tim Budd \\ $\;$ \\
Oregon State University \\
Corvallis, Oregon \\
USA \\
\end{center}
}

\slide{Outline -- Chapter 8}

{\bf Vectors}

\begin{itemize}
\item
Idea of vector
\item
Templates
\begin{itemize}
\item
Class templates
\item
Function templates
\end{itemize}
\item
Problems solved using vectors
\begin{itemize}
\item
Sieve of Erastosthenes
\item
Selection Sort
\item
Merge Sort
\item
Silly Sentence Generation
\end{itemize}
\item
Summary of vector operations
\item
The implementation of the vector data type
\end{itemize}

\slide{The Idea of a Vector}

Conceptually, a vector is simply a indexed collection of similarly typed
values.

\begin{center}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
element & element & element & element & element & element \\
0 & 1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}
\end{center}

A matrix is a two dimensional array, again of similar type values.

\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
element & element & element & element \\
0,0 & 0,1 & 0,2 & 0,3 \\
\hline
element & element & element & element \\
1,0 & 1,1 & 1,2 & 1,3 \\
\hline
element & element & element & element \\
2,0 & 2,1 & 2,2 & 2,3 \\
\hline
\end{tabular}
\end{center}

\slide{Why Build into an ADT?}

The C++ language has vector and matrix values, why build something else
on top of these?

\begin{itemize}
\item
Perform safety checkes (index bounds checks)
\item
Make values more ``self describing''
(and thus make programs more reliable)
\item
Permit the implementation of operations at a higher level of abstraction
(ability to dynamically make a vector grow, for example)
\end{itemize}

\slide{Templates}

One major difference between the vector ADT and 
the rational number or the string ADT is that
the vector ADT does not, by itself, describe the type of object it {\em
holds}.  Can have a vector of integers, a vector of reals, a vector of
strings, or any other type.

The idea of a {\em template} allows us to {\it parameterize} the type of object
held by a class.  You can think of a template as similar to a function
parameter, only it is a data structure parameter.

\slide{The Vector Template}

{\Large \begin{cprog}
template<class T> class vector {
public:
	typedef T * iterator;

				// constructors
	vector (unsigned int numberElements);
	vector (unsigned int numberElements, T initialValue);
	vector (const vector & source);
	~vector	();

		// member functions
	T       	back ();
	iterator	begin	();	
		...

		// operators
	T &	operator [ ]	(unsigned int);

private:	// data areas
	unsigned int    mySize;
	unsigned int	myCapacity
	T *             data;
};
\end{cprog}}

\slide{Declaring Template Types}

To declare a value with a template type, a type is provided in angle
brackets following the template class name.

\begin{cprog}

vector<int>       a(10);	
vector<double>  b(30);
vector<string>   c(15);

\end{cprog}

\slide{How a Template Works}

A template works as if {\tt int} replaced every occurrence of {\tt T}
and the class were renamed.

{\large\begin{cprog}

class vector_int {
public:
	typedef T * iterator;

				// constructors
	vector_int (unsigned int numberElements);
	vector_int (unsigned int numberElements, T initialValue);
	vector_int (const vector & source);
	~vector_int ();

		// member functions
	int       	back ();
	iterator	begin	();	
		...

		// operators
	int &	operator [ ]	(unsigned int);

private:	// data areas
	unsigned int    mySize;
	unsigned int	myCapacity
	int *             data;
};
\end{cprog}}

\slide{Naming}

When the \verb+vector<T>+ template class is instantiated by \verb+int+, its name becomes
\verb+vector<int>+.

Its constructor is named \verb+vector<int>::vector+.

Its member functions have names like \verb+vector<int>::size+.

To provide a {\it general} implementation of a member function, we use
the syntax

\begin{cprog}

template <class T>
unsigned int vector<T>::size()
{
   return mySize;
}
\end{cprog}

\slide{Function Templates}

Functions can also be parameterized using templates, as in the following:

\begin{cprog}

template <class T> T max(T a, T b)
	// return the maximum of a and b
{
	if (a < b)
		return b;
	return a;
}

\end{cprog}

\begin{cprog}

template <class T> void swap (T & a, T & b)
	// swap the values held by a and b
{
	T temp = a;
	a = b;
	b = temp;
}
\end{cprog}

\slide{Example Program -- Sieve of Erastosthenes}

{\Large \begin{cprog}

void sieve(vector<int> & values)
	// leave vector holding only prime numbers
{
	unsigned int max = values.size();

	// first initialize all cells
	for (int i = 0; i < max; i++)
		values[i] = i;

	// now search for non-zero cells
	for (i = 2; i*i < max; i++) {
		if (values[i] != 0) {
			// inv: i has no factors 
			for (int j = i + i; j < max; j += i) 
				values[j] = 0;
			// inv: all multiples of i have been cleared
			}
		// all nonzero values smaller than i are prime
		}
	// inv: all nonzero values are prime
}
\end{cprog}}

\slide{Another Example -- Selection Sort}

{\Large\begin{cprog}
template<class T> 
void selectionSort(vector<T> & data)
	// sort, in place, the vector argument 
	// into ascending order
{	
	unsigned int top;
	for (top = data.size()-1; top > 0; top = top - 1) {
		// find the position of the largest element
		unsigned int largeposition = 0;
		for (int j = 1; j <= top; j++) {
			// inv: data[largeposition] is largest element 
			// in 0..j-1
			if (data[largeposition] < data[j])
				largeposition = j;
			// inv: data[largeposition] is 
			// largest element in 0 .. j
			}
		if (top != largeposition)
			swap(data, top, largeposition);
		// inv: data[top .. n] is ordered
		}
}
\end{cprog}}

\slide{Merge Sort}

Unfortunately, Selection Sort is still $O(n^2)$ worst case.

Better algorithm can be built using the idea that two vectors can
be merged in linear time.

\setlength{\unitlength}{8mm}
\begin{center}
\begin{picture}(11,5)
\put(0,2){\framebox(5,1){}}
\put(0,2){\framebox(1,1){2}}
\put(1,2){\framebox(1,1){5}}
\put(2,2){\framebox(1,1){7}}
\put(3,2){\framebox(1,1){9}}
\put(4,2){\framebox(1,1){12}}
\put(6,2){\framebox(5,1){}}
\put(6,2){\framebox(1,1){3}}
\put(7,2){\framebox(1,1){6}}
\put(8,2){\framebox(1,1){8}}
\put(9,2){\framebox(1,1){9}}
\put(10,2){\framebox(1,1){14}}
\put(0.5,0){\framebox(10,1){}}
\put(0.5,0){\framebox(1,1){2}}
\put(1.5,0){\framebox(1,1){3}}
\put(2.5,0){\framebox(1,1){5}}
\put(3.5,0){\framebox(1,1){}}
\put(4.5,0){\framebox(1,1){}}
\put(5.5,0){\framebox(1,1){}}
\put(6.5,0){\framebox(1,1){}}
\put(7.5,0){\framebox(1,1){}}
\put(8.5,0){\framebox(1,1){}}
\put(9.5,0){\framebox(1,1){}}
\put(2.5,2){\line(1,-1){1}}
\put(7.5,2){\line(-4,-1){4}}
\end{picture}
\end{center}

\slide{In-Place Merge}

An in place merge can be performed for adjacent vector ranges:

\setlength{\unitlength}{7mm}
\begin{center}
\begin{picture}(11,3.6)
\put(0,0){\framebox(10,1){}}
\put(0,0){\framebox(1,1){2}}
\put(1,0){\framebox(1,1){3}}
\put(2,0){\framebox(1,1){5}}
\put(3,0){\framebox(1,1){7}}
\put(4,0){\framebox(1,1){9}}
\put(5,0){\framebox(1,1){12}}
\put(6,0){\framebox(1,1){3}}
\put(7,0){\framebox(1,1){6}}
\put(8,0){\framebox(1,1){8}}
\put(9,0){\framebox(1,1){9}}
\put(10,0){\framebox(1,1){14}}
\put(0,0.8){\makebox(1,1){$\downarrow$}}
\put(0,1.4){\makebox(1,1){\em start}}
\put(6,0.8){\makebox(1,1){$\downarrow$}}
\put(6,1.4){\makebox(1,1){\em center}}
\put(11,0.8){\makebox(1,1){$\downarrow$}}
\put(11,1.4){\makebox(1,1){\em end}}
\put(-1,2.2){\makebox(1,1){\bf input}}
\end{picture} \\
\begin{picture}(11,3.8)
\put(0,0){\framebox(10,1){}}
\put(0,0){\framebox(1,1){2}}
\put(1,0){\framebox(1,1){3}}
\put(2,0){\framebox(1,1){3}}
\put(3,0){\framebox(1,1){5}}
\put(4,0){\framebox(1,1){6}}
\put(5,0){\framebox(1,1){7}}
\put(6,0){\framebox(1,1){8}}
\put(7,0){\framebox(1,1){9}}
\put(8,0){\framebox(1,1){9}}
\put(9,0){\framebox(1,1){12}}
\put(10,0){\framebox(1,1){14}}
\put(0,0.8){\makebox(1,1){$\downarrow$}}
\put(0,1.4){\makebox(1,1){\em start}}
\put(11,0.8){\makebox(1,1){$\downarrow$}}
\put(11,1.4){\makebox(1,1){\em end}}
\put(-1,2.2){\makebox(1,1){\bf result}}
\end{picture}
\end{center}

Provided by generic function

\begin{cprog}

inplace_merge 
	(iterator start, iterator center, iterator end);

\end{cprog}

\slide{How to build a Sorting Algorithm}

First, break things apart, until you reach a single element

\setlength{\unitlength}{7mm}
\begin{center}
\begin{picture}(13,10)(0,-4)
% really bottom row
\put(2.7,-4){\framebox(1,1){3}}
\put(4.3,-4){\framebox(1,1){2}}
\put(9.7,-4){\framebox(1,1){7}}
\put(11.3,-4){\framebox(1,1){1}}
\put(3.6,-2){\vector(-1,-2){0.5}}
\put(4.4,-2){\vector(1,-2){0.5}}
\put(10.6,-2){\vector(-1,-2){0.5}}
\put(11.4,-2){\vector(1,-2){0.5}}
% almost bottom row
\put(0,-2){\framebox(1,1){7}}
\put(1.5,-2){\framebox(1,1){3}}
\put(3,-2){\framebox(1,1){3}}
\put(4,-2){\framebox(1,1){2}}
\put(5.5,-2){\framebox(1,1){9}}
\put(7,-2){\framebox(1,1){3}}
\put(8.5,-2){\framebox(1,1){6}}
\put(10,-2){\framebox(1,1){7}}
\put(11,-2){\framebox(1,1){1}}
\put(12.5,-2){\framebox(1,1){2}}
\put(1,0){\vector(-1,-2){0.5}}
\put(2,0){\vector(0,-1){1}}
\put(4.5,0){\vector(-1,-2){0.5}}
\put(6,0){\vector(0,-1){1}}
\put(8,0){\vector(-1,-2){0.5}}
\put(9,0){\vector(0,-1){1}}
\put(11.5,0){\vector(-1,-2){0.5}}
\put(13,0){\vector(0,-1){1}}
% bottom row
\put(0.5,0){\framebox(1,1){7}}
\put(1.5,0){\framebox(1,1){3}}
\put(3.5,0){\framebox(1,1){3}}
\put(4.5,0){\framebox(1,1){2}}
\put(5.5,0){\framebox(1,1){9}}
\put(7.5,0){\framebox(1,1){3}}
\put(8.5,0){\framebox(1,1){6}}
\put(10.5,0){\framebox(1,1){7}}
\put(11.5,0){\framebox(1,1){1}}
\put(12.5,0){\framebox(1,1){2}}
% middle row
\put(1,2){\framebox(1,1){7}}
\put(2,2){\framebox(1,1){3}}
\put(3,2){\framebox(1,1){3}}
\put(4,2){\framebox(1,1){2}}
\put(5,2){\framebox(1,1){9}}
\put(7,2){\framebox(1,1){3}}
\put(8,2){\framebox(1,1){6}}
\put(9,2){\framebox(1,1){7}}
\put(10,2){\framebox(1,1){1}}
\put(11,2){\framebox(1,1){2}}
% top row
\put(2,4){\framebox(1,1){7}}
\put(3,4){\framebox(1,1){3}}
\put(4,4){\framebox(1,1){3}}
\put(5,4){\framebox(1,1){2}}
\put(6,4){\framebox(1,1){9}}
\put(7,4){\framebox(1,1){3}}
\put(8,4){\framebox(1,1){6}}
\put(9,4){\framebox(1,1){7}}
\put(10,4){\framebox(1,1){1}}
\put(11,4){\framebox(1,1){2}}
% arrows
\put(2.5,2){\vector(-1,-1){1}}
\put(4,2){\vector(1,-1){1}}
\put(9.5,2){\vector(-1,-1){1}}
\put(11,2){\vector(1,-1){1}}
\put(4.5,4){\vector(-1,-1){1}}
\put(8.5,4){\vector(1,-1){1}}
\end{picture}
\end{center}

\slide{Then Put Together}

Then merge adjacent ranges as you come back out of the sequence of
recursive calls.

\setlength{\unitlength}{7mm}
\begin{center}
\begin{picture}(13,10)
% bottom row
\put(0.5,4){\framebox(1,1){3}}
\put(1.5,4){\framebox(1,1){7}}
\put(3.5,4){\framebox(1,1){2}}
\put(4.5,4){\framebox(1,1){3}}
\put(5.5,4){\framebox(1,1){9}}
\put(7.5,4){\framebox(1,1){3}}
\put(8.5,4){\framebox(1,1){6}}
\put(10.5,4){\framebox(1,1){1}}
\put(11.5,4){\framebox(1,1){2}}
\put(12.5,4){\framebox(1,1){7}}
% middle row
\put(1,2){\framebox(1,1){2}}
\put(2,2){\framebox(1,1){3}}
\put(3,2){\framebox(1,1){3}}
\put(4,2){\framebox(1,1){7}}
\put(5,2){\framebox(1,1){9}}
\put(7,2){\framebox(1,1){1}}
\put(8,2){\framebox(1,1){2}}
\put(9,2){\framebox(1,1){3}}
\put(10,2){\framebox(1,1){6}}
\put(11,2){\framebox(1,1){7}}
% top row
\put(2,0){\framebox(1,1){1}}
\put(3,0){\framebox(1,1){2}}
\put(4,0){\framebox(1,1){2}}
\put(5,0){\framebox(1,1){3}}
\put(6,0){\framebox(1,1){3}}
\put(7,0){\framebox(1,1){3}}
\put(8,0){\framebox(1,1){6}}
\put(9,0){\framebox(1,1){7}}
\put(10,0){\framebox(1,1){7}}
\put(11,0){\framebox(1,1){9}}
% next row
\put(0,6){\framebox(1,1){7}}
\put(1.5,6){\framebox(1,1){3}}
\put(3,6){\framebox(1,1){2}}
\put(4,6){\framebox(1,1){3}}
\put(5.5,6){\framebox(1,1){9}}
\put(7,6){\framebox(1,1){3}}
\put(8.5,6){\framebox(1,1){6}}
\put(10,6){\framebox(1,1){1}}
\put(11,6){\framebox(1,1){7}}
\put(12.5,6){\framebox(1,1){2}}
% topmost row
\put(2.7,8){\framebox(1,1){3}}
\put(4.3,8){\framebox(1,1){2}}
\put(9.7,8){\framebox(1,1){7}}
\put(11.3,8){\framebox(1,1){1}}
% arrows
\put(1.5,4){\vector(1,-1){1}}
\put(5,4){\vector(-1,-1){1}}
\put(8.5,4){\vector(1,-1){1}}
\put(12,4){\vector(-1,-1){1}}
\put(3.5,2){\vector(1,-1){1}}
\put(9.5,2){\vector(-1,-1){1}}
\put(0.5,6){\vector(1,-1){1}}
\put(2,6){\vector(-1,-2){0.5}}
\put(4,6){\vector(1,-1){1}}
\put(6,6){\vector(-1,-1){1}}
\put(7.5,6){\vector(1,-1){1}}
\put(9,6){\vector(-1,-2){0.5}}
\put(11,6){\vector(1,-1){1}}
\put(13,6){\vector(-1,-1){1}}
\put(3.1,8){\vector(1,-2){0.5}}
\put(4.6,8){\vector(-1,-2){0.5}}
\put(10.1,8){\vector(1,-2){0.5}}
\put(11.6,8){\vector(-1,-2){0.5}}
\end{picture}
\end{center}

\slide{The Merge Sort Algorithm}

\begin{cprog}

template <class Itr>
void m_sort(Itr start, unsigned low, unsigned high)
{
	if (low + 1 < high) {
		unsigned int center = (high + low) / 2;
		m_sort (start, low, center);
		m_sort (start, center, high);
		inplace_merge 
			(start + low, start + center, 
				start + high);
		}
}

template <class T> 
void mergeSort(vector<T> & s)
{
	m_sort(s.begin(), 0, s.size());
}

\end{cprog}

\slide{What is the Asympototic Complexity?}

\begin{itemize}
\item
Complexity is work at each level times number of levels of call.
\item
Work at each level is linear
\item
Number of recursive calls in $\log n$
\item
Total amount of work is $O(n \log n)$! 
\item
Much better than bubble sort or insertion sort
\end{itemize}

\slide{Picture of Complexity}

\setlength{\unitlength}{7mm}
\begin{center}
\begin{picture}(15,12.5)
% bottom row
\put(0.5,4){\framebox(1,1){3}}
\put(1.5,4){\framebox(1,1){7}}
\put(3.5,4){\framebox(1,1){2}}
\put(4.5,4){\framebox(1,1){3}}
\put(5.5,4){\framebox(1,1){9}}
\put(7.5,4){\framebox(1,1){3}}
\put(8.5,4){\framebox(1,1){6}}
\put(10.5,4){\framebox(1,1){1}}
\put(11.5,4){\framebox(1,1){2}}
\put(12.5,4){\framebox(1,1){7}}
% middle row
\put(1,2){\framebox(1,1){2}}
\put(2,2){\framebox(1,1){3}}
\put(3,2){\framebox(1,1){3}}
\put(4,2){\framebox(1,1){7}}
\put(5,2){\framebox(1,1){9}}
\put(7,2){\framebox(1,1){1}}
\put(8,2){\framebox(1,1){2}}
\put(9,2){\framebox(1,1){3}}
\put(10,2){\framebox(1,1){6}}
\put(11,2){\framebox(1,1){7}}
% top row
\put(2,0){\framebox(1,1){1}}
\put(3,0){\framebox(1,1){2}}
\put(4,0){\framebox(1,1){2}}
\put(5,0){\framebox(1,1){3}}
\put(6,0){\framebox(1,1){3}}
\put(7,0){\framebox(1,1){3}}
\put(8,0){\framebox(1,1){6}}
\put(9,0){\framebox(1,1){7}}
\put(10,0){\framebox(1,1){7}}
\put(11,0){\framebox(1,1){9}}
% next row
\put(0,6){\framebox(1,1){7}}
\put(1.5,6){\framebox(1,1){3}}
\put(3,6){\framebox(1,1){2}}
\put(4,6){\framebox(1,1){3}}
\put(5.5,6){\framebox(1,1){9}}
\put(7,6){\framebox(1,1){3}}
\put(8.5,6){\framebox(1,1){6}}
\put(10,6){\framebox(1,1){1}}
\put(11,6){\framebox(1,1){7}}
\put(12.5,6){\framebox(1,1){2}}
% topmost row
\put(2.7,8){\framebox(1,1){3}}
\put(4.3,8){\framebox(1,1){2}}
\put(9.7,8){\framebox(1,1){7}}
\put(11.3,8){\framebox(1,1){1}}
% arrows
\put(1.5,4){\vector(1,-1){1}}
\put(5,4){\vector(-1,-1){1}}
\put(8.5,4){\vector(1,-1){1}}
\put(12,4){\vector(-1,-1){1}}
\put(3.5,2){\vector(1,-1){1}}
\put(9.5,2){\vector(-1,-1){1}}
\put(0.5,6){\vector(1,-1){1}}
\put(2,6){\vector(-1,-2){0.5}}
\put(4,6){\vector(1,-1){1}}
\put(6,6){\vector(-1,-1){1}}
\put(7.5,6){\vector(1,-1){1}}
\put(9,6){\vector(-1,-2){0.5}}
\put(11,6){\vector(1,-1){1}}
\put(13,6){\vector(-1,-1){1}}
\put(3.1,8){\vector(1,-2){0.5}}
\put(4.6,8){\vector(-1,-2){0.5}}
\put(10.1,8){\vector(1,-2){0.5}}
\put(11.6,8){\vector(-1,-2){0.5}}
% surrounding labels
\put(0,9.5){\makebox(14,1){\em n elements wide}}
\put(3,10){\vector(-1,0){3}}
\put(11,10){\vector(1,0){3}}
\put(15,5){\makebox(1,1){$\log n$}}
\put(15,4){\makebox(1,1){\em calls}}
\put(15,3){\makebox(1,1){\em deep}}
\put(15.5,3){\vector(0,-1){3}}
\put(15.5,6){\vector(0,1){3}}
\end{picture}
\end{center}

\slide{Example Problem -- Silly Sentence Generation}

Generate a sequence of silly sentences.

Each sentence has form subject - verb - object.

First, allocate three vectors, with initially empty size.

\begin{cprog}



	vector<string> subject, verb, object;

\end{cprog}

\slide{Dynamically Extending the Size of Vectors}

Next, push values on to the end of the vectors.  Vectors are automatically
resized as necessary.

\begin{cprog}
	// add subjects
	subject.push_back("alice and fred");
	subject.push_back("cats");
	subject.push_back("people");
	subject.push_back("teachers");

	// add verbs
	verb.push_back("love");
	verb.push_back("hate");
	verb.push_back("eat");
	verb.push_back("hassle");

	// add objects
	object.push_back("dogs");
	object.push_back("cats");
	object.push_back("people");
	object.push_back("donuts");
\end{cprog}

\slide{Generating Sentences}

Use {\tt size} to compute size, {\tt randomInteger} to get a random
subscript.

\begin{cprog}


for (int i = 0; i < 10; i++) 
	cout << subject[randomInteger(subject.size())] 
		<< " "
		<< verb[randomInteger(verb.size())] 
		<< " "
		<< object[randomInteger(object.size())] 
		<< "\n"

\end{cprog}

Example Output

{\em \begin{verse}
alice and fred hate dogs \\
teachers hassle cats \\ 
alice and fred love cats \\
people hassle donuts \\
people hate dogs \\
\end{verse}}

\slide{Matrices}

Can even build vectors whos elements are themselves vectors -- this is
a reasonable approximation to a matrix.

\begin{cprog}

	vector< vector<int> > mat(5);

\end{cprog}

Initially each row has zero elements.  Must be resized to correct limit.

\begin{cprog}

	for (int i = 0; i < 5; i++)
		mat[i].resize(6);

\end{cprog}

\slide{Vector Operations}

{\Large\begin{center}
\begin{tabular}{| l | l |}
\hline
\multicolumn{2}{| l |}{\bf Constructors} \\
\hline
{\tt vector$<$T$>$ v;} & default constructor \\
{\tt vector$<$T$>$ v (int);} & initialized with explicit size \\
{\tt vector$<$T$>$ v (int, T);} & size and initial value \\
{\tt vector$<$T$>$ v (aVector);} & copy constructor \\ 
\hline
\multicolumn{2}{| l |}{\bf Element Access} \\
\hline
{\tt v[i]} & subscript access \\
{\tt v.front ()} & first value in collection \\
{\tt v.back ()} & last value in collection \\
\hline
\multicolumn{2}{| l |}{\bf Insertion} \\
\hline
{\tt v.push\_back (T)} & push element on to back of vector \\
{\tt v.insert(iterator, T)} & insert new element after iterator \\
{\tt v.swap(vector<T>)} & swap values with another vector \\
\hline
\multicolumn{2}{| l |}{\bf Removal} \\
\hline
{\tt v.pop\_back ()} & pop element from back of vector \\
{\tt v.erase(iterator)} & remove single element \\
{\tt v.erase(iterator, iterator)} & remove range of values \\
\hline
\multicolumn{2}{| l |}{\bf Size} \\
\hline
{\tt v.capacity ()} & number of elements buffer can hold\\
{\tt v.size ()} & number of elements currently held \\
{\tt v.resize (unsigned, T)} & change to size, padding with value \\
{\tt v.reserve (unsigned)} & set physical buffer size \\
{\tt v.empty ()} & true if vector is empty \\
\hline
\multicolumn{2}{| l |}{\bf Iterators} \\
\hline
{\tt vector<T>::iterator itr} & declare a new iterator \\
{\tt v.begin () } & starting iterator \\
{\tt v.end ()} & ending iterator \\
\hline
\end{tabular}
\end{center}}

\slide{Sizes of Vector}

Vectors will maintain an internal buffer.  Like the string, the physical
size of the buffer need not be the same as the logical size.

\begin{center}
\setlength{\unitlength}{1cm}
\begin{picture}(6,2)
\put(0,0){\framebox(1,1){2}}
\put(1,0){\framebox(1,1){4}}
\put(2,0){\framebox(1,1){3}}
\put(3,0){\framebox(1,1){7}}
\put(4,0){\framebox(1,1){}}
\put(5,0){\framebox(1,1){}}
\put(4,0){\line(1,1){1}}
\put(4,1){\line(1,-1){1}}
\put(5,0){\line(1,1){1}}
\put(5,1){\line(1,-1){1}}
\end{picture}
\end{center}

The two sizes can be accessed or set using member functions.

As with the string, a new buffer is allocated when the physical size
is exceeded.

\slide{Useful Generic Algorithms}

{\large\begin{tabular}{| l |}
\hline
{\tt fill (iterator start, iterator stop, value)} \\
fill vector with a given initial value \\
\hline
{\tt copy (iterator start, iterator stop, iterator destination)} \\
copy one sequence into another \\
\hline
{\tt max\_element(iterator start, iterator stop)} \\
find largest value in collection \\
\hline
{\tt min\_element(iterator start, iterator stop)} \\
find smallest value in collection \\
\hline
{\tt reverse (iterator start, iterator stop)} \\
reverse elements in the collection \\
\hline
{\tt count (iterator start, iterator stop, target value, counter)} \\
count elements that match target value, incrementing counter \\
\hline
{\tt count\_if (iterator start, iterator stop, unary fun, counter)} \\
count elements that satisfy function, incrementing counter \\
\hline
{\tt transform (iterator start, iterator stop, iterator destination, unary)} \\
transform elements using unary function from source, placing into destination \\
\hline
{\tt find (iterator start, iterator stop, value)} \\
find value in collection, returning iterator for location \\
\hline
{\tt find\_if (iterator start, iterator stop, unary function)} \\
find value for which function is true, returning iterator for location \\
\hline
{\tt replace (iterator start, iterator stop, target value, replacement value)} \\
replace target element with replacement value \\
\hline
{\tt replace\_if (iterator start, iterator stop, unary fun, replacement value)} \\
replace lements for which fun is true with replacement value \\
\hline
{\tt sort (iterator start, iterator stop)} \\
places elements into ascending order \\
\hline
{\tt for\_each (iterator start, iterator stop, function)} \\
execute function on each element of vector \\
\hline
{\tt iter\_swap (iterator, iterator)} \\
swap the values specified by two iterators \\
\hline
\end{tabular}}

\slide{Example, counting elements}

\begin{cprog}

	vector<int>::iterator start = aVec.begin();
	vector<int>::iterator stop = aVec.end();

	if (find(start, stop, 17) != stop) 
		...	// element has been found

	int counter = 0;
	count (start, stop, 17, counter);
	if (counter != 0)
		...	// element is in collection

\end{cprog}

\slide{Vector Implementation}

\begin{itemize}
\item
Like string, the vector holds a buffer that can dynamically grow if needed
\item
Maintains two sizes, physical and logical size
\item
Most operations have simple implementations, can be performed inline
\item
(Note that this implementation is simpler than the actual
commercial implementations, which are properitary)
\end{itemize}

\slide{Inline Definitions}

{\large\begin{cprog}
template <class T> class vector {
public:
	typedef T * iterator;

		// constructors
	vector 	()	{ buffer = 0; resize(0); }
	vector 	(unsigned int size)	{ buffer = 0; resize(size); }
	vector 	(unsigned int size, T initial);
	vector 	(vector & v);
	~vector	()	{ delete buffer; }

		// member functions
	T       	back () { assert(! empty()); return buffer[mySize - 1];}
	iterator	begin () { return buffer; }
	int     	capacity () { return myCapacity; }
	bool    	empty () { return mySize == 0; }
	iterator	end () { return begin() + mySize; }
	T       	front () { assert(! empty()); return buffer[0]; }
	void    	pop_back () { assert(! empty()); mySize--; }
	void    	push_back (T value);
	void    	reserve (unsigned int newCapacity);
	void    	resize (unsigned int newSize)
					{ reserve(newSize); mySize = newSize; }
	int     	size () { return mySize; }

		// operators
	T & 	operator [ ]	(unsigned int index)
			{ assert(index < mySize); return buffer[index]; }
private:
	unsigned int mySize;
	unsigned int myCapacity;
	T * buffer;
};
\end{cprog}}

\slide{Constructors}

The constructors use generic algorithms to fill initial values:

{\Large\begin{cprog}

template <class T> 
vector<T>::vector (unsigned int size, T initial)
	// create vector with given size, 
	// initialize each element with value
{
	buffer = 0;
	resize(size);
		// use fill algorithm to initialize each 
	fill (begin(), end(), initial);
}

template <class T> 
vector<T>::vector (vector & v)
	// create vector with given size, 
	// initialize elements by copying
{
	buffer = 0;
	resize(size);
		// use copy algorithm to initialize 
	copy (v.begin(), v.end(), begin());
}
\end{cprog}}

\slide{Reserve -- the workhorse method}

{\Large\begin{cprog}

template <class T> 
void vector<T>::reserve (unsigned int newCapacity)
	// reserve capacity at least as large as argument 
{
	if (buffer == 0) {
		mySize = 0;
		myCapaicty = 0;
		}
		// don't do anything if already large enough
	if (newCapacity <= myCapacity)
		return;
			// allocate new buffer, make sure successful
	T * newBuffer = new T [newCapacity];
	assert (newBuffer);
			// copy values into buffer
	copy (buffer, buffer + mySize, newBuffer);
			// reset data field
	myCapacity = newCapacity;
			// change buffer pointer
	delete buffer;
	buffer = newBuffer;
}
\end{cprog}}

\slide{Implementing Generic Algorithms}

Templates are also the key to the implementation of generic algorithms.

\begin{cprog}

template (class ItrType, class T)
	void fill (ItrType start, ItrType stop, T value)
{
	while (start != stop)
		*start++ = value;
}


template (class SourceItrType, class DestItrType)
	void copy (SourceItrType start, 
		SourceItrType stop, DestItrType dest)
{
	while (start != stop)
		*dest++ = *start++;
}
\end{cprog}

\end{document}

