ftwin 0.8.10
napr_hash.c File Reference

Implementation of the high-performance hash table. More...

#include "debug.h"
#include "napr_hash.h"
#include "xxhash.h"
Include dependency graph for napr_hash.c:

Go to the source code of this file.

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.
 
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.
 
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

Implementation of the high-performance hash table.

Definition in file napr_hash.c.

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 
)
extern

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 
)
extern

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 
)
extern

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 
)
extern

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 
)
extern

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.