Source of getHashIndex.cpp


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: template <class KeyType, class ValueType>
  5: int HashedDictionary<KeyType, ValueType>::getHashIndex(const KeyType& searchKey) const
  6: {
  7:    // We are creating a hash function type called hashFunction that hashes
  8:    // a search key. First we create an unordered_map object for our KeyType
  9:    // and ValueType.
 10:    std::unordered_map<KeyType, ValueType> mapper;
 11:    
 12:    // Then we invoke the method hash_function to return the hash function
 13:    // for the KeyType and assign it to 'hashFunction'.
 14:    typename std::unordered_map<KeyType, ValueType>::hasher hashFunction = mapper.hash_function();
 15:    
 16:    // Need static_cast because hashFunction returns an unsigned long.
 17:    return static_cast<int>(hashFunction(searchKey) % hashTableSize);
 18: }  // end getHashIndex