GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions
gp_htable.h File Reference

Simple hash table implementation. More...

#include <stddef.h>
#include <string.h>
#include <utils/gp_types.h>

Go to the source code of this file.

Data Structures

struct  gp_htable_rec
 A hash table record. More...
 
struct  gp_htable
 A hash table. More...
 

Macros

#define GP_HTABLE_FOREACH(table, var)
 

Enumerations

enum  gp_htable_flags { GP_HTABLE_COPY_KEY = 0x01 , GP_HTABLE_FREE_KEY = 0x02 , GP_HTABLE_FREE_SELF = 0x04 }
 Flags to change how to deal with the hash table string keys. More...
 

Functions

gp_htablegp_htable_new (unsigned int order, int flags)
 Allocates a hash table.
 
int gp_htable_init (gp_htable *self, unsigned int order, int flags)
 Initializes an hash table embedded in a different structure.
 
static size_t gp_htable_keys (gp_htable *self)
 Returns the number of keys in hash table.
 
void gp_htable_free (gp_htable *self)
 Frees a hash table.
 
static size_t gp_htable_strhash (const void *key, size_t htable_size)
 A string hashing function.
 
static int gp_htable_strcmp (const void *key1, const void *key2)
 A string matching function.
 
void gp_htable_put (gp_htable *self, void *val, char *key)
 Adds a pointer to a hash table.
 
void * gp_htable_get (gp_htable *self, const char *key)
 Search for an element given a string key.
 
void * gp_htable_rem (gp_htable *self, const char *key)
 Removes an entry from a hash table.
 

Detailed Description

Simple hash table implementation.

Definition in file gp_htable.h.

Macro Definition Documentation

◆ GP_HTABLE_FOREACH

#define GP_HTABLE_FOREACH (   table,
  var 
)
Value:
for (struct gp_htable_rec *var = (table)->recs; var < &((table)->recs[(table)->size]); var++) \
if (var->key)
A hash table record.
Definition gp_htable.h:34

Hash table iterator.

Definition at line 52 of file gp_htable.h.

Enumeration Type Documentation

◆ gp_htable_flags

Flags to change how to deal with the hash table string keys.

Enumerator
GP_HTABLE_COPY_KEY 

The key is copied on insert.

GP_HTABLE_FREE_KEY 

The key is freed on removal.

GP_HTABLE_FREE_SELF 

The gp_htable is freed at the end of gp_htable_free()

Definition at line 24 of file gp_htable.h.

Function Documentation

◆ gp_htable_free()

void gp_htable_free ( gp_htable self)

Frees a hash table.

Parameters
selfThe table to be freed.

◆ gp_htable_get()

void * gp_htable_get ( gp_htable self,
const char *  key 
)

Search for an element given a string key.

Parameters
selfA hash table.
keyA string key.
Returns
A value if found or NULL.

◆ gp_htable_init()

int gp_htable_init ( gp_htable self,
unsigned int  order,
int  flags 
)

Initializes an hash table embedded in a different structure.

Parameters
selfA pointer to an hash table to be initialized.
orderHint of log2(size) for expected number of hash elements, if unsure pass 0.
flagsSee enum gp_htable_flags.
Returns
Zero on success, non-zero on allocation failure.

◆ gp_htable_keys()

static size_t gp_htable_keys ( gp_htable self)
inlinestatic

Returns the number of keys in hash table.

Parameters
selfA hash table.
Returns
The number of keys in hash table.

Definition at line 87 of file gp_htable.h.

References gp_htable::used.

◆ gp_htable_new()

gp_htable * gp_htable_new ( unsigned int  order,
int  flags 
)

Allocates a hash table.

Parameters
orderHint of log2(size) for expected number of hash elements, if unsure pass 0.
flagsSee enum gp_htable_flags.

Note that GP_HTABLE_FREE_SELF is added automatically to the flags so that the resulting table is freed on gp_htable_free().

Returns
Newly allocated hash table or NULL in a case of a malloc failure.

◆ gp_htable_put()

void gp_htable_put ( gp_htable self,
void *  val,
char *  key 
)

Adds a pointer to a hash table.

Parameters
selfHash table.
valA value.
keyA string key.

◆ gp_htable_rem()

void * gp_htable_rem ( gp_htable self,
const char *  key 
)

Removes an entry from a hash table.

Parameters
selfA Hash table.
keyA string key.
Returns
A value for removed key or NULL if not found.

◆ gp_htable_strcmp()

static int gp_htable_strcmp ( const void *  key1,
const void *  key2 
)
inlinestatic

A string matching function.

Parameters
key1A string.
key2A string.
Returns
Non-zero if keys are equal.

Definition at line 124 of file gp_htable.h.

◆ gp_htable_strhash()

static size_t gp_htable_strhash ( const void *  key,
size_t  htable_size 
)
inlinestatic

A string hashing function.

Parameters
keyA string.
htable_sizeA hash table size.

Definition at line 105 of file gp_htable.h.