A dictionary is a container that acts as an associative array. In other words, a dictionary allows for storing (pointers to) objects according to a string (char*) index.
dictionary<T> d; T* p; char* n; string s; string* sp;
t[n] = p; results in n -> p (for char* n)
t[s] = p; idem: (char*)s -> p (with string)
t[sp] = p; idem: (char*)*s -> p
p = t[n]; retrieval
p = t[s];
p = t[sp];
iter<string> it = t; while ( (sp = it()) ) cout << *t[sp];
interface entry<T> { auxiliary class
T* operator=(T*); for assignment
operator T*(); for retrieval
};
interface dictionary<T> { <hush/dictionary.h>
examples/hush/tutorial/util/dictionary.c
dictionary(); creates empty dictionary
~dictionary(); deletes dictionary
void destroy(); to remov all entries
assignment and retrieval
entry<T>& operator[](char* k);
entry<T>& operator[](string* sp); string& is taken by coercion
T* del(char* k); to delete entry
operator iter<string>&; to obtain iterator for keys (as string)
char* stats(); // to print search statistics
};
The dictionary class provides an interface for the hashtable supported by Tcl. It is a very efficient structure to map strings to (pointers to) objects. Dictionaries are typically used to keep track of objects that have been assigned a name which is used in other parts of the program. Notice that, in line with the hush conventions, the template parameter indicates an object type whereas actually pointers to objects are stored. In a similar fashion, the iterator delivers pointers to objects.
Assignment and retrieval is done by using the indexing operator[] function. For technical reasons an additional template dictionary_entry is needed.
The example below declares a dictionary of handlers and associates two handlers (that is pointers to handler objects) to respectively the strings "handler1" and "handler2". Naturally, names can be arbitray.
handler* h1;
handler* h2;
dictionary<handler> d;
d["handler1"] = h1; // assignment
d["handler2"] = h2;
handler* hp = 0;
hp = d["handler1"]; // retrieval
d.del("handler1"]; // deletion
Note that the template argument for dictionary is handler,
whereas the dictionary actually stores pointers to handlers.
Assignment and retrieval is straigthforward, despite
the (somewhat complex) indirection via
dictionary-entry objects.
For an example of iterating over a dictionary see iter(4).
See also examples/hush/dictionary.c.
hush -- file <hush/dictionary.h>
|
Hush Online Technology
hush@cs.vu.nl
09/24/99 |
|
|