\documentstyle[cprog]{simslide}
\uppercorners{Data Structures in C++}{\thepage}
\lowercorners{Proving Correctness}{Chapter 6}
\begin{document}

\slide{}

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

\slide{Outline -- Chapter 6}

{\bf The Standard Library Container Classes}

\begin{itemize}
\item
Variations on Containers
\begin{itemize}
\item
Vector
\item
List
\item
Deque
\item
Stack
\item
Queue
\item
Priority Queue
\item
Set
\item
Map
\end{itemize}
\item
Iterators
\end{itemize}

\slide{Types of Collections in the Standard Library}

{\Large
\begin{tabular}{l l l l l}
 & Addition & Removal & Removal & Inclusion \\
Structure & of new & of first & of middle & test \\
	& element & element & element & \\
\hline
{\bf Vector} & $O(1)$ or & $O(n)$ & $O(1)$ or & $O(n)$ or \\
 & $O(n)$ \dag & & $O(n)$ \dag & $O(\log n)$ \ddag \\
\multicolumn{5}{l}{indexed, random access to elements, bounded size} \\
\hline
{\bf List} & $O(1)$ & $O(1)$ & $O(1)$ & $O(n)$ \\
\multicolumn{5}{l}{sequential access to elements, rapid insertion and removal} \\
\hline
{\bf Deque} & $O(1)$ & $O(1)$ & $O(n)$ & $O(n)$ or \\
 & & & & $O(\log n)$ \ddag \\
\multicolumn{5}{l}{random access, rapid insertion to front and back} \\
\hline
{\bf Stack} & $O(1)$ & $O(1)$ & NA & NA \\
\multicolumn{5}{l}{insertion and removal only from front} \\
\hline
{\bf Queue} & $O(1)$ & $O(1)$ & NA & NA \\
\multicolumn{5}{l}{insertion only from front, removal only from back} \\
\hline
{\bf Priority Queue} & $O(\log n)$ & $O(1)$ or & NA & NA \\
& & $O(\log n)$ \S & & \\
\multicolumn{5}{l}{rapid removal of largest element} \\
\hline
{\bf Set} & $O(\log n)$ & $O(\log n)$ & $O(\log n)$ & $O(\log n)$ \\
\multicolumn{5}{l}{ordered collection, unique values, rapid insertion, removal and test } \\
\multicolumn{5}{l}{a {\tt multiset} allows repeated elements} \\
\hline
{\bf Map} & $O(\log n)$ & $O(\log n)$ & $O(\log n)$ & $O(\log n)$ \\
\multicolumn{5}{l}{collection of key-value pairs} \\
\multicolumn{5}{l}{a {\tt multimap} allows multiple elements with same key} \\
\hline
& \multicolumn{4}{l}{ \dag constant if accessing existing position, } \\
{\bf Notes}   & \multicolumn{4}{l}{ $\hspace{5mm}$ linear if inserting/removing} \\
 & \multicolumn{4}{l}{ \ddag logarithmic if ordered, linear if not ordered} \\
 & \multicolumn{4}{l}{ \S constant access time, logarithmic removal} \\
\end{tabular}}

\slide{Vectors}

\begin{itemize}
\item
Generization of array
\item
Efficient, random access to elements
\item
High Level operations, such as dynamically increasing or decreasing in size
\end{itemize}

\setlength{\unitlength}{2.1cm}
\begin{center}
\begin{picture}(7,1.3)(1,0)
\put(0,0){\framebox(1,1){\tt v[0]}}
\put(1,0){\framebox(1,1){\tt v[1]}}
\put(2,0){\framebox(1,1){\tt v[2]}}
\put(3,0){\framebox(2,1){...}}
\put(5,0){\framebox(1,1){\tt v[n$-$2]}}
\put(6,0){\framebox(1,1){\tt v[n$-$1]}}
\end{picture}
\end{center}

\slide{Strings}

\begin{itemize}
\item
In one sense, a vector of character values
\item
In another sense, an entirely different high level data type
\item
Lots of string specific operations
\end{itemize}

\begin{cprog}

		string aName = "Benjamin Franklin";

\end{cprog}

\slide{Lists}
\begin{itemize}
\item
Arbitrary size, memory used efficiently as grows and shrinks
\item
Sequential access only, constant access to first or last element
\item
Efficient insertion or removal at any location
\end{itemize}

\setlength{\unitlength}{1.3cm}
\begin{center}
\begin{picture}(9,2)
\put(0,0){\framebox(1,1){\em head}}
\put(1,0.5){\vector(1,0){1}}
\put(2,0){\framebox(1,1){}}
\put(3,0.5){\vector(1,0){1}}
\put(4,0){\framebox(1,1){}}
\put(5,0.5){\vector(1,0){1}}
\put(6,0){\makebox(1,1){...}}
\put(7,0.5){\vector(1,0){1}}
\put(8,0){\framebox(1,1){\em tail}}
\end{picture}
\end{center}

\slide{Deque -- Double Ended Queue}

