Concord - C Discord API library
A Discord API wrapper library written in C
oa_hash.h File Reference
#include <stddef.h>
#include <string.h>
#include <stdint.h>
Include dependency graph for oa_hash.h:

Go to the source code of this file.

Data Structures

struct  oa_hash_entry
 Entry holding key-value pair in hash table. More...
 
struct  oa_hash
 Open addressing hash table. More...
 

Macros

#define OA_HASH_API   extern
 
#define __OA_HASH_ATTRS_const
 
#define __OA_HASH_ATTRS_mut
 
#define OA_HASH_ATTRS(_qualifier)   __OA_HASH_ATTRS_##_qualifier
 can be used to cast to struct oa_hash More...
 

Enumerations

enum  oa_hash_entry_state { OA_HASH_ENTRY_EMPTY = 0 , OA_HASH_ENTRY_OCCUPIED , OA_HASH_ENTRY_DELETED }
 Hash table entry state. More...
 

Functions

void oa_hash_init (struct oa_hash *ht, struct oa_hash_entry buckets[], const size_t capacity)
 Initialize hash table with given buckets array. More...
 
void oa_hash_cleanup (struct oa_hash *ht)
 Clean up hash table entries and struct. More...
 
const struct oa_hash_entryoa_hash_get_entry (const struct oa_hash *ht, const char key[], const size_t len)
 Retrieve entry by key. More...
 
void * oa_hash_get (const struct oa_hash *ht, const char key[], const size_t len)
 Retrieve value by key (wrapper around oa_hash_get_entry) More...
 
const struct oa_hash_entryoa_hash_set_entry (struct oa_hash *ht, const char key[], const size_t len, void *value)
 Insert or update entry. More...
 
void * oa_hash_set (struct oa_hash *ht, const char key[], const size_t len, void *value)
 Insert or update entry (wrapper around oa_hash_set_entry) More...
 
int oa_hash_remove (struct oa_hash *ht, const char key[], const size_t len)
 Remove entry by key. More...
 
struct oa_hash_entryoa_hash_rehash (struct oa_hash *ht, struct oa_hash_entry new_buckets[], const size_t new_capacity)
 Rehash table to new buckets array. More...
 

Macro Definition Documentation

◆ OA_HASH_API

#define OA_HASH_API   extern

◆ __OA_HASH_ATTRS_const

#define __OA_HASH_ATTRS_const
Value:
const size_t length; \
const size_t capacity; \
const struct oa_hash_entry *buckets
Entry holding key-value pair in hash table.
Definition: oa_hash.h:24

entries array

◆ __OA_HASH_ATTRS_mut

#define __OA_HASH_ATTRS_mut
Value:
size_t length; \
size_t capacity; \
struct oa_hash_entry *buckets

entries array

◆ OA_HASH_ATTRS

#define OA_HASH_ATTRS (   _qualifier)    __OA_HASH_ATTRS_##_qualifier

can be used to cast to struct oa_hash

Enumeration Type Documentation

◆ oa_hash_entry_state

Hash table entry state.

Enumerator
OA_HASH_ENTRY_EMPTY 

empty entry

OA_HASH_ENTRY_OCCUPIED 

occupied entry

OA_HASH_ENTRY_DELETED 

deleted entry

Function Documentation

◆ oa_hash_init()

void oa_hash_init ( struct oa_hash ht,
struct oa_hash_entry  buckets[],
const size_t  capacity 
)

Initialize hash table with given buckets array.

Parameters
[out]htthe hash table to be initialized
[out]bucketspre-allocated array of entries
[in]capacityamount of buckets

◆ oa_hash_cleanup()

void oa_hash_cleanup ( struct oa_hash ht)

Clean up hash table entries and struct.

Parameters
[out]htthe hash table to be cleaned

◆ oa_hash_get_entry()

const struct oa_hash_entry * oa_hash_get_entry ( const struct oa_hash ht,
const char  key[],
const size_t  len 
)

Retrieve entry by key.

Parameters
[in]htthe hash table
[in]keythe key to search for
[in]lenthe key length
Returns
entry if found, NULL otherwise

◆ oa_hash_get()

void * oa_hash_get ( const struct oa_hash ht,
const char  key[],
const size_t  len 
)

Retrieve value by key (wrapper around oa_hash_get_entry)

Parameters
[in]htthe hash table
[in]keythe key to search for
[in]lenthe key length
Returns
value if found, NULL otherwise

◆ oa_hash_set_entry()

const struct oa_hash_entry * oa_hash_set_entry ( struct oa_hash ht,
const char  key[],
const size_t  len,
void *  value 
)

Insert or update entry.

Parameters
[in,out]htthe hash table
[in]keythe key to insert/update
[in]lenthe key length
[in]valuethe value to be assigned
Returns
entry if successful, or NULL if no space left, in which case oa_hash_rehash() should be called

◆ oa_hash_set()

void * oa_hash_set ( struct oa_hash ht,
const char  key[],
const size_t  len,
void *  value 
)

Insert or update entry (wrapper around oa_hash_set_entry)

Parameters
[in,out]htthe hash table
[in]keythe key to insert/update
[in]lenthe key length
[in]valuethe value to be assigned
Returns
value if successful, or NULL if no space left, in which case oa_hash_rehash() should be called

◆ oa_hash_remove()

int oa_hash_remove ( struct oa_hash ht,
const char  key[],
const size_t  len 
)

Remove entry by key.

Parameters
[in,out]htthe hash table
[in]keythe key to be removed
[in]lenthe key length
Returns
1 if found and removed, 0 otherwise

◆ oa_hash_rehash()

struct oa_hash_entry * oa_hash_rehash ( struct oa_hash ht,
struct oa_hash_entry  new_buckets[],
const size_t  new_capacity 
)

Rehash table to new buckets array.

Parameters
[in,out]htthe hash table
[in,out]new_bucketsthe new buckets array
[in]new_capacitythe new buckets capacity
Returns
pointer to old (now unused) bucket if successful, or NULL otherwise