06.08.2013 Views

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

30 <strong>Programming</strong> with the STL<br />

find_if(v.begin(), v.end(), bind2nd(first_equal(), name))<br />

first equal is a functor (a class) that takes two parameters, a pair and a host name,<br />

and compares the first component of the pair with the name.<br />

b) Implement a class MapNameServer that uses a map to store the name/address pairs.<br />

The average search time in this implementation will be considerably better than that<br />

for the vector implementation.<br />

c) It is possible that the map implementation of the name server is sufficiently efficient,<br />

but you shall also try a third implementation. Implement a class HashNameServer<br />

that uses a hash table to store the name/address pairs. Use a vector of vector’s to<br />

store the name/address pairs. 8<br />

The hash table implementation is open for experimentation: you must select an<br />

appropriate size for the hash table and a suitable hash function. 9 It can also be<br />

worthwhile to exploit the fact that most (in fact all, in the test program) computer<br />

names start with “www.”. Without any greater difficulty, you should be able to<br />

improve the search times that you obtained in the map implementation by about 50%.<br />

Use the program nstest.cc to check that the insert/remove/lookup functions work correctly.<br />

Then, use the program nstime.cc to measure and print the search times for the three<br />

different implementations, using the file nameserverdata.txt as input (the file contains<br />

25,143 name/address pairs 10 ).<br />

A2. Examples of average search times in milliseconds for a name server with 25,143 names are<br />

in the following table.<br />

vector 0.185<br />

map 0.00066<br />

hash 0.00029<br />

25,143 100,000<br />

Search the Internet for information about search algorithms for different data structures, or<br />

use your knowledge from the algorithms and data structures course, and fill in the blanks<br />

in the table. Write a similar table for your own implementation.<br />

2 Bitsets, Subscripting, and Iterators<br />

2.1 Bitsets<br />

To manipulate individual bits in a word, <strong>C++</strong> provides the bitwise operators & (and), | (or),<br />

^ (exclusive or), and the shift operators > (shift right). The STL class bitset<br />

generalizes this notion and provides operations on a set of N bits indexed from 0 through N-1.<br />

N may be arbitrary large, indicating that the bitset may occupy many words.<br />

For historical reasons, bitset doesn’t provide any iterators. We will develop a simplified<br />

version of the bitset class where all the bits fit in one word, and extend the class with iterators<br />

so it becomes possible to use the standard STL algorithms with the class. Our goal is to provide<br />

enough functionality to make the following program work correctly:<br />

8 GNU STL contains a class hash map (in newer versions unordered map), a map implementation that uses a hash table.<br />

You should not use any of these classes in this assignment.<br />

9 Note that a good hash function should take all (or at least many) of the characters of a string into account and that<br />

"abc" and "cba" should have different hash codes. For instance, a hash function that merely adds the first and last<br />

characters of a string is not acceptable.<br />

10 It was difficult to find a file with real name/address pairs, so the computer names are common words from a dictionary<br />

file, with www. first and .se or similar last. The IP addresses are running numbers.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!