MCPcopy Create free account
hub / github.com/KaisenAmin/c_std

github.com/KaisenAmin/c_std @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
2,789 symbols 5,648 edges 110 files ⚖ ISC 2,428 documented · 87% ● updated 44d ago★ 1,2527 open issues

Browse by type

⨍ Functions 2,651◇ Types & classes 138
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

C STL — C++ Standard Library & Python-style utilities, reimplemented in C

c_std — C++ STL & Python-style utilities, reimplemented in pure C17

This project reimplements a large slice of the C++ Standard Library (containers, algorithms, smart pointers, …) together with many Python-style conveniences (statistics, random, secrets, json, regex, …) in pure C17. The goal is to give C developers familiar, well-documented building blocks — dynamic arrays, maps, strings, JSON, networking, big integers, and much more — without leaving the C ecosystem.

Highlights

  • Leak-checked. The runnable README examples were run on Linux under Valgrind (--leak-check=full --track-fds=yes) without leaked memory or descriptors. Examples that need a GUI, a PostgreSQL server, serial hardware or the internet were not part of that run.

  • Cross-platform. Builds and runs on Windows (MSVC and MinGW-w64) and Linux (GCC/Clang), with POSIX/Win32 backends behind one API. The library builds without warnings under -Wall -Wextra (GCC and Clang).

  • C17. No C++ — C17 with thin platform shims. The default build uses the GNU dialect (-std=gnu17, for the POSIX/Win32 declarations some modules need); pass -DCMAKE_C_EXTENSIONS=OFF for -std=c17. The <MODULE>_LOG debug macros and the unittest assertion macros are standard C (no , ##__VA_ARGS__), so they stay clean under -std=c17 -Wpedantic.

  • 40+ modules. Containers, algorithms, smart pointers, strings, JSON/XML/CSV/INI, networking (TCP/UDP/HTTP), crypto & JWT, arbitrary-precision math, graphics, and more.

  • Familiar APIs. Modeled on the C++ STL (vector, map, unique_ptr, …) and the Python standard library (random, statistics, json, regex, turtle, …).

  • Runnable examples. Every module README contains complete example programs with the output they print. (The repository does not ship a separate test suite.)

  • Fast & predictable. Efficient data structures (geometric vector growth with inlined push_back/at, typed vector macros and a pdqsort generator faster than std::sort, O(1) average hashmap, O(log n) map), clear ownership rules and deallocators.

  • CMake build. Build the whole library, a single module, or your own example programs, with GCC, Clang, or MSVC.

Benchmarks vs the C++ STL

The library aims to be fast where it counts. The table below pits each C container against its C++ <stl> counterpart on the same workload, the same data, and the same compiler family at -O2. Both programs walk an identical deterministic key stream and compute identical checksums, so they do exactly the same work — only the container implementation differs.

Environment: Windows 10, MinGW-w64 GCC / G++ 14.2.0, -O2, single thread, best of three runs (lower is better). Absolute numbers vary by machine; the ratio is what carries over.

C container ↔ C++ STL Workload (N) C STL C++ STL C ÷ C++
vector ↔ std::vector<int> push_back + sum, 10,000,000 0.040 s 0.030 s 1.3×
vector typed macros ↔ std::vector<int> VECTOR_PUSH_BACK + VECTOR_FOREACH, 10,000,000 0.037 s 0.030 s 1.2×
vector ↔ std::vector<int> fill 10,000,000 computed values (VECTOR_APPEND_UNINIT ↔ resize + []) 0.0099 s 0.0122 s 0.82×
VECTOR_DEFINE_SORT ↔ std::sort sort 5,000,000 random ints 0.157 s 0.317 s 0.50×
algorithm_sort (stable) ↔ std::stable_sort sort 5,000,000 random ints (comparator through a pointer) 0.451 s 0.383 s 1.18×
algorithm_sort_unstable ↔ std::sort sort 5,000,000 random ints (comparator through a pointer) 0.280 s 0.313 s 0.89×
algorithm_filter ↔ std::copy_if keep the odd ones of 5,000,000 random ints 0.0099 s 0.0171 s 0.58×
string ↔ std::string find in 4 MiB of text, ×20 0.0055 s 0.0276 s 0.20×
string ↔ std::string push_back ×20,000,000 / append "abc" ×10,000,000 0.034 / 0.034 s 0.035 / 0.033 s 0.97× / 1.02×
string ↔ std::string substr / trim ×2,000,000 / ×1,000,000 0.092 / 0.054 s 0.182 / 0.095 s 0.51× / 0.57×
map ↔ std::map<int,int> (ordered / RB-tree) insert + lookup, 1,000,000 1.00 s 1.20 s 0.83×
hashmap ↔ std::unordered_map<int,int> insert + lookup, 1,000,000 0.231 s 0.364 s 0.63×
set ↔ std::set<int> (ordered / RB-tree) insert + lookup, 1,000,000 1.17 s 1.05 s 1.1×
priority_queue ↔ std::priority_queue<int> push + pop (heap-sort), 1,000,000 0.156 s 0.108 s 1.4×
deque ↔ std::deque<int> push_back + iterate + pop_front, 1,000,000 0.009 s 0.004 s 2.2×
list ↔ std::list<int> push_back + iterate + teardown, 1,000,000 0.093 s 0.075 s 1.2×
forward_list ↔ std::forward_list<int> push_front + iterate + teardown, 1,000,000 0.080 s 0.076 s 1.05×
queue ↔ std::queue<int> enqueue + dequeue (FIFO), 1,000,000 0.006 s 0.003 s 2.0×
stack ↔ std::stack<int> push + pop (LIFO), 1,000,000 0.005 s 0.003 s 1.7×

C ÷ C++ is the C time divided by the C++ time — below 1.0 means the C library is faster.

  • Where C wins — map, hashmap, bulk vector work and sorting. Both allocate their tree nodes / buckets from a pooled slab allocator, so a million inserts cost a handful of big mallocs instead of a million tiny ones, beating the STL's per-node new. The vector fills in place without std::vector's zero-fill pass (VECTOR_APPEND_UNINIT; zero-initialised growth comes straight from calloc), and VECTOR_DEFINE_SORT generates a pattern-defeating quicksort (pdqsort) for the element type that is about 2× faster than libstdc++'s std::sort on random data. The full comparison — every common operation, generic API and typed macros — is in vector/README.md. algorithm_sort_unstable is a pdqsort too and beats std::sort even though it calls its comparator through a function pointer (algorithm_sort itself is stable: equal elements keep their order); algorithm_filter / copy_if collect the predicate's answers in a bit mask before copying, so a random predicate costs no branch mispredictions. Function-by-function numbers are in algorithm/README.md. String keeps up to 31 bytes inside the struct (small-string optimisation), inlines its hot paths like std::string does, and searches with SSE2 (5–10× faster find); creating many short heap Strings is the one place it trails, by the size of its 64-byte struct — see string/README.md.
  • About set — near parity. Same pooled-slab, inline-element RB-tree as map. Lookups are actually a hair faster than std::set; inserts are a little slower, so it nets ~1.1×. Unlike map (which stores caller-owned pointers and skips the copy), set has value semantics and deep-copies each element exactly like std::set, so it can't win the copy back — and its node carries the publicly-inspectable key/left/right/parent/color fields (so a test can walk them to check the red-black invariants), which a templated std::set node doesn't expose. A large RB-tree is cache-miss-bound, so that slightly larger node is the whole gap.
  • Where C++ wins — per-element vector pushes and priority_queue. Both are tight loops over raw ints. The C vector inlines push_back/at at the call site just like std::vector (reads, copies, appends, insert / erase and pop are at parity), but a growing loop of single pushes stays ~1.3× with the generic API: a runtime itemSize stride and a size-dispatched element copy where C++ has a compile-time sizeof(int), plus the bounds check in vector_at that operator[] skips. The typed macros (VECTOR_PUSH_BACK, VECTOR_FOREACH) remove the type erasure and land at ~1.1–1.2×; what is left is that the Vector is a heap object with public fields, so each push stores its size back to memory where std::vector keeps its pointers in registers. The C priority_queue size-dispatches its heap moves the same way (~1.4×); its residual is the comparator — one indirect call through a function pointer per comparison, which std::priority_queue inlines from its template argument. Either way it's a small fixed per-element cost — the price of a uniform C API — not a worse complexity.
  • About deque. The C deque stores elements inline in byte-budgeted block buffers (one allocation per block, not one malloc per element) and inlines front / back / at with shift-indexed block math like std::deque, so it lands at ~2.2×; the residual is the out-of-line push_back / pop_front calls that std::deque inlines from templates.
  • About list. Each node stores its element inline in the same allocation (one malloc per node, like std::list) instead of a second heap block per element, which roughly halves the allocation/teardown cost and lands it at ~1.2×. The residual is a slightly larger node — the type-erased API keeps a value pointer and aligns the inline element for any type — plus the out-of-line per-node call std::list inlines.
  • About forward_list — at parity. It already uses the same single-node- allocation, inline-element design as std::forward_list, so push_front / iterate / teardown match it within measurement noise (≈1.05×). No type-erasure penalty survives here because the per-node malloc dominates and both pay it once per element.
  • About queue. The C queue is a contiguous vector used as a ring: enqueue appends and dequeue advances a front index (an O(1) bump, not a buffer shift), with the dead front prefix reclaimed in bulk when the buffer fills — so both ends are amortized O(1), like std::queue. The hot ops are inlined; the ~2× residual is the per-element copy into the contiguous buffer vs std::queue's deque node writes. (Previously dequeue shifted the whole buffer — O(n), an O(n²) drain; that's fixed.)
  • About stack. LIFO at the back of a contiguous vector, so push and pop are already O(1). push / top / pop / empty are now inlined in the header (the way std::stack inlines through its container), which cut it from ~3.3× to ~1.7×; the residual is the vector's amortized realloc-copy on growth vs std::stack's default deque adding blocks — in exchange the C stack keeps its elements fully contiguous.

