Freeciv21
Develop your civilization from humble roots to a global empire
genhash.h File Reference
#include "iterator.h"
#include "support.h"
#include <stddef.h>
+ Include dependency graph for genhash.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define genhash_keys_iterate(ARG_ht, NAME_key)
 
#define genhash_keys_iterate_end   generic_iterate_end
 
#define genhash_values_iterate(ARG_ht, NAME_value)
 
#define genhash_values_iterate_end   generic_iterate_end
 
#define genhash_iterate(ARG_ht, NAME_iter)
 
#define genhash_iterate_end   generic_iterate_end
 

Typedefs

typedef unsigned int genhash_val_t
 
typedef genhash_val_t(* genhash_val_fn_t) (const void *)
 
typedef bool(* genhash_comp_fn_t) (const void *, const void *)
 
typedef void *(* genhash_copy_fn_t) (const void *)
 
typedef void(* genhash_free_fn_t) (void *)
 

Functions

genhash_val_t genhash_str_val_func (const char *vkey)
 A supplied genhash function appropriate to nul-terminated strings. More...
 
bool genhash_str_comp_func (const char *vkey1, const char *vkey2)
 A supplied function for comparison of nul-terminated strings: More...
 
char * genhash_str_copy_func (const char *vkey)
 Copy function for string allocation. More...
 
void genhash_str_free_func (char *vkey)
 Free function for string allocation. More...
 
struct genhashgenhash_new (genhash_val_fn_t key_val_func, genhash_comp_fn_t key_comp_func) fc__warn_unused_result
 Constructor with unspecified number of entries. More...
 
struct genhashgenhash_new_full (genhash_val_fn_t key_val_func, genhash_comp_fn_t key_comp_func, genhash_copy_fn_t key_copy_func, genhash_free_fn_t key_free_func, genhash_copy_fn_t data_copy_func, genhash_free_fn_t data_free_func) fc__warn_unused_result
 Constructor with unspecified number of entries. More...
 
struct genhashgenhash_new_nentries (genhash_val_fn_t key_val_func, genhash_comp_fn_t key_comp_func, size_t nentries) fc__warn_unused_result
 Constructor specifying number of entries. More...
 
struct genhashgenhash_new_nentries_full (genhash_val_fn_t key_val_func, genhash_comp_fn_t key_comp_func, genhash_copy_fn_t key_copy_func, genhash_free_fn_t key_free_func, genhash_copy_fn_t data_copy_func, genhash_free_fn_t data_free_func, size_t nentries) fc__warn_unused_result
 Constructor specifying number of entries. More...
 
void genhash_destroy (struct genhash *pgenhash)
 Destructor: free internal memory. More...
 
bool genhash_set_no_shrink (struct genhash *pgenhash, bool no_shrink)
 Prevent or allow the genhash table automatically shrinking. More...
 
size_t genhash_size (const struct genhash *pgenhash)
 Returns the number of entries in the genhash table. More...
 
size_t genhash_capacity (const struct genhash *pgenhash)
 Returns the number of buckets in the genhash table. More...
 
struct genhashgenhash_copy (const struct genhash *pgenhash) fc__warn_unused_result
 Returns a newly allocated mostly deep copy of the given genhash table. More...
 
void genhash_clear (struct genhash *pgenhash)
 Remove all entries of the genhash table. More...
 
bool genhash_insert (struct genhash *pgenhash, const void *key, const void *data)
 Insert entry: returns TRUE if inserted, or FALSE if there was already an entry with the same key, in which case the entry was not inserted. More...
 
bool genhash_replace (struct genhash *pgenhash, const void *key, const void *data)
 Insert entry, replacing any existing entry which has the same key. More...
 
bool genhash_replace_full (struct genhash *pgenhash, const void *key, const void *data, void **old_pkey, void **old_pdata)
 Insert entry, replacing any existing entry which has the same key. More...
 
bool genhash_lookup (const struct genhash *pgenhash, const void *key, void **pdata)
 Lookup data. More...
 
bool genhash_remove (struct genhash *pgenhash, const void *key)
 Delete an entry from the genhash table. More...
 
