Ptex: PtexDict< T > Class Template Reference

Ptex

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- Class List
- Class Hierarchy
- Class Members

Classes | Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
PtexDict< T > Class Template Reference

A string-keyed dictionary template, using a hash table. More...

`#include <PtexDict.h>`

## Classes
class | const_iterator
| Internal class used to provide iteration through the dictionary. More...
class | Entry
| This internal structure is used to store the dictionary elements. More...
class | iterator
| Internal class used to provide iteration through the dictionary. More...
struct | value_type
| Internal class used to provide a return value for the value type. More...

## Public Types
typedef const char * | key_type
| This is the lookup type. More...
typedef T | mapped_type
| The data type stored. More...

## Public Member Functions
| PtexDict ()
| Default contructor initializes the dictionary. More...
virtual | ~PtexDict ()
| clears the entries in the dictionary More...
T & | operator[] (const char *key)
| Locates an entry, creating a new one if necessary. More...
iterator | begin ()
| Returns an iterator referencing the beginning of the table. More...
iterator | end ()
| Returns an iterator referencing the end of the table. More...
const_iterator | begin () const
| Const access to the beginning of the list. More...
const_iterator | end () const
| Const access to the end of the list. More...
iterator | find (const char *key)
| Locates an entry, without creating a new one. More...
const_iterator | find (const char *key) const
| works the same as find above, but returns a constant iterator. More...
bool | erase (const char *key)
| Will remove an entry. It will return TRUE if an entry was found. More...
iterator | erase (iterator iter)
| Removes the entry referenced by the iterator, from the dictionary. More...
void | clear ()
| clear will remove all entries from the dictionary. O(n) More...
int | size () const
| Returns the number of entries in the dictionary. O(1) time to call. More...

## Private Member Functions
| PtexDict (const PtexDict &)
| Copy constructor prohibited by design. More...
PtexDict & | operator= (const PtexDict &)
| Assignment operator prohibited by design. More...
int | hash (const char *key, int &keylen) const
| Returns the integer hash index for the key and length of the key. More...
Entry ** | locate (const char *key, int &keylen, int &hashval) const
| Returns a pointer to the desired entry, based on the key. More...
void | grow ()
| Used to increase the size of the table if necessary. More...

## Static Private Member Functions
static bool | streq (const char *s1, const char *s2, int len)
| Used for string compares, much faster then strcmp. More...

## Private Attributes
int | _numEntries
| The number of entries in the dictionary. More...
int | _numBuckets
| The number of buckets in use. More...
int | _bucketMask
| The mask for the buckets. More...
Entry ** | _buckets
| The pointer to the bucket structure. More...

## Friends
class | iterator
| forward declared class More...
class | const_iterator

## Detailed Description

### template<class T>
class PtexDict< T >

A string-keyed dictionary template, using a hash table.

An efficient string dictionary template. A hash table is used that automatically doubles in size when it is more than 50% full. It is hard-coded to use string keys for efficiency.

The interface is compatible with std::hash_map<>, though not all methods are provided. Methods provided:

- iterator begin();
- const_iterator begin() const;
- iterator end();
- const_iterator end() const;
- T& operator[] (const char*);
- iterator find(const char* key);
- const_iterator find(const char* key) const;
- bool erase(const char* key);
- iterator erase(iterator);
- void clear();
- int size() const;

Unlike std::hash_map<>, PtexDict doesn't have to construct a string object to do a lookup. As a result, it is about 4-10 times faster depending on the length of the keys. And when compiling non-optimized, it is 6-10 times faster.

We have decided NOT to follow the coding standard's naming comvention for this class, since it needed to remain generic, and compatible with the STL std::hash_map<> class.

Author
  brentb
  longson

Version
  1.0 brentb 11/01/2000: Initial version
  1.1 longson 06/26/2001: Added file and class comment headers
  2.0 longson 01/16/2002: Updated to support most of the coding standards, except for the naming conventions. Added const_iterator to provide const-safe access. Fixed problem with iterators and not allowing modification.

Definition at line 111 of file PtexDict.h.

## Member Typedef Documentation

template<class T>

typedef const char* PtexDict< T >::key_type

This is the lookup type.

Definition at line 121 of file PtexDict.h.

template<class T>

typedef T PtexDict< T >::mapped_type

The data type stored.

