# PDictionary< K, D > Class Template Reference

This template class maps the `PAbstractDictionary` to a specific key and data types. More...

`#include <dict.h>`

Inheritance diagram for PDictionary< K, D >:

List of all members.

## Public Types

typedef std::pair< K, D * > | value_type

## Public Member Functions

Construction
| PDictionary ()
| Create a new, empty, dictionary.
Overrides from class PObject
virtual PObject * | Clone () const
| Make a complete duplicate of the dictionary.
New functions for class
D & | operator[] (const K &key) const
| Get the object contained in the dictionary at the `key` position.
PBoolean | Contains (const K &key) const
| Determine if the value of the object is contained in the hash table.
virtual D * | RemoveAt (const K &key)
| Remove an object at the specified `key`.
virtual PBoolean | SetAt (const K &key, D *obj)
| Add a new object to the collection.
virtual D * | GetAt (const K &key) const
| Get the object at the specified key position.
const K & | GetKeyAt (PINDEX index) const
| Get the key in the dictionary at the ordinal index position.
D & | GetDataAt (PINDEX index) const
| Get the data in the dictionary at the ordinal index position.
PArray< K > | GetKeys () const
| Get an array containing all the keys for the dictionary.

## Protected Member Functions

| PDictionary (int dummy, const PDictionary *c)

## Detailed Description

### template<class K, class D>
class PDictionary< K, D >

This template class maps the `PAbstractDictionary` to a specific key and data types.

The functions in this class primarily do all the appropriate casting of types.

Note that if templates are not used the `PDECLARE_DICTIONARY` macro will simulate the template instantiation.

## Member Typedef Documentation

template<class K, class D>

typedef std::pair<K, D *> PDictionary< K, D >::value_type

## Constructor & Destructor Documentation

template<class K, class D>

PDictionary< K, D >::PDictionary | ( |  | ) | `[inline]`

Create a new, empty, dictionary.

Note that by default, objects placed into the dictionary will be deleted when removed or when all references to the dictionary are destroyed.

template<class K, class D>

PDictionary< K, D >::PDictionary | ( | int | dummy,
|  | const PDictionary< K, D > * | c |
| ) |  |  | `[inline, protected]`

## Member Function Documentation

template<class K, class D>

virtual PObject* PDictionary< K, D >::Clone | ( |  | ) | const`[inline, virtual]`

Make a complete duplicate of the dictionary.

Note that all objects in the array are also cloned, so this will make a complete copy of the dictionary.

Reimplemented from PObject.

template<class K, class D>

PBoolean PDictionary< K, D >::Contains | ( | const K & | key | ) | const`[inline]`

Determine if the value of the object is contained in the hash table.

The object values are compared, not the pointers. So the objects in the collection must correctly implement the `PObject::Compare()` function. The hash table is used to locate the entry.

Returns:
  true if the object value is in the dictionary.

Parameters:

  key | Key to look for in the dictionary.

template<class K, class D>

virtual D* PDictionary< K, D >::GetAt | ( | const K & | key | ) | const`[inline, virtual]`

Get the object at the specified key position.

If the key was not in the collection then NULL is returned.

Returns:
  pointer to object at the specified key.

template<class K, class D>

D& PDictionary< K, D >::GetDataAt | ( | PINDEX | index | ) | const`[inline]`

Get the data in the dictionary at the ordinal index position.

The ordinal position in the dictionary is determined by the hash values of the keys and the order of insertion.

The last key/data pair is remembered by the class so that subseqent access is very fast.

Returns:
  reference to data at the index position.

Parameters:

  index | Ordinal position in dictionary for data.

template<class K, class D>

const K& PDictionary< K, D >::GetKeyAt | ( | PINDEX | index | ) | const`[inline]`

Get the key in the dictionary at the ordinal index position.

The ordinal position in the dictionary is determined by the hash values of the keys and the order of insertion.

The last key/data pair is remembered by the class so that subseqent access is very fast.

Returns:
  reference to key at the index position.

Parameters:

  index | Ordinal position in dictionary for key.

template<class K, class D>

PArray<K> PDictionary< K, D >::GetKeys | ( |  | ) | const`[inline]`

Get an array containing all the keys for the dictionary.

template<class K, class D>

D& PDictionary< K, D >::operator[] | ( | const K & | key | ) | const`[inline]`

Get the object contained in the dictionary at the `key` position.

The hash table is used to locate the data quickly via the hash function provided by the `key`.

The last key/data pair is remembered by the class so that subseqent access is very fast.

Returns:
  reference to the object indexed by the key.

Parameters:

  key | Key to look for in the dictionary.

template<class K, class D>

virtual D* PDictionary< K, D >::RemoveAt | ( | const K & | key | ) | `[inline, virtual]`

Remove an object at the specified `key`.

The returned pointer is then removed using the `SetAt()` function to set that key value to NULL. If the `AllowDeleteObjects` option is set then the object is also deleted.

Returns:
  pointer to the object being removed, or NULL if the key was not present in the dictionary. If the dictionary is set to delete objects upon removal, the value -1 is returned if the key existed prior to removal rather than returning an illegal pointer

Parameters:

  key | Key for position in dictionary to get object.

template<class K, class D>

virtual PBoolean PDictionary< K, D >::SetAt | ( | const K & | key,
|  | D * | obj |
| ) |  |  | `[inline, virtual]`

Add a new object to the collection.

If the objects value is already in the dictionary then the object is overrides the previous value. If the `AllowDeleteObjects` option is set then the old object is also deleted.

The object is placed in the an ordinal position dependent on the keys hash function. Subsequent searches use the hash function to speed access to the data item.

Returns:
  true if the object was successfully added.

The documentation for this class was generated from the following file:

- dict.h