WvStreams
wvhashtableex.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * WvHashTable sample program.
6  * Suppose you are learning a new language that is alphabetical and you want
7  * to register all the words you added to your vocabulary in that
8  * language. So you will need a dictionary where you can look up on words
9  * that you have learned.
10  * Assuming that you will not forget any word you had previously learned,
11  * this dictionary shall not contain repetitive words.
12  */
13 
14 #include "wvhashtable.h"
15 #include "wvstring.h"
16 #include "wvlinklist.h"
17 #include <stdio.h>
18 
19 // Declare a HashTable class that handles WvString data types
20 DeclareWvTable(WvString);
21 
22 /*this subfunction ascending is used for sorting*/
23 int ascending(const WvString *a, const WvString *b)
24 {
25  return strncasecmp(*a, *b, strlen(a->cstr()));
26 }
27 
28 
29 int main()
30 {
31  // This is a dictionary that can contain at most 10 words
32  WvStringTable t(100);
33 
34  // Here's a list of new words in your vocabulary
35  WvString s1("aussi"), s2("Bonjour"), s3("comment");
36  WvString s4("demain"), s5("non"), s6("oui");
37  WvString s7("matin"), s8("bonsoir"), s9("bien");
38  WvString s10("depanneur");
39 
40  // Add the list of new words to the dictionary
41  // false = do not autofree the WvString
42  t.add(&s1, false); t.add(&s2, false), t.add(&s3, false);
43  t.add(&s4, false); t.add(&s5, false), t.add(&s6, false);
44  t.add(&s7, false); t.add(&s8, false), t.add(&s9, false), t.add(&s10, false);
45 
46  // Using an iterator, we can print out the entire content of the hashtable
47  printf("What words do we have in the dictionary?\n");
48  WvStringTable::Iter i(t);
49  for( i.rewind(); i.next(); )
50  {
51  printf("%s\n", i->cstr() );
52  }
53 
54  printf("There are %d words stored in the dictionary so far.\n", t.count());
55 
56  // To look up words in the dictionary, put the WvString data inside the []
57  // and do the following to print it out
58 
59  WvString sample1("Bonjour");
60  printf("Is 'Bonjour' in the dictionary? %s\n", t[sample1]?"Yes":"No");
61  WvString sample2("Salut");
62  printf("Is 'Salut' in the dictionary? %s\n", t[sample2]?"Yes":"No");
63 
64  // To remove a word from the dictionary
65  // For example, if you want to remove the word "aussi" in your dictionary
66  t.remove(&s1);
67 
68  // Then print out the list of words in the dictionary again
69  printf("Modified List:\n");
70  for( i.rewind(); i.next(); )
71  {
72  printf("%s\n", i->cstr() );
73  }
74 
75  WvStringTable::Sorter s(t,ascending);
76  printf("Sorted modified List:\n");
77  for( s.rewind(); s.next(); )
78  {
79  printf("%s\n", s->cstr() );
80  }
81 
82 
83  // You can empty the entire dictionary by doing this:
84  t.zap();
85 
86  // Then print out the list of words in the dictionary again
87  printf("Empty List:\n");
88  for( i.rewind(); i.next(); )
89  {
90  printf("%s\n", i->cstr() );
91  }
92 
93 }
94 
WvStringTable
Definition: wvstringtable.h:17
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvFastString::cstr
const char * cstr() const
return a (const char *) for this string.
Definition: wvstring.h:267