DSA Hacking Club Library
Loading...
Searching...
No Matches
hashtable.h File Reference

A simple, generic hash table implementation using a dynamic struct. More...

#include <stdbool.h>
#include <stddef.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.

Classes

struct  hashtable
 The hash table structure. More...

Macros

#define DELETED_NODE    ((void *)0xFFFFFFFFFFFFFFFUL)
 Special marker for deleted entries.

Typedefs

typedef const char *(* get_key_func) (const void *)
 Function pointer type for extracting a string key from an object.

Functions

void init_hash_table (hashtable *ht, get_key_func get_key, size_t size)
 Initializes the hash table with a given size.
void free_hash_table (hashtable *ht)
 Frees the dynamically allocated table inside the hash table.
void print_table (const hashtable *ht)
 Prints the current state of the hash table to the console.
bool hash_table_insert (hashtable *ht, void *item)
 Inserts an object into the hash table using linear probing.
void * hash_table_lookup (const hashtable *ht, const char *key)
 Looks up an object in the hash table by its string key.
void * hash_table_delete (hashtable *ht, const char *key)
 Deletes an object from the hash table by its string key.

Detailed Description

A simple, generic hash table implementation using a dynamic struct.

Author
Alifoo
Date
2025-09-03

This file provides the public interface for a hash table that can store any kind of data. The table size is dynamic and allocated at runtime.

Typedef Documentation

◆ get_key_func

typedef const char *(* get_key_func) (const void *)

Function pointer type for extracting a string key from an object.

The function takes a pointer to an object and returns a pointer to a string (the key) associated with that object.

Function Documentation

◆ free_hash_table()

void free_hash_table ( hashtable * ht)

Frees the dynamically allocated table inside the hash table.

Does not free the stored objects themselves; the user must do that separately.

Parameters
[in,out]htA pointer to the hashtable instance.

◆ hash_table_delete()

void * hash_table_delete ( hashtable * ht,
const char * key )

Deletes an object from the hash table by its string key.

This function marks the slot as deleted to preserve the probing chain.

Parameters
[in,out]htA pointer to the hashtable instance.
[in]keyThe key string of the object to delete.
Returns
A pointer to the deleted object if found, otherwise NULL.

◆ hash_table_insert()

bool hash_table_insert ( hashtable * ht,
void * item )

Inserts an object into the hash table using linear probing.

Parameters
[in,out]htA pointer to the hashtable instance.
[in]itemA pointer to the object to insert. The object is not copied.
Returns
true if the insertion was successful, false if the table is full.

◆ hash_table_lookup()

void * hash_table_lookup ( const hashtable * ht,
const char * key )

Looks up an object in the hash table by its string key.

Parameters
[in]htA pointer to the hashtable instance.
[in]keyThe key string to search for.
Returns
A pointer to the object if found, otherwise NULL.

◆ init_hash_table()

void init_hash_table ( hashtable * ht,
get_key_func get_key,
size_t size )

Initializes the hash table with a given size.

Parameters
[in,out]htA pointer to the hashtable instance.
[in]get_keyA function that extracts a string key from stored objects.
[in]sizeNumber of buckets to allocate for the table.

◆ print_table()

void print_table ( const hashtable * ht)

Prints the current state of the hash table to the console.

Useful for debugging and visualizing the table's contents.

Parameters
[in]htA pointer to the hashtable instance.