ftwin 0.8.10
napr_hash.h File Reference

A high-performance hash table implementation built on APR. More...

#include <apr.h>
#include <apr_pools.h>
Include dependency graph for napr_hash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct napr_hash_t napr_hash_t
 Opaque hash table structure.
 
typedef struct napr_hash_index_t napr_hash_index_t
 Opaque hash table iterator structure.
 
typedef const void *() get_key_callback_fn_t(const void *)
 Callback function to extract the key from a stored data item.
 
typedef apr_size_t() get_key_len_callback_fn_t(const void *)
 Callback function to get the length of a key.
 
typedef int() key_cmp_callback_fn_t(const void *, const void *, apr_size_t)
 Callback function to compare two keys.
 
typedef apr_uint32_t() hash_callback_fn_t(register const void *, register apr_size_t)
 Callback function to compute the hash value of a key.
 

Functions

napr_hash_t * napr_hash_make (apr_pool_t *pool, apr_size_t nel, apr_size_t ffactor, get_key_callback_fn_t get_key, get_key_len_callback_fn_t get_key_len, key_cmp_callback_fn_t key_cmp, hash_callback_fn_t hash)
 Create a hash table with custom key handling and hashing functions.
 
napr_hash_t * napr_hash_str_make (apr_pool_t *pool, apr_size_t nel, apr_size_t ffactor)
 Create a hash table optimized for storing C strings as keys.
 
void * napr_hash_search (napr_hash_t *hash, const void *key, apr_size_t key_len, apr_uint32_t *hash_value)
 Searches the hash table for an item.
 
void napr_hash_remove (napr_hash_t *hash, void *data, apr_uint32_t hash_value)
 Removes an item from the hash table.
 
apr_status_t napr_hash_set (napr_hash_t *hash, void *data, apr_uint32_t hash_value)
 Inserts or updates an item in the hash table.
 
apr_pool_t * napr_hash_pool_get (const napr_hash_t *thehash)
 Get a pointer to the pool from which the hash table was allocated.
 
napr_hash_index_t * napr_hash_first (apr_pool_t *pool, napr_hash_t *hash)
 Start iterating over the entries in a hash table.
 
napr_hash_index_t * napr_hash_next (napr_hash_index_t *index)
 Continue iterating over the entries in a hash table.
 
void napr_hash_this (napr_hash_index_t *hi, const void **key, apr_size_t *klen, void **val)
 Get the current entry's details from the iteration state.
 

Detailed Description

A high-performance hash table implementation built on APR.

Definition in file napr_hash.h.

Typedef Documentation

◆ get_key_callback_fn_t

typedef const void *() get_key_callback_fn_t(const void *)

Callback function to extract the key from a stored data item.

Parameters
[in]dataA pointer to the stored data item.
Returns
A pointer to the key.

Definition at line 43 of file napr_hash.h.

◆ get_key_len_callback_fn_t

typedef apr_size_t() get_key_len_callback_fn_t(const void *)

Callback function to get the length of a key.

Parameters
[in]dataA pointer to the stored data item.
Returns
The length of the key.

Definition at line 50 of file napr_hash.h.

◆ hash_callback_fn_t

typedef apr_uint32_t() hash_callback_fn_t(register const void *, register apr_size_t)

Callback function to compute the hash value of a key.

Parameters
[in]keyThe key to hash.
[in]klenA pointer to the key's length.
Returns
The computed hash value.

Definition at line 68 of file napr_hash.h.

◆ key_cmp_callback_fn_t

typedef int() key_cmp_callback_fn_t(const void *, const void *, apr_size_t)

Callback function to compare two keys.

Parameters
[in]key1The first key.
[in]key2The second key.
[in]lenThe length to compare (can be ignored if keys are null-terminated).
Returns
An integer less than, equal to, or greater than zero if key1 is found, respectively, to be less than, to match, or be greater than key2.

Definition at line 60 of file napr_hash.h.

Function Documentation

◆ napr_hash_first()

napr_hash_index_t * napr_hash_first ( apr_pool_t *  pool,
napr_hash_t *  hash 
)

Start iterating over the entries in a hash table.

Parameters
[in]poolThe pool to allocate the iterator from. If NULL, a non-thread-safe internal iterator is used.
[in]hashThe hash table to iterate over.
Returns
A pointer to the iterator.
Remarks
There is no restriction on adding or deleting hash entries during an iteration, but the results may be unpredictable unless only deleting the current entry.
void *val;
for (hi = napr_hash_first(p, ht); hi; hi = napr_hash_next(hi)) {
napr_hash_this(hi, NULL, NULL, &val);
// process val
}
napr_hash_index_t * napr_hash_first(apr_pool_t *pool, napr_hash_t *hash)
Start iterating over the entries in a hash table.
Definition napr_hash.c:280
void napr_hash_this(napr_hash_index_t *hi, const void **key, apr_size_t *klen, void **val)
Get the current entry's details from the iteration state.
Definition napr_hash.c:314
napr_hash_index_t * napr_hash_next(napr_hash_index_t *index)
Continue iterating over the entries in a hash table.
Definition napr_hash.c:294
struct napr_hash_index_t napr_hash_index_t
Opaque hash table iterator structure.
Definition napr_hash.h:36

