hat-util  0.6.16
Utility library
ht.h File Reference

Hash table. More...

#include <stddef.h>
#include <stdint.h>
#include "allocator.h"
Include dependency graph for ht.h:

Go to the source code of this file.

Macros

#define HAT_HT_SUCCESS   0
 
#define HAT_HT_ERROR   (-1)
 

Typedefs

typedef struct hat_ht_t hat_ht_t
 Hash table. More...
 
typedef void * hat_ht_iter_t
 Hash table iterator. More...
 

Functions

hat_ht_that_ht_create (hat_allocator_t *a, size_t avg_count)
 Create new hash table. More...
 
void hat_ht_destroy (hat_ht_t *t)
 Destroy hash table. More...
 
int hat_ht_resize (hat_ht_t *t, size_t avg_count)
 Reallocate hash table based on new average count. More...
 
size_t hat_ht_count (hat_ht_t *t)
 Number of elements in table. More...
 
size_t hat_ht_avg_count (hat_ht_t *t)
 Initially estimated number of elements in table. More...
 
int hat_ht_set (hat_ht_t *t, uint8_t *key, size_t key_size, void *value)
 Set element in table. More...
 
int hat_ht_set_s (hat_ht_t *t, char *key, void *value)
 Set element in table (string key) More...
 
int hat_ht_set_i64 (hat_ht_t *t, int64_t key, void *value)
 Set element in table (int64_t key) More...
 
int hat_ht_set_u64 (hat_ht_t *t, uint64_t key, void *value)
 Set element in table (uint64_t key) More...
 
void * hat_ht_get (hat_ht_t *t, uint8_t *key, size_t key_size)
 Get element from table. More...
 
void * hat_ht_get_s (hat_ht_t *t, char *key)
 Get element from table (string key) More...
 
void * hat_ht_get_i64 (hat_ht_t *t, int64_t key)
 Get element from table (int64_t key) More...
 
void * hat_ht_get_u64 (hat_ht_t *t, uint64_t key)
 Get element from table (uint64_t key) More...
 
int hat_ht_del (hat_ht_t *t, uint8_t *key, size_t key_size)
 Delete element from table. More...
 
int hat_ht_del_s (hat_ht_t *t, char *key)
 Delete element from table (string key) More...
 
int hat_ht_del_i64 (hat_ht_t *t, int64_t key)
 Delete element from table (int64_t key) More...
 
int hat_ht_del_u64 (hat_ht_t *t, uint64_t key)
 Delete element from table (uint64_t key) More...
 
hat_ht_iter_t hat_ht_iter_next (hat_ht_t *t, hat_ht_iter_t prev)
 Get next iterator position. More...
 
int hat_ht_iter_key (hat_ht_iter_t i, uint8_t **key, size_t *key_size)
 Get current key. More...
 
int hat_ht_iter_key_s (hat_ht_iter_t i, char **key)
 Get current key (string key) More...
 
int hat_ht_iter_key_i64 (hat_ht_iter_t i, int64_t *key)
 Get current key (int64_t key) More...
 
int hat_ht_iter_key_u64 (hat_ht_iter_t i, uint64_t *key)
 Get current key (uint64_t key) More...
 
int hat_ht_iter_value (hat_ht_iter_t i, void **value)
 Get current value. More...
 

Detailed Description

Hash table.

This hash table implementation stores copies of key data and value pointers.

Macro Definition Documentation

◆ HAT_HT_ERROR

#define HAT_HT_ERROR   (-1)

◆ HAT_HT_SUCCESS

#define HAT_HT_SUCCESS   0

Typedef Documentation

◆ hat_ht_iter_t

typedef void* hat_ht_iter_t

Hash table iterator.

◆ hat_ht_t

typedef struct hat_ht_t hat_ht_t

Hash table.

Function Documentation

◆ hat_ht_avg_count()

size_t hat_ht_avg_count ( hat_ht_t t)

Initially estimated number of elements in table.

Parameters
[in]ttable
Returns
elements count

◆ hat_ht_count()

size_t hat_ht_count ( hat_ht_t t)

Number of elements in table.

Parameters
[in]ttable
Returns
elements count

◆ hat_ht_create()

hat_ht_t* hat_ht_create ( hat_allocator_t a,
size_t  avg_count 
)

Create new hash table.

Parameters
[in]aallocator
[in]avg_countestimated number of elements in table
Returns
table or NULL on failure

◆ hat_ht_del()

int hat_ht_del ( hat_ht_t t,
uint8_t *  key,
size_t  key_size 
)

Delete element from table.

