#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "hash_table.h"
#include "err.h"
#include "ckd_alloc.h"
#include "case.h"
void* hash_table_delete | ( | hash_table_t * | h, | |
const char * | key | |||
) |
Delete an entry with given key and associated value to hash table h. Return the value associated with the key (NULL if it did not exist)
h | In: Handle of hash table in which a key will be deleted | |
key | In: C-style NULL-terminated key string for the new entry |
void hash_table_display | ( | hash_table_t * | h, | |
int32 | showkey | |||
) |
Display a hash-with-chaining representation on the screen. Currently, it will only works for situation where hash_enter was used to enter the keys.
h | In: Hash table to display | |
showdisplay | In: Show the string or not, Use 0 if hash_enter_bkey was used. |
void hash_table_empty | ( | hash_table_t * | h | ) |
Delete all entries from a hash_table.
h | In: Handle of hash table |
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. If key doesn't already exist in hash table, the addition is successful, and the return value is val. But if key already exists, return its existing associated value. (The hash table is unchanged; it is up to the caller to resolve the conflict.)
h | In: Handle of hash table in which to create entry | |
key | In: C-style NULL-terminated key string for the new entry | |
val | In: Value to be associated with above key |
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. So the key string is a binary key (or bkey). Hash tables containing such keys should be created with the HASH_CASE_YES option. Otherwise, the results are unpredictable.
h | In: Handle of hash table in which to create entry | |
key | In: Key buffer | |
len | In: Length of above key buffer | |
val | In: Value to be associated with above key |
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 the table entries.
h | In: Handle of hash table to free |
hash_iter_t* hash_table_iter | ( | hash_table_t * | h | ) |
Start iterating over key-value pairs in a hash table.
void hash_table_iter_free | ( | hash_iter_t * | itor | ) |
Delete an unfinished iterator.
hash_iter_t* hash_table_iter_next | ( | hash_iter_t * | itor | ) |
Get the next key-value pair in iteration.
This function automatically frees the iterator object upon reaching the final entry.
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.
h | In: Handle of hash table being searched | |
key | In: C-style NULL-terminated string whose value is sought | |
val | Out: *val = value associated with key. If this is NULL, no value will be returned. |
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. So the key string is a binary key (or bkey). Hash tables containing such keys should be created with the HASH_CASE_YES option. Otherwise, the results are unpredictable.
h | In: Handle of hash table being searched | |
key | In: Key buffer | |
len | In: Length of above key buffer | |
val | Out: *val = value associated with key. If this is NULL, no value will be returned. |
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.
This function is the clean way to do this and avoid compiler warnings on 64-bit platforms.
h | In: Handle of hash table being searched | |
key | In: Key buffer | |
len | In: Length of above key buffer | |
val | Out: *val = value associated with key. If this is NULL, no value will be returned. |
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.
This function is the clean way to do this and avoid compiler warnings on 64-bit platforms.
h | In: Handle of hash table being searched | |
key | In: C-style NULL-terminated string whose value is sought | |
val | Out: *val = value associated with key. If this is NULL, no value will be returned. |
hash_table_t* hash_table_new | ( | int32 | size, | |
int32 | casearg | |||
) |
Allocate a new hash table for a given expected size.
size | In: Expected number of entries in the table | |
casearg | In: Whether case insensitive for key comparisons. When 1, case is insentitive, 0, case is sensitive. |
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. If the key already exists, its value is replaced with the given value, and the previous value is returned, otherwise val is returned.
A very important but subtle point: The key pointer in the hash table is replaced with the pointer passed to this function. In general you should always pass a pointer to hash_table_enter() whose lifetime matches or exceeds that of the hash table. In some rare cases it is convenient to initially enter a value with a short-lived key, then later replace that with a long-lived one. This behaviour allows this to happen.
h | In: Handle of hash table in which to create entry | |
key | In: C-style NULL-terminated key string for the new entry | |
val | In: Value to be associated with above key |
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. Return the list.
h | In: Hash table from which list is to be generated | |
count | Out: Number of entries in the list. If this is NULL, no count will be returned. |
{ 101, 211, 307, 401, 503, 601, 701, 809, 907, 1009, 1201, 1601, 2003, 2411, 3001, 4001, 5003, 6007, 7001, 8009, 9001, 10007, 12007, 16001, 20011, 24001, 30011, 40009, 50021, 60013, 70001, 80021, 90001, 100003, 120011, 160001, 200003, 240007, 300007, 400009, 500009, 600011, 700001, 800011, 900001, -1 }