\begin{itemize}
\item
Grows or shrinks as necessary
\item
Efficient insertion or removal from either end
\item
Random access to elements
\end{itemize}

\setlength{\unitlength}{2cm}
\begin{picture}(8,2)(1,0)
\put(1,0.3){\vector(-1,0){1}}
\put(0,0.7){\vector(1,0){1}}
\put(1,0){\framebox(1,1){\tt v[0]}}
\put(2,0){\framebox(1,1){\tt v[1]}}
\put(3,0){\framebox(2,1){...}}
\put(5,0){\framebox(1,1){\tt v[n$-$2]}}
\put(6,0){\framebox(1,1){\tt v[n$-$1]}}
\put(8,0.3){\vector(-1,0){1}}
\put(7,0.7){\vector(1,0){1}}
\end{picture}

\slide{Stacks and Queues}

\begin{itemize}
\item
Specialized form of deque
\item
Stack has LIFO, Last In, First Out protocol
\item
Queue has FIFO, First In, First Out protocol
\end{itemize}

\setlength{\unitlength}{1.5cm}
\begin{center}
\begin{picture}(10,6)
% the stack
\put(1,0){\framebox(1,1){}}
\put(1,1){\framebox(1,1){}}
\put(1,2){\framebox(1,1){}}
\put(1.3,3){\vector(0,1){1}}
\put(1.7,4){\vector(0,-1){1}}
% the queue
\put(4,2){\oval(2,1)[bl]}
\put(4,1.5){\vector(1,0){1}}
\put(5,1){\framebox(1,1){}}
\put(6,1){\framebox(1,1){}}
\put(7,1){\framebox(1,1){}}
\put(8,1){\framebox(1,1){}}
\put(9,1.5){\vector(1,0){1}}
\end{picture}
\end{center}

\slide{Sets}

\begin{itemize}
\item
Ordered collection
\item
Efficient (logarithm) insertion, removal, and test for inclusion
\item
Efficient merge, union, difference, and other set operations
\item
Multiset allows more than one entry with same value
\end{itemize}

\setlength{\unitlength}{3mm}
\begin{center}
\begin{picture}(22,15)
\put(5,10){\oval(10,10)}
\put(17,5){\oval(10,10)}
\put(1,13){\makebox(1,1){\em 3}}
\put(2,7){\makebox(1,1){\em 12}}
\put(3,9){\makebox(1,1){\em 14}}
\put(4,13){\makebox(1,1){\em 7}}
\put(6,7){\makebox(1,1){\em 9}}
\put(7,12){\makebox(1,1){\em 32}}
\put(8,9){\makebox(1,1){\em 11}}
\put(13,7){\makebox(1,1){\em 11}}
\put(14,3){\makebox(1,1){\em 12}}
\put(16,6){\makebox(1,1){\em 6}}
\put(17,2){\makebox(1,1){\em 22}}
\put(18,4){\makebox(1,1){\em 4}}
\put(20,7){\makebox(1,1){\em 17}}
\end{picture}
\end{center}

\slide{Priority Queue}

\begin{itemize}
\item
Efficient (logarithic) insertion of new values
\item
Efficient access to largest (or smallest) value.
(Constant time access, logarithmic removal)
\end{itemize}

\setlength{\unitlength}{0.6cm}
\begin{center}
\begin{picture}(10,12)
\put(0,5){\line(0,1){5}}
\put(0,10){\line(1,0){10}}
\put(10,10){\line(0,-1){5}}
\put(0,5){\line(4,-3){4}}
\put(6,2){\line(4,3){4}}
\put(4,0){\framebox(2,2){\em 47}}
\put(0.5,8.1){\makebox(2,2){\em 9}}
\put(1,4){\makebox(2,2){\em 41}}
\put(1,7){\makebox(2,2){\em 23}}
\put(3,6){\makebox(2,2){\em 32}}
\put(4,3){\makebox(2,2){\em 17}}
\put(5,8){\makebox(2,2){\em 19}}
\put(6,4){\makebox(2,2){\em 20}}
\put(7,6.5){\makebox(2,2){\em 7}}
\put(8,7.5){\makebox(2,2){\em 12}}
\end{picture}
\end{center}

\slide{Map (Dictionary)}

\begin{itemize}
\item
Collection of key and value pairs
\item
Keys can be any ordered data type (i.e., string)
\item
Values can be any data type
\item
Efficient insertion, removal, test for inclusion
\end{itemize}

\begin{center}
\begin{tabular}{l c l}
$key_1$ & $\rightarrow$ & $value_1$ \\
$key_2$ & $\rightarrow$ & $value_2$ \\
$key_3$ & $\rightarrow$ & $value_3$ \\
 & ... & \\
$key_n$ & $\rightarrow$ & $value_n$ \\
\end{tabular}
\end{center}

\slide{Selecting a Continer}

