lab_hash
Hellish Hash Tables
 All Classes Namespaces Files Functions Variables Pages
LPHashTable< K, V > Class Template Reference

LPHashTable: a HashTable implementation that uses linear probing as a collision resolution strategy. More...

#include <lphashtable.h>

Inheritance diagram for LPHashTable< K, V >:
[legend]
Collaboration diagram for LPHashTable< K, V >:
[legend]

Public Types

typedef HashTable< K, V >::iterator iterator
 

Public Member Functions

 LPHashTable (size_t tsize)
 Constructs a LPHashTable of the given size. More...
 
virtual ~LPHashTable ()
 Destructor for the LPHashTable. More...
 
const LPHashTable< K, V > & operator= (const LPHashTable< K, V > &rhs)
 Assignment operator. More...
 
 LPHashTable (const LPHashTable< K, V > &other)
 Copy constructor. More...
 
virtual void insert (const K &key, const V &value)
 Inserts the given key, value pair into the HashTable. More...
 
virtual void remove (const K &key)
 Removes the given key (and its associated data) from the HashTable. More...
 
virtual V find (const K &key) const
 Finds the value associated with a given key. More...
 
virtual bool keyExists (const K &key) const
 Determines if the given key exists in the HashTable. More...
 
virtual void clear ()
 Empties the HashTable (that is, all keys and values are removed). More...
 
virtual V & operator[] (const K &key)
 Access operator: Returns a reference to a value in the HashTable, so that it may be modified. More...
 
iterator begin () const
 Returns an iterator to the beginning of the HashTable. More...
 
iterator end () const
 Returns an iterator to the end of the HashTable. More...
 
- Public Member Functions inherited from HashTable< K, V >
virtual ~HashTable ()
 Destructor for a HashTable: made virtual to allow overloading in derived classes. More...
 
virtual bool isEmpty () const
 Determines if the HashTable is empty. More...
 
virtual size_t tableSize () const
 Used to determine the total size of the HashTable. More...
 

Private Member Functions

int findIndex (const K &key) const
 Helper function to determine the index where a given key lies in the LPHashTable. More...
 
virtual void resizeTable ()
 Private helper function to resize the HashTable. More...
 

Private Attributes

std::pair< K, V > ** table
 Storage for our LPHashTable. More...
 
bool * should_probe
 Flags for whether or not to probe forward when looking at a particular cell in the table. More...
 

Additional Inherited Members

- Protected Member Functions inherited from HashTable< K, V >
iterator makeIterator (HTIteratorImpl *impl) const
 Implementation for our iterator. More...
 
bool shouldResize () const
 Determines if the HashTable should resize. More...
 
size_t findPrime (size_t num)
 Finds the closest prime in our list to the given number. More...
 
- Protected Attributes inherited from HashTable< K, V >
size_t elems
 The current number of elements stored in the HashTable. More...
 
size_t size
 The current size of the HashTable (total cells). More...
 
- Static Protected Attributes inherited from HashTable< K, V >
static const size_t primes []
 List of primes for resizing. More...
 

Detailed Description

template<class K, class V>
class LPHashTable< K, V >

LPHashTable: a HashTable implementation that uses linear probing as a collision resolution strategy.

Author
Chase Geigle
Date
Spring 2011
Summer 2012

Constructor & Destructor Documentation

template<class K , class V >
LPHashTable< K, V >::LPHashTable ( size_t  tsize)

Constructs a LPHashTable of the given size.

Parameters
tsizeThe desired number of starting cells in the LPHashTable.
template<class K , class V >
LPHashTable< K, V >::~LPHashTable ( )
virtual

Destructor for the LPHashTable.

We use dynamic memory, and thus require the big three.

template<class K , class V >
LPHashTable< K, V >::LPHashTable ( const LPHashTable< K, V > &  other)

Copy constructor.

Parameters
otherThe LPHashTable to be copied.

Member Function Documentation

template<class K , class V >
iterator LPHashTable< K, V >::begin ( ) const
inlinevirtual

Returns an iterator to the beginning of the HashTable.

