Saturday, November 30, 2013

New Information Dictionary – Implementation

The information dictionary class was changed to a normal class derived from the dictionary class. The constructor is given a pointer to the information class instance created outside of the dictionary.  This pointer is saved in a pointer defined as an abstract information pointer, which can hold any information class pointer derived from the abstract class.

The add routine first adds the dictionary entry by calling the base dictionary class add routine with the token and case sensitivity option along with a pointer to the new entry flag so that it knows if a new entry was added, a removed entry was reused or an entry already exists.  If a new entry was added, an element is added to the additional information by calling the add element interface function of the information instance.  If the entry did not exist, the addition information is set from the token by calling the set element interface function of the information instance.  The index is returned.

The remove routine first removes the reference to the dictionary entry by calling the base dictionary class remove routine for the index specified.  If the entry was removed because it is no longer used, then the additional information for the element is cleared by calling the clear element interface function of the information instance.  The base dictionary class remove routine was modified to return whether  the entry was removed (made available to reused) or not.

The abstract information class defines the interface to the additional information.  The functions are defined as virtual functions with no default functionality so that derived information classes do not implement a function that it does not need.  The constant number and string information classes were changed from holding just a single element to being derived from the abstract class.

The constant number information class contains two vectors for the double and integer values.  The add element function just extends the two vectors by one element.  The set element function copies the token double and integer values into the respective vectors for the element specified.  No clear element function was needed since there is nothing to clear.  Two array access functions were implemented to access the data in the two vectors, which will be used at run-time.

The constant string information class contains a vector of string instance pointers.  The add element function appends a pointer to a newly created string instance to the vector.  The set element function copies the token string into the element specified.  The clear element function clears the string for the element specified.  Once the string instances are created, they will be reused if dictionary entries are removed.  A destructor was implemented to delete all of the string instances.   An array access function was implemented to access the data in the vector, which will be used at run-time.

Information instance pointers were added to the program model class.  These instances are created in the constructor and passed to their associated information dictionaries.  Both the constant number and string dictionaries are now information dictionaries.  The information instances are deleted in the destructor.  There are no longer any known memory issues.

[commit b9772d4149]

Information Dictionary – New Design

The original design of the information dictionary made the assumption that the additional information would be contained in a vector and was given a structure for the information.  The definition was a class template where the information structure was the argument, which was put into the vector, which the information dictionary handled directly.  However, in the case of the constant number dictionary, two vectors are needed so that memory is not wasted (see last post).

The details of the additional information need to be separated from the information dictionary.  In other words, the information dictionary should not know (or assume) that the additional information is a vector.  An abstract information class can be used that defines the interface to the additional information.  The interface requires several functions for accessing the information:
add element - add a new element to the end of the additional information

set element - set an element from information in a token used when a new element is added or an element previously deleted is reused

clear element - clear the contents of an element when the dictionary entry is removed (made available for reuse)
The actual information classes are derived from the abstract class and implement these functions to manipulate their information as required, which could be stored as a vector, two vectors, or something completely different.  The information dictionary has no knowledge of the information class internals and simply uses the interface functions.

The information dictionary class can be a normal class derived from the dictionary class containing a reference to the additional information.  The abstract information interface functions are used to manipulate the additional information.  The information dictionary needs re-implement these functions from the base dictionary class:
add - adds a new dictionary entry and additional information if not already in the dictionary and returns its index

remove - removes the additional information if the dictionary entry was removed