WvHashTable works a lot like WvLinkList, except it allows for fast indexing of objects in the table (or "dictionary") using the [] operator.
We implement a hash table as a fixed-size array of WvLinkLists. Someday, we might change the implementation to use a self-resizing array instead.
Iterators work with WvHashTable in exactly the same way as with WvLinkList.
WvHashTable usage is described more fully, along with examples, in wvhashtable.h.
You can create a WvHashTable of any data type you want. For example, a WvHashTable of WvString data type can be create like this:
DeclareWvTable(WvString);
This will create a hashtable that will contain data of WvString type. It actually instantiates a WvStringTable class that inherits from a WvHashTable class that handles WvString data types. Or you can just use WvStringTable.h.
You can set the size of your dictionary by invoking the constructor of the WvStringTable class, like this:
WvStringTable t(100);
The WvString hashtable now has 100 slots.
You can create some data of type WvString and put them in the dictionary like this:
t.add(address of data, false);
Note that you have to pass the address of data as the first argument, and not the data itself. Here the second parameter 'false' means that you don't want the data to be automatically destroyed at the end of the program. This is used because some data types like WvString already has the automatic self-destroy feature. For some other data types, you may need to set the second parameter to 'true'.
You may also use
t.append(address of data, false);
or
t.prepend(address of data, false);
to add to the dictionary.
You can use the iterator to go through the hashtable and print it out if you like.
WvStringTable::Iter i(t);
You can also count the number of words in the dictionary with:
t.count();
To find out if a word is already in the dictionary, you can invoke the comparison by using the [] operator. For example, you want to know if the word 'bijoux' exists in the dictionary. So you declare a WvString variable that contains the string 'bijoux', like this:
WvString sample("bijoux");
Then you can compare it like this:
t[sample] returns NULL if 'bijoux' is NOT in the dictionary. t[sample] returns the word that matches 'bijoux' in the dictionary if 'bijoux is in the dictionary.
To remove a word from the dictionary, do this:
t.remove(address of data);
To empty the entire dictionary, do this:
t.zap();
You may want to do an iterator with a sorted list, since it is a linked list. Here is how
WvStringTable::Sorter s(t,);
The following example shows all the mentioned features of a hashtable. Please take a look and try compile and run it to see for yourself.
/* * Worldvisions Weaver Software: * Copyright (C) 1997-2002 Net Integration Technologies, Inc. * * WvHashTable sample program. * Suppose you are learning a new language that is alphabetical and you want * to register all the words you added to your vocabulary in that * language. So you will need a dictionary where you can look up on words * that you have learned. * Assuming that you will not forget any word you had previously learned, * this dictionary shall not contain repetitive words. */ #include "wvhashtable.h" #include "wvstring.h" #include "wvlinklist.h" #include <stdio.h> // Declare a HashTable class that handles WvString data types DeclareWvTable(WvString); /*this subfunction ascending is used for sorting*/ int ascending(const WvString *a, const WvString *b) { return strncasecmp(*a, *b, strlen(a->cstr())); } int main() { // This is a dictionary that can contain at most 10 words WvStringTable t(100); // Here's a list of new words in your vocabulary WvString s1("aussi"), s2("Bonjour"), s3("comment"); WvString s4("demain"), s5("non"), s6("oui"); WvString s7("matin"), s8("bonsoir"), s9("bien"); WvString s10("depanneur"); // Add the list of new words to the dictionary // false = do not autofree the WvString t.add(&s1, false); t.add(&s2, false), t.add(&s3, false); t.add(&s4, false); t.add(&s5, false), t.add(&s6, false); t.add(&s7, false); t.add(&s8, false), t.add(&s9, false), t.add(&s10, false); // Using an iterator, we can print out the entire content of the hashtable printf("What words do we have in the dictionary?\n"); WvStringTable::Iter i(t); for( i.rewind(); i.next(); ) { printf("%s\n", i->cstr() ); } printf("There are %d words stored in the dictionary so far.\n", t.count()); // To look up words in the dictionary, put the WvString data inside the [] // and do the following to print it out WvString sample1("Bonjour"); printf("Is 'Bonjour' in the dictionary? %s\n", t[sample1]?"Yes":"No"); WvString sample2("Salut"); printf("Is 'Salut' in the dictionary? %s\n", t[sample2]?"Yes":"No"); // To remove a word from the dictionary // For example, if you want to remove the word "aussi" in your dictionary t.remove(&s1); // Then print out the list of words in the dictionary again printf("Modified List:\n"); for( i.rewind(); i.next(); ) { printf("%s\n", i->cstr() ); } WvStringTable::Sorter s(t,ascending); printf("Sorted modified List:\n"); for( s.rewind(); s.next(); ) { printf("%s\n", s->cstr() ); } // You can empty the entire dictionary by doing this: t.zap(); // Then print out the list of words in the dictionary again printf("Empty List:\n"); for( i.rewind(); i.next(); ) { printf("%s\n", i->cstr() ); } }
The result is:
What words do we have in the dictionary? matin oui Bonjour bonsoir demain comment non aussi depanneur bien There are 10 words stored in the dictionary so far. Is 'Bonjour' in the dictionary? Yes Is 'Salut' in the dictionary? No Modified List: matin oui Bonjour bonsoir demain comment non depanneur bien Sorted modified List: bien Bonjour bonsoir comment demain depanneur matin non oui Empty List: