Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
hash_example.cc
1  /************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /************************************************************************/
33  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
34  /* Date: Tue Apr 29 1997 */
35  /************************************************************************/
36  /* */
37  /* Example of hash table use. */
38  /* */
39  /************************************************************************/
40 
41 #include <cstdlib>
42 #include <iostream>
43 #include <cmath>
44 #include "EST_THash.h"
45 #include "EST_String.h"
46 
47 // a very boring thing to do to a pair, see map below
48 
49 static void look_at(EST_String &s, int &l)
50 {
51  (void)s;
52  (void)l;
53  cout << ".";
54 }
55 
56 int
57 main(void)
58 {
59 // Map from strings to numbers, hashed by default method.
60 
61 EST_TStringHash<int> lengths(100);
62 
63 lengths.add_item("fred", 4);
64 lengths.add_item("bill", 4);
65 lengths.add_item("harry", 5);
66 
67 const EST_TStringHash<int> const_lengths = lengths;
68 
69 // Map from ints to floats. Note, the other way around is alomst
70 // certainly a mistake.
71 
72 EST_THash<int,float> logs(100);
73 
74 logs.add_item(12, log(12.0));
75 logs.add_item(34, log(34.0));
76 
77 cout << "length of `fred' = " << lengths.val("fred") << "\n";
78 cout << "log of 34' = " << logs.val(34) << "\n";
79 
80 // remove items with the remove_item() method.
81 
82 logs.remove_item(34);
83 
84 cout << "now don't know log of 34' = " << logs.val(34) << "\n";
85 
86 // the second argument to val can be used to
87 // ask whether the result was actually found. Useful
88 // when the dummy value returned for an unknown key is
89 // actually a valid value for the application.
90 
91 int found;
92 float val = logs.val(123, found);
93 
94 cout << "log of 123";
95 
96 if (found)
97  cout << " = " << val;
98 else
99  cout << " not found";
100 
101 cout << "\n";
102 
103 // dump puts out a human readable version of the table for debugging
104 lengths.dump(cout);
105 
106 // map calls a function on each pair, in this case just prints one dot per
107 // pair (see definition of look_at earlier).
108 
109 lengths.map(look_at);
110 cout << "\n";
111 
112 // We can step through the table with an iterator.
113 
115 
116  for(them.begin(const_lengths); them; them++)
117  {
118  cout << them->k << " " << them->v << " ";
119  }
120 
121 cout << "\n";
122 
124 
125  for(rwthem.begin(lengths); rwthem; rwthem++)
126  {
127  rwthem->v *= 3;
128  cout << rwthem->k << " " << rwthem->v << " ";
129  }
130 
131 cout << "\n";
132 
133 
134  return 0;
135 }
136 
137 
138 template <> int EST_THash<int, float>::Dummy_Key = 0;
139 template <> float EST_THash<int, float>::Dummy_Value = 0.0;
140 
141 #if defined(INSTANTIATE_TEMPLATES)
142 #include "../base_class/EST_THash.cc"
143 
144 Instantiate_THash(int,float)
145 
146 #endif