Definition at line 122 of file PtexDict.h.

## Constructor & Destructor Documentation

template<class T>

PtexDict< T >::PtexDict | ( |  | ) |
| inline

Default contructor initializes the dictionary.

Definition at line 138 of file PtexDict.h.

template<class T>

virtual PtexDict< T >::~PtexDict | ( |  | ) |
| inlinevirtual

clears the entries in the dictionary

Definition at line 140 of file PtexDict.h.

template<class T>

PtexDict< T >::PtexDict | ( | const PtexDict< T > & |  | ) |
| private

Copy constructor prohibited by design.

## Member Function Documentation

template<class T>

iterator PtexDict< T >::begin | ( |  | ) |
| inline

Returns an iterator referencing the beginning of the table.

Definition at line 149 of file PtexDict.h.

Referenced by PtexDict< T >::clear(), PtexReaderCache::purgeAll(), and PtexReaderCache::removeBlankEntries().

template<class T>

const_iterator PtexDict< T >::begin | ( |  | ) | const
| inline

Const access to the beginning of the list.

Definition at line 165 of file PtexDict.h.

template<class T >

void PtexDict< T >::clear | ( |  | ) |

clear will remove all entries from the dictionary. O(n)

Definition at line 588 of file PtexDict.h.

References PtexDict< T >::_buckets, PtexDict< T >::_numBuckets, PtexDict< T >::_numEntries, PtexDict< T >::begin(), PtexDict< T >::end(), and PtexDict< T >::erase().

Referenced by PtexDict< PtexReader * >::~PtexDict().

template<class T>

iterator PtexDict< T >::end | ( |  | ) |
| inline

Returns an iterator referencing the end of the table.

Definition at line 162 of file PtexDict.h.

Referenced by PtexDict< T >::clear(), PtexDict< T >::find(), PtexReaderCache::purge(), PtexReaderCache::purgeAll(), and PtexReaderCache::removeBlankEntries().

template<class T>

const_iterator PtexDict< T >::end | ( |  | ) | const
| inline

Const access to the end of the list.

Definition at line 178 of file PtexDict.h.

template<class T >

bool PtexDict< T >::erase | ( | const char * | key | ) |

Will remove an entry. It will return TRUE if an entry was found.

Definition at line 555 of file PtexDict.h.

References PtexDict< T >::find().

Referenced by PtexDict< T >::clear(), PtexReaderCache::purge(), PtexReaderCache::purgeAll(), and PtexReaderCache::removeBlankEntries().

template<class T >

PtexDict< T >::iterator PtexDict< T >::erase | ( | iterator | iter | ) |

Removes the entry referenced by the iterator, from the dictionary.

It will return a iterator to the next element, or will equal the return value of end() if there is nothing else to erase. O(1)

Definition at line 566 of file PtexDict.h.

References PtexDict< T >::iterator::_e, PtexDict< T >::Entry::_next, PtexDict< T >::_numEntries, and PtexDict< T >::Entry::~Entry().

template<class T >

PtexDict< T >::iterator PtexDict< T >::find | ( | const char * | key | ) |

Locates an entry, without creating a new one.

find will locate an entry, but won't create a new one. The result is returned as a pair of key and value. The returned key points to the internal key string and will remain valid until the entry is deleted. If the key is not found, the returned iterator will be equal to the value returned by end(), and the iterator will be equal to false. O(1)

return a valid iterator if we found an entry, else return end()

Definition at line 478 of file PtexDict.h.

References PtexDict< T >::_bucketMask, PtexDict< T >::end(), PtexDict< T >::iterator, and PtexDict< T >::locate().

Referenced by PtexDict< T >::erase(), and PtexReaderCache::purge().

template<class T >

PtexDict< T >::const_iterator PtexDict< T >::find | ( | const char * | key | ) | const

works the same as find above, but returns a constant iterator.

return a valid iterator if we found an entry, else return end()

Definition at line 489 of file PtexDict.h.

References PtexDict< T >::_bucketMask, PtexDict< T >::const_iterator, PtexDict< T >::end(), and PtexDict< T >::locate().

template<class T >

void PtexDict< T >::grow | ( |  | ) |
| private

Used to increase the size of the table if necessary.

Definition at line 529 of file PtexDict.h.

