# stclib/stc architecture

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

- Interactive diagram: https://gitdiagram.com/stclib/stc
- Repository: https://github.com/stclib/stc

## Overview

##### Project type and purpose
- STC is a C99 library providing generic, typesafe, high-performance containers and algorithms (a “header-only” + mixed static/shared library).
- Users include templated headers for containers and link the small libstc for non-templated modules (cstr, cregex, cspan, random, etc.).
- The repo also contains docs, examples, tests, build scripts (Meson/Ninja, Makefile), and CI workflows.

##### File-structure overview
- Top level: meson.build, Makefile, .github/workflows/meson.yml
- /stc: public headers (containers, algorithms, common macros)
- src: .c implementations for non-templated modules + scripts (singleheader.py)
- docs: API docs (one .md per module), images
- examples: demonstration code organized by feature
- tests: unit tests driven by Meson and custom run.sh

##### High-level components
A. Build System
- Meson + Ninja (meson.build, meson_options.txt)
- Python scripts (singleheader.py)
- Makefile (legacy build target)
B. Public API
- /stc/*.h (templated containers & algorithms)
- Generated single-header variants
C. Library Core
- src/*.c (cstr, cregex, cspan, random, fmt, stc_core)
- Linked into libstc.{a,so,dll}
D. Documentation
- docs/*.md + docs/pics
- algorithm and container API references
E. Examples & Tests
- examples/**
- tests/**
- Dependent on build outputs and headers
F. CI/CD
- .github/workflows/meson.yml (build, test, publish)

##### Relationships & data flow
- Developer (or CI) invokes Meson → generates Ninja build graph.
- Ninja calls:
- Compile src/*.c → libstc
- Copy/include headers → install/CPATH
- Run tests linking libstc + headers
- Build examples linking libstc + headers
- Run singleheader.py → produce standalone headers
- Consumers (example code / user projects) →
- Templated headers for container usage
- Link against libstc for non-templated functionality
- Docs are authored in Markdown and served or published separately.

##### Architectural patterns and principles
- Modular layering:
- Templated code (header-only, inline/static functions)
- Shared library modules (non-templated core)
- Meta-programming via C macros for generic containers (i_type, c_keyclass, etc.)
- Dual-mode compilation: header-only vs separate implementation
- CI integration with Meson to ensure tests/examples build across platforms

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build System
    subgraph "Build System"
        MSbuild["meson.build"]:::buildscript
        MSopt["meson_options.txt"]:::buildscript
        Makefile["Makefile"]:::buildscript
        SHpy["singleheader.py"]:::script
    end

    %% Public API
    subgraph "Public API"
        PubH["include/stc/*.h"]:::code
        PrivH["include/stc/priv/*.h"]:::code
        FmtH["include/c11/fmt.h"]:::code
    end

    %% Library Core
    subgraph "Library Core"
        creg["src/cregex.c"]:::code
        cspanC["src/cspan.c"]:::code
        cstr_core["src/cstr_core.c"]:::code
        cstr_io["src/cstr_io.c"]:::code
        cstr_utf8["src/cstr_utf8.c"]:::code
        csview["src/csview.c"]:::code
        fmtc["src/fmt.c"]:::code
        randc["src/random.c"]:::code
        corec["src/stc_core.c"]:::code
        utf8gen["src/utf8_tab.py"]:::script
        libstc["libstc.{a/.so/.dll}"]:::output
    end

    %% Examples & Tests
    subgraph "Examples & Tests"
        examples["examples/"]:::example
        tests["tests/"]:::example
    end

    %% Documentation
    subgraph "Documentation"
        docsMd["docs/*.md"]:::doc
        docsPics["docs/pics/"]:::doc
    end

    %% CI/CD
    CI[".github/workflows/meson.yml"]:::ci

    %% User Code
    Consumers["User Projects & Examples"]:::external

    %% Relationships
    MSbuild -->|"compile"| libstc
    MSbuild -->|"install headers"| PubH
    MSbuild -->|"run"| SHpy
    SHpy -->|"generate single-header"| PubH

    MSbuild -->|"build & link"| tests
    MSbuild -->|"build & link"| examples

    Consumers -->|"include"| PubH
    Consumers -->|"link"| libstc

    CI -->|"triggers build"| MSbuild
    CI -->|"runs tests"| tests

    %% Click Events
    click MSbuild "https://github.com/stclib/stc/blob/master/meson.build"
    click MSopt "https://github.com/stclib/stc/blob/master/meson_options.txt"
    click Makefile "https://github.com/stclib/stc/tree/master/Makefile"
    click SHpy "https://github.com/stclib/stc/blob/master/src/singleheader.py"
    click PubH "https://github.com/stclib/stc/tree/master/include/stc/"
    click PrivH "https://github.com/stclib/stc/tree/master/include/stc/priv/"
    click FmtH "https://github.com/stclib/stc/blob/master/include/c11/fmt.h"
    click creg "https://github.com/stclib/stc/blob/master/src/cregex.c"
    click cspanC "https://github.com/stclib/stc/blob/master/src/cspan.c"
    click cstr_core "https://github.com/stclib/stc/blob/master/src/cstr_core.c"
    click cstr_io "https://github.com/stclib/stc/blob/master/src/cstr_io.c"
    click cstr_utf8 "https://github.com/stclib/stc/blob/master/src/cstr_utf8.c"
    click csview "https://github.com/stclib/stc/blob/master/src/csview.c"
    click fmtc "https://github.com/stclib/stc/blob/master/src/fmt.c"
    click randc "https://github.com/stclib/stc/blob/master/src/random.c"
    click corec "https://github.com/stclib/stc/blob/master/src/stc_core.c"
    click utf8gen "https://github.com/stclib/stc/blob/master/src/utf8_tab.py"
    click docsMd "https://github.com/stclib/stc/tree/master/docs/"
    click docsPics "https://github.com/stclib/stc/tree/master/docs/pics/"
    click examples "https://github.com/stclib/stc/tree/master/examples/"
    click tests "https://github.com/stclib/stc/tree/master/tests/"
    click CI "https://github.com/stclib/stc/blob/master/.github/workflows/meson.yml"

    %% Styles
    classDef buildscript fill:#f9f,stroke:#333,stroke-width:1px
    classDef script fill:#fcf,stroke:#333,stroke-width:1px
    classDef code fill:#ccf,stroke:#333,stroke-width:1px
    classDef output fill:#cfc,stroke:#333,stroke-width:1px
    classDef example fill:#ffc,stroke:#333,stroke-width:1px
    classDef doc fill:#fcc,stroke:#333,stroke-width:1px
    classDef ci fill:#cff,stroke:#333,stroke-width:1px
    classDef external fill:#eee,stroke:#333,stroke-width:1px
```
