Edinburgh Speech Tools 2.4-release
 
Loading...
Searching...
No Matches
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
49static void look_at(EST_String &s, int &l)
50{
51 (void)s;
52 (void)l;
53 cout << ".";
54}
55
56int
57main(void)
58{
59// Map from strings to numbers, hashed by default method.
60
62
63lengths.add_item("fred", 4);
64lengths.add_item("bill", 4);
65lengths.add_item("harry", 5);
66
68
69// Map from ints to floats. Note, the other way around is alomst
70// certainly a mistake.
71
73
74logs.add_item(12, log(12.0));
75logs.add_item(34, log(34.0));
76
77cout << "length of `fred' = " << lengths.val("fred") << "\n";
78cout << "log of 34' = " << logs.val(34) << "\n";
79
80// remove items with the remove_item() method.
81
82logs.remove_item(34);
83
84cout << "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
91int found;
92float val = logs.val(123, found);
93
94cout << "log of 123";
95
96if (found)
97 cout << " = " << val;
98else
99 cout << " not found";
100
101cout << "\n";
102
103// dump puts out a human readable version of the table for debugging
104lengths.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
109lengths.map(look_at);
110cout << "\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
121cout << "\n";
122
124
125 for(rwthem.begin(lengths); rwthem; rwthem++)
126 {
127 rwthem->v *= 3;
128 cout << rwthem->k << " " << rwthem->v << " ";
129 }
130
131cout << "\n";
132
133
134 return 0;
135}
136
137
138template <> int EST_THash<int, float>::Dummy_Key = 0;
139template <> float EST_THash<int, float>::Dummy_Value = 0.0;
140
141#if defined(INSTANTIATE_TEMPLATES)
142#include "../base_class/EST_THash.cc"
143
144Instantiate_THash(int,float)
145
146#endif
K k
The key.
Definition EST_THash.h:78
V v
The value.
Definition EST_THash.h:80