Edinburgh Speech Tools  2.1-release
hash_example.cc
Go to the documentation of this file.
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 using namespace std;
48 
49 // a very boring thing to do to a pair, see map below
50 
51 static void look_at(EST_String &s, int &l)
52 {
53  (void)s;
54  (void)l;
55  cout << ".";
56 }
57 
58 int
59 main(void)
60 {
61 // Map from strings to numbers, hashed by default method.
62 
63 EST_TStringHash<int> lengths(100);
64 
65 lengths.add_item("fred", 4);
66 lengths.add_item("bill", 4);
67 lengths.add_item("harry", 5);
68 
69 const EST_TStringHash<int> const_lengths = lengths;
70 
71 // Map from ints to floats. Note, the other way around is alomst
72 // certainly a mistake.
73 
74 EST_THash<int,float> logs(100);
75 
76 logs.add_item(12, log(12.0));
77 logs.add_item(34, log(34.0));
78 
79 cout << "length of `fred' = " << lengths.val("fred") << "\n";
80 cout << "log of 34' = " << logs.val(34) << "\n";
81 
82 // remove items with the remove_item() method.
83 
84 logs.remove_item(34);
85 
86 cout << "now don't know log of 34' = " << logs.val(34) << "\n";
87 
88 // the second argument to val can be used to
89 // ask whether the result was actually found. Useful
90 // when the dummy value returned for an unknown key is
91 // actually a valid value for the application.
92 
93 int found;
94 float val = logs.val(123, found);
95 
96 cout << "log of 123";
97 
98 if (found)
99  cout << " = " << val;
100 else
101  cout << " not found";
102 
103 cout << "\n";
104 
105 // dump puts out a human readable version of the table for debugging
106 lengths.dump(cout);
107 
108 // map calls a function on each pair, in this case just prints one dot per
109 // pair (see definition of look_at earlier).
110 
111 lengths.map(look_at);
112 cout << "\n";
113 
114 // We can step through the table with an iterator.
115 
117 
118  for(them.begin(const_lengths); them; them++)
119  {
120  cout << them->k << " " << them->v << " ";
121  }
122 
123 cout << "\n";
124 
126 
127  for(rwthem.begin(lengths); rwthem; rwthem++)
128  {
129  rwthem->v *= 3;
130  cout << rwthem->k << " " << rwthem->v << " ";
131  }
132 
133 cout << "\n";
134 
135 
136  return 0;
137 }
138 
139 
140 template <> int EST_THash<int, float>::Dummy_Key = 0;
141 template <> float EST_THash<int, float>::Dummy_Value = 0.0;
142 
143 #if defined(INSTANTIATE_TEMPLATES)
144 #include "../base_class/EST_THash.cc"
145 
146 Instantiate_THash(int,float)
147 
148 #endif
#define Instantiate_THash(KEY, VAL)
Definition: EST_THashI.h:73
void map(void(*func)(EST_String &, V &))
Apply func to each entry in the table.
A specialised hash table for when the key is an EST_String.
Definition: EST_THash.h:304
void begin(Container &over)
Set the iterator ready to run over this container.
int remove_item(const K &rkey, int quiet=0)
Remove an entry from the table.
Definition: EST_THash.cc:195
V & val(const EST_String &key, int &found) const
int main(void)
Definition: hash_example.cc:59
int add_item(const EST_String &key, const V &value, int no_search=0)
Add an entry to the table.
void begin(const Container &over)
Set the iterator ready to run over this container.
An open hash table. The number of buckets should be set to allow enough space that there are relative...
Definition: EST_THash.h:69
void dump(ostream &stream, int all=0)
Print the table to stream in a human readable format.