Concord - C Discord API library
A Discord API wrapper library written in C
chash.h File Reference

Go to the source code of this file.

Macros

#define CWARE_LIBCHASH_VERSION   "x.0.0"
 
#define CHASH_INITIAL_SIZE   10
 
#define CHASH_RESIZE(size)    ((size) * 1.3)
 
#define CHASH_LOAD_THRESHOLD   0.8
 
#define CHASH_COUNTER_TYPE   int
 
#define CHASH_KEY_FIELD   key
 
#define CHASH_VALUE_FIELD   value
 
#define CHASH_STATE_FIELD   state
 
#define CHASH_BUCKETS_FIELD   buckets
 
#define CHASH_LENGTH_FIELD   length
 
#define CHASH_CAPACITY_FIELD   capacity
 
#define CHASH_UNFILLED   0
 
#define CHASH_FILLED   1
 
#define CHASH_TOMBSTONE   2
 
#define chash_string_hash(key, hash)
 
#define chash_string_compare(cmp_a, cmp_b)    (strcmp((cmp_a), (cmp_b)) == 0)
 
#define chash_default_init(bucket, _key, _value)
 
#define __chash_abs(x)    ((x) < 0 ? (x) * - 1 : (x))
 
#define __chash_hash(mod, _key, namespace)
 
#define __chash_probe(hashtable, _key, namespace)
 
#define __chash_probe_to_unfilled(mod, _key, buffer, namespace)
 
#define __chash_resize(hashtable, namespace)
 
#define __chash_assert_nonnull(func, ptr)
 
#define __chash_init(hashtable, namespace)
 
#define chash_init(hashtable, namespace)
 
#define chash_init_stack(hashtable, buffer, _length, namespace)
 
#define chash_assign(hashtable, _key, _value, namespace)
 
#define chash_lookup(hashtable, _key, storage, namespace)
 
#define chash_delete(hashtable, _key, namespace)
 
#define chash_contains(hashtable, _key, storage, namespace)
 
#define chash_lookup_bucket(hashtable, _key, storage, namespace)
 
#define __chash_free(hashtable, namespace)
 
#define chash_free(hashtable, namespace)
 
#define chash_is_full(hashtable, namespace)    (((hashtable)->CHASH_LENGTH_FIELD) == ((hashtable)->CHASH_CAPACITY_FIELD))
 
#define chash_iter(hashtable, index, _key, _value)
 
#define chash_skip(hashtable, index)
 

Macro Definition Documentation

◆ CWARE_LIBCHASH_VERSION

#define CWARE_LIBCHASH_VERSION   "x.0.0"

◆ CHASH_INITIAL_SIZE

#define CHASH_INITIAL_SIZE   10

◆ CHASH_RESIZE

#define CHASH_RESIZE (   size)     ((size) * 1.3)

◆ CHASH_LOAD_THRESHOLD

#define CHASH_LOAD_THRESHOLD   0.8

◆ CHASH_COUNTER_TYPE

#define CHASH_COUNTER_TYPE   int

◆ CHASH_KEY_FIELD

#define CHASH_KEY_FIELD   key

◆ CHASH_VALUE_FIELD

#define CHASH_VALUE_FIELD   value

◆ CHASH_STATE_FIELD

#define CHASH_STATE_FIELD   state

◆ CHASH_BUCKETS_FIELD

#define CHASH_BUCKETS_FIELD   buckets

◆ CHASH_LENGTH_FIELD

#define CHASH_LENGTH_FIELD   length

◆ CHASH_CAPACITY_FIELD

#define CHASH_CAPACITY_FIELD   capacity

◆ CHASH_UNFILLED

#define CHASH_UNFILLED   0

◆ CHASH_FILLED

#define CHASH_FILLED   1

◆ CHASH_TOMBSTONE

#define CHASH_TOMBSTONE   2

◆ chash_string_hash

#define chash_string_hash (   key,
  hash 
)
Value:
5031; \
do { \
int __CHASH_HINDEX = 0; \
\
for(__CHASH_HINDEX = 0; (key)[__CHASH_HINDEX] != '\0'; \
__CHASH_HINDEX++) { \
(hash) = (((hash) << 1) + (hash)) + (key)[__CHASH_HINDEX]; \
} \
} while(0)

◆ chash_string_compare