{\Large
\begin{itemize}
\item
{\em How are values going to be accessed?}

\begin{verse}
random -- vector or deque \\
ordered -- set or map \\
sequential -- list \\
\end{verse}

\item
{\em Is the order in which values are maintained in the collection important?}

\begin{verse}
ordered -- set \\
can be sorted -- vector or deque \\
insertion time dependent -- stack or queue \\
\end{verse}

\item
{\em Will the size of the structure vary widely over the course of execution?}

\begin{verse}
Yes -- use a list or set \\
No -- use a vector or deque \\
\end{verse}

\item
{\em Is it possible to estimate the size of the collection?}

\begin{verse}
Yes -- use a vector \\
\end{verse}

\item
{\em Is testing to see whether a value is contained in the collection 
a frequent operation?}

\begin{verse}
Yes -- use a set \\
\end{verse}

\item
{\em Is the collection indexed?  
That is, can the collection be viewed as a series of key/value pairs?}

\begin{verse}
Index values are integer -- use a vector or deque \\
Otherwise use a map\\
\end{verse}


\item
{\em Can values be related to each other?}

\begin{verse}
Sets require relational operators \\
Vectors or Lists do not \\
\end{verse}

\item
{\em Is finding and removing the largest value from the collection a frequent
operation?}

\begin{verse}
Yes -- use priority queue \\
(slightly faster than set) \\
\end{verse}

\item
{\em At what positions are values inserted into or removed from the structure?}

\begin{verse}
Insertions to middle of list are efficient \\
Insertions into middle of vectors are not \\
Stacks and Queues can be inserted only at end \\
\end{verse}

\item
{\em Is a frequent operation the merging of two or more sequences into one?}

\begin{verse}
If ordered -- use a set \\
Otherwise -- use a list and splice \\
\end{verse}
\end{itemize}}

\slide{Iterators}

\begin{itemize}
\item
Basic problem -- how do you allow access to elements of collection,
without knowing how the collection is organized?
\item
Solution, define a new data type specifically for creating loops
\item
A large number of algorithms are provided by the standard library,
all built using iterators.
\end{itemize}

\slide{How do you describe a range of values}

\setlength{\unitlength}{3.2mm}
\begin{center}
\begin{picture}(54,7)(10,0)
\put(0,0){\framebox(8,3){\tt card[0]}}
\put(8,0){\framebox(8,3){\tt card[1]}}
\put(16,0){\framebox(8,3){\tt card[2]}}
\put(24,0){\framebox(10,3){\tt ...}}
\put(34,0){\framebox(8,3){\tt card[50]}}
\put(42,0){\framebox(8,3){\tt card[51]}}
\put(0,3){\makebox(2,2){$\downarrow$}}
\put(8,3){\makebox(2,2){$\downarrow$}}
\put(16,3){\makebox(2,2){$\downarrow$}}
\put(34,3){\makebox(2,2){$\downarrow$}}
\put(42,3){\makebox(2,2){$\downarrow$}}
\put(50,3){\makebox(2,2){$\downarrow$}}
\put(0,4){\makebox(4,2){\tt cards}}
\put(8,4){\makebox(4,2){\tt cards+1}}
\put(16,4){\makebox(4,2){\tt cards+2}}
\put(34,4){\makebox(4,2){\tt cards+50}}
\put(42,4){\makebox(4,2){\tt cards+51}}
\put(50,4){\makebox(4,2){\tt cards+52}}
\end{picture}
\end{center}

\begin{itemize}
\item
Notice how a range of values is often described by a starting value
and a {\em past-the-end} value.
\item
The past the end value is not part of the collection, but just a marker.
\end{itemize}

\slide{Begin and End}

By convention, containers return a starting value in response
to {\tt begin()}, and a past-the-end value in response to {\tt end()}.

For example, to shuffle a vector of values:

\begin{cprog}

	random_shuffle
		(aVector.begin(), aVector.end(), 
			randomInteger);

\end{cprog}

\slide{What must iterators do}

To see what iterators must do, consider a typical algorithm:

\begin{cprog}

	iterator find 
		(iterator first, iterator last, T & value)
	{
		while (first != last && *first != value)
			++first;
		return first;
	}

\end{cprog}

Could be used to find values in an array, or in a list:

\begin{cprog}

int data[100];
	...
int * where = find(data, data+100, 7);

list<int>::iterator where = 
	find(aList.begin(), aList.end(), 7);
\end{cprog}

\slide{Iterator Operations}

\begin{itemize}
\item
An iterator can be compared for equality to another iterator.  They are equal when they point to the same position, and are otherwise not equal.
\item
An iterator can be dereferenced using the $*$ operator, 
to obtain the value being denoted by the iterator.
Depending upon the type of iterator and variety of underlying container,
this value can also sometimes be used as the target of an assignment in 
order to change the value being held by the container.
\item
An iterator can be incremented, so that it refers to the next element in sequence, using the operator {\tt ++}.
\end{itemize}

What makes iterators possible is that all of these can be overloaded.

Will make extensive use of iterators in the following chapters.

\end{document}