bool genhash_remove_full (struct genhash *pgenhash, const void *key, void **deleted_pkey, void **deleted_pdata)
 Delete an entry from the genhash table. More...
 
bool genhashs_are_equal (const struct genhash *pgenhash1, const struct genhash *pgenhash2)
 Returns TRUE iff the hash tables contains the same pairs of key/data. More...
 
bool genhashs_are_equal_full (const struct genhash *pgenhash1, const struct genhash *pgenhash2, genhash_comp_fn_t data_comp_func)
 Returns TRUE iff the hash tables contains the same pairs of key/data. More...
 
size_t genhash_iter_sizeof ()
 "Sizeof" function implementation for generic_iterate genhash iterators. More...
 
struct iteratorgenhash_key_iter_init (struct genhash_iter *iter, const struct genhash *hash)
 Returns an iterator over the genhash table's k genhashgenhashenhashys. More...
 
struct iteratorgenhash_value_iter_init (struct genhash_iter *iter, const struct genhash *hash)
 Returns an iterator over the hash table's values. More...
 
struct iteratorgenhash_iter_init (struct genhash_iter *iter, const struct genhash *hash)
 Returns an iterator that iterates over both keys and values of the genhash table. More...
 
void * genhash_iter_key (const struct iterator *genhash_iter)
 Helper function for genhash (key, value) pair iteration. More...
 
void * genhash_iter_value (const struct iterator *genhash_iter)
 Helper function for genhash (key, value) pair iteration. More...
 

Macro Definition Documentation

◆ genhash_iterate

