libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
hashtable.h File Reference
#include <stdint.h>
#include "types/err.h"
#include "types/hashtable.h"
Include dependency graph for hashtable.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void hashtable_init (hashtable_t *hashtable, unsigned int size)
 Initializes a hashtable.
 
void hashtable_close (hashtable_t *hashtable)
 Close the given hashtable.
 
void hashtable_set (hashtable_t *hashtable, const char *key, int64_t value)
 Set the value of the specified key inside the hashtable.
 
error_code_t hashtable_get (hashtable_t *hashtable, const char *key, int64_t *value)
 Get the value of the specified key inside the hashtable.
 

Function Documentation

◆ hashtable_close()

void hashtable_close ( hashtable_t * hashtable)

Close the given hashtable.

Parameters
hashtableThe hashtable to be closed.

Definition at line 26 of file hashtable.c.

27{
28 for (int i = 0; i < hashtable->_size; i++) {
29 if (! hashtable->items[i].is_set) {
30 continue;
31 }
32
34
35 if (hashtable->items[i]._collisions_length == 0) {
36 continue;
37 }
38
39 for (int j = 0; j < hashtable->items[i]._collisions_length; j++) {
41 }
42
43 free(hashtable->items[i]._collisions);
44 }
45
46 free(hashtable->items);
47 hashtable->items = NULL;
48}
void dstring_close(dstring_t *string)
Closes the dynamic string.
Definition dstring.c:18
struct hashitem * _collisions
List of another items with hash colliding with this.
Definition hashtable.h:21
bool is_set
Is true if this item has set.
Definition hashtable.h:18
dstring_t key
The item's key.
Definition hashtable.h:19
unsigned int _collisions_length
The number of items on the _collisions list.
Definition hashtable.h:23
A hashtable.
Definition hashtable.h:31
unsigned int _size
The number of item slots of the items allocated memory.
Definition hashtable.h:33
hashitem_t * items
List of items on the hashtable.
Definition hashtable.h:32

◆ hashtable_get()

error_code_t hashtable_get ( hashtable_t * hashtable,
const char * key,
int64_t * value )

Get the value of the specified key inside the hashtable.

Parameters
hashtableThe hashtable where to get the value.
keyThe key of the value.
valuePointer to where the value will be saved.
Returns
ERR_OK on the key has found in hashtable and him value has get.
ERR_INVALID_INDEX on the key is not set.

Definition at line 73 of file hashtable.c.

74{
75 hash_t index = _hash_djb2(key) % hashtable->_size;
76 hashitem_t* item = &hashtable->items[index];
77
78 if (! item->is_set) {
79 return ERR_INVALID_INDEX;
80 }
81
82 if (strcmp(item->key.data, key) == 0) {
83 *value = item->value;
84 return ERR_OK;
85 }
86
87 item = _hashtable_find_collision(item, key);
88 if (item == NULL) {
89 return ERR_INVALID_INDEX;
90 }
91
92 *value = item->value;
93 return ERR_OK;
94}
@ ERR_INVALID_INDEX
Definition err.h:19
@ ERR_OK
Definition err.h:15
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12
int64_t value
The value of the item.
Definition hashtable.h:20
struct hashitem hashitem_t
An item inside a hashtable.
uint64_t hash_t
Definition hashtable.h:11

◆ hashtable_init()

void hashtable_init ( hashtable_t * hashtable,
unsigned int size )

Initializes a hashtable.

Parameters
hashtableThe hashtable to be initialized.
sizeThe number of item slots of the items allocated memory. Bigger means: + memory, - collision.

Definition at line 18 of file hashtable.c.

19{
20 hashtable->_size = size;
21
22 hashtable->items = calloc(size, sizeof(hashitem_t));
23}

◆ hashtable_set()

void hashtable_set ( hashtable_t * hashtable,
const char * key,
int64_t value )

Set the value of the specified key inside the hashtable.

Parameters
hashtableThe hashtable where the key is.
keyThe key.
valueThe value to set for the key.

Definition at line 51 of file hashtable.c.

52{
53 hash_t index = _hash_djb2(key) % hashtable->_size;
54 hashitem_t* item = &hashtable->items[index];
55
56 if (! item->is_set) {
57 item->is_set = true;
58 item->value = value;
59 dstring_init(&item->key, 0);
60 dstring_copy(&item->key, key);
61 return;
62 }
63
64 if (strcmp(item->key.data, key) == 0) {
65 item->value = value;
66 return;
67 }
68
69 _hashtable_add_collision(item, key, value);
70}
void dstring_init(dstring_t *string, unsigned int buffer_size)
Initializes a dynamic string (dstring).
Definition dstring.c:10
void dstring_copy(dstring_t *string, const char *source)
Copies the content of source to the dstring.
Definition dstring.c:50