#define chash_string_compare (   cmp_a,
  cmp_b 
)     (strcmp((cmp_a), (cmp_b)) == 0)

◆ chash_default_init

#define chash_default_init (   bucket,
  _key,
  _value 
)
Value:
(bucket).CHASH_KEY_FIELD = (_key); \
(bucket).CHASH_VALUE_FIELD = _value
#define CHASH_VALUE_FIELD
Definition: chash.h:79
#define CHASH_KEY_FIELD
Definition: chash.h:74

◆ __chash_abs

#define __chash_abs (   x)     ((x) < 0 ? (x) * - 1 : (x))

◆ __chash_hash

#define __chash_hash (   mod,
  _key,
  namespace 
)
Value:
__CHASH_HASH = namespace ## _HASH((_key), __CHASH_HASH); \
__CHASH_HASH = __CHASH_HASH % (mod); \
__CHASH_HASH = __chash_abs(__CHASH_HASH);
#define __chash_abs(x)
Definition: chash.h:148

◆ __chash_probe

#define __chash_probe (   hashtable,
  _key,
  namespace 
)
Value:
while(__CHASH_INDEX < (hashtable)->CHASH_CAPACITY_FIELD) { \
if((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD == \
break; \
\
if((namespace ## _COMPARE((_key), \
(hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_KEY_FIELD)) == 1) { \
\
__CHASH_INDEX = -1; \
break; \
} \
\
__CHASH_HASH = (__CHASH_HASH + 1) % (hashtable)->CHASH_CAPACITY_FIELD; \
__CHASH_INDEX++; \
} \
#define CHASH_BUCKETS_FIELD
Definition: chash.h:89
#define CHASH_CAPACITY_FIELD
Definition: chash.h:99
#define CHASH_STATE_FIELD
Definition: chash.h:84
#define CHASH_UNFILLED
Definition: chash.h:103

◆ __chash_probe_to_unfilled

#define __chash_probe_to_unfilled (   mod,
  _key,
  buffer,
  namespace 
)
Value:
while(1) { \
if(buffer[__CHASH_HASH].CHASH_STATE_FIELD != CHASH_FILLED) \
break; \
\
if((namespace ## _COMPARE((_key), buffer[__CHASH_HASH].CHASH_KEY_FIELD)) \
== 1) \
break; \
\
__CHASH_HASH = (__CHASH_HASH + 1) % mod; \
} \
#define CHASH_FILLED
Definition: chash.h:104

◆ __chash_resize

#define __chash_resize (   hashtable,
  namespace 
)

◆ __chash_assert_nonnull

#define __chash_assert_nonnull (   func,
  ptr 
)
Value:
do { \
if((ptr) == NULL) { \
fprintf(stderr, #func ": " #ptr " cannot be null (%s:%i)\n", \
__FILE__, __LINE__); \
abort(); \
} \
} while(0)

◆ __chash_init

#define __chash_init (   hashtable,
  namespace 
)
Value:
(hashtable)->CHASH_LENGTH_FIELD = 0; \
(hashtable)->CHASH_BUCKETS_FIELD = malloc(CHASH_INITIAL_SIZE \
* sizeof(*((hashtable)->CHASH_BUCKETS_FIELD))); \
memset((hashtable)->CHASH_BUCKETS_FIELD, 0, \
sizeof(*((hashtable)->CHASH_BUCKETS_FIELD)) * CHASH_INITIAL_SIZE)
#define CHASH_INITIAL_SIZE
Definition: chash.h:50
#define CHASH_LENGTH_FIELD
Definition: chash.h:94

◆ chash_init

#define chash_init (   hashtable,
  namespace 
)
Value:
NULL; \
\
(hashtable) = malloc(sizeof((*(hashtable)))); \
__chash_init(hashtable, namespace)

◆ chash_init_stack

#define chash_init_stack (   hashtable,
  buffer,
  _length,
  namespace 
)
Value:
(*(hashtable)); \
\
if((_length) <= 0) { \
fprintf(stderr, "chash_init_stack: hashtable cannot have a maximum " \
"length of 0 or less (%s:%i)\n", __FILE__, __LINE__); \
abort(); \
} \
\
(hashtable)->CHASH_LENGTH_FIELD = 0; \
(hashtable)->CHASH_CAPACITY_FIELD = _length; \
(hashtable)->CHASH_BUCKETS_FIELD = buffer
#define __chash_assert_nonnull(func, ptr)
Definition: chash.h:243
#define chash_init_stack(hashtable, buffer, _length, namespace)
Definition: chash.h:282