The benchmark programs themselves (bench.c / bench.cpp) are not part of the repository, so treat the table as indicative. To measure on your machine, write a C and a C++ program that run the workloads above over the same key stream, and link the C one against the built library, which contains every container:

cmake -S . -B build && cmake --build build
gcc -O2 bench.c -I. build/libcstd.a -o bench_c && ./bench_c
g++ -O2 -std=c++17 bench.cpp -o bench_cpp && ./bench_cpp

A personal note

I undertake this project out of a deep affection for the C programming language. C remains an essential tool for any computer engineer, providing the foundation needed to build efficient and robust software. This effort aims to enrich the language with the conveniences found in higher-level standard libraries.


Table of contents


Modules

Every module lives in its own directory with a .c source, a .h header, and a README.md containing a full API reference and runnable examples.

Containers

Module Analogous to Description
array std::array Fixed-size array wrapper with bounds-checked access.
vector std::vector Dynamic, automatically resizing array (one contiguous buffer, grown geometrically).
string std::string Growable, binary-safe string with small-string optimisation, SSE2 search and a rich (Python-style) manipulation API (string/std_string.h).
list std::list Doubly linked list.
forward_list std::forward_list Singly linked list.
deque std::deque Double-ended queue with fast insertion/removal at both ends.
queue std::queue FIFO queue.
stack std::stack LIFO stack.
priority_queue std::priority_queue Heap-backed priority queue with a custom comparator.
span std::span Non-owning view over a contiguous sequence.
bitset std::bitset Fixed-size sequence of bits with bitwise operations.
map std::map Ordered associative container (Red-Black tree, O(log n)).
hashmap std::unordered_map Hash table with O(1) average insert/lookup and automatic rehashing.
set std::set Ordered, unique associative container (Red-Black tree, O(log n));
tuple std::tuple Fixed-size

Core symbols most depended-on inside this repo

browse all functions →

Shape

Function 2,651
Class 137
Enum 1

Languages

C96%
C++4%

Modules by API surface

matrix/matrix.c306 symbols
string/std_string.c120 symbols
sysinfo/sysinfo.c103 symbols
variant/variant.c98 symbols
jwt/jwt.c97 symbols
algorithm/algorithm.c89 symbols
database/postgres.c88 symbols
xml/xml.c74 symbols
network/http.c74 symbols
turtle/turtle.c71 symbols
json/json.c70 symbols
network/tcp.c67 symbols

Datastores touched

appDatabase · 1 repos

For agents

$ claude mcp add c_std \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page