Source of add.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: bool HashedDictionary<KeyType, ValueType>::add(const KeyType& searchKey, const ValueType& newValue)
  6: {
  7:    // Create entry to add to dictionary
  8:    auto entryToAddPtr = std::make_shared<HashedEntry<KeyType, ValueType>>(searchKey, newValue);
  9:    
 10:    // Compute the hashed index into the array
 11:    int hashIndex = getHashIndex(searchKey);
 12:    
 13:    // Add the entry to the chain at hashIndex
 14:    if (hashTable[hashIndex] == nullptr)
 15:    {
 16:       hashTable[hashIndex] = entryToAddPtr;
 17:    }
 18:    else
 19:    {
 20:       entryToAddPtr->setNext(hashTable[hashIndex]);
 21:       hashTable[hashIndex] = entryToAddPtr;
 22:    }  // end if
 23:    
 24:    return true;
 25: }  // end add