Convert Figma logo to code with AI

jamesroutley logowrite-a-hash-table

✏️ Learn how to write a hash table in C

3,435
279
3,435
20

Top Related Projects

4,117

A standalone and lightweight C library

4,076

C macros for hash tables and more

4,889

Simple Dynamic Strings library for C

26,312

stb single-file public domain libraries for C/C++

Quick Overview

The "write-a-hash-table" repository by James Routley is an educational project that guides developers through the process of implementing a hash table data structure in C. It provides a step-by-step tutorial on creating a hash table from scratch, explaining key concepts and implementation details along the way.

Pros

  • Offers a hands-on learning experience for understanding hash table internals
  • Provides clear, well-commented code examples for each step of the implementation
  • Includes explanations of important concepts like hash functions and collision resolution
  • Serves as a practical resource for both beginners and intermediate C programmers

Cons

  • Limited to a basic hash table implementation, not covering advanced features or optimizations
  • Focuses solely on C, which may not be ideal for developers working with other languages
  • Does not include extensive performance comparisons or benchmarks
  • May require some prior knowledge of C programming and data structures

Code Examples

  1. Creating a new hash table:
ht_hash_table* ht = ht_new();
  1. Inserting a key-value pair:
ht_insert(ht, "key", "value");
  1. Retrieving a value by key:
char* value = ht_search(ht, "key");
  1. Deleting a key-value pair:
ht_delete(ht, "key");

Getting Started

To get started with the project:

  1. Clone the repository:

    git clone https://github.com/jamesroutley/write-a-hash-table.git
    
  2. Navigate to the project directory:

    cd write-a-hash-table
    
  3. Compile the code:

    gcc -Wall -g src/*.c -o hash_table
    
  4. Run the compiled program:

    ./hash_table
    
  5. Follow the tutorial in the README.md file to understand the implementation details and experiment with the code.

Competitor Comparisons

4,117

A standalone and lightweight C library

Pros of klib

  • Comprehensive library with multiple data structures and algorithms
  • Highly optimized for performance
  • Extensively tested and widely used in production environments

Cons of klib

  • More complex and harder to understand for beginners
  • Less focused on educational purposes
  • Requires more setup and integration into existing projects

Code Comparison

write-a-hash-table:

typedef struct {
    char* key;
    char* value;
} ht_item;

typedef struct {
    int size;
    int count;
    ht_item** items;
} ht_hash_table;

klib:

#include "khash.h"
KHASH_MAP_INIT_STR(str, char*)
khash_t(str) *h;
h = kh_init(str);
int ret, is_missing;
khiter_t k = kh_put(str, h, "key", &ret);
kh_value(h, k) = strdup("value");

Summary

write-a-hash-table is designed as an educational resource for understanding hash table implementation, making it ideal for beginners. It provides a clear, step-by-step approach to building a hash table from scratch.

klib, on the other hand, is a more comprehensive and performance-oriented library. It offers a wide range of data structures and algorithms, including hash tables, making it suitable for production use. However, its complexity may be overwhelming for those new to the concept.

The code comparison shows the difference in approach: write-a-hash-table uses a more traditional struct-based implementation, while klib employs macros for efficient, type-safe generic programming.

4,076

C macros for hash tables and more

Pros of uthash

  • More feature-rich and versatile, supporting various hash table operations
  • Better performance optimization for large-scale applications
  • Extensive documentation and examples for different use cases

Cons of uthash

  • More complex implementation, potentially harder for beginners to understand
  • Requires including a header file, which may increase compilation time
  • Less focused on educational purposes compared to write-a-hash-table

Code Comparison

write-a-hash-table:

typedef struct {
    char* key;
    char* value;
} ht_item;

typedef struct {
    int size;
    int count;
    ht_item** items;
} ht_hash_table;

uthash:

#include "uthash.h"

struct my_struct {
    int id;
    char name[10];
    UT_hash_handle hh;
};

uthash offers a more compact way to define hash table structures using macros, while write-a-hash-table provides a more explicit and educational approach to implementing hash tables from scratch. The uthash implementation is more suitable for production use, whereas write-a-hash-table is better for learning the inner workings of hash tables.

4,889

Simple Dynamic Strings library for C

Pros of sds

  • More comprehensive and production-ready implementation
  • Optimized for performance with low memory overhead
  • Widely used in real-world applications (e.g., Redis)

Cons of sds

  • More complex codebase, potentially harder to understand for beginners
  • Focuses solely on string handling, not a complete hash table implementation
  • Requires more setup and integration effort for use in projects

Code Comparison

write-a-hash-table:

typedef struct {
    char* key;
    char* value;
} ht_item;

typedef struct {
    int size;
    int count;
    ht_item** items;
} ht_hash_table;

sds:

typedef char *sds;

struct sdshdr {
    unsigned int len;
    unsigned int free;
    char buf[];
};

Summary

write-a-hash-table is a simpler, educational implementation of a hash table, making it easier for beginners to understand the core concepts. sds, on the other hand, is a more advanced and optimized string handling library, focusing on performance and real-world usage. While write-a-hash-table provides a complete hash table implementation, sds specializes in efficient string manipulation, which is crucial for many applications, including Redis.

26,312

stb single-file public domain libraries for C/C++

Pros of stb

  • Comprehensive collection of single-file libraries for various purposes
  • Well-established and widely used in the industry
  • Designed for easy integration into existing projects

Cons of stb

  • Larger codebase, potentially more complex to understand fully
  • May include unnecessary functionality for specific use cases
  • Less focused on educational purposes compared to write-a-hash-table

Code Comparison

write-a-hash-table:

typedef struct {
    char* key;
    char* value;
} ht_item;

typedef struct {
    int size;
    int count;
    ht_item** items;
} ht_hash_table;

stb:

typedef struct
{
   void *next;
   char *key;
   union {
      void *value;
      int i;
      float f;
   };
} stb_idict;

Summary

write-a-hash-table is a focused, educational project aimed at teaching hash table implementation. It provides a clear, step-by-step approach to building a hash table from scratch.

stb, on the other hand, is a collection of versatile, single-file libraries for various purposes, including hash tables. It offers a more comprehensive set of tools but may be less suitable for learning the specifics of hash table implementation.

The code comparison shows that write-a-hash-table uses a simpler structure for hash table items, while stb's implementation is more flexible, supporting different value types through a union.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Write a hash table in C

Hash tables are one of the most useful data structures. Their quick and scalable insert, search and delete make them relevant to a large number of computer science problems.

In this tutorial, we implement an open-addressed, double-hashed hash table in C. By working through this tutorial, you will gain:

  • Understanding of how a fundamental data structure works under the hood
  • Deeper knowledge of when to use hash tables, when not to use them, and how they can fail
  • Exposure to new C code

C is a great language to write a hash table in because:

  • The language doesn't come with one included
  • It is a low-level language, so you get deeper exposure to how things work at a machine level

This tutorial assumes some familiarity with programming and C syntax. The code itself is relatively straightforward, and most issues should be solvable with a web search. If you run into further problems, please open a GitHub Issue.

The full implementation is around 200 lines of code, and should take around an hour or two to work through.

Contents

  1. Introduction
  2. Hash table structure
  3. Hash functions
  4. Handling collisions
  5. Hash table methods
  6. Resizing tables
  7. Appendix: alternative collision handling

Credits

This tutorial was written by James Routley, who blogs at routley.io.