hash_table.h
Go to the documentation of this file.
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
* Copyright (c) 1999-2004 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
/*
* hash.h -- Hash table module.
*
* **********************************************
* CMU ARPA Speech Project
*
* Copyright (c) 1999 Carnegie Mellon University.
* ALL RIGHTS RESERVED.
* **********************************************
*
* HISTORY
* $Log: hash.h,v $
* Revision 1.7 2005/06/22 03:04:01 arthchan2003
* 1, Implemented hash_delete and hash_display, 2, Fixed doxygen documentation, 3, Added keyword.
*
* Revision 1.8 2005/05/24 01:10:54 archan
* Fix a bug when the value only appear in the hash but there is no chain. Also make sure that prev was initialized to NULL. All success cases were tested, but not tested with the deletion is tested.
*
* Revision 1.7 2005/05/24 00:12:31 archan
* Also add function prototype for hash_display in hash.h
*
* Revision 1.4 2005/05/03 04:09:11 archan
* Implemented the heart of word copy search. For every ci-phone, every word end, a tree will be allocated to preserve its pathscore. This is different from 3.5 or below, only the best score for a particular ci-phone, regardless of the word-ends will be preserved at every frame. The graph propagation will not collect unused word tree at this point. srch_WST_propagate_wd_lv2 is also as the most stupid in the century. But well, after all, everything needs a start. I will then really get the results from the search and see how it looks.
*
* Revision 1.3 2005/03/30 01:22:48 archan
* Fixed mistakes in last updates. Add
*
*
* 05-May-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon
* Removed hash_key2hash(). Added hash_enter_bkey() and hash_lookup_bkey(),
* and len attribute to hash_entry_t.
*
* 30-Apr-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon
* Added hash_key2hash().
*
* 18-Jun-97 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon
* Included case sensitive/insensitive option.
*
* 08-31-95 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon
* Created.
*/
#ifndef _LIBUTIL_HASH_H_
#define _LIBUTIL_HASH_H_
/* Win32/WinCE DLL gunk */
#include <sphinxbase/sphinxbase_export.h>
#include <sphinxbase/prim_type.h>
#include <sphinxbase/glist.h>
#ifdef __cplusplus
extern "C" {
#endif
#if 0
/* Fool Emacs. */
}
#endif
typedef struct hash_entry_s {
const char *key;
size_t len;
void *val;
struct hash_entry_s *next;
} hash_entry_t;
typedef struct {
hash_entry_t *table;
int32 size;
int32 inuse;
int32 nocase;
} hash_table_t;
typedef struct hash_iter_s {
hash_table_t *ht;
hash_entry_t *ent;
size_t idx;
} hash_iter_t;
#define hash_entry_val(e) ((e)->val)
#define hash_entry_key(e) ((e)->key)
#define hash_entry_len(e) ((e)->len)
#define hash_table_inuse(h) ((h)->inuse)
#define hash_table_size(h) ((h)->size)
SPHINXBASE_EXPORT
hash_table_t * hash_table_new(int32 size,
int32 casearg
);
#define HASH_CASE_YES 0
#define HASH_CASE_NO 1
SPHINXBASE_EXPORT
void hash_table_free(hash_table_t *h
);
SPHINXBASE_EXPORT
void *hash_table_enter(hash_table_t *h,
const char *key,
void *val
);
#define hash_table_enter_int32(h,k,v) \
((int32)(long)hash_table_enter((h),(k),(void *)(long)(v)))
SPHINXBASE_EXPORT
void *hash_table_replace(hash_table_t *h,
const char *key,
void *val
);
#define hash_table_replace_int32(h,k,v) \
((int32)(long)hash_table_replace((h),(k),(void *)(long)(v)))
SPHINXBASE_EXPORT
void *hash_table_delete(hash_table_t *h,
const char *key
);
SPHINXBASE_EXPORT
void *hash_table_delete_bkey(hash_table_t *h,
const char *key,
size_t len
);
SPHINXBASE_EXPORT
void hash_table_empty(hash_table_t *h
);
SPHINXBASE_EXPORT
void *hash_table_enter_bkey(hash_table_t *h,
const char *key,
size_t len,
void *val
);
#define hash_table_enter_bkey_int32(h,k,l,v) \
((int32)(long)hash_table_enter_bkey((h),(k),(l),(void *)(long)(v)))
SPHINXBASE_EXPORT
void *hash_table_replace_bkey(hash_table_t *h,
const char *key,
size_t len,
void *val
);
#define hash_table_replace_bkey_int32(h,k,l,v) \
((int32)(long)hash_table_replace_bkey((h),(k),(l),(void *)(long)(v)))
SPHINXBASE_EXPORT
int32 hash_table_lookup(hash_table_t *h,
const char *key,
void **val
);
SPHINXBASE_EXPORT
int32 hash_table_lookup_int32(hash_table_t *h,
const char *key,
int32 *val
);
SPHINXBASE_EXPORT
int32 hash_table_lookup_bkey(hash_table_t *h,
const char *key,
size_t len,
void **val
);
SPHINXBASE_EXPORT
int32 hash_table_lookup_bkey_int32(hash_table_t *h,
const char *key,
size_t len,
int32 *val
);
SPHINXBASE_EXPORT
hash_iter_t *hash_table_iter(hash_table_t *h);
SPHINXBASE_EXPORT
hash_iter_t *hash_table_iter_next(hash_iter_t *itor);
SPHINXBASE_EXPORT
void hash_table_iter_free(hash_iter_t *itor);
SPHINXBASE_EXPORT
glist_t hash_table_tolist(hash_table_t *h,
int32 *count
);
SPHINXBASE_EXPORT
void hash_table_display(hash_table_t *h,
int32 showkey
);
#ifdef __cplusplus
}
#endif
#endif
hash_table_delete_bkey
SPHINXBASE_EXPORT void * hash_table_delete_bkey(hash_table_t *h, const char *key, size_t len)
Like hash_table_delete, but with an explicitly specified key length, instead of a NULL-terminated...
Definition: hash_table.c:568
hash_table_lookup_int32
SPHINXBASE_EXPORT int32 hash_table_lookup_int32(hash_table_t *h, const char *key, int32 *val)
Look up a 32-bit integer value in a hash table.
Definition: hash_table.c:329
hash_table_enter_bkey
SPHINXBASE_EXPORT void * hash_table_enter_bkey(hash_table_t *h, const char *key, size_t len, void *val)
Like hash_table_enter, but with an explicitly specified key length, instead of a NULL-terminated, C-style key string.
Definition: hash_table.c:542
hash_iter_s::ent
hash_entry_t * ent
Current entry in that table.
Definition: hash_table.h:170
hash_table_lookup
SPHINXBASE_EXPORT int32 hash_table_lookup(hash_table_t *h, const char *key, void **val)
Look up a key in a hash table and optionally return the associated value.
Definition: hash_table.c:309
hash_table_display
SPHINXBASE_EXPORT void hash_table_display(hash_table_t *h, int32 showkey)
Display a hash-with-chaining representation on the screen.
Definition: hash_table.c:581
hash_table_lookup_bkey
SPHINXBASE_EXPORT int32 hash_table_lookup_bkey(hash_table_t *h, const char *key, size_t len, void **val)
Like hash_lookup, but with an explicitly specified key length, instead of a NULL-terminated, C-style key string.
Definition: hash_table.c:344
hash_table_replace
SPHINXBASE_EXPORT void * hash_table_replace(hash_table_t *h, const char *key, void *val)
Add a new entry with given key and value to hash table h.
Definition: hash_table.c:519
hash_entry_s::next
struct hash_entry_s * next
Value associated with above key.
Definition: hash_table.h:156
hash_table_iter_free
SPHINXBASE_EXPORT void hash_table_iter_free(hash_iter_t *itor)
Delete an unfinished iterator.
Definition: hash_table.c:689
hash_table_tolist
SPHINXBASE_EXPORT glist_t hash_table_tolist(hash_table_t *h, int32 *count)
Build a glist of valid hash_entry_t pointers from the given hash table.
Definition: hash_table.c:623
gnode_s
A node in a generic list.
Definition: glist.h:100
hash_table_iter
SPHINXBASE_EXPORT hash_iter_t * hash_table_iter(hash_table_t *h)
Start iterating over key-value pairs in a hash table.
Definition: hash_table.c:653
prim_type.h
Basic type definitions used in Sphinx.
hash_table_new
SPHINXBASE_EXPORT hash_table_t * hash_table_new(int32 size, int32 casearg)
Allocate a new hash table for a given expected size.
Definition: hash_table.c:158
hash_table_empty
SPHINXBASE_EXPORT void hash_table_empty(hash_table_t *h)
Delete all entries from a hash_table.
Definition: hash_table.c:490
hash_table_free
SPHINXBASE_EXPORT void hash_table_free(hash_table_t *h)
Free the specified hash table; the caller is responsible for freeing the key strings pointed to by th...
Definition: hash_table.c:695
hash_entry_t
struct hash_entry_s hash_entry_t
A note by ARCHAN at 20050510: Technically what we use is so-called "hash table with buckets" which is...
hash_entry_s
A note by ARCHAN at 20050510: Technically what we use is so-called "hash table with buckets" which is...
Definition: hash_table.h:149
hash_table_t
Definition: hash_table.h:159
hash_table_lookup_bkey_int32
SPHINXBASE_EXPORT int32 hash_table_lookup_bkey_int32(hash_table_t *h, const char *key, size_t len, int32 *val)
Look up a 32-bit integer value in a hash table.
Definition: hash_table.c:365
hash_table_t::inuse
int32 inuse
Primary hash table size, (is a prime#); NOTE: This is the number of primary entries ALLOCATED...
Definition: hash_table.h:164
hash_entry_s::len
size_t len
Key string, NULL if this is an empty slot.
Definition: hash_table.h:153
hash_iter_s::idx
size_t idx
Index of next bucket to search.
Definition: hash_table.h:171
hash_iter_s::ht
hash_table_t * ht
Hash table we are iterating over.
Definition: hash_table.h:169
hash_table_t::size
int32 size
Primary hash table, excluding entries that collide.
Definition: hash_table.h:161
hash_iter_s
Definition: hash_table.h:168
glist.h
Generic linked-lists maintenance.
hash_table_enter
SPHINXBASE_EXPORT void * hash_table_enter(hash_table_t *h, const char *key, void *val)
Try to add a new entry with given key and associated value to hash table h.
Definition: hash_table.c:508
hash_table_iter_next
SPHINXBASE_EXPORT hash_iter_t * hash_table_iter_next(hash_iter_t *itor)
Get the next key-value pair in iteration.
Definition: hash_table.c:663
hash_table_delete
SPHINXBASE_EXPORT void * hash_table_delete(hash_table_t *h, const char *key)
Delete an entry with given key and associated value to hash table h.
Definition: hash_table.c:530
hash_entry_s::val
void * val
Key-length; the key string does not have to be a C-style NULL terminated string; it can have arbitrar...
Definition: hash_table.h:155
hash_table_replace_bkey
SPHINXBASE_EXPORT void * hash_table_replace_bkey(hash_table_t *h, const char *key, size_t len, void *val)
Like hash_table_replace, but with an explicitly specified key length, instead of a NULL-terminated...
Definition: hash_table.c:555
hash_table_t::nocase
int32 nocase
Number of valid entries in the table.
Definition: hash_table.h:165