khash.h


Includes: "khash.h", <stdint.h>, <stdlib.h>, <string.h>
Discussion



Generic hash table library.



Functions

__ac_X31_hash_string
const char* hash function

__ac_X31_hash_string


const char* hash function

static inline khint_t __ac_X31_hash_string(
    const char *s) 
Parameters
s
Pointer to a null terminated string
Return Value

The hash value

#defines


kh_int_hash_func


Integer hash function

#define kh_int_hash_func(key)  
Parameters
key
The integer [uint32_t]
Return Value

The hash value [khint_t]


kh_int_hash_equal


Integer comparison function

#define kh_int_hash_equal(a, b)  


kh_int64_hash_func


64-bit integer hash function

#define kh_int64_hash_func(key)  
Parameters
key
The integer [uint64_t]
Return Value

The hash value [khint_t]


kh_int64_hash_equal


64-bit integer comparison function

#define kh_int64_hash_equal(a, b)  


kh_str_hash_func


Another interface to const char* hash function

#define kh_str_hash_func(key) __ac_X31_hash_string(key) 
Parameters
key
Pointer to a null terminated string [const char*]
Return Value

The hash value [khint_t]


kh_str_hash_equal


Const char* comparison function

#define kh_str_hash_equal(a, b)  


khash_t


Type of the hash table.

#define khash_t(name) kh_##name##_t 
Parameters
name
Name of the hash table [symbol]


kh_init


Initiate a hash table.

#define kh_init(name) kh_init_##name() 
Parameters
name
Name of the hash table [symbol]
Return Value

Pointer to the hash table [khash_t(name)*]


kh_destroy


Destroy a hash table.

#define kh_destroy(name, h) kh_destroy_##name(h) 
Parameters
name
Name of the hash table [symbol]
h
Pointer to the hash table [khash_t(name)*]


kh_clear


Reset a hash table without deallocating memory.

#define kh_clear(name, h) kh_clear_##name(h) 
Parameters
name
Name of the hash table [symbol]
h
Pointer to the hash table [khash_t(name)*]


kh_resize


Resize a hash table.

#define kh_resize(name, h, s) kh_resize_##name(h, s) 
Parameters
name
Name of the hash table [symbol]
h
Pointer to the hash table [khash_t(name)*]
s
New size [khint_t]


kh_put


Insert a key to the hash table.

#define kh_put(name, h, k, r) kh_put_##name(h, k, r) 
Parameters
name
Name of the hash table [symbol]
h
Pointer to the hash table [khash_t(name)*]
k
Key [type of keys]
r
Extra return code: 0 if the key is present in the hash table; 1 if the bucket is empty (never used); 2 if the element in the bucket has been deleted [int*]
Return Value

Iterator to the inserted element [khint_t]


kh_get


Retrieve a key from the hash table.

#define kh_get(name, h, k) kh_get_##name(h, k) 
Parameters
name
Name of the hash table [symbol]
h
Pointer to the hash table [khash_t(name)*]
k
Key [type of keys]
Return Value

Iterator to the found element, or kh_end(h) is the element is absent [khint_t]


kh_del


Remove a key from the hash table.

#define kh_del(name, h, k) kh_del_##name(h, k) 
Parameters
name
Name of the hash table [symbol]
h
Pointer to the hash table [khash_t(name)*]
k
Iterator to the element to be deleted [khint_t]


kh_exist


Test whether a bucket contains data.

#define kh_exist(h, x)  
Parameters
h
Pointer to the hash table [khash_t(name)*]
x
Iterator to the bucket [khint_t]
Return Value

1 if containing data; 0 otherwise [int]


kh_key


Get key given an iterator

#define kh_key(h, x)  
Parameters
h
Pointer to the hash table [khash_t(name)*]
x
Iterator to the bucket [khint_t]
Return Value

Key [type of keys]


kh_val


Get value given an iterator

#define kh_val(h, x)  
Parameters
h
Pointer to the hash table [khash_t(name)*]
x
Iterator to the bucket [khint_t]
Return Value

Value [type of values]

Discussion

For hash sets, calling this results in segfault.


kh_value


Alias of kh_val()

#define kh_value(h, x)  


kh_begin


Get the start iterator

#define kh_begin(h)  
Parameters
h
Pointer to the hash table [khash_t(name)*]
Return Value

The start iterator [khint_t]


kh_end


Get the end iterator

#define kh_end(h)  
Parameters
h
Pointer to the hash table [khash_t(name)*]
Return Value

The end iterator [khint_t]


kh_size


Get the number of elements in the hash table

#define kh_size(h)  
Parameters
h
Pointer to the hash table [khash_t(name)*]
Return Value

Number of elements in the hash table [khint_t]


kh_n_buckets


Get the number of buckets in the hash table

#define kh_n_buckets(h)  
Parameters
h
Pointer to the hash table [khash_t(name)*]
Return Value

Number of buckets in the hash table [khint_t]


KHASH_SET_INIT_INT


Instantiate a hash set containing integer keys

#define KHASH_SET_INIT_INT(name) \ 
    KHASH_INIT(name, uint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal) 
Parameters
name
Name of the hash table [symbol]


KHASH_MAP_INIT_INT


Instantiate a hash map containing integer keys

#define KHASH_MAP_INIT_INT(name, khval_t) \ 
    KHASH_INIT(name, uint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal) 
Parameters
name
Name of the hash table [symbol]
khval_t
Type of values [type]


KHASH_SET_INIT_INT64


Instantiate a hash map containing 64-bit integer keys

#define KHASH_SET_INIT_INT64(name) \ 
    KHASH_INIT(name, uint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal) 
Parameters
name
Name of the hash table [symbol]


KHASH_MAP_INIT_INT64


Instantiate a hash map containing 64-bit integer keys

#define KHASH_MAP_INIT_INT64(name, khval_t) \ 
    KHASH_INIT(name, uint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) 
Parameters
name
Name of the hash table [symbol]
khval_t
Type of values [type]


KHASH_SET_INIT_STR


Instantiate a hash map containing const char* keys

#define KHASH_SET_INIT_STR(name) \ 
    KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal) 
Parameters
name
Name of the hash table [symbol]


KHASH_MAP_INIT_STR


Instantiate a hash map containing const char* keys

#define KHASH_MAP_INIT_STR(name, khval_t) \ 
    KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal) 
Parameters
name
Name of the hash table [symbol]
khval_t
Type of values [type]

© Heng Li Last Updated: Tuesday, November 25, 2008