Returns
An iterator to the beginning of the HashTable.

Implements HashTable< K, V >.

template<class K , class V >
void LPHashTable< K, V >::clear ( )
virtual

Empties the HashTable (that is, all keys and values are removed).

Implements HashTable< K, V >.

template<class K , class V >
iterator LPHashTable< K, V >::end ( ) const
inlinevirtual

Returns an iterator to the end of the HashTable.

Note that this is essentially like returning an index to one past the final element in an array (that is, end() itself gives an iterator to one past the last thing in the HashTable).

Returns
An iterator to the end of the HashTable.

Implements HashTable< K, V >.

template<class K , class V >
V LPHashTable< K, V >::find ( const K &  key) const
virtual

Finds the value associated with a given key.

Parameters
keyThe key whose data we want to find.
Returns
The value associated with this key, or the default value (V()) if it was not found.

Implements HashTable< K, V >.

template<class K , class V >
int LPHashTable< K, V >::findIndex ( const K &  key) const
private

Helper function to determine the index where a given key lies in the LPHashTable.

If the key does not exist in the table, it will return -1.

Parameters
keyThe key to look for.
Returns
The index of this key, or -1 if it was not found.
Todo:
Implement this function

Be careful in determining when the key is not in the table!

template<class K , class V >
void LPHashTable< K, V >::insert ( const K &  key,
const V &  value 
)
virtual

Inserts the given key, value pair into the HashTable.

Parameters
keyThe key to be inserted.
valueThe value to be inserted.
Todo:
Implement this function.
Note
Remember to resize the table when necessary (load factor >= 0.7). Do this check after increasing elems (but before inserting)!! Also, don't forget to mark the cell for probing with should_probe!

Implements HashTable< K, V >.

template<class K , class V >
bool LPHashTable< K, V >::keyExists ( const K &  key) const
virtual

Determines if the given key exists in the HashTable.

Parameters
keyThe key we want to find.
Returns
A boolean value indicating whether the key was found in the HashTable.

Implements HashTable< K, V >.

template<class K , class V >
LPHashTable< K, V > const & LPHashTable< K, V >::operator= ( const LPHashTable< K, V > &  rhs)

Assignment operator.

Parameters
rhsThe LPHashTable we want to assign into the current one.
Returns
A const reference to the current LPHashTable.
template<class K , class V >
V & LPHashTable< K, V >::operator[] ( const K &  key)
virtual

Access operator: Returns a reference to a value in the HashTable, so that it may be modified.

If the key you are searching for is not found in the table, this method inserts it with the default value V() (which you then may modify).

Examples:

hashtable["mykey"]; // returns the value for "mykey", or the
                    // default value if "mykey" is not in
                    // the table

hashtable["myOtherKey"] = "myNewValue";
Parameters
keyThe key to be found in the HashTable.
Returns
A reference to the value for this key contained in the table.

Implements HashTable< K, V >.

template<class K , class V >
void LPHashTable< K, V >::remove ( const K &  key)
virtual

Removes the given key (and its associated data) from the HashTable.

Parameters
keyThe key to be removed.
Todo:
: implement this function

Implements HashTable< K, V >.

template<class K , class V >
void LPHashTable< K, V >::resizeTable ( )
privatevirtual

Private helper function to resize the HashTable.

This should be called when the load factor is >= 0.7 (this is somewhat arbitrary, but used for grading).

Todo:
Implement this function

The size of the table should be the closest prime to size * 2.

Use findPrime()!

Implements HashTable< K, V >.

Member Data Documentation

template<class K , class V >
bool* LPHashTable< K, V >::should_probe
private

Flags for whether or not to probe forward when looking at a particular cell in the table.

This is a dynamic array of booleans that represents if a slot is (or previously was) occupied. This allows us determine whether or not we need to probe forward to look for our key.

template<class K , class V >
std::pair<K, V>** LPHashTable< K, V >::table
private

Storage for our LPHashTable.

With linear probing, we only need the array, not buckets for each array index. Note that we use an array of pointers to pairs in this case since the check for an "empty" slot is simply a check against NULL in that cell.


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