Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
HashTable.h File Reference

Hash tables with user-supplied hash and equality functions for raw C types. More...

Go to the source code of this file.

Data Structures

struct  HashTable
 Hash tables with user-supplied hash and equality functions. More...
 

Typedefs

typedef void(* HashTableEnumerator) (const HashTable *table, ident key, ident value, ident data)
 The HashTableEnumerator function type.
 
typedef bool(* HashTableEqualFunc) (const ident a, const ident b)
 A function that tests equality of two keys.
 
typedef size_t(* HashTableHashFunc) (const ident key)
 A function that computes a hash for the given key.
 

Functions

OBJECTIVELY_EXPORT Class_HashTable (void)
 
OBJECTIVELY_EXPORT bool HashTableEqualDirect (const ident a, const ident b)
 
OBJECTIVELY_EXPORT bool HashTableEqualStr (const ident a, const ident b)
 
OBJECTIVELY_EXPORT bool HashTableEqualStri (const ident a, const ident b)
 
OBJECTIVELY_EXPORT size_t HashTableHashDirect (const ident key)
 
OBJECTIVELY_EXPORT size_t HashTableHashStr (const ident key)
 Common hash functions for use with HashTable.
 
OBJECTIVELY_EXPORT size_t HashTableHashStri (const ident key)
 

Detailed Description

Hash tables with user-supplied hash and equality functions for raw C types.

Definition in file HashTable.h.

Typedef Documentation

◆ HashTableEnumerator

typedef void(* HashTableEnumerator) (const HashTable *table, ident key, ident value, ident data)

The HashTableEnumerator function type.

Parameters
tableThe HashTable.
keyThe key.
valueThe value.
dataUser data.

Definition at line 53 of file HashTable.h.

◆ HashTableEqualFunc

typedef bool(* HashTableEqualFunc) (const ident a, const ident b)

A function that tests equality of two keys.

Definition at line 44 of file HashTable.h.

◆ HashTableHashFunc

typedef size_t(* HashTableHashFunc) (const ident key)

A function that computes a hash for the given key.

Definition at line 39 of file HashTable.h.

Function Documentation

◆ _HashTable()

OBJECTIVELY_EXPORT Class * _HashTable ( void  )

Definition at line 328 of file HashTable.c.

328 {
329 static Class *clazz;
330 static Once once;
331
332 do_once(&once, {
333 clazz = _initialize(&(const ClassDef) {
334 .name = "HashTable",
335 .superclass = _Object(),
336 .instanceSize = sizeof(HashTable),
337 .interfaceOffset = offsetof(HashTable, interface),
338 .interfaceSize = sizeof(HashTableInterface),
340 });
341 });
342
343 return clazz;
344}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
static void initialize(Class *clazz)
Definition HashTable.c:310
Class * _Object(void)
Definition Object.c:136
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:95
Hash tables with user-supplied hash and equality functions.
Definition HashTable.h:80

◆ HashTableEqualDirect()

OBJECTIVELY_EXPORT bool HashTableEqualDirect ( const ident  a,
const ident  b 
)
See also
HashTableEqualDirect

Definition at line 92 of file HashTable.c.

92 {
93 return a == b;
94}

◆ HashTableEqualStr()

OBJECTIVELY_EXPORT bool HashTableEqualStr ( const ident  a,
const ident  b 
)
See also
HashTableEqualStr

Definition at line 58 of file HashTable.c.

58 {
59 return strcmp((const char *) a, (const char *) b) == 0;
60}

◆ HashTableEqualStri()

OBJECTIVELY_EXPORT bool HashTableEqualStri ( const ident  a,
const ident  b 
)
See also
HashTableEqualStri

Definition at line 78 of file HashTable.c.

78 {
79 return strcasecmp((const char *) a, (const char *) b) == 0;
80}

◆ HashTableHashDirect()

OBJECTIVELY_EXPORT size_t HashTableHashDirect ( const ident  key)
See also
HashTableHashDirect

Definition at line 85 of file HashTable.c.

85 {
86 return (size_t) (uintptr_t) key;
87}

◆ HashTableHashStr()

OBJECTIVELY_EXPORT size_t HashTableHashStr ( const ident  key)

Common hash functions for use with HashTable.

See also
HashTableHashStr

Definition at line 45 of file HashTable.c.

45 {
46 const char *s = (const char *) key;
47 size_t h = 5381;
48 int c;
49 while ((c = *s++)) {
50 h = ((h << 5) + h) + (size_t) c;
51 }
52 return h;
53}

◆ HashTableHashStri()

OBJECTIVELY_EXPORT size_t HashTableHashStri ( const ident  key)
See also
HashTableHashStri

Definition at line 65 of file HashTable.c.

65 {
66 const char *s = (const char *) key;
67 size_t h = 5381;
68 int c;
69 while ((c = *s++)) {
70 h = ((h << 5) + h) + (size_t) tolower(c);
71 }
72 return h;
73}