parallax 1.0
command-line based task/todo manager
Loading...
Searching...
No Matches
hashmap.c File Reference

Contains detailed implementation of the public procedures in the hashmap API. More...

#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

Go to the source code of this file.

Functions

HashMaphashmap_create (size_t table_size)
 Creates a hashmap of a fixed size.
void hashmap_set (HashMap *map, char *key, void *value)
 Adds an entry to a hashmap by a key string and value.
void * hashmap_get (HashMap *map, char *key)
 Finds an entry in a non-empty hashmap by a key.
void hashmap_elem_remove (HashMap *map, char *key)
 Removes an entry in a hashmap by its key.
void hashmap_destroy (HashMap *map)
 Frees a hashmap to memory.

Detailed Description

Contains detailed implementation of the public procedures in the hashmap API.

Author
Rommel John Ronduen
Date
2025-10-07

Definition in file hashmap.c.

Function Documentation

◆ hashmap_create()

HashMap * hashmap_create ( size_t table_size)

Creates a hashmap of a fixed size.

Parameters
table_sizeSize of the hash table
Returns
Pointer to the created hashmap

Definition at line 32 of file hashmap.c.

33{
34 HashMap* map = (HashMap*)malloc(sizeof(HashMap));
35 // Allocate memory for the array of buckets
36 map->buckets = (Entry**)calloc(table_size, sizeof(Entry*));
37
38 map->table_size = table_size;
39 return map;
40}
Linked list node definition of an entry in a hash map.
Definition hashmap.h:15
Hashmap structure.
Definition hashmap.h:26
size_t table_size
Definition hashmap.h:28
Entry ** buckets
Definition hashmap.h:27

◆ hashmap_destroy()

void hashmap_destroy ( HashMap * map)

Frees a hashmap to memory.

Parameters
mapPointer to the hashmap

Definition at line 132 of file hashmap.c.

133{
134 size_t table_size = map->table_size;
135 for (size_t i = 0; i < table_size; i++) {
136 Entry* current = map->buckets[i];
137 while (current != NULL) {
138 Entry* temp = current;
139 current = current->next;
140 free(temp->key); // Free the duplicated key
141 free(temp); // Free the entry node
142 }
143 }
144 free(map->buckets); // Free the buckets array
145 free(map); // Free the map structure
146}
struct Entry * next
Definition hashmap.h:18
char * key
Definition hashmap.h:16

◆ hashmap_elem_remove()

void hashmap_elem_remove ( HashMap * map,
char * key )

Removes an entry in a hashmap by its key.

Parameters
mapPointer to the hashmap
keyPointer to the key string

Definition at line 102 of file hashmap.c.

103{
104 unsigned long hash = hash_function(key);
105 size_t index = hash % map->table_size;
106 Entry* current = map->buckets[index];
107 Entry* prev = NULL;
108 while (current != NULL)
109 {
110 if (strcmp(current->key, key) == 0)
111 {
112 break;
113 }
114 prev = current;
115 current = current->next;
116 }
117 if (current == NULL)
118 {
119 printf("Hashmap error: delete node cannot be found\n");
120 return;
121 }
122 if (prev == NULL)
123 {
124 map->buckets[index] = current->next;
125 } else {
126 prev->next = current->next;
127 }
128 free(current->key);
129 free(current);
130}

◆ hashmap_get()

void * hashmap_get ( HashMap * map,
char * key )

Finds an entry in a non-empty hashmap by a key.

Utilizes the standard approach of hashing and appending to non-empty entries.

Parameters
mapPointer to the hashmap
keyPointer to the key string
Returns
Pointer to the found element. NULL if not found

Definition at line 80 of file hashmap.c.

81{
82 if (map->table_size <= 0) // invalid table size fallback
83 {
84 printf("Hashmap error: Invalid table size (%zu)\n", map->table_size);
85 return NULL;
86 }
87
88 unsigned long hash = hash_function(key);
89 int index = hash % map->table_size;
90
91 Entry* current = map->buckets[index];
92 while (current != NULL) {
93 if (strcmp(current->key, key) == 0) {
94 return current->value; // Key found
95 }
96 current = current->next;
97 }
98
99 return NULL;
100}
void * value
Definition hashmap.h:17

◆ hashmap_set()

void hashmap_set ( HashMap * map,
char * key,
void * value )

Adds an entry to a hashmap by a key string and value.

Parameters
mapPointer to the hashmap
keyPointer to the key string
valuePointer to the value
Note
This function utilizes the standard approach in inserting elements into the hash table. The private hash function is used for transforming the key into a hash. The modulo of the hash with the table size is the unique index of the key.
There are two cases in setting entries:
  • (Case 1) The entry is empty, where it is replaced with the valid entry
  • (Case 2) An existing entry exists. Append to the end of the linked list.

Definition at line 42 of file hashmap.c.

43{
44 unsigned long hash = hash_function(key);
45 int index = hash % map->table_size;
46
59
60 // Case 2: with hash collision
61 Entry* current = map->buckets[index];
62 while (current != NULL) {
63 if (strcmp(current->key, key) == 0) {
64 current->value = value;
65 return;
66 }
67 current = current->next;
68 }
69
70 // Case 1: no hash collision
71 Entry* new_entry = (Entry*)malloc(sizeof(Entry));
72 new_entry->key = strdup(key); // Use strdup to copy the key
73 new_entry->value = value;
74 new_entry->next = NULL;
75
76 new_entry->next = map->buckets[index];
77 map->buckets[index] = new_entry;
78}