c_hash

Table Of Contents

Introduction, c_hash

Like c_map, c_hash is an associative collection class. Unlike c_map, c_hash does not store it's elements pre-sorted. c_hash stores it's elemnts in what appears to be a random order, yet still provides fast key-value insertion, and key-value retreival, In fact, insertion and retrival is significantly faster than with c_map.

To store objects that you have created in a c_hash object, you will need to implment the following member functions for your objects:

Member Function Description
KEY::KEY() a default constructor for KEY
KEY::KEY(const KEY&) a copy constructor for KEY
VALUE::VALUE() a default constructor for VALUE
VALUE::VALUE(const VALUE&) a default constructor for VALUE
KEY::operator=() the assignment operator for KEY
KEY::operator==() the equality operator for KEY
KEY::operator<() the less than operator for KEY
VALUE::operator=() the assignment operator for VALUE
VALUE::operator==() the equality operator for VALUE

As with c_map, c_hash also uses c_map_pair for insertion and retreival. This is so it will conform to the interface for c_collection.

API Refrence
  • c_hash( int size = default[137] )

    This is the default constructor for c_hash. It takes a single argument: an integer to set the size of the internal hash table. This value can be used to tune the performance of the hash object.

  • c_hash( const c_hash<K,V>& that )

    Copy constructor for c_hash. The constructed will become a copy of the object specified by that.

  • ~c_hash( void )

    Destructor for c_hash. It will deallocate all the nodes, freeing all allocated memory.

  • virtual int find( K& key ) const

    Returns true if key exists in the hash table, otherwise it returns FALSE.

  • virtual int find( const K& key, V& value ) const

    Returns true, and sets value equal to the value for key, if key exists in the hash table, otherwise it returns FALSE, and leaves value unmodified.

  • virtual int find( c_map_pair<K,V>& pair ) const

    Returns true, and sets pair.m_value equal to the value for pair.m_key, if pair.m_key exists in the hash table, otherwise it returns FALSE, and leaves pair.m_valuevalue unmodified.

  • virtual int exists( c_map_pair<K,V>& pair ) const

    Returns true if key exists in the hash table, otherwise it returns FALSE.

  • virtual int add( const K& key )

    Adds a new key to the table, leaving the value for the node blank.

  • virtual int add( const K& key V& value )

    Adds a new key to the table, setting the value for the node to the argument given.

  • virtual int add( c_map_pair<K,V>& pair )

    Adds a new key to the table, setting the value for the node to pair.m_value.

  • virtual int get_count( void ) const

    Returns a count of the number of elements in the hash.

  • virtual int remove_all( void )

    Removes all the nodes from the hash table. This frees allocated memory for all the nodes.

  • virtual int clear_values( void )

    Iterates through the entire hash table, setting the value of each node equal to a default VALUE object.

  • virtual c_collection_iter<c_map_pair<K,V>>* prepare_iter( const enum collection_sorting_order ord = raw_order ) const
    virtaul int first( c_collection_iter<c_map_pair<K,V>>* iter, c_map_pair<K,V>& pair ) const
    virtaul int next( c_collection_iter<c_map_pair<K,V>>* iter, c_map_pair<K,V>& pair ) const
    virtaul int prev( c_collection_iter<c_map_pair<K,V>>* iter, c_map_pair<K,V>& pair ) const
    virtaul int last( c_collection_iter<c_map_pair<K,V>>* iter, c_map_pair<K,V>& pair ) const
    virtaul int complete_iter( c_collection_iter<c_map_pair<K,V>>* iter )

    These functions are c_hash's implementation of the c_collection interface.

  • virtual const c_hash<K,V> operator=( const c_hash<K,V>& right )

    Assignment operator for c_hash. The lvalue is emptied, all memory freed, and it then makes itself a copy of the rvalue.

Example Code:
void dea_search_and_seisure( void )
{
  c_person bob("Bob");
  c_hash<c_string,c_string> bobs_car;
  c_string not_here("no drugs"), here_they_are("drugs");

  bobs_car.add( c_string("the radio"),        not_here );
  bobs_car.add( c_string("the trunk"),        not_here );
  bobs_car.add( c_string("the gas tank"),     not_here );
  bobs_car.add( c_string("under the seat"),   not_here );

  if( has_drugs( bobs_car ) ) {
    cout << "you have the right to remain silent..." << endl;
  }
  else {
    cout << "you got lucky...this time." << endl;
  }
}

void plant_evidence( c_hash<c_string,c_string>& vehicle ) 
{
  vehicle.add( c_string("the tires"), c_string("drugs") );
}

int has_drugs( c_hash<c_string,c_string>& vehicle ) 
{
  c_map_pair<c_string,c_string> hiding_place;
  c_string has_drugs("drugs");

  hiding_place.m_key = "the radio";
  vehicle.find( hiding_place );
  if( hiding_place.m_value == drugs ) {
    return TRUE;
  }

  hiding_place.m_key = "under the seat";
  vehicle.find( hiding_place );
  if( hiding_place.m_value == drugs ) {
    return TRUE;
  }

  hiding_place.m_key = "the tires";
  vehicle.find( hiding_place );
  if( hiding_place.m_value == drugs ) {
    return TRUE;
  }

  return FALSE;
}

Up