explaingit

attractivechaos/klib

4,661CAudience · developerComplexity · 3/5LicenseSetup · easy

TLDR

A fast, header-only C library providing hash tables, trees, dynamic arrays, sorting algorithms, and string utilities as single files you copy into your project with no build system or external dependencies.

Mindmap

mindmap
  root((klib))
    Data structures
      Hash table khash
      B-tree and AVL tree
      Dynamic array kvec
      Linked list with pool
    Algorithms
      Introsort
      Merge sort
    Utilities
      String library
      Argument parser
      RNG
    Design
      Header-only files
      Macro-generated code
      No dependencies
    License
      MIT permissive
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Add a high-performance hash table to a C project by copying a single header file with no build system changes.

USE CASE 2

Replace a slow sorting implementation in a C program with klib's introsort or merge sort for better performance.

USE CASE 3

Use klib's type-safe dynamic array as a growable list in C without writing boilerplate container code by hand.

Tech stack

C

Getting it running

Difficulty · easy Time to first run · 30min

The macro-based code generation can make debugging harder, there is no build system, copy the header files you want directly into your project.

Use, modify, and share freely for any purpose, including commercial use, as long as you keep the copyright notice.

In plain English

Klib is a small collection of building blocks for C programs. It provides things like hash tables, sorted search trees, dynamic arrays, sorting algorithms, and string utilities in a single library with no external dependencies beyond the standard C library. Each component is contained in one or two files, so you can drop just the pieces you need directly into your own project rather than linking in a whole framework. The library is aimed at C developers who want these common data structures at high speed and low memory use. The README claims that components like the hash table and sorting implementations compete with the best equivalents found in any programming language, in both speed and memory. It backs this up by explaining the design choice that makes this possible: the library uses C macros extensively to generate type-specific code at compile time. This is different from using a generic void pointer approach, where the compiler cannot optimize the code because it does not know the actual type being stored. The tradeoff for that speed is that the code looks unusual. Using the hash table, for example, requires calling a macro that expands into dozens of lines of type-specific struct definitions and functions. This is not how typical library code looks, and the README acknowledges it can make code harder to read and debug. The advantage is that the compiler sees fully typed code and can optimize it as aggressively as if you had written it by hand for a specific type. Components include a hash table with open addressing, a B-tree and an AVL tree for sorted data, several sorting algorithms including introsort and merge sort, a generic dynamic array, a linked list with a memory pool, a string library, a command-line argument parser, and a pseudorandom number generator. There are also more specialized components for reading remote files over HTTP or FTP and for parsing bioinformatics file formats. Klib is distributed under the MIT license and has no required build system: you copy the files you want and compile them with your code.

Copy-paste prompts

Prompt 1
How do I use klib's khash.h to create a hash table mapping strings to integers in a C program?
Prompt 2
Show me how to include klib's dynamic array kvec.h in my C project and push and pop elements from it.
Prompt 3
How do I use klib's ksort.h to sort an array of custom structs by a specific field in C?
Prompt 4
What is the tradeoff between klib's macro-based approach to data structures and a void-pointer generic library in C?
Open on GitHub → Explain another repo

← attractivechaos on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.