References PtexDict< T >::_bucketMask, PtexDict< T >::_buckets, PtexDict< T >::Entry::_next, and PtexDict< T >::_numBuckets.

Referenced by PtexDict< T >::operator[]().

template<class T>

int PtexDict< T >::hash | ( | const char * | key,
|  | int & | keylen
| ) |  | const
| inlineprivate

Returns the integer hash index for the key and length of the key.

Definition at line 237 of file PtexDict.h.

Referenced by PtexDict< PtexReader * >::locate().

template<class T>

Entry** PtexDict< T >::locate | ( | const char * | key,
|  | int & | keylen,
|  | int & | hashval
| ) |  | const
| inlineprivate

Returns a pointer to the desired entry, based on the key.

Definition at line 249 of file PtexDict.h.

Referenced by PtexDict< T >::find(), and PtexDict< T >::operator[]().

template<class T>

PtexDict& PtexDict< T >::operator= | ( | const PtexDict< T > & |  | ) |
| private

Assignment operator prohibited by design.

template<class T >

T & PtexDict< T >::operator[] | ( | const char * | key | ) |

Locates an entry, creating a new one if necessary.

operator[] will look up an entry and return the value. A new entry will be created (using the default ctor for T) if one doesn't exist.

Definition at line 500 of file PtexDict.h.

References PtexDict< T >::_bucketMask, PtexDict< T >::_buckets, PtexDict< T >::Entry::_hashval, PtexDict< T >::Entry::_key, PtexDict< T >::Entry::_keylen, PtexDict< T >::Entry::_next, PtexDict< T >::_numBuckets, PtexDict< T >::_numEntries, PtexDict< T >::Entry::_u, PtexDict< T >::Entry::_val, PtexDict< T >::grow(), if(), PtexDict< T >::locate(), and PtexDict< T >::value_type::second.

template<class T>

int PtexDict< T >::size | ( |  | ) | const
| inline

Returns the number of entries in the dictionary. O(1) time to call.

Definition at line 202 of file PtexDict.h.

template<class T>

static bool PtexDict< T >::streq | ( | const char * | s1,
|  | const char * | s2,
|  | int | len
| ) |  |
| inlinestaticprivate

Used for string compares, much faster then strcmp.

This is MUCH faster than strcmp and even memcmp, partly because it is inline and partly because it can do 4 chars at a time

Definition at line 264 of file PtexDict.h.

Referenced by PtexDict< PtexReader * >::locate().

## Friends And Related Function Documentation

template<class T>

friend class const_iterator
| friend

Definition at line 119 of file PtexDict.h.

Referenced by PtexDict< PtexReader * >::begin(), PtexDict< PtexReader * >::end(), and PtexDict< T >::find().

template<class T>

friend class iterator
| friend

forward declared class

Definition at line 117 of file PtexDict.h.

Referenced by PtexDict< PtexReader * >::begin(), PtexDict< PtexReader * >::end(), and PtexDict< T >::find().

## Member Data Documentation

template<class T>

int PtexDict< T >::_bucketMask
| private

The mask for the buckets.

Definition at line 286 of file PtexDict.h.

Referenced by PtexDict< T >::find(), PtexDict< T >::grow(), PtexDict< PtexReader * >::locate(), and PtexDict< T >::operator[]().

template<class T>

Entry** PtexDict< T >::_buckets
| private

The pointer to the bucket structure.

Definition at line 287 of file PtexDict.h.

Referenced by PtexDict< PtexReader * >::begin(), PtexDict< T >::clear(), PtexDict< T >::grow(), PtexDict< PtexReader * >::locate(), PtexDict< T >::iterator::operator++(), and PtexDict< T >::operator[]().

template<class T>

int PtexDict< T >::_numBuckets
| private

The number of buckets in use.

Definition at line 285 of file PtexDict.h.

Referenced by PtexDict< PtexReader * >::begin(), PtexDict< T >::clear(), PtexDict< T >::grow(), PtexDict< T >::iterator::operator++(), and PtexDict< T >::operator[]().

template<class T>

int PtexDict< T >::_numEntries
| private

The number of entries in the dictionary.

Definition at line 284 of file PtexDict.h.

Referenced by PtexDict< T >::clear(), PtexDict< T >::erase(), PtexDict< T >::operator[](), and PtexDict< PtexReader * >::size().

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

- PtexDict.h