#define genhash_iterate (   ARG_ht,
  NAME_iter 
)
Value:
generic_iterate(struct genhash_iter, struct iterator *, NAME_iter, \
size_t genhash_iter_sizeof()
"Sizeof" function implementation for generic_iterate genhash iterators.
Definition: genhash.cpp:787
struct iterator * genhash_iter_init(struct genhash_iter *iter, const struct genhash *hash)
Returns an iterator that iterates over both keys and values of the genhash table.
Definition: genhash.cpp:880
#define generic_iterate(TYPE_it, TYPE_a, NAME_a, FUNC_size, FUNC_init,...)
Definition: iterator.h:73

Definition at line 113 of file genhash.h.

◆ genhash_iterate_end

#define genhash_iterate_end   generic_iterate_end

Definition at line 116 of file genhash.h.

◆ genhash_keys_iterate

#define genhash_keys_iterate (   ARG_ht,
  NAME_key 
)
Value:
generic_iterate(struct genhash_iter, const void *, NAME_key, \
struct iterator * genhash_key_iter_init(struct genhash_iter *iter, const struct genhash *hash)
Returns an iterator over the genhash table's k genhashgenhashenhashys.
Definition: genhash.cpp:889

Definition at line 97 of file genhash.h.

◆ genhash_keys_iterate_end

#define genhash_keys_iterate_end   generic_iterate_end

Definition at line 100 of file genhash.h.

◆ genhash_values_iterate

#define genhash_values_iterate (   ARG_ht,
  NAME_value 
)
Value:
generic_iterate(struct genhash_iter, void *, NAME_value, \
struct iterator * genhash_value_iter_init(struct genhash_iter *iter, const struct genhash *hash)
Returns an iterator over the hash table's values.
Definition: genhash.cpp:898

Definition at line 104 of file genhash.h.

◆ genhash_values_iterate_end

#define genhash_values_iterate_end   generic_iterate_end

Definition at line 107 of file genhash.h.

Typedef Documentation

◆ genhash_comp_fn_t

typedef bool(* genhash_comp_fn_t) (const void *, const void *)

Definition at line 31 of file genhash.h.

◆ genhash_copy_fn_t

typedef void*(* genhash_copy_fn_t) (const void *)

Definition at line 32 of file genhash.h.

◆ genhash_free_fn_t

typedef void(* genhash_free_fn_t) (void *)

Definition at line 33 of file genhash.h.

◆ genhash_val_fn_t

typedef genhash_val_t(* genhash_val_fn_t) (const void *)

Definition at line 30 of file genhash.h.

◆ genhash_val_t

typedef unsigned int genhash_val_t

Definition at line 27 of file genhash.h.

Function Documentation

◆ genhash_capacity()

size_t genhash_capacity ( const struct genhash pgenhash)

Returns the number of buckets in the genhash table.

Definition at line 528 of file genhash.cpp.

◆ genhash_clear()

void genhash_clear ( struct genhash pgenhash)

Remove all entries of the genhash table.

Definition at line 575 of file genhash.cpp.

Referenced by conn_reset_delta_state(), and genhash_destroy().

◆ genhash_copy()

struct genhash* genhash_copy ( const struct genhash pgenhash)

Returns a newly allocated mostly deep copy of the given genhash table.

Definition at line 537 of file genhash.cpp.

◆ genhash_destroy()

void genhash_destroy ( struct genhash pgenhash)

Destructor: free internal memory.

Definition at line 284 of file genhash.cpp.

Referenced by free_packet_hashes().

◆ genhash_insert()

bool genhash_insert ( struct genhash pgenhash,
const void *  key,
const void *  data 
)

Insert entry: returns TRUE if inserted, or FALSE if there was already an entry with the same key, in which case the entry was not inserted.

Definition at line 597 of file genhash.cpp.

◆ genhash_iter_init()

struct iterator* genhash_iter_init ( struct genhash_iter iter,
const struct genhash pgenhash 
)

Returns an iterator that iterates over both keys and values of the genhash table.

NB: iterator_get() returns an iterator pointer, so use the helper functions genhash_iter_get_{key,value} to access the key and value.

Definition at line 880 of file genhash.cpp.

◆ genhash_iter_key()

void* genhash_iter_key ( const struct iterator genhash_iter)

Helper function for genhash (key, value) pair iteration.

Definition at line 792 of file genhash.cpp.

Referenced by genhash_key_iter_init().

◆ genhash_iter_sizeof()

size_t genhash_iter_sizeof ( )

"Sizeof" function implementation for generic_iterate genhash iterators.

Definition at line 787 of file genhash.cpp.

◆ genhash_iter_value()

void* genhash_iter_value ( const struct iterator genhash_iter)

Helper function for genhash (key, value) pair iteration.

Definition at line 801 of file genhash.cpp.

Referenced by genhash_value_iter_init().

◆ genhash_key_iter_init()

struct iterator* genhash_key_iter_init ( struct genhash_iter iter,
const struct genhash hash 
)

Returns an iterator over the genhash table's k genhashgenhashenhashys.

Definition at line 889 of file genhash.cpp.

◆ genhash_lookup()

bool genhash_lookup ( const struct genhash pgenhash,
const void *  key,
void **  pdata 
)

Lookup data.

Return TRUE on success, then pdata - if not nullptr will be set to the data value.

Definition at line 675 of file genhash.cpp.

◆ genhash_new()

struct genhash* genhash_new ( genhash_val_fn_t  key_val_func,
genhash_comp_fn_t  key_comp_func 
)

Constructor with unspecified number of entries.

Definition at line 274 of file genhash.cpp.

◆ genhash_new_full()

struct genhash* genhash_new_full ( genhash_val_fn_t  key_val_func,
genhash_comp_fn_t  key_comp_func,
genhash_copy_fn_t  key_copy_func,
genhash_free_fn_t  key_free_func,
genhash_copy_fn_t  data_copy_func,
genhash_free_fn_t  data_free_func 
)

Constructor with unspecified number of entries.

Allows to specify functions to free the memory allocated for the key and user-data that get called when removing the bucket from the hash table or changing key/user-data values.

Definition at line 259 of file genhash.cpp.

◆ genhash_new_nentries()

struct genhash* genhash_new_nentries ( genhash_val_fn_t  key_val_func,
genhash_comp_fn_t  key_comp_func,
size_t  nentries 
)

Constructor specifying number of entries.

Definition at line 244 of file genhash.cpp.

◆ genhash_new_nentries_full()

struct genhash* genhash_new_nentries_full ( genhash_val_fn_t  key_val_func,
genhash_comp_fn_t  key_comp_func,
genhash_copy_fn_t  key_copy_func,
genhash_free_fn_t  key_free_func,
genhash_copy_fn_t  data_copy_func,
genhash_free_fn_t  data_free_func,
size_t  nentries 
)

Constructor specifying number of entries.

Allows to specify functions to free the memory allocated for the key and user-data that get called when removing the bucket from the hash table or changing key/user-data values.

Definition at line 228 of file genhash.cpp.

◆ genhash_remove()

bool genhash_remove ( struct genhash pgenhash,
const void *  key 
)

Delete an entry from the genhash table.

Returns TRUE on success.

Definition at line 696 of file genhash.cpp.

◆ genhash_remove_full()

bool genhash_remove_full ( struct genhash pgenhash,
const void *  key,
void **  deleted_pkey,
void **  deleted_pdata 
)

Delete an entry from the genhash table.

Returns TRUE on success.

Returns in 'deleted_pkey' and 'deleted_pdata' the old contents of the deleted entry if not nullptr. NB: It can returns freed pointers if free functions were supplied to the genhash table.

Definition at line 708 of file genhash.cpp.

Referenced by genhash_remove().

◆ genhash_replace()

bool genhash_replace ( struct genhash pgenhash,
const void *  key,
const void *  data 
)

Insert entry, replacing any existing entry which has the same key.

Returns TRUE if a data have been replaced, FALSE if it was a simple insertion.

Definition at line 625 of file genhash.cpp.

◆ genhash_replace_full()

bool genhash_replace_full ( struct genhash pgenhash,
const void *  key,
const void *  data,
void **  old_pkey,
void **  old_pdata 
)

Insert entry, replacing any existing entry which has the same key.

Returns TRUE if a data have been replaced, FALSE if it was a simple insertion.

Returns in 'old_pkey' and 'old_pdata' the old content of the bucket if they are not nullptr. NB: It can returns freed pointers if free functions were supplied to the genhash table.

Definition at line 640 of file genhash.cpp.

Referenced by genhash_replace().

◆ genhash_set_no_shrink()

bool genhash_set_no_shrink ( struct genhash pgenhash,
bool  no_shrink 
)

Prevent or allow the genhash table automatically shrinking.

Returns the old value of the setting.

Definition at line 506 of file genhash.cpp.

◆ genhash_size()

size_t genhash_size ( const struct genhash pgenhash)

Returns the number of entries in the genhash table.

Definition at line 519 of file genhash.cpp.

◆ genhash_str_comp_func()

bool genhash_str_comp_func ( const char *  vkey1,
const char *  vkey2 
)

A supplied function for comparison of nul-terminated strings:

Definition at line 119 of file genhash.cpp.

◆ genhash_str_copy_func()

char* genhash_str_copy_func ( const char *  vkey)

Copy function for string allocation.

Definition at line 127 of file genhash.cpp.

◆ genhash_str_free_func()

void genhash_str_free_func ( char *  vkey)

Free function for string allocation.

Definition at line 135 of file genhash.cpp.

◆ genhash_str_val_func()

genhash_val_t genhash_str_val_func ( const char *  vkey)

A supplied genhash function appropriate to nul-terminated strings.

Prefers table sizes that are prime numbers.

Definition at line 104 of file genhash.cpp.

◆ genhash_value_iter_init()

struct iterator* genhash_value_iter_init ( struct genhash_iter iter,
const struct genhash hash 
)

Returns an iterator over the hash table's values.

Definition at line 898 of file genhash.cpp.

◆ genhashs_are_equal()

bool genhashs_are_equal ( const struct genhash pgenhash1,
const struct genhash pgenhash2 
)

Returns TRUE iff the hash tables contains the same pairs of key/data.

Definition at line 734 of file genhash.cpp.

◆ genhashs_are_equal_full()

bool genhashs_are_equal_full ( const struct genhash pgenhash1,
const struct genhash pgenhash2,
genhash_comp_fn_t  data_comp_func 
)

Returns TRUE iff the hash tables contains the same pairs of key/data.

Definition at line 743 of file genhash.cpp.

Referenced by genhashs_are_equal().