# thealrashid/hash-table-in-c architecture

> Architecture diagram of the GitHub repository thealrashid/hash-table-in-c, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thealrashid/hash-table-in-c
- Repository: https://github.com/thealrashid/hash-table-in-c

## Overview

##### Project Type and Purpose
- This is a standalone C library providing a generic hash‐table implementation (not a full‐stack app or microservice).
- Its primary goal is to offer a lightweight, dynamically resizing hash table with a simple API that client code (e.g., `main.c`, `user_manager.c`) can plug into for storing arbitrary `void*` key/value pairs.

##### File Structure Analysis
- Top‐level files:
- `hashtable.h` – public API and data‐structure definitions
- `main.c` & `user_manager.c` – example/consumer code showing how to use the hash table
- `README.md`, `LICENSE`, `.gitignore` – documentation and project metadata
- There is no separate build or config folder—this is a header + C‐file library you compile/ link with your client code.

##### Key Architectural Elements from the README
- Dynamic resizing (growth & shrink)
- Collision resolution via separate chaining (linked lists of nodes in buckets)
- Customizable hooks for `hash()`, `compare()`, `free_key()`, and `free_value()`
- Simple iterator API

a. Client Module
- `main.c` / `user_manager.c`
- Calls into the public API (`hash_table_insert`, `hash_table_get`, etc.)
b. HashTable Core (Library)
- Public API layer (functions declared in `hashtable.h`)
- `HashTable` struct (contains pointer to bucket array, size counters, load thresholds)
c. Bucket Array
- An array of N buckets (each bucket is a pointer to a linked‐list head)
- Dynamically resized (rehashed) when load factor crosses thresholds
d. Linked‐List Nodes (Chaining)
- Each node holds a key‐value pair and a pointer to the next node in its bucket
e. User‐Provided Callbacks
- `hash(void *key)` → returns bucket index
- `compare(void *a, void *b)` → resolves key equality
- `free_key`, `free_value` → optional cleanup functions
f. Memory Management
- Allocation during insert/resize
- Deallocation during remove/destroy

##### Component Relationships and Data Flows
- Client Module → (calls) → Public API → (invokes) →
- Hash function callback to determine bucket index
- Linked‐list operations for insert/get/remove
- Resize logic when threshold exceeded or shrunk
- Function call direction (e.g., client → `hash_table_insert`)
- Data flow into buckets and nodes
- Callback invocation from core to user code

##### Architectural Patterns & Design Principles
- Separation of concerns: public API vs. internal data structures vs. user callbacks
- Dependency injection via function pointers for hash/compare/free
- Encapsulation of resizing logic inside the library
- Use of separate chaining for collision resolution

##### Essential Elements
- Clear component labels (e.g., “HashTable API,” “Bucket Array,” “Node List”)
- Callout notes for load‐factor thresholds and chaining method

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Modules
    subgraph "Client Modules"
        direction TB
        Main["main.c"]:::client
        UserMgr["user_manager.c"]:::client
    end

    %% Library Core
    subgraph "HashTable Library Core"
        direction TB
        API["HashTable API (hashtable.h)"]:::core
        Struct["HashTable Struct & Internals"]:::core
        Buckets["Bucket Array"]:::storage
        Nodes["Linked-List Nodes"]:::node
        Resize["Dynamic Resizing Logic"]:::core
    end

    %% Callback Hooks
    subgraph "User-Provided Callbacks"
        direction TB
        HashFn(("hash()")):::callback
        Compare(("compare()")):::callback
        FreeKey(("free_key()")):::callback
        FreeValue(("free_value()")):::callback
    end

    %% Documentation & Metadata
    subgraph "Project Documentation & Metadata"
        direction TB
        Readme["README.md"]:::doc
        License["LICENSE"]:::doc
        Gitignore[".gitignore"]:::doc
    end

    %% Connections
    Main -->|calls insert/get/remove| API
    UserMgr -->|calls insert/get/remove| API

    API -->|"compute index"| HashFn
    API -->|"check equality"| Compare
    API -->|"insert/remove node"| Nodes
    API -->|"trigger resize"| Resize

    Resize -->|"rehash buckets"| Buckets
    API -->|"access bucket"| Buckets

    API -->|"cleanup key/value"| FreeKey
    API -->|"cleanup key/value"| FreeValue

    %% Legend
    subgraph "Legend"
        direction TB
        C1["Rectangle (blue) = Core Library Components"]:::core
        C2["Oval (orange) = Callback Hooks"]:::callback
        C3["Cylinder (green) = Storage Structures"]:::storage
        C4["Box (gray) = Chaining Nodes"]:::node
        C5["Document (yellow) = Client & Docs"]:::doc
    end

    %% Click Events
    click Main "https://github.com/thealrashid/hash-table-in-c/blob/master/main.c"
    click UserMgr "https://github.com/thealrashid/hash-table-in-c/blob/master/user_manager.c"
    click API "https://github.com/thealrashid/hash-table-in-c/blob/master/hashtable.h"
    click Readme "https://github.com/thealrashid/hash-table-in-c/blob/master/README.md"
    click License "https://github.com/thealrashid/hash-table-in-c/tree/master/LICENSE"
    click Gitignore "https://github.com/thealrashid/hash-table-in-c/blob/master/.gitignore"

    %% Styles
    classDef core fill:#add8e6,stroke:#333,stroke-width:1px
    classDef callback fill:#ffa500,stroke:#333,stroke-width:1px
    classDef storage fill:#90ee90,stroke:#333,stroke-width:1px
    classDef node fill:#d3d3d3,stroke:#333,stroke-width:1px
    classDef client fill:#fff3cd,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef doc fill:#fff3cd,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
