Wednesday, February 11, 2015

Dictionary – Proper Base Class

The Dictionary class was not designed to be an abstract base class in a dictionary class hierarchy.  An abstract base class contains virtual functions, which the Dictionary class did not have.  The four functions, clear, add, remove and string were changed to virtual functions.

The Info Dictionary class is derived from the Dictionary class.  This is an intermediate class used as a base class for the Constant Number Dictionary and Constant String Dictionary classes.  This class implemented its own clear, add and remove.  They did their action before or after calling the base Dictionary class function.  The add and remove functions had different function signatures from the base class, so these needed to be modified to be the same so that they can override the base class functions.

The Dictionary class add function returned the index of the entry and contained an optional argument for returning the entry type (New, Reused or Exists).  Only the Info Dictionary class add function used this argument.  It is not a good practice to use an argument for output.  The return value of the base add function was modified to return a standard pair of the index and entry type, and the optional output argument removed.  The  Info Dictionary class add function signature was made to match and was modified to use the standard pair return value. Instead of the output argument.

The return value of the Dictionary base class remove function was corrected from integer to boolean since that is what type of value was returned.  The Info Dictionary class remove function signature was made to match and changed modified to return a value (previously it did not have a return value), but note that no caller actually uses this return value.

The six table encode functions were modified to use the first value of the pair returned by the add function.  This is temporary until the changes described in the last post are fully implemented.  The dereference operator used with the back insert iterator was removed as it is not necessary since the assignment operator of these iterators is also defined to insert to the back of the container.

A small correction was made to the Dictionary class clear function in how the free stack is cleared.  Previously an empty initializer {} was used.  However, this gave and error with GCC 4.9.2 (the latest available in the tool chains repository, see post on August 5 for how to install or upgrade to GCC 4.9 - substitute "4.9" for "4.8" in the instructions).  The empty initializer was changed to std::stack<uint16_t>{} to eliminate the error, which also works with GCC 4.8 (both versions of GCC will be still supported).

[branch table commit c3f9335697]