◆ chash_assign

#define chash_assign (   hashtable,
  _key,
  _value,
  namespace 
)
Value:
do { \
long __CHASH_HASH = 0; \
namespace ## _BUCKET __CHASH_KEY_BUCKET; \
memset(&__CHASH_KEY_BUCKET, 0, sizeof(namespace ## _BUCKET)); \
namespace ## _INIT(__CHASH_KEY_BUCKET, _key, _value); \
__chash_assert_nonnull(chash_assign, (hashtable)->CHASH_BUCKETS_FIELD); \
__chash_resize(hashtable, namespace); \
__chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
__chash_probe_to_unfilled((hashtable)->CHASH_CAPACITY_FIELD, \
(_key), (hashtable)->CHASH_BUCKETS_FIELD, namespace) \
\
if((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD == \
namespace ## _FREE_VALUE( \
(hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_VALUE_FIELD); \
} else { \
(hashtable)->CHASH_LENGTH_FIELD++; \
} \
\
(hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH] = __CHASH_KEY_BUCKET; \
(hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD = \
} while(0)
#define chash_assign(hashtable, _key, _value, namespace)
Definition: chash.h:297

◆ chash_lookup

#define chash_lookup (   hashtable,
  _key,
  storage,
  namespace 
)
Value:
storage; \
\
do { \
int __CHASH_INDEX = 0; \
long __CHASH_HASH = 0; \
namespace ## _BUCKET __CHASH_KEY_BUCKET; \
memset(&__CHASH_KEY_BUCKET, 0, sizeof(namespace ## _BUCKET)); \
namespace ## _INIT(__CHASH_KEY_BUCKET, _key, \
__CHASH_KEY_BUCKET.CHASH_VALUE_FIELD); \
\
(void) __CHASH_KEY_BUCKET; \
__chash_assert_nonnull(chash_lookup, (hashtable)->CHASH_BUCKETS_FIELD); \
__chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
__chash_probe(hashtable, _key, namespace) \
\
if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
CHASH_FILLED) || __CHASH_INDEX != -1) { \
fprintf(stderr, "chash_lookup: failed to find key in hashtable (%s:%i)" \
"\n", __FILE__, __LINE__); \
abort(); \
} \
\
storage = (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_VALUE_FIELD; \
} while(0)
#define chash_lookup(hashtable, _key, storage, namespace)
Definition: chash.h:324

◆ chash_delete

#define chash_delete (   hashtable,
  _key,
  namespace 
)
Value:
do { \
int __CHASH_INDEX = 0; \
long __CHASH_HASH = 0; \
__chash_assert_nonnull(chash_delete, (hashtable)->CHASH_BUCKETS_FIELD); \
__chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
__chash_probe(hashtable, _key, namespace) \
\
if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
CHASH_FILLED) || __CHASH_INDEX != -1) { \
fprintf(stderr, "chash_delete: failed to find key in hashtable (%s:%i)" \
"\n", __FILE__, __LINE__); \
abort(); \
} \
\
namespace ## _FREE_KEY((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH] \
namespace ## _FREE_VALUE( \
(hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_VALUE_FIELD); \
(hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD = \
(hashtable)->CHASH_LENGTH_FIELD--; \
} while(0)
#define CHASH_TOMBSTONE
Definition: chash.h:105
#define chash_delete(hashtable, _key, namespace)
Definition: chash.h:352

◆ chash_contains

#define chash_contains (   hashtable,
  _key,
  storage,
  namespace 
)
Value:
1; \
\
do { \
int __CHASH_INDEX = 0; \
long __CHASH_HASH = 0; \
__chash_assert_nonnull(chash_contents, hashtable); \
__chash_assert_nonnull(chash_contents, (hashtable)->CHASH_BUCKETS_FIELD); \
__chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
__chash_probe(hashtable, _key, namespace) \
\
if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
CHASH_FILLED) || __CHASH_INDEX != -1) { \
storage = 0; \
} \
} while(0)

◆ chash_lookup_bucket

