Rudiments
Public Member Functions | List of all members
dictionary< keytype, valuetype > Class Template Reference

Public Member Functions

 dictionary ()
 
 dictionary (bool trackinsertionorder)
 
virtual ~dictionary ()
 
bool setTrackInsertionOrder (bool trackinsertionorder)
 
bool getTrackInsertionOrder ()
 
void setValue (keytype key, valuetype value)
 
bool getValue (keytype key, valuetype *value)
 
dictionarynode< keytype, valuetype > * getNode (keytype key)
 
valuetype getValue (keytype key)
 
bool remove (keytype key)
 
bool remove (dictionarynode< keytype, valuetype > *node)
 
linkedlist< keytype > * getKeys ()
 
avltree< dictionarynode< keytype, valuetype > * > * getTree ()
 
linkedlist< dictionarynode< keytype, valuetype > * > * getList ()
 
void clear ()
 
void print ()
 

Detailed Description

template<class keytype, class valuetype>
class dictionary< keytype, valuetype >

The dictionary class allows you to store arbitrary numbers of key/value pairs.

Each dictionary is composed of a set of dictionarynodes. Each dictionarynode contains the key and value.

Internally, the dictionary class uses a linkedlist to store the values though this is potentially inefficient though, and may change in a future version.

Constructor & Destructor Documentation

◆ dictionary() [1/2]

template<class keytype , class valuetype >
dictionary< keytype, valuetype >::dictionary ( )

Creates an empty instance of the dictionary class.

◆ dictionary() [2/2]

template<class keytype , class valuetype >
dictionary< keytype, valuetype >::dictionary ( bool  trackinsertionorder)

Creates an empty instance of the dictionary class.

If "trackinsertionorder" is true then the order of key insertion is tracked and getKeys() and getList() below return a list of key-value pairs in the order that they were inserted.

If "trackinsertionorder" is false then the order of key insertion is not tracked and getList() and getList() below return alist of key-value pairs in ascending order.

◆ ~dictionary()

template<class keytype , class valuetype >
virtual dictionary< keytype, valuetype >::~dictionary ( )
virtual

Deletes this instance of the dictionary class and all of its dictionarynodes. Note however, that neither the key nor value stored in each dictionarynode are deleted by this call.

Member Function Documentation

◆ clear()

template<class keytype , class valuetype >
void dictionary< keytype, valuetype >::clear ( )

Deletes all dictionarynodes currently in the dictionary.

◆ getKeys()

template<class keytype , class valuetype >
linkedlist<keytype>* dictionary< keytype, valuetype >::getKeys ( )

Returns a list of the keys in the dictionary.

Note that the linkedlist returned is allocated internally and returned. The calling program must delete the linkedlist.

◆ getList()

template<class keytype , class valuetype >
linkedlist< dictionarynode<keytype,valuetype> *>* dictionary< keytype, valuetype >::getList ( )

Returns the list used internally.

◆ getNode()

template<class keytype , class valuetype >
dictionarynode<keytype,valuetype>* dictionary< keytype, valuetype >::getNode ( keytype  key)

Returns the node associated with "key" or NULL if "key" wasn't found.

◆ getTrackInsertionOrder()

template<class keytype , class valuetype >
bool dictionary< keytype, valuetype >::getTrackInsertionOrder ( )

Returns "true" if insertion order tracking is enabled and false otherwise.

◆ getTree()

template<class keytype , class valuetype >
avltree< dictionarynode<keytype,valuetype> *>* dictionary< keytype, valuetype >::getTree ( )

Returns the tree used internally.

◆ getValue() [1/2]

template<class keytype , class valuetype >
bool dictionary< keytype, valuetype >::getValue ( keytype  key,
valuetype *  value 
)

Sets "value" to the value associated with "key". Returns true on success or false if "key" wasn't found.

◆ getValue() [2/2]

template<class keytype , class valuetype >
valuetype dictionary< keytype, valuetype >::getValue ( keytype  key)

Returns the value associated with "key" or NULL if "key" wasn't found. Note that there is no way to distinguish between failure to find "key" and a valid value of NULL associated with "key".

◆ print()

template<class keytype , class valuetype >
void dictionary< keytype, valuetype >::print ( )

Prints out a representation of the dictionary.

◆ remove() [1/2]

template<class keytype , class valuetype >
bool dictionary< keytype, valuetype >::remove ( keytype  key)

Removes the dictionarynode associated with "key". Returns true on success or false if "key" wasn't found.

◆ remove() [2/2]

template<class keytype , class valuetype >
bool dictionary< keytype, valuetype >::remove ( dictionarynode< keytype, valuetype > *  node)

Removes the specified dictionarynode. Returns true on success or false if "node" was NULL.

◆ setTrackInsertionOrder()

template<class keytype , class valuetype >
bool dictionary< keytype, valuetype >::setTrackInsertionOrder ( bool  trackinsertionorder)

Sets whether tracking of the order of key insertion is enabled or not.

If "trackinsertionorder" is true then the order of key insertion is tracked and getKeys() and getList() below return a list of key-value pairs in the order that they were inserted.

If "trackinsertionorder" is false then the order of key insertion is not tracked and getList() and getList() below return alist of key-value pairs in ascending order.

Defaults to "true".

Note, this can only be changed when there are no nodes in the dictionary. Eg. before the first call to setValue() or after a call to clear().

Returns true if the call succeeded and false if it failed (eg. because the dictionary contained nodes).

◆ setValue()

template<class keytype , class valuetype >
void dictionary< keytype, valuetype >::setValue ( keytype  key,
valuetype  value 
)

Sets the value associated with "key" to "value". If "key" already exists, the value currently accociated with it is replaced with "value".