Source of add.cpp


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

  4: // One definition:
  5: template <class KeyType, class ValueType>
  6: bool TreeDictionary<KeyType, ValueType>::add(const KeyType& searchKey,
  7:                                              const ValueType& newValue)
  8:                                              throw(PrecondViolatedExcept)
  9: {
 10:    return itemTree.add(Entry<KeyType, ValueType>(searchKey, newValue));
 11: }  // end add

 13: // Another definition:
 14: template <class KeyType, class ValueType>
 15: bool TreeDictionary<KeyType, ValueType>::add(const KeyType& searchKey,
 16:                                              const ValueType& newValue)
 17:                                              throw(PrecondViolatedExcept)
 18: {
 19:    Entry<KeyType, ValueType> newEntry(searchKey, newValue);
 20:    
 21:    // Enforce precondition: Ensure distinct search keys
 22:    if (!itemTree.contains(newEntry))
 23:    {
 24:       // Add new entry and return boolean result
 25:       return itemTree.add(Entry<KeyType, ValueType>(searchKey, newValue));
 26:    }
 27:    else
 28:    {
 29:       auto message = "Attempt to add an entry whose search key exists in dictionary.";
 30:       throw(PrecondViolatedExcept(message)); // Exit the method
 31:    }  // end if
 32: }  // end add