Map< ValueType > Class Template Reference

#include "map.h"

List of all members.

Public Member Functions

 Map (int sizeHint=101)
 ~Map ()
int size ()
bool isEmpty ()
void add (string key, ValueType value)
void remove (string key)
bool containsKey (string key)
ValueType getValue (string key)
ValueType & operator[] (string key)
void clear ()
void mapAll (void(fn)(string key, ValueType val))
template<typename ClientDataType>
void mapAll (void(fn)(string key, ValueType val, ClientDataType &data), ClientDataType &data)
Iterator iterator ()
const Mapoperator= (const Map &rhs)
 Map (const Map &rhs)
template<typename ClientData>
void mapAll (void(*fn)(string key, ValueType value, ClientData &cd), ClientData &data)
template<typename ValueType>
void mapAll (void(*fn)(string key, ValueType value))

Friends

class Iterator

Classes

struct  cell
class  Iterator


Detailed Description

template<typename ValueType>
class Map< ValueType >

This interface defines a class template that stores a collection of key-value pairs. For maximum generality, the Map is supplied as a class template. The keys are always of type string, but the value type is set by the client. The client specializes the map to hold values of a specific type, e.g. Map<int> or Map<studentT>, as needed.


Constructor & Destructor Documentation

template<typename ValueType>
Map< ValueType >::Map ( int  sizeHint = 101  )  [explicit]

The constructor initializes a new empty map. The optional argument is a hint about the expected number of entries that this map will hold, which allows the map to configure itself for efficiency at that size. If not specified, a reasonable default is used and the map will adapt as entries are added. The explicit keyword is used to prevent accidental construction of a Map from an integer. Raises an error if sizeHint is negative.

template<typename ValueType>
Map< ValueType >::~Map (  ) 

The destructor deallocates storage associated with this map.

template<typename ValueType>
Map< ValueType >::Map ( const Map< ValueType > &  rhs  ) 


Member Function Documentation

template<typename ValueType>
int Map< ValueType >::size (  ) 

This member function returns the number of entries in this map.

template<typename ValueType>
bool Map< ValueType >::isEmpty (  ) 

This member function returns true if this map contains no entries, false otherwise.

template<typename ValueType>
void Map< ValueType >::add ( string  key,
ValueType  value 
)

This member function associates key with value in this map. Any previous value associated with key is replaced by this new entry. If there was already an entry for this key, the map's size is unchanged; otherwise, it increments by one.

template<typename ValueType>
void Map< ValueType >::remove ( string  key  ) 

This member function removes any entry for key from this map. If there is no entry for the key, the map is unchanged. Otherwise, the key and its associated value are removed and the map's size decreases by one.

template<typename ValueType>
bool Map< ValueType >::containsKey ( string  key  ) 

Returns true if there is an entry for key in this map, false otherwise.

template<typename ValueType>
ValueType Map< ValueType >::getValue ( string  key  ) 

If key is found in this map, this member function returns the associated value. If key is not found, raises an error. The containsKey member function can be used to verify the presence of a key in the map before attempting to get its value.

template<typename ValueType>
ValueType & Map< ValueType >::operator[] ( string  key  ) 

This member function overloads [] to access values from this map by key. The argument inside the brackets is the key (a string). This allows the client to use notation of an "associative-array" to get/set the value associated with a key. If the key is already present in the map, this function returns a reference to its associated value, and the size of the map is unchanged. If key is not present in the map, a new entry for the key is added, and the size of the map increases by one. The value for the newly entered key is set to the default for value type, and a reference to that value is returned. Because this function returns the value by reference, it allows in-place modification of the value.

template<typename ValueType>
void Map< ValueType >::clear (  ) 

This member function removes all entries from this map. The map is made empty and will have size() = 0 after being cleared.

template<typename ValueType>
void Map< ValueType >::mapAll ( void(fn)(string key, ValueType val)   ) 

This member function goes through every entry in this map and calls the function fn, passing it two arguments: the key and its associated value.

template<typename ValueType>
template<typename ClientDataType>
void Map< ValueType >::mapAll ( void(fn)(string key, ValueType val, ClientDataType &data)  ,
ClientDataType &  data 
)

This member function goes through every entry in this map and calls the function fn, passing it three arguments: the key, its associated value, and the client's data. That data can be of whatever type is needed for the client's callback.

template<typename ValueType>
Map< ValueType >::Iterator Map< ValueType >::iterator (  ) 

This member function creates an iterator that allows the client to iterate through the keys in this map. Note that the iterator returns the _keys_ one by one. If needed, you can retrieve the associated value from the map using getValue or operator[].

template<typename ValueType>
const Map< ValueType > & Map< ValueType >::operator= ( const Map< ValueType > &  rhs  ) 

This copy constructor and operator= are defined to make a deep copy, making it possible to pass/return maps by value and assign from one map to another. The entire contents of the map, including all entries, are copied. Each map entry is copied from the original map to the copy using assignment (operator=). Making copies is generally avoided because of the expense and thus, maps are typically passed by reference, however, when a copy is needed, these operations are supported.

template<typename ValueType>
template<typename ClientData>
void Map< ValueType >::mapAll ( void(*)(string key, ValueType value, ClientData &cd)  fn,
ClientData &  data 
)

template<typename ValueType>
template<typename ValueType>
void Map< ValueType >::mapAll ( void(*)(string key, ValueType value)  fn  ) 


Friends And Related Function Documentation

template<typename ValueType>
friend class Iterator [friend]


The documentation for this class was generated from the following file:
Generated on Mon Feb 12 23:30:08 2007 for CS 106B Libraries by  doxygen 1.5.1