#define chash_lookup_bucket (   hashtable,
  _key,
  storage,
  namespace 
)
Value:
storage; \
\
do { \
CHASH_COUNTER_TYPE __CHASH_INDEX = 0; \
long __CHASH_HASH = 0; \
namespace ## _BUCKET __CHASH_KEY_BUCKET; \
memset(&__CHASH_KEY_BUCKET, 0, sizeof(namespace ## _BUCKET)); \
namespace ## _INIT(__CHASH_KEY_BUCKET, _key, \
__CHASH_KEY_BUCKET.CHASH_VALUE_FIELD); \
\
(void) __CHASH_KEY_BUCKET; \
__chash_assert_nonnull(chash_lookup_bucket, \
(hashtable)->CHASH_BUCKETS_FIELD); \
__chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
__chash_probe(hashtable, _key, namespace) \
\
if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
CHASH_FILLED) || __CHASH_INDEX != -1) { \
fprintf(stderr, "chash_lookup_bucket: failed to find key in hashtable" \
"(%s:%i) \n", __FILE__, __LINE__); \
abort(); \
} \
\
storage = ((hashtable)->CHASH_BUCKETS_FIELD + __CHASH_HASH); \
} while(0)
#define chash_lookup_bucket(hashtable, _key, storage, namespace)
Definition: chash.h:396

◆ __chash_free

#define __chash_free (   hashtable,
  namespace 
)
Value:
do { \
__chash_assert_nonnull(__chash_free, hashtable); \
__chash_assert_nonnull(__chash_free, (hashtable)->CHASH_BUCKETS_FIELD); \
(hashtable)->CHASH_CAPACITY_FIELD--; \
\
while((hashtable)->CHASH_CAPACITY_FIELD != -1) { \
if((hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
(hashtable)->CHASH_CAPACITY_FIELD--; \
continue; \
} \
\
namespace ##_FREE_KEY( \
(hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
namespace ##_FREE_VALUE( \
(hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
(hashtable)->CHASH_CAPACITY_FIELD--; \
(hashtable)->CHASH_LENGTH_FIELD--; \
} \
\
if((namespace ## _HEAP) == 1) { \
free((hashtable)->CHASH_BUCKETS_FIELD); \
} \
} while(0)
#define __chash_free(hashtable, namespace)
Definition: chash.h:425

◆ chash_free

#define chash_free (   hashtable,
  namespace 
)
Value:
do { \
__chash_assert_nonnull(chash_free, hashtable); \
__chash_assert_nonnull(chash_free, (hashtable)->CHASH_BUCKETS_FIELD); \
(hashtable)->CHASH_CAPACITY_FIELD--; \
\
while((hashtable)->CHASH_CAPACITY_FIELD != -1) { \
if((hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
(hashtable)->CHASH_CAPACITY_FIELD--; \
continue; \
} \
\
namespace ##_FREE_KEY( \
(hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
namespace ##_FREE_VALUE( \
(hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
(hashtable)->CHASH_CAPACITY_FIELD--; \
(hashtable)->CHASH_LENGTH_FIELD--; \
} \
\
if((namespace ## _HEAP) == 1) { \
free((hashtable)->CHASH_BUCKETS_FIELD); \
free((hashtable)); \
} \
} while(0)
#define chash_free(hashtable, namespace)
Definition: chash.h:453

◆ chash_is_full

#define chash_is_full (   hashtable,
  namespace 
)     (((hashtable)->CHASH_LENGTH_FIELD) == ((hashtable)->CHASH_CAPACITY_FIELD))

◆ chash_iter

#define chash_iter (   hashtable,
  index,
  _key,
  _value 
)
Value:
for((index) = 0, (_key) = (hashtable)->CHASH_BUCKETS_FIELD[index]. \
(_value) = (hashtable)->CHASH_BUCKETS_FIELD[index].CHASH_VALUE_FIELD; \
(index) < (hashtable)->CHASH_CAPACITY_FIELD; \
(index) = ((index) < (hashtable)->CHASH_CAPACITY_FIELD) \
? ((index) + 1) : index, \
(_key) = (hashtable)->CHASH_BUCKETS_FIELD[index].CHASH_KEY_FIELD, \
(_value) = (hashtable)->CHASH_BUCKETS_FIELD[index].CHASH_VALUE_FIELD, \
(index) = (hashtable)->CHASH_CAPACITY_FIELD)

◆ chash_skip

#define chash_skip (   hashtable,
  index 
)
Value:
if((hashtable)->CHASH_BUCKETS_FIELD[index]. \
continue;