attractivechaos/klib logo

attractivechaos/klib

Free

A standalone and lightweight C library

FreeFree tier
Type
Open Source

About attractivechaos/klib

Klib is a standalone, lightweight, and efficient generic library for the C programming language, distributed under the MIT/X11 license. It consists of a collection of independent components, each of which can be used by simply copying a few source files into a project, eliminating external library dependencies beyond the standard C library. Klib emphasizes performance and minimal memory footprint, with components such as khash.h (open-addressing hash table), kbtree.h (B-tree based search tree), kavl.h (intrusive AVL tree), ksort.h (various sorting algorithms including introsort and mergesort), kseq.h (stream buffer and FASTA/FASTQ parser), kvec.h (dynamic array), klist.h (linked list with memory pool), and kstring.h (string library). It also includes utilities for command-line parsing (ketopt.h), numerical routines, HMM, suffix array construction, Smith-Waterman alignment, and more. The library relies on C macros for generic programming, achieving type-specific efficiency without using void* pointers, unlike generic libraries such as Glib.

Key Features

Generic hash table with open addressing (khash.h)
Generic search tree based on B-tree (kbtree.h)
Generic intrusive AVL tree (kavl.h)
Multiple sorting algorithms: introsort, merge sort, heap sort, comb sort, Knuth shuffle, k-small algorithm (ksort.h)
Stream buffer and FASTA/FASTQ parser (kseq.h)
Dynamic array (kvec.h)
Single-linked list with memory pool (klist.h)
String library (kstring.h)
Numerical routines: MT19937-64 pseudorandom generator, nonlinear programming, special math functions (kmath.h)
Portable command-line argument parser with getopt_long-like API (ketopt.h)

Pros & Cons

Pros
  • Standalone and lightweight: no external dependencies beyond the standard C library
  • Components are independent; only need to copy the required files
  • Highly efficient implementations, among the fastest in their class (e.g., khash, kbtree, ksort)
  • Small memory footprint
  • MIT license allows free use in both open-source and proprietary projects
  • Macro-based generic containers achieve type-specific efficiency without type erasure overhead
  • Well-suited for systems programming and bioinformatics applications
Cons
  • Requires working knowledge of C macros; code can be difficult to read and debug
  • No support for C++ directly (though may be usable with extern "C")
  • Lack of dynamic memory safety guarantees inherent in languages with generics or managed memory
  • Documentation is primarily in the README and a separate documentation page; no formal API reference beyond header comments
  • Not a comprehensive standard library like Glib; focuses on a specific set of efficient components

Best For

Embedding efficient data structures (hash tables, trees, arrays, lists) in C programsBioinformatics: parsing FASTA/FASTQ files, sequence alignment with Smith-Waterman, HMM modeling, suffix array constructionNumerical computing: random number generation, nonlinear optimization, special functionsBuilding command-line tools with portable argument parsingRemote file access and streaming for networked applicationsSorting and selecting data with a variety of algorithmsEducation and study of efficient generic programming in C using macros

FAQ

What license is Klib under?
Klib is distributed under the MIT/X11 license.
Does Klib require any external libraries?
Most components are independent of external libraries, except the standard C library.
How do I use Klib in my project?
You only need to copy the relevant source files to your source code tree. No linker dependencies are required.
How does Klib achieve generic containers in C?
Klib extensively uses C macros to instantiate type-specific methods, avoiding the performance overhead of void* pointers used in libraries like Glib.
What are some examples of components available?
Common components include khash.h (hash table), kbtree.h (B-tree), kavl.h (AVL tree), ksort.h (sorting), kseq.h (FASTA/FASTQ parser), kvec.h (dynamic array), klist.h (linked list), kstring.h (string utilities), and kmath.h (numerical routines).