Parameters
[in]ttable
[in]keykey
[in]key_sizekey size
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_del_i64()

int hat_ht_del_i64 ( hat_ht_t t,
int64_t  key 
)

Delete element from table (int64_t key)

Parameters
[in]ttable
[in]keykey
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_del_s()

int hat_ht_del_s ( hat_ht_t t,
char *  key 
)

Delete element from table (string key)

Parameters
[in]ttable
[in]keykey
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_del_u64()

int hat_ht_del_u64 ( hat_ht_t t,
uint64_t  key 
)

Delete element from table (uint64_t key)

Parameters
[in]ttable
[in]keykey
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_destroy()

void hat_ht_destroy ( hat_ht_t t)

Destroy hash table.

Parameters
[in]ttable

◆ hat_ht_get()

void* hat_ht_get ( hat_ht_t t,
uint8_t *  key,
size_t  key_size 
)

Get element from table.

Parameters
[in]ttable
[in]keykey
[in]key_sizekey size
Returns
value or NULL on failure

◆ hat_ht_get_i64()

void* hat_ht_get_i64 ( hat_ht_t t,
int64_t  key 
)

Get element from table (int64_t key)

Parameters
[in]ttable
[in]keykey
Returns
value or NULL on failure

◆ hat_ht_get_s()

void* hat_ht_get_s ( hat_ht_t t,
char *  key 
)

Get element from table (string key)

Parameters
[in]ttable
[in]keykey
Returns
value or NULL on failure

◆ hat_ht_get_u64()

void* hat_ht_get_u64 ( hat_ht_t t,
uint64_t  key 
)

Get element from table (uint64_t key)

Parameters
[in]ttable
[in]keykey
Returns
value or NULL on failure

◆ hat_ht_iter_key()

int hat_ht_iter_key ( hat_ht_iter_t  i,
uint8_t **  key,
size_t *  key_size 
)

Get current key.

Parameters
[in]iiterator
[out]keykey
[out]key_sizekey size
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_iter_key_i64()

int hat_ht_iter_key_i64 ( hat_ht_iter_t  i,
int64_t *  key 
)

Get current key (int64_t key)

Parameters
[in]iiterator
[out]keykey
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_iter_key_s()

int hat_ht_iter_key_s ( hat_ht_iter_t  i,
char **  key 
)

Get current key (string key)

Parameters
[in]iiterator
[out]keykey
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_iter_key_u64()

int hat_ht_iter_key_u64 ( hat_ht_iter_t  i,
uint64_t *  key 
)

Get current key (uint64_t key)

Parameters
[in]iiterator
[out]keykey
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_iter_next()

hat_ht_iter_t hat_ht_iter_next ( hat_ht_t t,
hat_ht_iter_t  prev 
)

Get next iterator position.

Parameters
[in]ttable
[in]prevprevious iterator position
Returns
iterator or NULL

If prev is NULL, new iterator points to first entry. If all entries are iterated, NULL is returned.

◆ hat_ht_iter_value()

int hat_ht_iter_value ( hat_ht_iter_t  i,
void **  value 
)

Get current value.

Parameters
[in]iiterator
[out]valuevalue
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_resize()

int hat_ht_resize ( hat_ht_t t,
size_t  avg_count 
)

Reallocate hash table based on new average count.

Parameters
[in]ttable
[in]avg_countnew estimated number of elements in table
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

◆ hat_ht_set()

int hat_ht_set ( hat_ht_t t,
uint8_t *  key,
size_t  key_size,
void *  value 
)

Set element in table.

Parameters
[in]ttable
[in]keykey
[in]key_sizekey size
[in]valuevalue
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

If key already exist, value is overridden.

◆ hat_ht_set_i64()

int hat_ht_set_i64 ( hat_ht_t t,
int64_t  key,
void *  value 
)

Set element in table (int64_t key)

Parameters
[in]ttable
[in]keykey
[in]valuevalue
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

If key already exist, value is overridden.

◆ hat_ht_set_s()

int hat_ht_set_s ( hat_ht_t t,
char *  key,
void *  value 
)

Set element in table (string key)

Parameters
[in]ttable
[in]keykey
[in]valuevalue
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

If key already exist, value is overridden.

◆ hat_ht_set_u64()

int hat_ht_set_u64 ( hat_ht_t t,
uint64_t  key,
void *  value 
)

Set element in table (uint64_t key)

Parameters
[in]ttable
[in]keykey
[in]valuevalue
Returns
HAT_HT_SUCCESS or HAT_HT_ERROR

If key already exist, value is overridden.