/usr/include/speech_tools/EST_THash.h is in libestools2.1-dev 1:2.1~release-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /************************************************************************/
/* */
/* Centre for Speech Technology Research */
/* University of Edinburgh, UK */
/* Copyright (c) 1996,1997 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/************************************************************************/
/* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
/************************************************************************/
#ifndef __EST_THASH_H__
#define __EST_THASH_H__
#include <iostream>
using namespace std;
#include "EST_String.h"
#include "EST_system.h"
#include "EST_bool.h"
#include "EST_TIterator.h"
#include "instantiate/EST_THashI.h"
#include "instantiate/EST_TStringHashI.h"
/**@name Hash Tables
*
* @author Richard Caley <rjc@cstr.ed.ac.uk>
* @version $Id: EST_THash.h,v 1.6 2004/09/29 08:24:17 robert Exp $
*/
//@{
/** This is just a convenient place to put some useful hash functions.
*/
class EST_HashFunctions {
public:
/// A generally useful hash function.
static unsigned int DefaultHash(const void *data, size_t size, unsigned int n);
/// A hash function for strings.
static unsigned int StringHash(const EST_String &key, unsigned int size);
};
template<class K, class V> class EST_THash;
/** This class is used in hash tables to hold a key value pair.
* Not much to say beyond that.
*/
template<class K, class V>
class EST_Hash_Pair {
public:
/// The key
K k;
/// The value
V v;
private:
/// Pointer used to chain entries into buckets.
EST_Hash_Pair<K,V> *next;
/// The hash table must be a friend so it can see the pointer.
friend class EST_THash<K, V>;
};
/** An open hash table. The number of buckets should be set to allow
* enough space that there are relatively few entries per bucket on
* average.
*/
template<class K, class V>
class EST_THash : protected EST_HashFunctions {
private:
/// Something to return when there is no entry in the table.
static V Dummy_Value;
static K Dummy_Key;
/// Total number of entries.
unsigned int p_num_entries;
/// Number of buckets.
unsigned int p_num_buckets;
/// Pointer to an array of <variable>p_num_buckets</variable>buckets.
EST_Hash_Pair<K,V> **p_buckets;
/// The hash function to use on this table.
unsigned int (*p_hash_function)(const K &key, unsigned int size);
public:
/** Create a table with the given number of buckets. Optionally setting
* a custom hash function.
*/
EST_THash(int size,
unsigned int (*hash_function)(const K &key, unsigned int size)= NULL);
/// Create a copy
EST_THash(const EST_THash<K,V> &from);
/// Destroy the table.
~EST_THash(void);
/// Empty the table.
void clear(void);
/// Return the total number of entries in the table.
unsigned int num_entries(void) const
{ return p_num_entries; };
/// Does the key have an entry?
int present(const K &key) const;
/** Return the value associated with the key.
* <parameter>found</parameter> is set to whether such an entry was found.
*/
V &val(const K &key, int &found) const;
/// Return the value associated with the key.
V &val(const K &key) const {int x; return val(key, x); }
const K &key(const V &val, int &found) const;
const K &key(const V &val) const {int x; return key(val, x); }
/// Copy all entries
void copy(const EST_THash<K,V> &from);
/// Apply <parameter>func</parameter> to each entry in the table.
void map(void (*func)(K&, V&));
/// Add an entry to the table.
int add_item(const K &key, const V &value, int no_search = 0);
/// Remove an entry from the table.
int remove_item(const K &rkey, int quiet = 0);
/// Assignment is a copy operation
EST_THash<K,V> &operator = (const EST_THash<K,V> &from);
/// Print the table to <parameter>stream</parameter> in a human readable format.
void dump(ostream &stream, int all=0);
/**@name Pair Iteration
*
* This iterator steps through the table returning key-value pairs.
*/
//@{
protected:
/** A position in the table is given by a bucket number and a
* pointer into the bucket.
*/
// struct IPointer{ unsigned int b; EST_Hash_Pair<K, V> *p; };
struct IPointer_s{ unsigned int b; EST_Hash_Pair<K, V> *p; };
typedef struct IPointer_s IPointer;
/// Shift to point at something.
void skip_blank(IPointer &ip) const
{
while (ip.p==NULL && ip.b<p_num_buckets)
{ip.b++; ip.p = ip.b<p_num_buckets?p_buckets[ip.b]:0; }
}
/// Go to start of the table.
void point_to_first(IPointer &ip) const
{ ip.b=0; ip.p = ip.b<p_num_buckets?p_buckets[ip.b]:0;
skip_blank(ip);}
/// Move pointer forwards, at the end of the bucket, move down.
void move_pointer_forwards(IPointer &ip) const
{
ip.p = ip.p->next;
skip_blank(ip);
}
/// We are at the end if the pointer ever becomes NULL
bool points_to_something(const IPointer &ip) const { return ip.b<p_num_buckets; }
/// Return the contents of this entry.
EST_Hash_Pair<K, V> &points_at(const IPointer &ip) { return *(ip.p); }
/// The iterator must be a friend to access this private interface.
friend class EST_TStructIterator< EST_THash<K, V>, IPointer, EST_Hash_Pair<K, V> >;
friend class EST_TRwStructIterator< EST_THash<K, V>, IPointer, EST_Hash_Pair<K, V> >;
friend class EST_TIterator< EST_THash<K, V>, IPointer, EST_Hash_Pair<K, V> >;
friend class EST_TRwIterator< EST_THash<K, V>, IPointer, EST_Hash_Pair<K, V> >;
public:
/// An entry returned by the iterator is a key value pair.
typedef EST_Hash_Pair<K, V> Entry;
/// Give the iterator a sensible name.
typedef EST_TStructIterator< EST_THash<K, V>, IPointer, EST_Hash_Pair<K, V> > Entries;
typedef EST_TRwStructIterator< EST_THash<K, V>, IPointer, EST_Hash_Pair<K, V> > RwEntries;
//@}
/**@name Key Iteration
*
* This iterator steps through the table returning just keys.
*/
//@{
protected:
/** A position in the table is given by a bucket number and a
* pointer into the bucket.
*/
struct IPointer_k_s { unsigned int b; EST_Hash_Pair<K, V> *p; };
typedef struct IPointer_k_s IPointer_k;
/// Shift to point at something.
void skip_blank(IPointer_k &ip) const
{
while (ip.p==NULL && ip.b<p_num_buckets)
{ip.b++; ip.p = ip.b<p_num_buckets?p_buckets[ip.b]:0; }
}
/// Go to start of the table.
void point_to_first(IPointer_k &ip) const
{ ip.b=0; ip.p = ip.b<p_num_buckets?p_buckets[ip.b]:0;
skip_blank(ip);}
/// Move pointer forwards, at the end of the bucket, move down.
void move_pointer_forwards(IPointer_k &ip) const
{
ip.p = ip.p->next;
skip_blank(ip);
}
/// We are at the end if the pointer ever becomes NULL
bool points_to_something(const IPointer_k &ip) const { return ip.b<p_num_buckets; }
/// Return the key of this entry.
K &points_at(const IPointer_k &ip) { return (ip.p)->k; }
/// The iterator must be a friend to access this private interface.
friend class EST_TIterator< EST_THash<K, V>, IPointer_k, K >;
friend class EST_TRwIterator< EST_THash<K, V>, IPointer_k, K >;
public:
/// An entry returned by this iterator is just a key.
typedef K KeyEntry;
/// Give the iterator a sensible name.
typedef EST_TIterator< EST_THash<K, V>, IPointer_k, K > KeyEntries;
typedef EST_TRwIterator< EST_THash<K, V>, IPointer_k, K > KeyRwEntries;
//@}
};
/** A specialised hash table for when the key is an EST_String.
*
* This is just a version of <classname>EST_THash</classname> which
* has a different default hash function.
*/
template<class V>
class EST_TStringHash : public EST_THash<EST_String, V> {
public:
/// Create a string hash table of <parameter>size</parameter> buckets.
EST_TStringHash(int size) : EST_THash<EST_String, V>(size, EST_HashFunctions::StringHash) {};
/// An entry returned by the iterator is a key value pair.
typedef EST_Hash_Pair<EST_String, V> Entry;
/* struct IPointer_s{ unsigned int b; Entry *p; };
typedef struct IPointer_s IPointer; */
// Convince GCC that the IPointer we're going to use is a typename
typedef typename EST_THash<EST_String, V>::IPointer TN_IPointer;
/// Give the iterator a sensible name.
typedef EST_TStructIterator< EST_THash<EST_String, V>, typename EST_THash<EST_String, V>::IPointer,
EST_Hash_Pair<EST_String, V> > Entries;
typedef EST_TRwStructIterator< EST_THash<EST_String, V>, typename EST_THash<EST_String, V>::IPointer,
EST_Hash_Pair<EST_String, V> > RwEntries;
//@}
typedef EST_String KeyEntry;
/* struct IPointer_k_s { unsigned int b; EST_Hash_Pair<EST_String, V> *p; };
typedef struct IPointer_k_s IPointer_k; */
/// Give the iterator a sensible name.
// Convince GCC that the IPointer_k we're going to use is a typename
typedef typename EST_THash<EST_String, V>::IPointer_k TN_IPointer_k;
typedef EST_TIterator< EST_THash<EST_String, V>, typename EST_THash<EST_String, V>::IPointer_k,
EST_String > KeyEntries;
typedef EST_TRwIterator< EST_THash<EST_String, V>, typename EST_THash<EST_String, V>::IPointer_k,
EST_String > KeyRwEntries;
};
/** The default hash function used by <classname>EST_THash</classname>
*/
inline static unsigned int DefaultHashFunction(const void *data, size_t size, unsigned int n)
{
unsigned int x=0;
const char *p = (const char *)data;
for(; size>0 ; p++, size--)
x = ((x+*p)*33) % n;
return x;
}
//@}
#endif
|