Definition at line 280 of file napr_hash.c.

References napr_hash_next().

Here is the call graph for this function:

◆ napr_hash_make()

napr_hash_t * napr_hash_make ( apr_pool_t *  pool,
apr_size_t  nel,
apr_size_t  ffactor,
get_key_callback_fn_t  get_key,
get_key_len_callback_fn_t  get_key_len,
key_cmp_callback_fn_t  key_cmp,
hash_callback_fn_t  hash 
)

Create a hash table with custom key handling and hashing functions.

Parameters
[in]poolThe pool to allocate the hash table from.
[in]nelThe desired number of pre-allocated buckets (will be rounded up to the next power of 2).
[in]ffactorThe maximum number of collisions for a given key before resizing.
[in]get_keyA callback to extract the key from a data item.
[in]get_key_lenA callback to get the length of the key.
[in]key_cmpA callback to compare two keys.
[in]hashA callback to compute the hash of a key.
Returns
A pointer to the newly created hash table.

Definition at line 100 of file napr_hash.c.

References DEBUG_ERR.

Referenced by napr_hash_str_make().

Here is the caller graph for this function:

◆ napr_hash_next()

napr_hash_index_t * napr_hash_next ( napr_hash_index_t *  index)

Continue iterating over the entries in a hash table.

Parameters
[in]indexThe current iteration state.
Returns
A pointer to the next iteration state, or NULL if there are no more entries.

Definition at line 294 of file napr_hash.c.

Referenced by napr_hash_first().

Here is the caller graph for this function:

◆ napr_hash_pool_get()

apr_pool_t * napr_hash_pool_get ( const napr_hash_t *  thehash)

Get a pointer to the pool from which the hash table was allocated.

Parameters
[in]thehashThe hash table.
Returns
The APR pool.

Definition at line 275 of file napr_hash.c.

◆ napr_hash_remove()

void napr_hash_remove ( napr_hash_t *  hash,
void *  data,
apr_uint32_t  hash_value 
)

Removes an item from the hash table.

Parameters
[in]hashThe hash table.
[in]dataThe data item to remove.
[in]hash_valueThe pre-computed hash of the item's key.

Definition at line 210 of file napr_hash.c.

References DEBUG_DBG.

◆ napr_hash_search()

void * napr_hash_search ( napr_hash_t *  hash,
const void *  key,
apr_size_t  key_len,
apr_uint32_t *  hash_value 
)

Searches the hash table for an item.

Parameters
[in]hashThe hash table to search.
[in]keyThe key to search for.
[in]key_lenThe length of the key.
[out]hash_valueA pointer to store the computed hash of the key. Can be NULL.
Returns
A pointer to the found data item, or NULL if not found. If an item is not found, the computed hash value is stored in hash_value for efficient subsequent insertion.

Definition at line 145 of file napr_hash.c.

Referenced by ft_report_duplicates(), and ft_report_json().

Here is the caller graph for this function:

◆ napr_hash_set()

apr_status_t napr_hash_set ( napr_hash_t *  hash,
void *  data,
apr_uint32_t  hash_value 
)

Inserts or updates an item in the hash table.

Parameters
[in]hashThe hash table.
[in]dataThe data item to insert.
[in]hash_valueThe pre-computed hash of the item's key.
Returns
APR_SUCCESS on success.

Definition at line 242 of file napr_hash.c.

References DEBUG_ERR.

◆ napr_hash_str_make()

napr_hash_t * napr_hash_str_make ( apr_pool_t *  pool,
apr_size_t  nel,
apr_size_t  ffactor 
)

Create a hash table optimized for storing C strings as keys.

Parameters
[in]poolThe pool to allocate the hash table from.
[in]nelThe desired number of pre-allocated buckets.
[in]ffactorThe maximum number of collisions for a given key.
Returns
A pointer to the newly created string-keyed hash table.

Definition at line 95 of file napr_hash.c.

References napr_hash_make().

Here is the call graph for this function:

◆ napr_hash_this()

void napr_hash_this ( napr_hash_index_t *  hi,
const void **  key,
apr_size_t *  klen,
void **  val 
)

Get the current entry's details from the iteration state.

Parameters
[in]hiThe iteration state.
[out]keyPointer to store the key. Can be NULL.
[out]klenPointer to store the key length. Can be NULL.
[out]valPointer to store the value (the data item). Can be NULL.

Definition at line 314 of file napr_hash.c.