diff --git a/.gitignore b/.gitignore index db73348..c85a53d 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,8 @@ npm-debug.log .nextflow.log* .nextflow/ nf_workspace/ +work/ +nextflow # Input/Output directories input/ @@ -60,3 +62,8 @@ uniprot/outputs/parquet/ # Generated ModelCIF metadata examples/complexes/modelcif_metadata/ examples/multimer_examples/*.test.cif + +# ipSAE C++ build artifacts and fetched dependencies +afdb_integration_kit/ipsae/ipsae_cpp +afdb_integration_kit/ipsae/deps/eigen-*/ +afdb_integration_kit/ipsae/deps/*.tar.gz diff --git a/README.md b/README.md index bc8d495..6609989 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,9 @@ A comprehensive toolkit for integrating structural models into the AlphaFold Dat - [3. Install Mol\* CLI](#3-install-mol-cli) - [4. Install DSSP](#4-install-dssp) - [5. Download mmCIF Dictionary (Required for ModelCIF Generator)](#5-download-mmcif-dictionary-required-for-modelcif-generator) - - [6. Install Nextflow (Optional)](#6-install-nextflow-optional) - - [7. Install Docker (Optional)](#7-install-docker-optional) + - [6. Install Production Pipeline Dependencies (Optional)](#6-install-production-pipeline-dependencies-optional) + - [7. Install Nextflow (Optional)](#7-install-nextflow-optional) + - [8. Install Docker (Optional)](#8-install-docker-optional) - [Quick Start](#quick-start) - [Verify Installation](#verify-installation) - [Basic Usage Example](#basic-usage-example) @@ -24,6 +25,8 @@ A comprehensive toolkit for integrating structural models into the AlphaFold Dat - [CIF to BCIF Converter](#cif-to-bcif-converter) - [DSSP Secondary Structure Assignment](#dssp-secondary-structure-assignment) - [Metadata Schema Validation](#metadata-schema-validation) + - [Production Pipeline](#production-pipeline) + - [Prepare Inputs (Standalone)](#prepare-inputs-standalone) - [Docker Usage](#docker-usage) - [Use Prebuilt Docker Image (Recommended)](#use-prebuilt-docker-image-recommended) - [Build Docker Image (Optional)](#build-docker-image-optional) @@ -54,6 +57,7 @@ A comprehensive toolkit for integrating structural models into the AlphaFold Dat - **Metadata Schema Validation**: Validate model and provider metadata JSONs against AFDB-defined schemas - **UniProt Metadata Tooling**: Streamline UniProt subset extraction and AF metadata generation (see [uniprot/README.md](uniprot/README.md)) - **Automated Workflows**: Nextflow-based end-to-end processing pipelines +- **Production Pipeline**: Standalone Python pipeline with logging, caching, resume capability, structure analysis (clash detection, interface residues), iPSAE quality scoring, and mmCIF QA metric embedding - **Docker Support**: Containerized execution for reproducible results - **Validation Tools**: Built-in testing and validation utilities @@ -110,7 +114,9 @@ Without nvm: npm install -g molstar ``` -### 4. Install DSSP +### 4. Install DSSP (Nextflow workflow only) + +The production pipeline uses built-in Python DSSP algorithms (`pydssp`, `psea`, `tmalign`) and does **not** require an external DSSP binary. This step is only needed if you use the Nextflow workflow. We use the modern DSSP implementation by the PDB-REDO team: @@ -141,7 +147,44 @@ curl -o mmcif_ma.dic https://raw.githubusercontent.com/ihmwg/ModelCIF/refs/heads **Note:** This step is automatically handled in the Docker environment, but is required for local installations. -### 6. Install Nextflow (Optional) +### 6. Install Production Pipeline Dependencies (Optional) + +The production pipeline (`scripts/production_pipeline.py`) requires additional dependencies for structure analysis (clash detection, interface residues). These use PyTorch and torch_cluster. + +**Option A: Using `environment.yml` (recommended):** + +```bash +conda env create -f environment.yml +conda activate afdb-toolkit + +# Install Mol* CLI into the environment +npm install -g molstar +``` + +This installs everything (core + production + C++ build tools + Node.js) in one step. + +**Option B: Manual pip installation:** + +```bash +# Install PyTorch 2.8.0 (CPU version) - pinned for torch_cluster compatibility +pip install torch==2.8.0 --index-url https://download.pytorch.org/whl/cpu + +# Install torch_cluster (CPU version) +pip install torch_cluster -f https://data.pyg.org/whl/torch-2.8.0+cpu.html + +# Install other production dependencies +uv pip install ".[production]" +``` + +**Verify installation:** + +```bash +python -c "import torch; from torch_cluster import radius_graph; print('torch_cluster OK')" +``` + +For available torch_cluster versions, see: https://data.pyg.org/whl/ + +### 7. Install Nextflow (Optional) For workflow automation: @@ -154,7 +197,7 @@ chmod +x nextflow sudo mv nextflow /usr/local/bin/ ``` -### 7. Install Docker (Optional) +### 8. Install Docker (Optional) For containerized execution: - **macOS/Windows**: Download Docker Desktop from https://www.docker.com/products/docker-desktop @@ -198,59 +241,27 @@ uv run main.py run-dssp \ Convert ColabFold score JSON + PDB to AFDB ingest JSONs (pLDDT/PAE) and optional UniProt-style manifests. -#### Included example data - -Sample ColabFold outputs are bundled under `examples/colabfold-output/` as zipped result folders: - -- `ACATN_HUMAN_19de7.result.zip` -- `C76C2_ARATH_6db51.result.zip` -- `CDK9_CAEEL_5ca86.result.zip` - -Each archive contains the files produced by ColabFold (scores JSON, PAE JSON, per-model unrelaxed PDBs, `config.json`, run markers, etc.). Unpack one to inspect or test the converter: - -```bash -unzip examples/colabfold-output/ACATN_HUMAN_19de7.result.zip -d /tmp/colabfold -ls /tmp/colabfold/ACATN_HUMAN_19de7 -``` - -#### Walkthrough using the bundled data - -Prerequisites for `afdb-colabfold-convert`: - -- Python deps: `orjson`, `duckdb` (install via `uv pip install -r requirements.txt` if you haven't already) -- (Optional) AFDB chain manifest CSV with columns `model_entity_id,entity_id,chain_id,uniprot_ac` (see merge instructions below) -- (Optional) DuckDB generated from UniProt flat files (built once per UniProt release with `afdb-uniprot-extract`/`afdb-uniprot-build-db`) +Requirements: `orjson`, `duckdb`, a chain manifest (`model_entity_id,entity_id,chain_id,uniprot_ac` at minimum), and a DuckDB built from the UniProt subset. -The converter can emit pLDDT/PAE JSONs using just the ColabFold score JSON + PDB. Provide the manifest + DuckDB when you also need UniProt-aware chain metadata or want to write chain/model manifest CSVs. - -Run the converter on a sample ColabFold model by pointing to the unpacked score JSON and a PDB of your choice: +Example (per model, safer for many parallel jobs): ``` afdb-colabfold-convert \ - /tmp/colabfold/ACATN_HUMAN_19de7/ACATN_HUMAN_19de7_scores_rank_001_alphafold2_ptm_model_1_seed_000.json \ - /tmp/colabfold/ACATN_HUMAN_19de7/ACATN_HUMAN_19de7_unrelaxed_rank_001_alphafold2_ptm_model_1_seed_000.pdb \ + /path/to/_scores_rank_001_alphafold2_multimer_v3_model_1_seed_000.json \ + /path/to/_unrelaxed_rank_001_alphafold2_multimer_v3_model_1_seed_000.pdb \ --manifest /mnt/disks/data/sample/config/uniprot_afid_mapping.csv \ --duckdb /mnt/disks/data/sample/db/uniprot_2025_04.duckdb \ --model-entity-id AF-0000000000001201 \ - --outdir /mnt/disks/data/sample/colabfold_output/ACATN_HUMAN_19de7-model_v4 \ + --outdir /mnt/disks/data/sample/colabfold_output/-model_v4 \ --chain-manifest-dir /mnt/disks/data/sample/per_accession/manifests/chains \ --model-manifest-dir /mnt/disks/data/sample/per_accession/manifests/models ``` -Drop the `--manifest`, `--duckdb`, `--chain-manifest-dir`, and `--model-manifest-dir` flags if you only need the AFDB JSON outputs; they are optional extras for UniProt-aware metadata. - -**What the manifest directories do:** - -- `--chain-manifest-dir`: writes `_afid_mapping.csv` per run containing chain-level averages/fractions (pLDDT bins, residue ranges) sourced from the manifest/DuckDB. These files mirror the schema expected by `uniprot_afid_mapping.csv` and live in a staging area until you merge them. -- `--model-manifest-dir`: writes `_model_metadata.csv` per run with model-level averages (pLDDT only). These append into the global `uniprot_model_metadata.csv` referenced by other tooling. - -Use these directories when you want each ColabFold conversion to emit the per-model snippets that eventually roll up into the UniProt manifests; merge them later using the commands below once you finish processing a batch. - -Outputs from the walkthrough: - -- AFDB JSONs in `--outdir`: `-confidence_v1.json` (pLDDT) and `-predicted_aligned_error_v1.json` (PAE) -- Per-model manifest CSVs (created inside the respective `--chain-manifest-dir` / `--model-manifest-dir` paths) for aggregating pLDDT summaries -- Optional UniProt-style manifests can be merged across models as described next +Outputs: +- AFDB JSONs: `-confidence_v1.json` and `-predicted_aligned_error_v1.json` in `--outdir`. +- Per-model manifests: + - Chains: `_afid_mapping.csv` with pLDDT averages/fractions and local 1..N residue ranges. + - Models: `_model_metadata.csv` with average pLDDT and ipTM (if present in scores JSON). Merge per-model manifests when needed (keep the header, append rows): @@ -340,7 +351,11 @@ uv run main.py run-cif2bcif -i -o ### DSSP Secondary Structure Assignment -Assigns secondary structure annotations based on atomic coordinates. +Assigns 3-state secondary structure annotations (helix, strand, coil) based on atomic coordinates. Three algorithms are available: + +- **pydssp** (default) — hydrogen-bond based assignment +- **psea** — geometry-based assignment using CA coordinates +- **tmalign** — CA-CA distance-based assignment **Command:** ```bash @@ -437,6 +452,120 @@ uv run main.py validate-sequences-file --file path/to/sequences.fasta Each command exits with code `1` if it encounters validation errors, making them easy to embed in automated pipelines. +### Production Pipeline + +The production pipeline (`scripts/production_pipeline.py`) provides a standalone alternative to the Nextflow workflow with comprehensive logging, caching, and resume capability. It processes models through 16 stages (executed in this order): + +1. **Prepare assets** – symlink PDB + meta JSON to staging +2. **Validate assets** – check PDB/JSON consistency +3. **Convert ColabFold** – produce AFDB-format confidence & PAE JSONs +4. **Merge manifests** – merge per-model chain/model manifests +5. **Calculate ipSAE scores** – interface quality metrics (ipSAE, pDockQ, LIS) +6. **Analyze clashes/interfaces** – VDW clashes, interface residues +7. **Export model metadata** – generate per-model metadata JSONs (enriched with iPSAE/clash metrics) +8. **Export chain metadata** – generate per-chain metadata JSONs (enriched with iPSAE metrics) +9. **Combine model metadata** – batch into chunked JSONs +10. **Combine chain metadata** – batch into chunked JSONs +11. **Export ModelCIF input** – prepare ModelCIF metadata from template +12. **Generate ModelCIF** – PDB → mmCIF with full metadata and optional QA metrics +13. **DSSP** – secondary structure annotation (3-state: helix/strand/coil) +14. **Enrich PDB** – add AFDB headers to PDB files +15. **CIF → BCIF** – BinaryCIF conversion +16. **Cleanup** – optional intermediate file cleanup (skipped by default) + +> **Note:** ipSAE and clash analysis (stages 5-6) run *before* metadata export (stages 7-8) so that quality metrics are available for JSON enrichment and CIF embedding. + +**Prerequisites:** Install production dependencies first (see [Installation section 6](#6-install-production-pipeline-dependencies-optional)), or use the `environment.yml`: + +```bash +conda env create -f environment.yml +conda activate afdb-toolkit +``` + +#### Homodimer mode (default) + +All config files are provided up front — no API calls, no manifest resolution: + +```bash +python scripts/production_pipeline.py \ + --output-dir /path/to/output \ + --input-dir /path/to/input \ + --mapping-file /path/to/mapping.tsv \ + --chain-mapping /path/to/manifest.csv \ + --dataset-config /path/to/config.json \ + --provider-json /path/to/provider.json \ + --uniprot-db /path/to/uniprot.duckdb \ + --workers 30 \ + --cif-qa-metrics auto +``` + +#### Heterodimer mode + +Enable with `--heterodimers`. Requires `--chain-mapping` and `--uniprot-db`. Config files (mapping TSV, dataset config, provider JSON) are auto-generated if not provided. Model IDs are derived from the chain mapping CSV. + +```bash +python scripts/production_pipeline.py \ + --output-dir /path/to/output \ + --input-dir /path/to/raw_colabfold \ + --heterodimers \ + --chain-mapping /path/to/manifest.csv \ + --uniprot-db /path/to/uniprot.duckdb \ + --workers 4 \ + --cif-qa-metrics auto +``` + +The `--input-dir` may contain raw ColabFold outputs (long suffixes like `_unrelaxed_rank_001_alphafold2_multimer_v3_model_1_seed_000.pdb` are detected automatically). + +#### Key options + +| Flag | Description | +|------|-------------| +| `--resume` | Resume from previous run (skip completed stages) | +| `--skip-stages stage_12,stage_13` | Skip specific stages (comma-separated) | +| `--dry-run` | Show what would be executed without running | +| `--dssp-algorithm` | Secondary structure algorithm: `psea`, `pydssp` (default), or `tmalign` | +| `--workers N` | Parallel workers (default: all CPUs) | +| `--pae-cutoff` / `--dist-cutoff` | ipSAE thresholds (default: 10.0 / 15.0) | +| `--clash-cutoff` / `--interface-cutoff` | Clash/interface thresholds (default: 0.4 / 8.0 Å) | +| `--analysis-batch-size N` | Batch size for clash/interface GPU analysis (default: 4) | +| `--cif-qa-metrics` | QA metrics to embed in mmCIF: `auto` (default, all metrics) or comma-separated list (e.g. `ipsae_AB,iptm_af,N_clash_backbone`) | +| `--enrichment-metrics` | iPSAE/clash metric names to include in model/chain metadata JSONs (default: all known metrics) | +| `--interface-clash-analysis` | Which analyses to run: `interface`, `backbone_clashes`, `heavy_atom_clashes` (default: all three) | +| `--modelcif-template` | Path to ModelCIF metadata template JSON (default: `uniprot/templates/modelcif_metadata.json`) | + +**Output:** Results are written to the output directory with logs in `logs/`, cache in `.pipeline_cache.json`, and a results summary in `pipeline_results.json`. + +Run `python scripts/production_pipeline.py --help` for full documentation. + +### Prepare Inputs (Standalone) + +`scripts/prepare_inputs.py` can also be used independently (outside the production pipeline) to prepare ColabFold outputs into the canonical layout the pipeline expects. It scans for matched PDB + scores-JSON pairs, builds config files, and symlinks inputs. + +**Production mode** (pre-built assets, no network): + +```bash +python scripts/prepare_inputs.py \ + --input-dir /data/colabfold/gpu0 \ + --output-dir /data/workdir \ + --chain-mapping /data/prebuilt_manifest.csv \ + --uniprot-db /data/uniprot.duckdb \ + --provider-id afcdb-heterodimers \ + --provider-name "AFCDB Heterodimers" +``` + +**Dev mode** (resolves AF-IDs from the AFCDB manifest + fetches from UniProt API): + +```bash +python scripts/prepare_inputs.py \ + --input-dir ./gpu0 \ + --output-dir ./workdir \ + --build-from-api /data/afdb_toolkit_manifest_file.csv \ + --provider-id afcdb-heterodimers \ + --provider-name "AFCDB Heterodimers" +``` + +By default, scores files are **symlinked** as meta JSONs (zero I/O). Pass `--extract-meta` to parse and re-write leaner JSONs, or `--copy` to copy instead of symlink. + ## Docker Usage ### Use Prebuilt Docker Image (Recommended) @@ -619,8 +748,6 @@ AF-0001234567890125 AF-0001234567890126 ``` -The ModelPDB step also requires provider metadata. By default the workflow reads this from `input/provider.json`; override it with `--provider_json ` if your provider file is elsewhere. - **Example input.txt:** ```bash # Create the input list file diff --git a/afdb_integration_kit/cif2bcif/convert.py b/afdb_integration_kit/cif2bcif/convert.py index 419c1da..041101e 100644 --- a/afdb_integration_kit/cif2bcif/convert.py +++ b/afdb_integration_kit/cif2bcif/convert.py @@ -1,80 +1,313 @@ -import concurrent.futures +""" +CIF to BinaryCIF conversion. + +Prefers Biotite (when version is supported) for in-process table-copy conversion; +falls back to Mol* cif2bcif CLI for Mol*/gemmi compatibility when Biotite is +unavailable, too old, or conversion fails. +Uses ProcessPoolExecutor for CPU-bound parallel execution. +""" +import gzip import logging +import os +import shutil import subprocess +import tempfile +from concurrent.futures import ProcessPoolExecutor from pathlib import Path +from typing import Tuple + +import numpy as np +from biotite.structure.io.pdbx import CIFFile, BinaryCIFFile, BinaryCIFBlock +from biotite.structure.io.pdbx.bcif import BinaryCIFCategory, BinaryCIFColumn + +MOLSTAR_CIF2BCIF_CMD = "cif2bcif" + +# BinaryCIF mask values: 0=present, 1="." (inapplicable), 2="?" (missing) +_MASK_PRESENT = np.uint8(0) +_MASK_DOT = np.uint8(1) +_MASK_QMARK = np.uint8(2) +_MISSING_TOKENS = frozenset((".", "?")) + + +def _build_bcif_column(raw_strings) -> BinaryCIFColumn: + """Build a BinaryCIFColumn with proper mask and type detection. + + Detects missing value tokens ('.' and '?'), builds a BinaryCIF mask, + and promotes data to the tightest numeric type (int32 -> float64 -> string). + """ + values = list(raw_strings) + if not values: + return BinaryCIFColumn(np.array([], dtype="U1")) + + mask = np.array( + [_MASK_DOT if v == "." else (_MASK_QMARK if v == "?" else _MASK_PRESENT) + for v in values], + dtype=np.uint8, + ) + has_missing = bool(np.any(mask != _MASK_PRESENT)) + present_idx = [i for i, v in enumerate(values) if v not in _MISSING_TOKENS] + + # Try int32 + try: + data = np.zeros(len(values), dtype=np.int32) + for i in present_idx: + data[i] = int(values[i]) + return BinaryCIFColumn(data, mask if has_missing else None) + except (ValueError, OverflowError): + pass + + # Try float64 + try: + data = np.zeros(len(values), dtype=np.float64) + for i in present_idx: + data[i] = float(values[i]) + return BinaryCIFColumn(data, mask if has_missing else None) + except (ValueError, OverflowError): + pass + + # String fallback + data = np.array(values, dtype="U") + return BinaryCIFColumn(data, mask if has_missing else None) +BIOTITE_MIN_VERSION = (0, 40, 0) + +_biotite_version_ok_cache: bool | None = None + + +def _biotite_version_ok() -> bool: + """Return True if Biotite is installed and version >= BIOTITE_MIN_VERSION. Cached.""" + global _biotite_version_ok_cache + if _biotite_version_ok_cache is not None: + return _biotite_version_ok_cache + try: + import biotite + raw = getattr(biotite, "__version__", "0") + parts = raw.split(".")[:3] + ver = [] + for p in parts: + digits = "".join(c for c in str(p) if c.isdigit()) or "0" + ver.append(int(digits)) + while len(ver) < 3: + ver.append(0) + _biotite_version_ok_cache = tuple(ver) >= BIOTITE_MIN_VERSION + except Exception: + _biotite_version_ok_cache = False + return _biotite_version_ok_cache -# Configure logger logger = logging.getLogger("cif2bcif") logger.setLevel(logging.INFO) -handler = logging.StreamHandler() -formatter = logging.Formatter("[%(levelname)s] %(message)s") -handler.setFormatter(formatter) -logger.addHandler(handler) +if not logger.handlers: + handler = logging.StreamHandler() + formatter = logging.Formatter("[%(levelname)s] %(message)s") + handler.setFormatter(formatter) + logger.addHandler(handler) -def process_file(input_path, output_path): - command = [ - "cif2bcif", - str(input_path), - str(output_path), - ] - result = subprocess.run(command, capture_output=True, text=True) - return result.returncode, input_path, result.stdout, result.stderr +def _process_single_cif(args: Tuple[Path, Path, str]) -> Tuple[str, bool]: + """ + Process a single CIF file. Module-level function for ProcessPoolExecutor pickling. + + Args: + args: Tuple of (input_file, output_dir, extension) + + Returns: + Tuple of (filename, success) + """ + input_file, output_dir, ext = args + output_file = output_dir / (input_file.stem + ext) + success = run_cif2bcif(input_file, output_file) + return input_file.name, success + + +def _run_molstar_cif2bcif(input_file: Path, output_file: Path) -> bool: + """ + Convert CIF to BCIF using Mol* (cif2bcif). Tries PATH first, then npx. + Produces BCIF compatible with Mol* and gemmi. Gemmi cannot write BCIF. + """ + for cmd in (_molstar_cmd_path(), _molstar_cmd_npx()): + if not cmd: + continue + result = subprocess.run( + cmd + [str(input_file), str(output_file)], + capture_output=True, + text=True, + timeout=900, + ) + if result.returncode == 0: + return True + logger.debug( + f"Mol* cif2bcif failed: {result.stderr or result.stdout}" + ) + return False + + +def _molstar_cmd_path() -> list[str] | None: + """Command list if cif2bcif is on PATH.""" + cmd = shutil.which(MOLSTAR_CIF2BCIF_CMD) + return [cmd] if cmd else None + + +def _molstar_cmd_npx() -> list[str] | None: + """Command list for npx -p molstar cif2bcif (no global install).""" + if not shutil.which("npx"): + return None + return ["npx", "--yes", "-p", "molstar", MOLSTAR_CIF2BCIF_CMD] -def run_cif2bcif(input_file: Path, output_file: Path): - if not ( - str(output_file).endswith(".bcif") or str(output_file).endswith(".bcif.gz") - ): +def _run_biotite_cif2bcif( + input_file: Path, output_file: Path, tmpdir: str | None = None +) -> bool: + """ + Convert CIF to BinaryCIF using Biotite (table-copy, no AtomArray). + Preserves all categories and metadata. Writes via TMPDIR then rename for atomic Lustre-safe output. + """ + output_str = str(output_file) + base_tmpdir = tmpdir or os.environ.get("TMPDIR") or tempfile.gettempdir() + output_file = Path(output_file) + output_file.parent.mkdir(parents=True, exist_ok=True) + try: + cif = CIFFile.read(str(input_file)) + + bcif = BinaryCIFFile() + for block_name in cif.keys(): + cif_block = cif[block_name] + bcif_block = BinaryCIFBlock() + for cat_name in cif_block.keys(): + cif_cat = cif_block[cat_name] + columns = {} + for col_name in cif_cat.keys(): + col = cif_cat[col_name] + # Get raw strings so we can detect '.' and '?' for masks + raw = col.as_array(str) + columns[col_name] = _build_bcif_column(raw) + bcif_cat = BinaryCIFCategory(columns) + bcif_block[cat_name] = bcif_cat + bcif[block_name] = bcif_block + + if output_str.endswith(".bcif.gz"): + tmp_path = Path(base_tmpdir) / (output_file.name + ".tmp.bcif") + bcif.write(str(tmp_path)) + try: + with open(tmp_path, "rb") as f_in: + with gzip.open(output_file, "wb") as f_out: + f_out.write(f_in.read()) + finally: + if tmp_path.exists(): + tmp_path.unlink(missing_ok=True) + else: + tmp_path = Path(base_tmpdir) / (output_file.name + ".tmp") + bcif.write(str(tmp_path)) + try: + tmp_path.replace(output_file) + except OSError: + # Cross-device rename (e.g. /tmp -> /lustre); fall back to copy+delete + shutil.move(str(tmp_path), str(output_file)) + return True + + except Exception as e: + logger.debug(f"Biotite conversion failed for {input_file}: {e}") + import traceback + traceback.print_exc() + return False + + +def run_cif2bcif( + input_file: Path, output_file: Path, tmpdir: str | None = None +) -> bool: + """ + Convert CIF to BinaryCIF. Prefers Biotite (when version supported) for in-process + table-copy; falls back to Mol* cif2bcif CLI for Mol*/gemmi compatibility. + + Args: + input_file: Input CIF file path + output_file: Output BCIF file path (.bcif or .bcif.gz) + tmpdir: Optional directory for temporary writes (default: TMPDIR or system temp). + Used for atomic rename to final path on Lustre. + + Returns: + True on success, False on failure + """ + output_str = str(output_file) + if not (output_str.endswith(".bcif") or output_str.endswith(".bcif.gz")): logger.warning( - "The output file extension '%s' is not '.bcif' or '.bcif.gz'.", - output_file.suffix, + f"Output file extension '{output_file.suffix}' is not '.bcif' or '.bcif.gz'" ) - logger.info("Running command: cif2bcif") - logger.info("%s %s", str(input_file), str(output_file)) - code, _, out, err = process_file(input_file, output_file) - if code == 0: - logger.info("Command executed successfully:\n%s", out) + + logger.info(f"Converting {input_file} to {output_file}") + + if _biotite_version_ok(): + if _run_biotite_cif2bcif(input_file, output_file, tmpdir=tmpdir): + logger.info(f"Conversion complete (Biotite): {output_file}") + return True + logger.warning("Biotite conversion failed; trying Mol* cif2bcif.") else: - logger.error("Error executing command:\n%s", err) + logger.warning( + "Biotite missing or version < %s; using Mol* cif2bcif. " + "Install/upgrade biotite for faster in-process conversion.", + ".".join(str(x) for x in BIOTITE_MIN_VERSION), + ) + if _run_molstar_cif2bcif(input_file, output_file): + logger.info(f"Conversion complete (Mol*): {output_file}") + return True + logger.error(f"Conversion failed for {input_file} (Biotite and Mol* both failed)") + return False def run_batch_cif2bcif( - input_dir: Path, output_dir: Path, workers: int = 4, gzip: bool = False -): - input_files = list(input_dir.glob("*.cif")) - if output_dir.exists(): - logger.info("Output directory %s already exists.", output_dir) - else: - output_dir.mkdir(parents=True, exist_ok=True) - logger.info("Created output directory %s.", output_dir) - - def process(input_file): - ext = ".bcif.gz" if gzip else ".bcif" - output_file = output_dir / (input_file.stem + ext) - if not ( - str(output_file).endswith(".bcif") or str(output_file).endswith(".bcif.gz") - ): - logger.warning( - "The output file extension '%s' is not '.bcif' or '.bcif.gz'. File: %s", - output_file.suffix, - output_file, - ) - code, _, out, err = process_file(input_file, output_file) - if code == 0: - return (input_file.name, True, out) - else: - return (input_file.name, False, err) + input_dir: Path, + output_dir: Path, + workers: int = 8, + gzip: bool = False, + pattern: str = "*.cif" +) -> Tuple[int, int]: + """ + Batch CIF to BinaryCIF conversion. Uses Biotite first (when version supported), + then Mol* CLI on failure; ProcessPoolExecutor for parallel execution. - logger.info("Processing %d files with %d workers...", len(input_files), workers) - with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor: - futures = {executor.submit(process, f): f for f in input_files} - for future in concurrent.futures.as_completed(futures): - fname, ok, msg = future.result() + Args: + input_dir: Directory containing input CIF files + output_dir: Output directory for BCIF files + workers: Number of parallel workers + gzip: Whether to gzip output files (.bcif.gz) + pattern: Glob pattern for input files + + Returns: + Tuple of (success_count, error_count) + """ + input_files = list(input_dir.glob(pattern)) + if not input_files: + logger.warning(f"No files matching '{pattern}' in {input_dir}") + return 0, 0 + + output_dir.mkdir(parents=True, exist_ok=True) + + ext = ".bcif.gz" if gzip else ".bcif" + + # Prepare arguments for each file (module-level function for pickling) + work_items = [(f, output_dir, ext) for f in input_files] + + logger.info(f"Processing {len(input_files)} files with {workers} workers (ProcessPool)") + + success_count = 0 + error_count = 0 + + with ProcessPoolExecutor(max_workers=workers) as executor: + results = executor.map(_process_single_cif, work_items) + for fname, ok in results: if ok: - logger.info("[OK] %s", fname) + success_count += 1 + logger.info(f"[OK] {fname}") else: - short_msg = msg[:80] - suffix = "..." if len(msg) > 80 else "" - error_line = "[ERROR] " + fname + ": " + short_msg + suffix - logger.error(error_line) + error_count += 1 + logger.error(f"[FAILED] {fname}") + + logger.info(f"Batch conversion complete: {success_count} success, {error_count} errors") + return success_count, error_count + + +# Legacy function for backwards compatibility with molstar cif2bcif subprocess +def process_file(input_path, output_path): + """Legacy subprocess wrapper - kept for compatibility but not recommended.""" + import subprocess + command = ["cif2bcif", str(input_path), str(output_path)] + result = subprocess.run(command, capture_output=True, text=True) + return result.returncode, input_path, result.stdout, result.stderr diff --git a/afdb_integration_kit/colabfold/converter.py b/afdb_integration_kit/colabfold/converter.py index 624df4b..ba7628d 100644 --- a/afdb_integration_kit/colabfold/converter.py +++ b/afdb_integration_kit/colabfold/converter.py @@ -8,9 +8,117 @@ from pathlib import Path from typing import Any, Dict, Iterable, List, Sequence, TypedDict +import gemmi +import numpy as np import orjson +import threading + logger = logging.getLogger(__name__) +# Module-level manifest cache to avoid re-parsing the same CSV file +_MANIFEST_CACHE: dict[str, list[dict[str, str]]] = {} +_MANIFEST_CACHE_LOCK = threading.Lock() + +# Thread-local storage for DuckDB connections (DuckDB connections are not thread-safe) +_THREAD_LOCAL = threading.local() + +# Module-level cache for prefetched DuckDB metadata (thread-safe read after initial population) +_DUCKDB_METADATA_CACHE: dict[str, dict[str, dict[str, Any]]] = {} +_DUCKDB_METADATA_LOCK = threading.Lock() + + +def prefetch_duckdb_metadata(db_path: str, accessions: list[str]) -> None: + """ + Prefetch metadata for all accessions from DuckDB into module-level cache. + Call this ONCE before batch processing to avoid per-model queries. + Thread-safe for initial population; subsequent reads don't need locks. + """ + if not accessions: + return + + cache_key = str(Path(db_path).resolve()) + + with _DUCKDB_METADATA_LOCK: + if cache_key in _DUCKDB_METADATA_CACHE: + return # Already populated + + try: + import duckdb + except ModuleNotFoundError as exc: + raise ModuleNotFoundError( + "duckdb Python package is required to read DuckDB manifests. " + "Install with `pip install duckdb`." + ) from exc + + con = duckdb.connect(db_path, read_only=True) + duckdb_mem = os.environ.get("DUCKDB_MEMORY_LIMIT", "512MB") + con.execute(f"SET memory_limit = '{duckdb_mem}'") + try: + unique_accs = list(set(accessions)) + placeholders = ",".join("?" for _ in unique_accs) + query = ( + "SELECT primary_ac, protein_full_names, sequence " + "FROM entry WHERE primary_ac IN ({})" + ).format(placeholders) + rows_rel = con.execute(query, unique_accs) + rows = rows_rel.fetchall() + col_index = {name: idx for idx, name in enumerate([col[0] for col in (rows_rel.description or [])])} + + entry_lookup: dict[str, dict[str, Any]] = {} + for row in rows: + entry_lookup[str(row[col_index["primary_ac"]])] = { + name: row[col_index[name]] for name in col_index + } + + _DUCKDB_METADATA_CACHE[cache_key] = entry_lookup + logger.info("Prefetched DuckDB metadata for %d/%d accessions", len(entry_lookup), len(unique_accs)) + finally: + con.close() + + +def _get_prefetched_metadata(db_path: str) -> dict[str, dict[str, Any]] | None: + """Get prefetched metadata if available, otherwise return None.""" + cache_key = str(Path(db_path).resolve()) + return _DUCKDB_METADATA_CACHE.get(cache_key) + + +def _get_duckdb_connection(db_path: str) -> "duckdb.DuckDBPyConnection": + """Get or create a thread-local DuckDB connection.""" + try: + import duckdb + except ModuleNotFoundError as exc: + raise ModuleNotFoundError( + "duckdb Python package is required to read DuckDB manifests. " + "Install with `pip install duckdb`." + ) from exc + + if not hasattr(_THREAD_LOCAL, "duckdb_conns"): + _THREAD_LOCAL.duckdb_conns = {} + + if db_path not in _THREAD_LOCAL.duckdb_conns: + conn = duckdb.connect(db_path, read_only=True) + duckdb_mem = os.environ.get("DUCKDB_MEMORY_LIMIT", "512MB") + conn.execute(f"SET memory_limit = '{duckdb_mem}'") + _THREAD_LOCAL.duckdb_conns[db_path] = conn + + return _THREAD_LOCAL.duckdb_conns[db_path] + + +def cleanup_caches() -> None: + """Close all cached DuckDB connections and clear caches. Call after batch processing.""" + # Close thread-local DuckDB connections if they exist + if hasattr(_THREAD_LOCAL, "duckdb_conns"): + for conn in _THREAD_LOCAL.duckdb_conns.values(): + try: + conn.close() + except Exception: + pass + _THREAD_LOCAL.duckdb_conns.clear() + with _MANIFEST_CACHE_LOCK: + _MANIFEST_CACHE.clear() + with _DUCKDB_METADATA_LOCK: + _DUCKDB_METADATA_CACHE.clear() + class ChainMetadata(TypedDict): name: str @@ -83,10 +191,98 @@ def _iterate_pdb_residues(pdb_path: Path) -> Iterable[tuple[str, int, str]]: def _chain_spans_from_pdb( pdb_path: Path, chain_display_names: Dict[str, str] | None = None, +) -> tuple[list[ChainMetadata], int]: + """ + Parse the PDB to derive chain metadata using gemmi for speed. + Falls back to line-by-line parsing if gemmi fails. + """ + try: + return _chain_spans_from_pdb_gemmi(pdb_path, chain_display_names) + except Exception: + logger.warning("gemmi parsing failed, falling back to line parser") + return _chain_spans_from_pdb_legacy(pdb_path, chain_display_names) + + +def _chain_spans_from_pdb_gemmi( + pdb_path: Path, + chain_display_names: Dict[str, str] | None = None, +) -> tuple[list[ChainMetadata], int]: + """Fast PDB parsing using gemmi.""" + structure = gemmi.read_structure(str(pdb_path)) + if not structure: + raise ValueError(f"No structure found in {pdb_path}") + + model = structure[0] + chains: list[ChainMetadata] = [] + total_residues = 0 + + for idx, chain in enumerate(model, start=1): + # Count unique residues (excluding water) + seen_residues: set[tuple[int, str]] = set() + for residue in chain: + if residue.name == "HOH": + continue + seen_residues.add((residue.seqid.num, residue.seqid.icode)) + + if not seen_residues: + continue + + chain_id = chain.name if chain.name.strip() else f"Chain{idx}" + label = chain_id if chain_id != "_" else f"Chain{idx}" + display_name = chain_display_names.get(chain_id, label) if chain_display_names else label + + # Expose per-chain local residue ranges in JSON metadata. + start = 1 + end = len(seen_residues) + chains.append({ + "name": display_name, + "label_asym_id": label, + "sequenceStart": start, + "sequenceEnd": end, + }) + total_residues += len(seen_residues) + + if not chains: + raise ValueError(f"No chains found in {pdb_path}") + + return chains, total_residues + + +def _get_pdb_chain_ids(pdb_path: Path) -> list[str]: + """ + Extract ordered list of chain IDs from PDB file. + Uses gemmi for fast parsing, falls back to line-by-line if needed. + Returns chain IDs in the order they appear in the PDB. + """ + try: + structure = gemmi.read_structure(str(pdb_path)) + if not structure: + raise ValueError(f"No structure found in {pdb_path}") + model = structure[0] + chain_ids = [] + for chain in model: + # Skip chains with no residues (excluding water) + has_residues = any(r.name != "HOH" for r in chain) + if has_residues: + chain_id = chain.name if chain.name.strip() else "_" + chain_ids.append(chain_id) + return chain_ids + except Exception: + # Fallback to line-by-line parsing + seen_chains: list[str] = [] + for chain_id, _, _ in _iterate_pdb_residues(pdb_path): + if chain_id not in seen_chains: + seen_chains.append(chain_id) + return seen_chains + + +def _chain_spans_from_pdb_legacy( + pdb_path: Path, + chain_display_names: Dict[str, str] | None = None, ) -> tuple[list[ChainMetadata], int]: """ Parse the PDB to derive chain metadata aligned to pLDDT/PAE indices. - Returns (chains, total_residues). + Returns (chains, total_residues). Legacy line-by-line fallback. """ residues: OrderedDict[str, list[tuple[int, str]]] = OrderedDict() for chain_id, resseq, insertion_code in _iterate_pdb_residues(pdb_path): @@ -99,12 +295,13 @@ def _chain_spans_from_pdb( raise ValueError(f"No ATOM records found in {pdb_path} to derive chains.") chains: list[ChainMetadata] = [] - running_total = 1 + total_residues = 0 for idx, (chain_id, res_list) in enumerate(residues.items(), start=1): label = chain_id if chain_id != "_" else f"Chain{idx}" display_name = chain_display_names.get(chain_id) if chain_display_names else label - start = running_total - end = running_total + len(res_list) - 1 + # Expose per-chain local residue ranges in JSON metadata. + start = 1 + end = len(res_list) chains.append( { "name": display_name, @@ -113,9 +310,8 @@ def _chain_spans_from_pdb( "sequenceEnd": end, } ) - running_total = end + 1 + total_residues += len(res_list) - total_residues = running_total - 1 return chains, total_residues @@ -148,10 +344,16 @@ def _load_manifest_chains( """ Load chain_id + uniprot_ac pairs for the requested model_entity_id. Returns the resolved model_entity_id and ordered chain rows. + Uses module-level cache to avoid re-parsing the same manifest file. """ - with manifest_path.open(newline="") as handle: - reader = csv.DictReader(handle) - rows = list(reader) + cache_key = str(manifest_path.resolve()) + + with _MANIFEST_CACHE_LOCK: + if cache_key not in _MANIFEST_CACHE: + with manifest_path.open(newline="") as handle: + reader = csv.DictReader(handle) + _MANIFEST_CACHE[cache_key] = list(reader) + rows = _MANIFEST_CACHE[cache_key] if not rows: raise ValueError(f"Manifest {manifest_path} is empty.") @@ -235,72 +437,122 @@ def _as_string_list(value: Any) -> list[str]: def _load_chain_metadata_from_duckdb( db_path: Path, manifest_chains: list[dict[str, str]], -) -> tuple[list[ChainMetadata], list[int]]: + pdb_path: Path | None = None, +) -> tuple[list[ChainMetadata], list[int], list[dict[str, str]]]: """ Resolve chain names and residue ranges from DuckDB using accessions from the CSV manifest. - Uses uniprot_ac to find matching rows in the entry table. - Uses the first protein_full_names entry as the chain name (required). - - Derives sequenceStart/sequenceEnd as 1..len(sequence) (required). - """ - try: - import duckdb # type: ignore - except ModuleNotFoundError as exc: - raise ModuleNotFoundError( - "duckdb Python package is required to read DuckDB manifests. " - "Install with `pip install duckdb`." - ) from exc + - Derives sequenceStart/sequenceEnd as per-chain local ranges 1..len(sequence). - con = duckdb.connect(str(db_path), read_only=True) - try: - accs = [c["uniprot_ac"] for c in manifest_chains] - placeholders = ",".join("?" for _ in accs) + If pdb_path is provided, detects actual chains in the PDB and auto-expands + the manifest for homomultimers (multiple chains with the same UniProt accession). + + Returns: + (chains, residue_numbers, effective_manifest_chains) + - effective_manifest_chains may be expanded for homomultimers + + Uses prefetched metadata cache if available (populated by prefetch_duckdb_metadata). + """ + # Auto-expand manifest for homomultimers if PDB has more chains than manifest + effective_chains = manifest_chains + if pdb_path is not None: + pdb_chain_ids = _get_pdb_chain_ids(pdb_path) + if len(pdb_chain_ids) > len(manifest_chains): + # Check if this is a homomultimer (single accession for multiple chains) + unique_accs = {c["uniprot_ac"] for c in manifest_chains} + if len(unique_accs) == 1: + # Homodimer/homomultimer: replicate the single accession for each PDB chain + base_acc = list(unique_accs)[0] + base_entity_id = manifest_chains[0].get("entity_id", "1") + effective_chains = [] + for chain_id in pdb_chain_ids: + effective_chains.append({ + "chain_id": chain_id, + "uniprot_ac": base_acc, + "entity_id": base_entity_id, + }) + logger.info( + "Auto-expanded manifest for homomultimer: %d chains -> %d chains (accession: %s)", + len(manifest_chains), len(effective_chains), base_acc + ) + else: + # Heteromultimer with incomplete manifest - warn but proceed + logger.warning( + "PDB has %d chains but manifest has %d entries with %d unique accessions. " + "Cannot auto-expand heteromultimer manifests.", + len(pdb_chain_ids), len(manifest_chains), len(unique_accs) + ) + + cache_key = str(db_path.resolve()) + accs = [c["uniprot_ac"] for c in effective_chains] + + # Try to use prefetched metadata first (fast path) + prefetched = _get_prefetched_metadata(cache_key) + if prefetched is not None: + entry_lookup = prefetched + else: + # Fallback to per-model query (slow path) + con = _get_duckdb_connection(cache_key) + unique_accs_list = list(set(accs)) + placeholders = ",".join("?" for _ in unique_accs_list) query = ( "SELECT primary_ac, protein_full_names, sequence " "FROM entry WHERE primary_ac IN ({})" ).format(placeholders) - rows_rel = con.execute(query, accs) + rows_rel = con.execute(query, unique_accs_list) rows = rows_rel.fetchall() if not rows: - raise ValueError(f"No matching accessions found in DuckDB entry table for {accs}.") + raise ValueError(f"No matching accessions found in DuckDB entry table for {unique_accs_list}.") col_index = {name: idx for idx, name in enumerate([col[0] for col in (rows_rel.description or [])])} entry_lookup: Dict[str, Dict[str, Any]] = {} for row in rows: entry_lookup[str(row[col_index["primary_ac"]])] = {name: row[col_index[name]] for name in col_index} - chains: list[ChainMetadata] = [] - residue_numbers: list[int] = [] - running_total = 0 - for chain in manifest_chains: - acc = chain["uniprot_ac"] - chain_id = chain["chain_id"] - entry = entry_lookup.get(acc) - if entry is None: - raise ValueError(f"Accession {acc} not found in DuckDB entry table.") - names = _as_string_list(entry.get("protein_full_names")) - if not names: - raise ValueError(f"No protein_full_names found in DuckDB entry table for accession {acc}.") + # Verify all unique accessions are in the lookup + unique_accs_set = set(accs) + missing = [acc for acc in unique_accs_set if acc not in entry_lookup] + if missing: + raise ValueError(f"No matching accessions found in DuckDB entry table for {missing}.") + + chains: list[ChainMetadata] = [] + residue_numbers: list[int] = [] + for chain in effective_chains: + acc = chain["uniprot_ac"] + chain_id = chain["chain_id"] + entry = entry_lookup.get(acc) + if entry is None: + raise ValueError(f"Accession {acc} not found in DuckDB entry table.") + # Try protein_full_names first, then fall back to entry_name or gene_names + names = _as_string_list(entry.get("protein_full_names")) + if names: desc = names[0] - seq = entry.get("sequence") or "" - seqlen = len(seq) - if seqlen == 0: - raise ValueError(f"No sequence found in DuckDB entry table for accession {acc}.") - seq_start = running_total + 1 - seq_end = running_total + seqlen - chains.append( - { - "name": desc, - "label_asym_id": chain_id, - "sequenceStart": seq_start, - "sequenceEnd": seq_end, - } - ) - residue_numbers.extend(range(seq_start, seq_end + 1)) - running_total = seq_end + elif entry.get("entry_name"): + desc = str(entry["entry_name"]) + elif entry.get("gene_names"): + desc = str(entry["gene_names"]) + else: + desc = acc # Ultimate fallback to accession itself + seq = entry.get("sequence") or "" + seqlen = len(seq) + if seqlen == 0: + raise ValueError(f"No sequence found in DuckDB entry table for accession {acc}.") + # Expose per-chain local residue ranges in JSON metadata. + seq_start = 1 + seq_end = seqlen + chains.append( + { + "name": desc, + "label_asym_id": chain_id, + "sequenceStart": seq_start, + "sequenceEnd": seq_end, + } + ) + # Keep global residue numbering for the flattened confidence arrays. + residue_numbers.extend(range(len(residue_numbers) + 1, len(residue_numbers) + seqlen + 1)) - return chains, residue_numbers - finally: - con.close() + return chains, residue_numbers, effective_chains def _compute_plddt_metrics( @@ -314,24 +566,28 @@ def _compute_plddt_metrics( """ acc_lookup = {c["chain_id"]: c["uniprot_ac"] for c in manifest_chains} entity_lookup = {c["chain_id"]: c.get("entity_id", "") for c in manifest_chains} + all_scores = np.asarray(plddt, dtype=np.float64) chain_rows: list[dict[str, Any]] = [] total_sum = 0.0 total_count = 0 + running_offset = 0 for chain in chains: start = chain["sequenceStart"] end = chain["sequenceEnd"] length = end - start + 1 - # chains are 1-based inclusive for global indices - values = plddt[start - 1 : end] - if not values: + values = all_scores[running_offset : running_offset + length] + if len(values) == 0: raise ValueError(f"No pLDDT values found for chain {chain['label_asym_id']}.") - avg = round(sum(values) / len(values), 2) - total_sum += sum(values) - total_count += len(values) - fraction_plddt_very_low = round(sum(1 for v in values if v < 50.0) / len(values), 3) - fraction_plddt_low = round(sum(1 for v in values if 50.0 <= v < 70.0) / len(values), 3) - fraction_plddt_confident = round(sum(1 for v in values if 70.0 <= v <= 90.0) / len(values), 3) - fraction_plddt_very_high = round(sum(1 for v in values if v > 90.0) / len(values), 3) + running_offset += length + n = len(values) + chain_sum = float(np.sum(values)) + avg = round(chain_sum / n, 2) + total_sum += chain_sum + total_count += n + fraction_plddt_very_low = round(int(np.sum(values < 50.0)) / n, 3) + fraction_plddt_low = round(int(np.sum((values >= 50.0) & (values < 70.0))) / n, 3) + fraction_plddt_confident = round(int(np.sum((values >= 70.0) & (values <= 90.0))) / n, 3) + fraction_plddt_very_high = round(int(np.sum(values > 90.0)) / n, 3) chain_rows.append( { "model_entity_id": None, # filled later @@ -407,22 +663,32 @@ def plddt_to_ingest( raise ValueError( f"Residue numbers length ({len(residue_numbers)}) does not match pLDDT length ({len(plddt)})." ) - categories = [_categorise_confidence(x) for x in plddt] + scores = np.asarray(plddt, dtype=np.float64) + rounded_scores = np.round(scores, 2) + + categories = np.full(len(rounded_scores), "D", dtype="U1") + categories[rounded_scores >= 30.0] = "L" + categories[rounded_scores >= 50.0] = "M" + categories[rounded_scores >= 70.0] = "H" + categories[rounded_scores > 90.0] = "V" + return { "residueNumber": residue_numbers, - "confidenceScore": list(plddt), - "confidenceCategory": categories, + "confidenceScore": rounded_scores, + "confidenceCategory": categories.tolist(), "chains": chains, } def pae_to_ingest(pae: Sequence[Sequence[float]], max_pae: float, chains: list[ChainMetadata]) -> list[PAEItem]: """Build the AFDB PAE payload with light validation.""" - if not pae or any(len(row) != len(pae) for row in pae): + pae_arr = np.asarray(pae, dtype=np.float64) + if pae_arr.ndim != 2 or pae_arr.shape[0] != pae_arr.shape[1] or pae_arr.shape[0] == 0: raise ValueError("PAE must be a non-empty square matrix (NxN).") + rounded_pae = np.round(pae_arr, 2) return [ { - "predicted_aligned_error": pae, + "predicted_aligned_error": rounded_pae, "max_predicted_aligned_error": round(max_pae, 2), "chains": chains, } @@ -476,11 +742,13 @@ def convert_file( chains: list[ChainMetadata] residue_numbers: list[int] | None = None + effective_manifest_chains: list[dict[str, str]] = manifest_chains if manifest_chains and duckdb_path: - chains, residue_numbers = _load_chain_metadata_from_duckdb( + chains, residue_numbers, effective_manifest_chains = _load_chain_metadata_from_duckdb( Path(duckdb_path), manifest_chains=manifest_chains, + pdb_path=pdb, ) if len(residue_numbers) != len(plddt): raise ValueError( @@ -501,9 +769,9 @@ def convert_file( chain_manifest_rows: list[dict[str, Any]] | None = None model_avg_plddt: float | None = None if out_chain_manifest or out_model_manifest or chain_manifest_dir or model_manifest_dir: - if not manifest_chains: + if not effective_manifest_chains: raise ValueError("--manifest is required when writing pLDDT manifests.") - chain_manifest_rows, model_avg_plddt = _compute_plddt_metrics(plddt, chains, manifest_chains) + chain_manifest_rows, model_avg_plddt = _compute_plddt_metrics(plddt, chains, effective_manifest_chains) if model_entity_id: for row in chain_manifest_rows: row["model_entity_id"] = model_entity_id @@ -527,7 +795,7 @@ def convert_file( def _dump(obj: Any, path: str) -> None: with open(path, "wb") as f: - f.write(orjson.dumps(obj)) + f.write(orjson.dumps(obj, option=orjson.OPT_SERIALIZE_NUMPY)) _dump(plddt_payload, out_plddt_path) _dump(pae_payload, out_pae_path) diff --git a/afdb_integration_kit/dssp/dssp.py b/afdb_integration_kit/dssp/dssp.py index 9b693ef..72493a9 100644 --- a/afdb_integration_kit/dssp/dssp.py +++ b/afdb_integration_kit/dssp/dssp.py @@ -1,34 +1,484 @@ +""" +Secondary structure calculation with multiple algorithm support. + +Supports three algorithms for secondary structure assignment: +1. P-SEA (Biotite's annotate_sse): Geometry-based, ~95% agreement with DSSP +2. PyDSSP: Simplified H-bond based DSSP, ~97% agreement with DSSP +3. TM-align: CA-CA distance based (from Foldseek/TM-align), very fast + +All produce 3-state output: helix, strand, coil (turn mapped to coil for 3-state). +Uses gemmi for mmCIF I/O to produce complete _struct_conf annotations with all +standard fields (label/auth identifiers, insertion codes). +""" import logging -import subprocess +from collections import namedtuple +from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed from pathlib import Path +from typing import List, Tuple, Literal, Optional + +import gemmi +import numpy as np +import pydssp +import torch -# Configure logger logger = logging.getLogger("dssp") logger.setLevel(logging.INFO) -handler = logging.StreamHandler() -formatter = logging.Formatter("[%(levelname)s] %(message)s") -handler.setFormatter(formatter) -logger.addHandler(handler) +if not logger.handlers: + handler = logging.StreamHandler() + formatter = logging.Formatter("[%(levelname)s] %(message)s") + handler.setFormatter(formatter) + logger.addHandler(handler) +Algorithm = Literal["psea", "pydssp", "tmalign"] + +PSEA_CODE_MAP = { + 'a': 'HELX_P', + 'b': 'STRN', + 'c': None, +} + +PYDSSP_CODE_MAP = { + 'H': 'HELX_P', + 'E': 'STRN', + '-': None, +} + +TMALIGN_CODE_MAP = { + 1: None, + 2: 'HELX_P', + 3: None, + 4: 'STRN', +} + +ResidueInfo = namedtuple("ResidueInfo", [ + "label_asym_id", + "auth_asym_id", + "label_seq_id", + "auth_seq_id", + "comp_id", + "ins_code", +]) + +_CURRENT_ALGORITHM: Algorithm = "psea" +_CURRENT_DEVICE: str = "cpu" + +_BACKBONE_ATOMS = ("N", "CA", "C", "O") + +_STRUCT_CONF_FIELDS = [ + "id", "conf_type_id", + "beg_label_comp_id", "beg_label_asym_id", "beg_label_seq_id", + "pdbx_beg_PDB_ins_code", + "end_label_comp_id", "end_label_asym_id", "end_label_seq_id", + "pdbx_end_PDB_ins_code", + "beg_auth_comp_id", "beg_auth_asym_id", "beg_auth_seq_id", + "end_auth_comp_id", "end_auth_asym_id", "end_auth_seq_id", +] + + +def _make_residue_info(chain: gemmi.Chain, residue: gemmi.Residue) -> ResidueInfo: + icode = residue.seqid.icode + has_icode = icode not in ('\0', ' ', '') + label_seq = str(residue.label_seq) if residue.label_seq else str(residue.seqid.num) + return ResidueInfo( + label_asym_id=residue.subchain or chain.name, + auth_asym_id=chain.name, + label_seq_id=label_seq, + auth_seq_id=str(residue.seqid.num), + comp_id=residue.name, + ins_code=str(icode) if has_icode else "?", + ) + + +def _extract_polymer_residue_info(structure: gemmi.Structure) -> List[ResidueInfo]: + """Extract per-residue metadata for all polymer residues in the first model.""" + info: List[ResidueInfo] = [] + for chain in structure[0]: + polymer = chain.get_polymer() + if not polymer: + continue + for residue in polymer: + info.append(_make_residue_info(chain, residue)) + return info + + +def _extract_backbone_coords( + structure: gemmi.Structure, +) -> Tuple[Optional[np.ndarray], List[ResidueInfo]]: + """ + Extract N, CA, C, O backbone coordinates and residue metadata. + + Returns (coords shaped (n_residues, 4, 3), residue_info) or (None, []) + if any polymer residue is missing a backbone atom. + """ + coords_list: list = [] + residue_info: List[ResidueInfo] = [] + + for chain in structure[0]: + polymer = chain.get_polymer() + if not polymer: + continue + for residue in polymer: + atom_coords = [] + for atom_name in _BACKBONE_ATOMS: + atom = residue.find_atom(atom_name, '\0') + if atom is None: + return None, [] + atom_coords.append([atom.pos.x, atom.pos.y, atom.pos.z]) + coords_list.append(atom_coords) + residue_info.append(_make_residue_info(chain, residue)) + + if not coords_list: + return None, [] + return np.array(coords_list, dtype=np.float32), residue_info + + +def _extract_ca_coords( + structure: gemmi.Structure, +) -> Tuple[Optional[np.ndarray], List[ResidueInfo]]: + """ + Extract CA coordinates and residue metadata. + + Returns (coords shaped (n_residues, 3), residue_info) or (None, []) + if any polymer residue is missing CA. + """ + coords_list: list = [] + residue_info: List[ResidueInfo] = [] + + for chain in structure[0]: + polymer = chain.get_polymer() + if not polymer: + continue + for residue in polymer: + ca = residue.find_atom("CA", '\0') + if ca is None: + return None, [] + coords_list.append([ca.pos.x, ca.pos.y, ca.pos.z]) + residue_info.append(_make_residue_info(chain, residue)) + + if not coords_list: + return None, [] + return np.array(coords_list, dtype=np.float32), residue_info + + +def _tmalign_sec_str(d13: float, d14: float, d15: float, + d24: float, d25: float, d35: float) -> int: + """ + Classify secondary structure based on CA-CA distances. + Implements the sec_str function from TM-align. + + Returns: 1 = coil, 2 = helix, 3 = turn, 4 = strand + """ + delta_helix = 2.1 + if (abs(d15 - 6.37) < delta_helix and abs(d14 - 5.18) < delta_helix and + abs(d25 - 5.18) < delta_helix and abs(d13 - 5.45) < delta_helix and + abs(d24 - 5.45) < delta_helix and abs(d35 - 5.45) < delta_helix): + return 2 + + delta_strand = 1.42 + if (abs(d15 - 13.0) < delta_strand and abs(d14 - 10.4) < delta_strand and + abs(d25 - 10.4) < delta_strand and abs(d13 - 6.1) < delta_strand and + abs(d24 - 6.1) < delta_strand and abs(d35 - 6.1) < delta_strand): + return 4 + + if d15 < 8: + return 3 + return 1 + + +def _compute_sse_tmalign(ca_coords: np.ndarray) -> np.ndarray: + """Compute SSE using TM-align's make_sec algorithm from CA coordinates.""" + n_residues = len(ca_coords) + sse = np.ones(n_residues, dtype=np.int32) + + for i in range(2, n_residues - 2): + d13 = np.linalg.norm(ca_coords[i - 2] - ca_coords[i]) + d14 = np.linalg.norm(ca_coords[i - 2] - ca_coords[i + 1]) + d15 = np.linalg.norm(ca_coords[i - 2] - ca_coords[i + 2]) + d24 = np.linalg.norm(ca_coords[i - 1] - ca_coords[i + 1]) + d25 = np.linalg.norm(ca_coords[i - 1] - ca_coords[i + 2]) + d35 = np.linalg.norm(ca_coords[i] - ca_coords[i + 2]) + sse[i] = _tmalign_sec_str(d13, d14, d15, d24, d25, d35) + + return sse + + +def _compute_sse_pydssp(coords: np.ndarray, device: str = "cpu") -> np.ndarray: + """Compute SSE using PyDSSP from pre-extracted backbone coordinates.""" + if device != "cpu" and torch.cuda.is_available(): + coords_tensor = torch.tensor(coords, dtype=torch.float32, device=device) + return pydssp.assign(coords_tensor, out_type='c3') + return pydssp.assign(coords, out_type='c3') + + +def _compute_sse_psea(input_file: Path) -> Optional[np.ndarray]: + """ + Compute SSE using P-SEA (Biotite). Requires a secondary Biotite read + since annotate_sse operates on Biotite AtomArrays. + + Returns array of SSE codes ('a', 'b', 'c') per residue, or None on failure. + """ + from biotite.structure import annotate_sse + from biotite.structure.io.pdbx import CIFFile as BiotiteCIFFile, get_structure + + try: + cif = BiotiteCIFFile.read(str(input_file)) + atom_array = get_structure(cif, model=1) + return annotate_sse(atom_array) + except Exception as e: + logger.error(f"P-SEA computation failed: {e}") + return None -def run_command(input_path, output_path): - command = [ - "mkdssp", - str(input_path), - str(output_path), - ] - result = subprocess.run(command, capture_output=True, text=True) - return result.returncode, input_path, result.stdout, result.stderr +def _make_range_dict(conf_type_id: str, beg: ResidueInfo, end: ResidueInfo) -> dict: + return { + 'conf_type_id': conf_type_id, + 'beg_label_comp_id': beg.comp_id, + 'beg_label_asym_id': beg.label_asym_id, + 'beg_label_seq_id': beg.label_seq_id, + 'pdbx_beg_PDB_ins_code': beg.ins_code, + 'end_label_comp_id': end.comp_id, + 'end_label_asym_id': end.label_asym_id, + 'end_label_seq_id': end.label_seq_id, + 'pdbx_end_PDB_ins_code': end.ins_code, + 'beg_auth_comp_id': beg.comp_id, + 'beg_auth_asym_id': beg.auth_asym_id, + 'beg_auth_seq_id': beg.auth_seq_id, + 'end_auth_comp_id': end.comp_id, + 'end_auth_asym_id': end.auth_asym_id, + 'end_auth_seq_id': end.auth_seq_id, + } -def run_dssp(input_file: Path, output_file: Path): - # output should be a .cif file - if not output_file.suffix == ".cif": + +def _find_secondary_structure_ranges( + sse_codes: np.ndarray, + residue_info: List[ResidueInfo], + code_map: dict, +) -> List[dict]: + """ + Convert per-residue SSE codes into contiguous ranges for _struct_conf. + + Each range contains all fields needed for a complete _struct_conf entry + including label/auth identifiers and insertion codes. + """ + ranges: List[dict] = [] + if len(sse_codes) == 0: + return ranges + + current_type = None + start_idx = 0 + + for i, code in enumerate(sse_codes): + ss_type = code_map.get(code) + + if ss_type != current_type: + if current_type is not None and i > start_idx: + ranges.append(_make_range_dict( + current_type, residue_info[start_idx], residue_info[i - 1], + )) + current_type = ss_type + start_idx = i + + if current_type is not None and len(sse_codes) > start_idx: + ranges.append(_make_range_dict( + current_type, residue_info[start_idx], residue_info[-1], + )) + + return ranges + + +def _get_algorithm_label(algorithm: str) -> str: + labels = { + "psea": "P-SEA", + "pydssp": "PyDSSP", + "tmalign": "TM-align", + } + return labels.get(algorithm, algorithm) + + +def _add_struct_conf_to_cif( + block: gemmi.cif.Block, + ranges: List[dict], + algorithm: str, +) -> None: + """Add _struct_conf and _struct_conf_type categories to a gemmi CIF block.""" + if not ranges: + return + + criteria_label = _get_algorithm_label(algorithm) + + conf_types = sorted(set(r['conf_type_id'] for r in ranges)) + type_loop = block.init_loop("_struct_conf_type.", ["id", "criteria"]) + for ct in conf_types: + type_loop.add_row([ct, criteria_label]) + + conf_loop = block.init_loop("_struct_conf.", _STRUCT_CONF_FIELDS) + type_counters: dict = {} + for r in ranges: + ct = r['conf_type_id'] + type_counters[ct] = type_counters.get(ct, 0) + 1 + conf_id = f"{ct}{type_counters[ct]}" + + conf_loop.add_row([ + conf_id, + r['conf_type_id'], + r['beg_label_comp_id'], + r['beg_label_asym_id'], + r['beg_label_seq_id'], + r['pdbx_beg_PDB_ins_code'], + r['end_label_comp_id'], + r['end_label_asym_id'], + r['end_label_seq_id'], + r['pdbx_end_PDB_ins_code'], + r['beg_auth_comp_id'], + r['beg_auth_asym_id'], + r['beg_auth_seq_id'], + r['end_auth_comp_id'], + r['end_auth_asym_id'], + r['end_auth_seq_id'], + ]) + + +def run_dssp( + input_file: Path, + output_file: Path, + algorithm: Algorithm = "psea", + device: str = "cpu", +) -> bool: + """ + Compute secondary structure and write annotated CIF. + + Reads the input CIF with gemmi (preserving all existing categories), + computes SSE with the chosen algorithm, adds _struct_conf / _struct_conf_type, + and writes the annotated CIF. + """ + if not str(output_file).endswith(".cif"): raise ValueError("Output file must have a .cif extension") - logger.info("Running command: mkdssp") - code, _, out, err = run_command(input_file, output_file) - if code == 0: - logger.info("Command executed successfully:\n%s", out) - else: - logger.error("Error executing command:\n%s", err) + try: + doc = gemmi.cif.read(str(input_file)) + block = doc.sole_block() + structure = gemmi.make_structure_from_block(block) + + if algorithm == "psea": + sse = _compute_sse_psea(input_file) + if sse is None: + logger.error(f"P-SEA failed for {input_file}") + return False + residue_info = _extract_polymer_residue_info(structure) + if len(sse) != len(residue_info): + logger.error( + f"Residue count mismatch for P-SEA in {input_file}: " + f"biotite={len(sse)}, gemmi={len(residue_info)}" + ) + return False + code_map = PSEA_CODE_MAP + + elif algorithm == "pydssp": + coords, residue_info = _extract_backbone_coords(structure) + if coords is None: + logger.error(f"Backbone extraction failed for {input_file}") + return False + sse = _compute_sse_pydssp(coords, device=device) + code_map = PYDSSP_CODE_MAP + + elif algorithm == "tmalign": + ca_coords, residue_info = _extract_ca_coords(structure) + if ca_coords is None: + logger.error(f"CA extraction failed for {input_file}") + return False + sse = _compute_sse_tmalign(ca_coords) + code_map = TMALIGN_CODE_MAP + + else: + logger.error(f"Unknown algorithm: {algorithm}") + return False + + ranges = _find_secondary_structure_ranges(sse, residue_info, code_map) + _add_struct_conf_to_cif(block, ranges, algorithm) + + doc.write_file(str(output_file)) + return True + + except Exception as e: + logger.error(f"Secondary structure failed for {input_file}: {e}") + import traceback + traceback.print_exc() + return False + + +def _process_single_dssp(args: Tuple[Path, Path]) -> Tuple[str, bool]: + """Worker function for batch DSSP. Works with both ProcessPool and ThreadPool.""" + input_file, output_dir = args + output_file = output_dir / input_file.name + success = run_dssp( + input_file, output_file, + algorithm=_CURRENT_ALGORITHM, device=_CURRENT_DEVICE, + ) + return input_file.name, success + + +def run_batch_dssp( + input_dir: Path, + output_dir: Path, + workers: int = 8, + pattern: str = "*.cif", + algorithm: Algorithm = "psea", + device: str = "cpu", +) -> Tuple[int, int]: + """ + Batch secondary structure processing with parallel execution. + + When device is 'cuda', uses ThreadPoolExecutor (CUDA contexts are not + fork-safe) and runs PyDSSP H-bond / helix / strand math on GPU. + Otherwise uses ProcessPoolExecutor for CPU parallelism. + """ + global _CURRENT_ALGORITHM, _CURRENT_DEVICE + _CURRENT_ALGORITHM = algorithm + _CURRENT_DEVICE = device + + input_files = list(input_dir.glob(pattern)) + if not input_files: + logger.warning(f"No files matching '{pattern}' in {input_dir}") + return 0, 0 + + output_dir.mkdir(parents=True, exist_ok=True) + work_items = [(f, output_dir) for f in input_files] + + algo_name = _get_algorithm_label(algorithm) + device_label = f"GPU ({device})" if device != "cpu" else "CPU" + logger.info( + f"Processing {len(input_files)} files with {workers} workers " + f"using {algo_name} on {device_label}" + ) + + success_count = 0 + error_count = 0 + + use_gpu = device != "cpu" and torch.cuda.is_available() + PoolClass = ThreadPoolExecutor if use_gpu else ProcessPoolExecutor + + with PoolClass(max_workers=workers) as executor: + futures = { + executor.submit(_process_single_dssp, item): item + for item in work_items + } + for future in as_completed(futures): + fname, ok = future.result() + if ok: + success_count += 1 + if success_count % 100 == 0: + logger.info(f"Processed {success_count}/{len(input_files)} files") + else: + error_count += 1 + logger.error(f"[FAILED] {fname}") + + logger.info(f"Batch complete: {success_count} success, {error_count} errors") + return success_count, error_count + + +def run_command(input_path, output_path): + """Legacy subprocess wrapper - kept for compatibility but not recommended.""" + import subprocess + command = ["mkdssp", str(input_path), str(output_path)] + result = subprocess.run(command, capture_output=True, text=True) + return result.returncode, input_path, result.stdout, result.stderr diff --git a/afdb_integration_kit/gpu/README.md b/afdb_integration_kit/gpu/README.md new file mode 100644 index 0000000..2026405 --- /dev/null +++ b/afdb_integration_kit/gpu/README.md @@ -0,0 +1,107 @@ +# Clashes - GPU-Accelerated Protein Structure Analysis + +High-throughput analysis of protein structures for clashes and interfaces using GPU acceleration. Designed for processing millions of generated complexes. + +## Features + +- **Fast**: ~1500 proteins/s on a single GPU +- **Scalable**: Process 10M complexes in ~2 hours +- **Batched**: Efficient GPU memory utilization +- **Parallel parsing**: Multi-CPU PDB parsing with fastpdb +- **Complete analysis**: Clashes (backbone + heavy) and interface residues + +## Installation + +```bash +# PyTorch (adjust for your CUDA version) +pip install torch --index-url https://download.pytorch.org/whl/cu118 + +# torch_cluster +pip install torch_cluster -f https://data.pyg.org/whl/torch-2.0.0+cu118.html + +# Other dependencies +pip install fastpdb biotite numpy tqdm +``` + +## Quick Start + +```python +from gpu import analyze_pdb_files_pipelined + +# Analyze PDB files and write results to JSON +results = analyze_pdb_files_pipelined( + ["complex1.pdb", "complex2.pdb", ...], + output_path="results", + batch_size=512, + device="cuda", + n_workers=16, +) + +# Each result contains: +for r in results: + print(f"{r.path}:") + print(f" Backbone clashes: {r.n_backbone_clashes}") + print(f" Heavy atom clashes: {r.n_heavy_clashes}") + print(f" Clashing residues: {r.backbone_clashing_residues}") + print(f" Interface residues: {r.interface_residues}") +``` + +## Module Structure + +``` +gpu/ +├── __init__.py # Package exports +├── protein.py # Protein dataclass +├── parse.py # PDB parser (fastpdb + multiprocessing) +├── batch.py # GPU batching utilities +├── clashes.py # Clash detection +├── interface.py # Interface residue detection +├── analyze.py # Master analysis pipeline +└── README.md +``` + +## Output Format + +```json +[ + { + "path": "complex1.pdb", + "n_residues": 512, + "n_atoms": 4190, + "n_backbone_clashes": 97, + "n_heavy_clashes": 561, + "backbone_clashing_residues": [ + {"res_id": 45, "chain_id": "A"}, + {"res_id": 52, "chain_id": "A"} + ], + "heavy_clashing_residues": [ + {"res_id": 45, "chain_id": "A"}, + {"res_id": 52, "chain_id": "A"}, + {"res_id": 78, "chain_id": "B"} + ], + "interface_residues": [ + {"res_id": 100, "chain_id": "A"}, + {"res_id": 200, "chain_id": "B"} + ] + } +] +``` + +## Definitions + +### Clashes +Two atoms clash when their distance is less than the sum of their VDW radii minus a tolerance: +``` +distance < (vdw_i + vdw_j) * (1 - clash_cutoff) +``` + +VDW radii (element-specific): +- C: 1.70 Å, N: 1.55 Å, O: 1.52 Å, S: 1.80 Å + +Default parameters: +- `clash_cutoff=0.4` (40% overlap) +- `min_seq_sep=3` (ignore i, i+1, i+2 neighbors) + +### Interface Residues +A residue is an interface residue if its CA atom is within `interface_cutoff` (default 8.0 Å) of a CA atom from a different chain. + diff --git a/afdb_integration_kit/gpu/__init__.py b/afdb_integration_kit/gpu/__init__.py new file mode 100644 index 0000000..a143102 --- /dev/null +++ b/afdb_integration_kit/gpu/__init__.py @@ -0,0 +1,111 @@ +# Copyright 2026 Maciej Majewski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +""" +Clashes - GPU-accelerated protein structure analysis. + +Computes atomic clashes and interface residues using efficient +batched processing on GPU with torch_cluster. + +Example (small datasets): + >>> from clashes import analyze_pdb_files + >>> results = analyze_pdb_files( + ... ["complex1.pdb", "complex2.pdb"], + ... output_dir="results/", + ... device="cuda", + ... ) + +Example (large datasets, >500 proteins): + >>> from clashes import analyze_pdb_files_pipelined + >>> results = analyze_pdb_files_pipelined( + ... pdb_paths, # List of 10M paths + ... output_dir="results/", + ... device="cuda", + ... ) + # ~1.4x faster due to pipelined parsing/compute/writing +""" + +# Core data structures +from .protein import Protein, empty_protein, MAX_HEAVY_ATOMS, AA_ORDER_HEAVY + +# Parsing +from .parse import parse_protein, parse_proteins + +# Batching +from .batch import ProteinBatch, create_batch, iter_batches, VDW_RADII + +# Clash detection +from .clashes import ( + count_clashes, + get_clash_pairs, + compute_clashes_from_batch, + compute_clashing_residues_from_batch, + SULFUR_VDW, + DISULFIDE_MAX, +) + +# Interface detection +from .interface import ( + compute_interface_residues, + compute_interface_residues_flat, +) + +# High-level analysis +from .analyze import ( + ALL_ANALYSES, + ClashContact, + InterfaceContact, + ProteinAnalysisResult, + analyze_batch, + analyze_proteins, + analyze_pdb_files, + analyze_pdb_files_pipelined, +) + +# Schema conversion +from .schema import result_to_interface_schema, result_to_clash_schema + +__all__ = [ + # Protein + "Protein", + "empty_protein", + "MAX_HEAVY_ATOMS", + "AA_ORDER_HEAVY", + # Parsing + "parse_protein", + "parse_proteins", + # Batching + "ProteinBatch", + "create_batch", + "iter_batches", + "VDW_RADII", + # Clashes + "count_clashes", + "get_clash_pairs", + "compute_clashes_from_batch", + "compute_clashing_residues_from_batch", + "SULFUR_VDW", + "DISULFIDE_MAX", + # Interface + "compute_interface_residues", + "compute_interface_residues_flat", + # Analysis + "ALL_ANALYSES", + "ClashContact", + "InterfaceContact", + "ProteinAnalysisResult", + "analyze_batch", + "analyze_proteins", + "analyze_pdb_files", + "analyze_pdb_files_pipelined", + # Schema + "result_to_interface_schema", + "result_to_clash_schema", +] diff --git a/afdb_integration_kit/gpu/analyze.py b/afdb_integration_kit/gpu/analyze.py new file mode 100644 index 0000000..f549d36 --- /dev/null +++ b/afdb_integration_kit/gpu/analyze.py @@ -0,0 +1,969 @@ +# Copyright 2026 Maciej Majewski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +""" +Master analysis script for protein structure quality metrics. + +Computes clashes (backbone and all atoms) and interface residues +in a batched, GPU-accelerated pipeline. + +Designed for high-throughput processing of millions of structures. + +Pipeline architecture: + [Parser Pool] --> [Parse Queue] --> [GPU] --> [Write Queue] --> [Writer Pool] + +All three stages run concurrently for maximum throughput. +""" +from __future__ import annotations + +import json +import queue +import threading +from concurrent.futures import ThreadPoolExecutor, Future +from dataclasses import dataclass, asdict, field +from pathlib import Path +from typing import List, Optional, Sequence, Set, Union + +import torch +from tqdm import tqdm + +try: + from .protein import Protein + from .parse import parse_protein, parse_proteins + from .batch import ProteinBatch, create_batch, iter_batches + from .clashes import compute_clashes_from_batch + from .interface import compute_interface_residues_flat + from .schema import result_to_interface_schema, result_to_clash_schema +except ImportError: + from protein import Protein + from parse import parse_protein, parse_proteins + from batch import ProteinBatch, create_batch, iter_batches + from clashes import compute_clashes_from_batch + from interface import compute_interface_residues_flat + from schema import result_to_interface_schema, result_to_clash_schema + + +@dataclass +class InterfaceContact: + """A single inter-chain CA-CA contact.""" + chain_1: str + res_1_id: int + aa_1_type: str + chain_2: str + res_2_id: int + aa_2_type: str + distance: float + + +@dataclass +class ClashContact: + """A single atomic clash between two residues.""" + chain_1: str + res_1_id: int + aa_1_type: str + atom_1_name: str + chain_2: str + res_2_id: int + aa_2_type: str + atom_2_name: str + distance: float + overlap: float # vdw_sum - distance (Angstroms) + + +@dataclass +class ProteinAnalysisResult: + """Analysis results for a single protein.""" + # Identification + path: str + n_residues: int + n_atoms: int + + # Clash counts + n_backbone_clashes: int + n_heavy_clashes: int + + # Clashing residues (list of {"res_id": int, "chain_id": str, "aa_type": str}) + backbone_clashing_residues: List[dict] + heavy_clashing_residues: List[dict] + + # Interface residues (list of {"res_id": int, "chain_id": str, "aa_type": str}) + interface_residues: List[dict] + + # Inter-chain contacts with distances + interface_contacts: List[InterfaceContact] = field(default_factory=list) + + # Clash contacts with distances and overlaps + backbone_clash_contacts: List[ClashContact] = field(default_factory=list) + heavy_clash_contacts: List[ClashContact] = field(default_factory=list) + + def to_dict(self) -> dict: + return asdict(self) + + +def _materialize_contacts(result: ProteinAnalysisResult) -> None: + """Build contact lists from raw arrays attached by analyze_batch. + + Called in the writer thread so that dataclass construction does not + block the GPU thread between batches. + """ + if not hasattr(result, '_protein'): + return # already materialized or built directly + + p = result._protein + n_res = p.n_residues + + # --- Interface contacts --- + src, dst, dist = result._iface_src, result._iface_dst, result._iface_dist + if len(src) > 0: + c1, r1, a1 = p.chain_ids[src], p.res_ids[src], p.res_names[src] + c2, r2, a2 = p.chain_ids[dst], p.res_ids[dst], p.res_names[dst] + result.interface_contacts = [ + InterfaceContact( + chain_1=str(c1[k]), res_1_id=int(r1[k]), aa_1_type=str(a1[k]), + chain_2=str(c2[k]), res_2_id=int(r2[k]), aa_2_type=str(a2[k]), + distance=float(dist[k]), + ) + for k in range(len(src)) + ] + + # --- Clash contacts --- + result.backbone_clash_contacts = _clash_list_from_arrays(p, result._bb_clash) + result.heavy_clash_contacts = _clash_list_from_arrays(p, result._hv_clash) + + # Free raw data (no longer needed) + del result._protein + del result._iface_src, result._iface_dst, result._iface_dist + del result._bb_clash, result._hv_clash + + +def _clash_list_from_arrays( + p: Protein, arrays: tuple, +) -> List[ClashContact]: + """Build ClashContact list from per-protein clash arrays.""" + ri, rj, ai, aj, dists, overlaps = arrays + if len(ri) == 0: + return [] + + valid = (ri < p.n_residues) & (rj < p.n_residues) + if not valid.all(): + ri, rj, ai, aj = ri[valid], rj[valid], ai[valid], aj[valid] + dists, overlaps = dists[valid], overlaps[valid] + + if len(ri) == 0: + return [] + + chains_i, res_ids_i, aa_i = p.chain_ids[ri], p.res_ids[ri], p.res_names[ri] + anames_i = p.atom_names[ri, ai] + chains_j, res_ids_j, aa_j = p.chain_ids[rj], p.res_ids[rj], p.res_names[rj] + anames_j = p.atom_names[rj, aj] + + return [ + ClashContact( + chain_1=str(chains_i[k]), + res_1_id=int(res_ids_i[k]), + aa_1_type=str(aa_i[k]), + atom_1_name=str(anames_i[k]) if anames_i[k] else f"slot{ai[k]}", + chain_2=str(chains_j[k]), + res_2_id=int(res_ids_j[k]), + aa_2_type=str(aa_j[k]), + atom_2_name=str(anames_j[k]) if anames_j[k] else f"slot{aj[k]}", + distance=float(dists[k]), + overlap=float(overlaps[k]), + ) + for k in range(len(ri)) + ] + + +def _write_result_json( + result: ProteinAnalysisResult, + output_dir: Path, + interface_cutoff: float = 8.0, +) -> List[str]: + """Write interface and clash schema JSONs as separate files. + + Produces up to two files per structure: + - {stem}_interface.json (if interface contacts exist) + - {stem}_clashes.json (if clashes exist) + + Returns list of output paths written. + """ + # Materialize contact lists from raw arrays (deferred from GPU thread) + _materialize_contacts(result) + + stem = Path(result.path).stem + paths: List[str] = [] + + # Interface file + iface = result_to_interface_schema(result, interface_cutoff=interface_cutoff) + if iface is not None: + p = output_dir / f"{stem}_interface.json" + with open(p, "w") as f: + json.dump(iface, f) + paths.append(str(p)) + + # Clashes file (always written — includes both sites with n_clashes=0 when clean) + clash = result_to_clash_schema(result) + p = output_dir / f"{stem}_clashes.json" + with open(p, "w") as f: + json.dump(clash, f) + paths.append(str(p)) + + return paths + + +def _write_batch_results( + results: List[ProteinAnalysisResult], + output_dir: Path, + interface_cutoff: float = 8.0, +) -> List[str]: + """Write a batch of results to individual JSON files.""" + paths = [] + for result in results: + paths.extend(_write_result_json(result, output_dir, interface_cutoff=interface_cutoff)) + return paths + + +ALL_ANALYSES = {"interface", "backbone_clashes", "heavy_atom_clashes"} + + +@torch.no_grad() +def analyze_batch( + batch: ProteinBatch, + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + interface_cutoff: float = 8.0, + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, + analyses: Optional[Set[str]] = None, +) -> List[ProteinAnalysisResult]: + """ + Analyze a batch of proteins on GPU. + + Computes (controlled by *analyses*): + - Backbone clashes (N, CA, C, O atoms) + - Heavy atom clashes (all non-hydrogen atoms, excluding disulfide S-S bonds) + - Interface residues (CA-CA distance < interface_cutoff between chains) + + Args: + analyses: Set of analyses to run. Valid values are + ``"interface"``, ``"backbone_clashes"``, ``"heavy_atom_clashes"``. + ``None`` (default) runs all three. + """ + if analyses is None: + analyses = ALL_ANALYSES + + B = batch.batch_size + N = batch.max_residues + device = batch.coords.device + _empty = torch.empty(0, dtype=torch.long, device=device) + _empty_f = torch.empty(0, dtype=torch.float, device=device) + + # ===== BACKBONE CLASHES ===== + if "backbone_clashes" in analyses: + # Note: backbone doesn't include S, so exclude_disulfides has no effect + bb_prot, bb_ri, bb_rj, bb_ai, bb_aj, bb_dists, bb_overlaps = compute_clashes_from_batch( + batch.coords, + batch.mask, + batch.vdw_radii, + selection="backbone", + clash_cutoff=clash_cutoff, + min_seq_sep=min_seq_sep, + max_num_neighbors=max_num_neighbors, + exclude_disulfides=False, # No S in backbone + ) + + # Compute backbone counts and mask + if bb_prot.numel() > 0: + bb_counts = torch.bincount(bb_prot, minlength=B) + bb_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + bb_mask[bb_prot, bb_ri] = True + bb_mask[bb_prot, bb_rj] = True + else: + bb_counts = torch.zeros(B, dtype=torch.long, device=device) + bb_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + else: + bb_counts = torch.zeros(B, dtype=torch.long, device=device) + bb_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + bb_prot = bb_ri = bb_rj = bb_ai = bb_aj = _empty + bb_dists = bb_overlaps = _empty_f + + # ===== HEAVY ATOM CLASHES ===== + if "heavy_atom_clashes" in analyses: + hv_prot, hv_ri, hv_rj, hv_ai, hv_aj, hv_dists, hv_overlaps = compute_clashes_from_batch( + batch.coords, + batch.mask, + batch.vdw_radii, + selection="heavy", + clash_cutoff=clash_cutoff, + min_seq_sep=min_seq_sep, + max_num_neighbors=max_num_neighbors, + exclude_disulfides=exclude_disulfides, + ) + + # Compute heavy counts and mask + if hv_prot.numel() > 0: + heavy_counts = torch.bincount(hv_prot, minlength=B) + heavy_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + heavy_mask[hv_prot, hv_ri] = True + heavy_mask[hv_prot, hv_rj] = True + else: + heavy_counts = torch.zeros(B, dtype=torch.long, device=device) + heavy_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + else: + heavy_counts = torch.zeros(B, dtype=torch.long, device=device) + heavy_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + hv_prot = hv_ri = hv_rj = hv_ai = hv_aj = _empty + hv_dists = hv_overlaps = _empty_f + + # ===== INTERFACE RESIDUES ===== + if "interface" in analyses: + interface_mask, contact_src, contact_dst, contact_dist = compute_interface_residues_flat( + batch.ca_coords, + batch.ca_mask, + batch.chain_ids, + contact_cutoff=interface_cutoff, + max_num_neighbors=max_num_neighbors, + ) + else: + interface_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + contact_src = contact_dst = _empty + contact_dist = _empty_f + + # ===== MOVE TO CPU ===== + bb_counts_np = bb_counts.cpu().numpy() + bb_mask_np = bb_mask.cpu().numpy() + bb_prot_np = bb_prot.cpu().numpy() + bb_ri_np = bb_ri.cpu().numpy() + bb_rj_np = bb_rj.cpu().numpy() + bb_ai_np = bb_ai.cpu().numpy() + bb_aj_np = bb_aj.cpu().numpy() + bb_dists_np = bb_dists.cpu().numpy() + bb_overlaps_np = bb_overlaps.cpu().numpy() + + heavy_counts_np = heavy_counts.cpu().numpy() + heavy_mask_np = heavy_mask.cpu().numpy() + hv_prot_np = hv_prot.cpu().numpy() + hv_ri_np = hv_ri.cpu().numpy() + hv_rj_np = hv_rj.cpu().numpy() + hv_ai_np = hv_ai.cpu().numpy() + hv_aj_np = hv_aj.cpu().numpy() + hv_dists_np = hv_dists.cpu().numpy() + hv_overlaps_np = hv_overlaps.cpu().numpy() + + interface_mask_np = interface_mask.cpu().numpy() + contact_src_np = contact_src.cpu().numpy() + contact_dst_np = contact_dst.cpu().numpy() + contact_dist_np = contact_dist.cpu().numpy() + + # ===== BUILD RESULTS ===== + results = [] + + for i in range(B): + p = batch.proteins[i] + n_res = p.n_residues + + # Extract clashing residue indices via numpy (vectorized) + bb_res_indices = bb_mask_np[i, :n_res].nonzero()[0] + heavy_res_indices = heavy_mask_np[i, :n_res].nonzero()[0] + interface_indices = interface_mask_np[i, :n_res].nonzero()[0] + + # Map to (res_id, chain_id, aa_type) format (vectorized lookups) + def indices_to_residue_list(idx_arr): + if len(idx_arr) == 0: + return [] + ids = p.res_ids[idx_arr] + chains = p.chain_ids[idx_arr] + names = p.res_names[idx_arr] + return [ + {"res_id": int(ids[k]), "chain_id": str(chains[k]), "aa_type": str(names[k])} + for k in range(len(idx_arr)) + ] + + # --- Slice interface arrays (no contact construction yet) --- + lo = i * N + hi = (i + 1) * N + edge_mask = (contact_src_np >= lo) & (contact_src_np < hi) + p_src = contact_src_np[edge_mask] - lo + p_dst = contact_dst_np[edge_mask] - lo + p_dist = contact_dist_np[edge_mask] + + dedup = p_src < p_dst + p_src, p_dst, p_dist = p_src[dedup], p_dst[dedup], p_dist[dedup] + valid_if = (p_src < n_res) & (p_dst < n_res) + if not valid_if.all(): + p_src, p_dst, p_dist = p_src[valid_if], p_dst[valid_if], p_dist[valid_if] + + # --- Slice clash arrays per-protein --- + bb_sel = bb_prot_np == i + hv_sel = hv_prot_np == i + + result = ProteinAnalysisResult( + path=p.path, + n_residues=n_res, + n_atoms=int(p.mask.sum()), + n_backbone_clashes=int(bb_counts_np[i]), + n_heavy_clashes=int(heavy_counts_np[i]), + backbone_clashing_residues=indices_to_residue_list(bb_res_indices), + heavy_clashing_residues=indices_to_residue_list(heavy_res_indices), + interface_residues=indices_to_residue_list(interface_indices), + # Contact lists are materialized later by _materialize_contacts + # (runs in writer thread, concurrent with next GPU batch) + ) + + # Attach raw arrays for deferred contact materialization + result._protein = p + result._iface_src = p_src + result._iface_dst = p_dst + result._iface_dist = p_dist + result._bb_clash = ( + bb_ri_np[bb_sel], bb_rj_np[bb_sel], bb_ai_np[bb_sel], + bb_aj_np[bb_sel], bb_dists_np[bb_sel], bb_overlaps_np[bb_sel], + ) + result._hv_clash = ( + hv_ri_np[hv_sel], hv_rj_np[hv_sel], hv_ai_np[hv_sel], + hv_aj_np[hv_sel], hv_dists_np[hv_sel], hv_overlaps_np[hv_sel], + ) + results.append(result) + + return results + + +def _extract_clash_contacts( + p: Protein, + protein_idx: int, + prot_np, ri_np, rj_np, ai_np, aj_np, dists_np, overlaps_np, + selection: str, +) -> List[ClashContact]: + """Extract ClashContact list for a single protein from batch clash arrays.""" + mask = prot_np == protein_idx + if not mask.any(): + return [] + + indices = mask.nonzero()[0] + + # Batch-extract all arrays at once (one fancy-index op each) + ri = ri_np[indices] + rj = rj_np[indices] + ai = ai_np[indices] + aj = aj_np[indices] + dists = dists_np[indices] + overlaps = overlaps_np[indices] + + # Filter out-of-bounds residues + valid = (ri < p.n_residues) & (rj < p.n_residues) + if not valid.all(): + ri, rj, ai, aj = ri[valid], rj[valid], ai[valid], aj[valid] + dists, overlaps = dists[valid], overlaps[valid] + + if len(ri) == 0: + return [] + + # Vectorized lookups into protein arrays + chains_i = p.chain_ids[ri] + res_ids_i = p.res_ids[ri] + aa_i = p.res_names[ri] + anames_i = p.atom_names[ri, ai] + + chains_j = p.chain_ids[rj] + res_ids_j = p.res_ids[rj] + aa_j = p.res_names[rj] + anames_j = p.atom_names[rj, aj] + + contacts = [] + for k in range(len(ri)): + a1 = str(anames_i[k]) if anames_i[k] else f"slot{ai[k]}" + a2 = str(anames_j[k]) if anames_j[k] else f"slot{aj[k]}" + contacts.append(ClashContact( + chain_1=str(chains_i[k]), + res_1_id=int(res_ids_i[k]), + aa_1_type=str(aa_i[k]), + atom_1_name=a1, + chain_2=str(chains_j[k]), + res_2_id=int(res_ids_j[k]), + aa_2_type=str(aa_j[k]), + atom_2_name=a2, + distance=float(dists[k]), + overlap=float(overlaps[k]), + )) + + return contacts + + +def analyze_proteins( + proteins: Sequence[Protein], + batch_size: int = 32, + device: str = "cuda", + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + interface_cutoff: float = 8.0, + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, + progress: bool = True, + output_dir: Optional[Path] = None, + n_write_workers: int = 4, + analyses: Optional[Set[str]] = None, +) -> List[ProteinAnalysisResult]: + """ + Analyze a list of proteins with optional parallel output writing. + + Args: + proteins: List of Protein objects + batch_size: Number of proteins per GPU batch + device: Computation device ("cuda" or "cpu") + clash_cutoff: VDW overlap fraction for clashes + min_seq_sep: Minimum sequence separation for clashes + interface_cutoff: CA-CA distance cutoff for interface + max_num_neighbors: Max neighbors for radius_graph + exclude_disulfides: If True, S-S pairs within 2.5 Å are not counted as clashes + progress: Show progress bar + output_dir: If provided, write individual JSON files here + n_write_workers: Number of parallel writers (overlaps I/O with compute) + + Returns: + List of ProteinAnalysisResult + """ + results = [] + n_proteins = len(proteins) + + iterator = range(0, n_proteins, batch_size) + if progress: + iterator = tqdm(iterator, desc="Analyzing", unit="batch") + + # Set up parallel writer if output_dir provided + writer_pool = None + pending_writes: List[Future] = [] + + if output_dir is not None: + output_dir = Path(output_dir) + output_dir.mkdir(parents=True, exist_ok=True) + writer_pool = ThreadPoolExecutor(max_workers=n_write_workers) + + try: + for batch_start in iterator: + batch_end = min(batch_start + batch_size, n_proteins) + batch_proteins = proteins[batch_start:batch_end] + indices = list(range(batch_start, batch_end)) + + batch = create_batch(batch_proteins, indices, device) + batch_results = analyze_batch( + batch, + clash_cutoff=clash_cutoff, + min_seq_sep=min_seq_sep, + interface_cutoff=interface_cutoff, + max_num_neighbors=max_num_neighbors, + exclude_disulfides=exclude_disulfides, + analyses=analyses, + ) + results.extend(batch_results) + + # Submit batch results to writer pool (overlaps with next batch compute) + if writer_pool is not None: + future = writer_pool.submit( + _write_batch_results, batch_results, output_dir, + interface_cutoff, + ) + pending_writes.append(future) + + # Clear GPU cache periodically + if device == "cuda" and batch_start % (batch_size * 10) == 0: + torch.cuda.empty_cache() + + # Wait for all writes to complete + if writer_pool is not None: + for future in pending_writes: + future.result() # Raises any exceptions + + finally: + if writer_pool is not None: + writer_pool.shutdown(wait=True) + + return results + + +def analyze_pdb_files( + paths: Sequence[Union[str, Path]], + output_dir: Optional[Union[str, Path]] = None, + batch_size: int = 32, + n_workers: int = 4, + device: str = "cuda", + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + interface_cutoff: float = 8.0, + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, + progress: bool = True, + analyses: Optional[Set[str]] = None, +) -> List[ProteinAnalysisResult]: + """ + Analyze PDB files and write individual JSON results. + + This is the main entry point for batch processing. + + Args: + paths: List of PDB file paths + output_dir: Directory for output JSON files (one per PDB) + batch_size: Number of proteins per GPU batch + n_workers: Number of CPUs for parallel PDB parsing and JSON writing + device: Computation device + clash_cutoff: VDW overlap fraction for clashes + min_seq_sep: Minimum sequence separation for clashes + interface_cutoff: CA-CA distance cutoff for interface + max_num_neighbors: Max neighbors for radius_graph + exclude_disulfides: If True, S-S pairs within 2.5 Å are not counted as clashes + progress: Show progress bars + + Returns: + List of ProteinAnalysisResult + + Example: + >>> from clashes import analyze_pdb_files + >>> results = analyze_pdb_files( + ... ["complex1.pdb", "complex2.pdb"], + ... output_dir="results/", + ... batch_size=64, + ... device="cuda", + ... ) + # Creates: results/complex1.json, results/complex2.json + """ + # Parse PDB files + if progress: + print(f"Parsing {len(paths)} PDB files...") + proteins = parse_proteins(paths, n_jobs=n_workers) + + if progress: + print(f"Parsed {len(proteins)} proteins successfully") + + # Convert output_dir to Path if provided + output_path = Path(output_dir) if output_dir is not None else None + + # Analyze with parallel writing + results = analyze_proteins( + proteins, + batch_size=batch_size, + device=device, + clash_cutoff=clash_cutoff, + min_seq_sep=min_seq_sep, + interface_cutoff=interface_cutoff, + max_num_neighbors=max_num_neighbors, + exclude_disulfides=exclude_disulfides, + progress=progress, + output_dir=output_path, + n_write_workers=n_workers, # Use same number of workers + analyses=analyses, + ) + + if progress and output_dir is not None: + print(f"Results written to {output_dir}/ ({len(results)} files)") + + return results + + +def analyze_pdb_files_pipelined( + paths: Sequence[Union[str, Path]], + output_dir: Optional[Union[str, Path]] = None, + batch_size: int = 32, + n_workers: int = 4, + device: str = "cuda", + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + interface_cutoff: float = 8.0, + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, + progress: bool = True, + prefetch_batches: int = 2, + analyses: Optional[Set[str]] = None, +) -> List[ProteinAnalysisResult]: + """ + Analyze PDB files with pipelined parsing, computation, and writing. + + Pipeline stages overlap: + - Batch N-1: being written to disk (ThreadPoolExecutor) + - Batch N: being computed on GPU + - Batch N+1: being parsed from disk (multiprocessing.Pool) + + Performance notes: + - For small datasets (<200 proteins), use analyze_pdb_files instead + (multiprocessing overhead dominates) + - For large datasets (>500 proteins), this is ~1.4x faster + - At 1000 proteins: ~490 proteins/s vs ~350 proteins/s sequential + - For 10M proteins: ~5.7 hours vs ~7.8 hours sequential + + Args: + paths: List of PDB file paths + output_dir: Directory for output JSON files (one per PDB) + batch_size: Number of proteins per GPU batch + n_workers: Number of CPUs for parsing and writing + device: Computation device + clash_cutoff: VDW overlap fraction for clashes + min_seq_sep: Minimum sequence separation for clashes + interface_cutoff: CA-CA distance cutoff for interface + max_num_neighbors: Max neighbors for radius_graph + exclude_disulfides: If True, S-S pairs within 2.5 Å are not counted as clashes + progress: Show progress bars + prefetch_batches: Number of batches to parse ahead (currently unused) + + Returns: + List of ProteinAnalysisResult + """ + import multiprocessing as mp + + n_paths = len(paths) + if n_paths == 0: + return [] + + # Set up output directory + output_path = None + if output_dir is not None: + output_path = Path(output_dir) + output_path.mkdir(parents=True, exist_ok=True) + + all_results: List[ProteinAnalysisResult] = [] + pending_writes: List[Future] = [] + + # Set up writer pool for async writing + writer_pool = None + if output_path is not None: + writer_pool = ThreadPoolExecutor(max_workers=n_workers) + + try: + # Use multiprocessing.Pool with imap_unordered for streaming parsing + # chunksize controls how many files each worker processes at once + chunksize = max(1, batch_size // n_workers) + + with mp.Pool(processes=n_workers) as parse_pool: + # Start parsing all files asynchronously + protein_iter = parse_pool.imap_unordered( + parse_protein, + paths, + chunksize=chunksize, + ) + + pbar = None + if progress: + pbar = tqdm(total=n_paths, desc="Processing", unit="protein") + + current_batch: List[Protein] = [] + + for protein in protein_iter: + if protein is None: + if pbar: + pbar.update(1) + continue + + current_batch.append(protein) + + # Process batch when full + if len(current_batch) >= batch_size: + batch_results = _process_batch( + current_batch, device, + clash_cutoff, min_seq_sep, interface_cutoff, + max_num_neighbors, exclude_disulfides, + analyses=analyses, + ) + all_results.extend(batch_results) + + # Async write (overlaps with next batch parse/compute) + if writer_pool is not None and output_path is not None: + future = writer_pool.submit( + _write_batch_results, batch_results, output_path, + interface_cutoff, + ) + pending_writes.append(future) + + if pbar: + pbar.update(len(current_batch)) + + current_batch = [] + + # Process remaining proteins + if current_batch: + batch_results = _process_batch( + current_batch, device, + clash_cutoff, min_seq_sep, interface_cutoff, + max_num_neighbors, exclude_disulfides, + analyses=analyses, + ) + all_results.extend(batch_results) + + if writer_pool is not None and output_path is not None: + future = writer_pool.submit( + _write_batch_results, batch_results, output_path, + interface_cutoff, + ) + pending_writes.append(future) + + if pbar: + pbar.update(len(current_batch)) + + if pbar: + pbar.close() + + # Wait for all writes to complete + for future in pending_writes: + future.result() + + if progress and output_path is not None: + print(f"Results written to {output_dir}/ ({len(all_results)} files)") + + return all_results + + finally: + if writer_pool is not None: + writer_pool.shutdown(wait=True) + + +def _process_batch( + proteins: List[Protein], + device: str, + clash_cutoff: float, + min_seq_sep: int, + interface_cutoff: float, + max_num_neighbors: int, + exclude_disulfides: bool, + analyses: Optional[Set[str]] = None, +) -> List[ProteinAnalysisResult]: + """Create batch, move to GPU, compute metrics.""" + indices = list(range(len(proteins))) + batch = create_batch(proteins, indices, device) + return analyze_batch( + batch, + clash_cutoff=clash_cutoff, + min_seq_sep=min_seq_sep, + interface_cutoff=interface_cutoff, + max_num_neighbors=max_num_neighbors, + exclude_disulfides=exclude_disulfides, + analyses=analyses, + ) + + +def main(): + """Command-line interface.""" + import argparse + import glob + + parser = argparse.ArgumentParser( + description="Analyze protein structures for clashes and interfaces" + ) + parser.add_argument( + "inputs", + nargs="+", + help="PDB files or glob patterns", + ) + parser.add_argument( + "-o", "--output-dir", + required=True, + help="Output directory for JSON files (one per PDB)", + ) + parser.add_argument( + "--batch-size", + type=int, + default=32, + help="Proteins per GPU batch (default: 32)", + ) + parser.add_argument( + "--workers", + type=int, + default=4, + help="CPUs for PDB parsing and JSON writing (default: 4)", + ) + parser.add_argument( + "--device", + default="cuda", + choices=["cuda", "cpu"], + help="Computation device (default: cuda)", + ) + parser.add_argument( + "--clash-cutoff", + type=float, + default=0.12, + help="VDW overlap fraction for clashes (default: 0.12, ~0.4 A overlap)", + ) + parser.add_argument( + "--interface-cutoff", + type=float, + default=8.0, + help="CA-CA distance for interface (default: 8.0 Å)", + ) + parser.add_argument( + "--quiet", + action="store_true", + help="Suppress progress output", + ) + parser.add_argument( + "--no-disulfide-exclusion", + action="store_true", + help="Count S-S bonds as clashes (default: exclude disulfide bonds)", + ) + parser.add_argument( + "--analyses", + nargs="+", + choices=["interface", "backbone_clashes", "heavy_atom_clashes"], + default=["interface", "backbone_clashes", "heavy_atom_clashes"], + help="Which analyses to run (default: all three)", + ) + parser.add_argument( + "--no-pipeline", + action="store_true", + help="Disable pipelined I/O (parse all first, then compute)", + ) + parser.add_argument( + "--prefetch", + type=int, + default=2, + help="Number of batches to prefetch in pipeline mode (default: 2)", + ) + + args = parser.parse_args() + + # Expand glob patterns + paths = [] + for pattern in args.inputs: + expanded = glob.glob(pattern) + if expanded: + paths.extend(expanded) + else: + paths.append(pattern) # Might be a literal path + + if not paths: + parser.error("No input files found") + + # Run analysis (pipelined by default) + analyses_set = set(args.analyses) + if args.no_pipeline: + analyze_pdb_files( + paths, + output_dir=args.output_dir, + batch_size=args.batch_size, + n_workers=args.workers, + device=args.device, + clash_cutoff=args.clash_cutoff, + interface_cutoff=args.interface_cutoff, + exclude_disulfides=not args.no_disulfide_exclusion, + progress=not args.quiet, + analyses=analyses_set, + ) + else: + analyze_pdb_files_pipelined( + paths, + output_dir=args.output_dir, + batch_size=args.batch_size, + n_workers=args.workers, + device=args.device, + clash_cutoff=args.clash_cutoff, + interface_cutoff=args.interface_cutoff, + exclude_disulfides=not args.no_disulfide_exclusion, + progress=not args.quiet, + prefetch_batches=args.prefetch, + analyses=analyses_set, + ) + + +if __name__ == "__main__": + main() diff --git a/afdb_integration_kit/gpu/batch.py b/afdb_integration_kit/gpu/batch.py new file mode 100644 index 0000000..28d0d10 --- /dev/null +++ b/afdb_integration_kit/gpu/batch.py @@ -0,0 +1,203 @@ +# Copyright 2026 Maciej Majewski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +""" +GPU batching utilities for protein structure analysis. + +Creates GPU-resident tensors with coordinates and metadata for efficient +batched computation of clashes, interfaces, and other metrics. + +Optimized for high-throughput processing of millions of structures. +""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import List, Sequence, Iterator + +import numpy as np +import torch + +try: + from .protein import Protein, MAX_HEAVY_ATOMS +except ImportError: + from protein import Protein, MAX_HEAVY_ATOMS + + +@dataclass +class ProteinBatch: + """ + GPU-resident batch of protein structures with metadata. + """ + coords: torch.Tensor # (B, max_res, max_atoms, 3) + mask: torch.Tensor # (B, max_res, max_atoms) + ca_coords: torch.Tensor # (B, max_res, 3) + ca_mask: torch.Tensor # (B, max_res) + chain_ids: torch.Tensor # (B, max_res) + res_ids: torch.Tensor # (B, max_res) + vdw_radii: torch.Tensor # (B, max_res, max_atoms) + batch_indices: List[int] + proteins: List[Protein] + device: str + + @property + def batch_size(self) -> int: + return self.coords.shape[0] + + @property + def max_residues(self) -> int: + return self.coords.shape[1] + + @property + def max_atoms(self) -> int: + return self.coords.shape[2] + + +# Van der Waals radii +VDW_RADII = { + "C": 1.70, + "N": 1.55, + "O": 1.52, + "S": 1.80, + "default": 1.70, +} + +# Sidechain heteroatom positions per residue type +# Format: {res_name: [(slot_index, vdw_radius), ...]} +_SIDECHAIN_HETEROATOMS = { + "ARG": [(7, 1.55), (9, 1.55), (10, 1.55)], # NE, NH1, NH2 + "ASN": [(6, 1.52), (7, 1.55)], # OD1, ND2 + "ASP": [(6, 1.52), (7, 1.52)], # OD1, OD2 + "CYS": [(5, 1.80)], # SG + "GLN": [(7, 1.52), (8, 1.55)], # OE1, NE2 + "GLU": [(7, 1.52), (8, 1.52)], # OE1, OE2 + "HIS": [(6, 1.55), (9, 1.55)], # ND1, NE2 + "LYS": [(8, 1.55)], # NZ + "MET": [(6, 1.80)], # SD + "SER": [(5, 1.52)], # OG + "THR": [(5, 1.52)], # OG1 + "TRP": [(8, 1.55)], # NE1 + "TYR": [(11, 1.52)], # OH +} + + +def create_batch( + proteins: Sequence[Protein], + indices: List[int], + device: str = "cuda", +) -> ProteinBatch: + """ + Create a GPU-resident batch from proteins. + + Optimized using array slicing instead of loops. + """ + B = len(proteins) + if B == 0: + raise ValueError("Cannot create batch from empty protein list") + + # Get dimensions + max_res = max(p.n_residues for p in proteins) + max_atoms = max(p.coords.shape[1] for p in proteins) + + if max_res == 0: + raise ValueError("All proteins have 0 residues") + + # Allocate arrays + coords_np = np.zeros((B, max_res, max_atoms, 3), dtype=np.float32) + mask_np = np.zeros((B, max_res, max_atoms), dtype=bool) + res_ids_np = np.zeros((B, max_res), dtype=np.int32) + chain_ids_np = np.zeros((B, max_res), dtype=np.int32) + res_names_np = np.empty((B, max_res), dtype=' Iterator[ProteinBatch]: + """Iterate over proteins in batches.""" + n = len(proteins) + for start in range(0, n, batch_size): + end = min(start + batch_size, n) + batch_proteins = proteins[start:end] + indices = list(range(start, end)) + yield create_batch(batch_proteins, indices, device) diff --git a/afdb_integration_kit/gpu/clashes.py b/afdb_integration_kit/gpu/clashes.py new file mode 100644 index 0000000..588a98d --- /dev/null +++ b/afdb_integration_kit/gpu/clashes.py @@ -0,0 +1,656 @@ +# Copyright 2026 Maciej Majewski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +""" +GPU-accelerated atomic clash detection. + +Uses torch_cluster.radius_graph when available (CUDA); otherwise falls back +to a pure-PyTorch implementation so clash detection works on systems where +the torch_cluster wheel cannot be loaded (e.g. older glibc). + +Handles disulfide bonds: S-S pairs within ~2.5 Å are excluded from +clash detection since they represent covalent bonds, not steric clashes. +""" +from __future__ import annotations + +from typing import List, Literal, Sequence + +import numpy as np +import torch + +try: + from torch_cluster import radius_graph as _radius_graph_ext + _RADIUS_GRAPH_USE_FALLBACK = False +except (ImportError, OSError): + _radius_graph_ext = None + _RADIUS_GRAPH_USE_FALLBACK = True + +try: + from .protein import Protein, MAX_HEAVY_ATOMS +except ImportError: + from protein import Protein, MAX_HEAVY_ATOMS + +# Chunk size for fallback radius graph to limit cdist memory +_RADIUS_GRAPH_CHUNK_SIZE = 2000 + + +def _radius_graph_pytorch( + x: torch.Tensor, + r: float, + batch: torch.Tensor | None = None, + loop: bool = False, + max_num_neighbors: int = 128, +) -> torch.Tensor: + """Pure-PyTorch fallback for radius_graph when torch_cluster is unavailable.""" + device = x.device + n = x.size(0) + if batch is None: + batch = torch.zeros(n, dtype=torch.long, device=device) + batch_ids = batch.unique(sorted=True) + src_list: List[torch.Tensor] = [] + dst_list: List[torch.Tensor] = [] + for b in batch_ids: + mask_b = batch == b + idx_b = mask_b.nonzero(as_tuple=True)[0] + x_b = x[idx_b] + n_b = x_b.size(0) + if n_b < 2: + continue + chunk_size = min(_RADIUS_GRAPH_CHUNK_SIZE, n_b) + for start in range(0, n_b, chunk_size): + end = min(start + chunk_size, n_b) + x_chunk = x_b[start:end] + d = torch.cdist(x_chunk, x_b) + if not loop: + diag_idx = torch.arange(end - start, device=device) + start + d[torch.arange(end - start, device=device), diag_idx] = float("inf") + in_radius = d <= r + for i in range(end - start): + j_candidates = in_radius[i].nonzero(as_tuple=True)[0] + if j_candidates.numel() == 0: + continue + dists = d[i, j_candidates] + _, order = dists.sort() + j_keep = j_candidates[order[:max_num_neighbors]] + global_i = idx_b[start + i] + global_j = idx_b[j_keep] + src_list.append(global_i.expand_as(global_j)) + dst_list.append(global_j) + if not src_list: + return torch.empty(2, 0, dtype=torch.long, device=device) + return torch.stack([torch.cat(src_list), torch.cat(dst_list)], dim=0) + + +def radius_graph( + x: torch.Tensor, + r: float, + batch: torch.Tensor | None = None, + loop: bool = False, + max_num_neighbors: int = 128, +) -> torch.Tensor: + """Build edge index for pairs of points within radius r. Uses torch_cluster if available, else PyTorch fallback.""" + if _RADIUS_GRAPH_USE_FALLBACK or _radius_graph_ext is None: + return _radius_graph_pytorch(x, r, batch=batch, loop=loop, max_num_neighbors=max_num_neighbors) + return _radius_graph_ext(x, r, batch, loop, max_num_neighbors) + + +# Van der Waals radii for common protein atoms (in Angstroms) +VDW_RADII = { + "C": 1.70, + "N": 1.55, + "O": 1.52, + "S": 1.80, + "default": 1.70, +} + +# Disulfide bond parameters +SULFUR_VDW = 1.80 # Used to identify sulfur atoms by VDW radius +DISULFIDE_MAX = 2.5 # Max S-S distance to consider a disulfide bond (Å) + + +@torch.no_grad() +def count_clashes( + proteins: Sequence[Protein], + selection: Literal["heavy", "backbone"] = "heavy", + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + device: str = "cpu", + batch_size: int = 32, + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, +) -> List[int]: + """ + Count atomic clashes for a list of proteins. + + A clash occurs when two atoms are closer than the sum of their VDW radii + minus a tolerance (defined by clash_cutoff). + + Args: + proteins: List of Protein objects + selection: Atom selection + - "heavy": All heavy atoms + - "backbone": Only backbone atoms (N, CA, C, O) + clash_cutoff: Fraction of VDW overlap to consider a clash (default 0.4 = 40%) + min_seq_sep: Minimum sequence separation between residues (default 3) + device: Computation device ("cpu" or "cuda") + batch_size: Number of proteins per batch + max_num_neighbors: Max neighbors for radius_graph + exclude_disulfides: If True, S-S pairs within 2.5 Å are not counted as clashes + + Returns: + List of clash counts, one per protein + """ + n_proteins = len(proteins) + + if n_proteins == 0: + return [] + + clash_counts = [] + + # Process in batches + for batch_start in range(0, n_proteins, batch_size): + batch_end = min(batch_start + batch_size, n_proteins) + batch_proteins = proteins[batch_start:batch_end] + + batch_counts = _compute_clashes_batch( + batch_proteins, + selection=selection, + clash_cutoff=clash_cutoff, + min_seq_sep=min_seq_sep, + device=device, + max_num_neighbors=max_num_neighbors, + exclude_disulfides=exclude_disulfides, + ) + + clash_counts.extend(batch_counts) + + return clash_counts + + +@torch.no_grad() +def _compute_clashes_batch( + proteins: Sequence[Protein], + selection: Literal["heavy", "backbone"], + clash_cutoff: float, + min_seq_sep: int, + device: str, + max_num_neighbors: int, + exclude_disulfides: bool = True, +) -> List[int]: + """Compute clashes for a batch of proteins.""" + B = len(proteins) + + if B == 0: + return [] + + # Determine atom slots to use + if selection == "backbone": + n_slots = 4 # N, CA, C, O + else: + n_slots = MAX_HEAVY_ATOMS + + # Find max residue count for padding + max_res = max(p.n_residues for p in proteins) + + if max_res == 0: + return [0] * B + + # Pad and stack coordinates and masks + padded_coords = np.zeros((B, max_res, n_slots, 3), dtype=np.float32) + padded_mask = np.zeros((B, max_res, n_slots), dtype=bool) + padded_vdw = np.full((B, max_res, n_slots), VDW_RADII["default"], dtype=np.float32) + + for i, p in enumerate(proteins): + n_res = p.n_residues + if n_res == 0: + continue + + if selection == "backbone": + padded_coords[i, :n_res] = p.coords[:, :4] + padded_mask[i, :n_res] = p.mask[:, :4] + # Set VDW radii based on atom type (N, CA, C, O) + padded_vdw[i, :n_res, 0] = VDW_RADII["N"] # N + padded_vdw[i, :n_res, 1] = VDW_RADII["C"] # CA + padded_vdw[i, :n_res, 2] = VDW_RADII["C"] # C + padded_vdw[i, :n_res, 3] = VDW_RADII["O"] # O + else: + padded_coords[i, :n_res] = p.coords + padded_mask[i, :n_res] = p.mask + # Set VDW radii based on atom names + for r in range(n_res): + for a in range(n_slots): + if p.mask[r, a]: + atom_name = p.atom_names[r, a] + element = atom_name[0] if atom_name else "C" + padded_vdw[i, r, a] = VDW_RADII.get(element, VDW_RADII["default"]) + + # Move to device + coords = torch.tensor(padded_coords, dtype=torch.float32, device=device) + mask = torch.tensor(padded_mask, dtype=torch.bool, device=device) + vdw = torch.tensor(padded_vdw, dtype=torch.float32, device=device) + + N = max_res + A = n_slots + + # Flatten all data + coords_flat = coords.reshape(B * N * A, 3) + mask_flat = mask.reshape(B * N * A) + vdw_flat = vdw.reshape(B * N * A) + + # Create protein, residue, and atom indices + protein_idx = torch.arange(B, device=device).repeat_interleave(N * A) + res_idx = torch.arange(N, device=device).repeat_interleave(A).repeat(B) + atom_idx = torch.arange(A, device=device).repeat(B * N) + + # Filter to valid atoms + valid = mask_flat + coords_valid = coords_flat[valid] + vdw_valid = vdw_flat[valid] + prot_valid = protein_idx[valid] + res_valid = res_idx[valid] + atom_valid = atom_idx[valid] + + if coords_valid.shape[0] < 2: + return [0] * B + + # Sort by protein index for radius_graph (required for correct batch handling) + sort_idx = prot_valid.argsort(stable=True) + coords_sorted = coords_valid[sort_idx] + vdw_sorted = vdw_valid[sort_idx] + prot_sorted = prot_valid[sort_idx] + res_sorted = res_valid[sort_idx] + atom_sorted = atom_valid[sort_idx] + + # Use radius_graph with batch indices + max_vdw_sum = 2 * VDW_RADII["default"] * 2 # Conservative upper bound + + edge_index = radius_graph( + coords_sorted, + r=max_vdw_sum, + batch=prot_sorted, + loop=False, + max_num_neighbors=max_num_neighbors, + ) + + if edge_index.shape[1] == 0: + return [0] * B + + src, dst = edge_index[0], edge_index[1] + + # Get attributes for edge endpoints + prot_src = prot_sorted[src] + res_src = res_sorted[src] + res_dst = res_sorted[dst] + atom_src = atom_sorted[src] + atom_dst = atom_sorted[dst] + vdw_src = vdw_sorted[src] + vdw_dst = vdw_sorted[dst] + + # Compute distances + d = torch.linalg.norm(coords_sorted[src] - coords_sorted[dst], dim=-1) + + # Compute VDW sum and threshold + vdw_sum = vdw_src + vdw_dst + clash_threshold = vdw_sum * (1.0 - clash_cutoff) + + # Filter criteria: + # 1. Avoid double counting (only keep src < dst by some ordering) + # 2. Minimum sequence separation + # 3. Distance less than clash threshold + seq_sep = torch.abs(res_dst - res_src) + order_mask = (res_src < res_dst) | ((res_src == res_dst) & (atom_src < atom_dst)) + sep_mask = seq_sep >= min_seq_sep + clash_mask = d < clash_threshold + + keep = order_mask & sep_mask & clash_mask + + # Exclude disulfide bonds (S-S pairs within bond distance) + # Sulfur identified by exact VDW radius match (faster than isclose) + if exclude_disulfides: + is_sulfur_src = vdw_src == SULFUR_VDW + is_sulfur_dst = vdw_dst == SULFUR_VDW + is_disulfide = is_sulfur_src & is_sulfur_dst & (d <= DISULFIDE_MAX) + keep = keep & ~is_disulfide + + # Count clashes per protein + prot_clash = prot_src[keep] + clash_counts = torch.bincount(prot_clash, minlength=B).tolist() + + return clash_counts + + +def get_clash_pairs( + protein: Protein, + selection: Literal["heavy", "backbone"] = "heavy", + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + device: str = "cpu", + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, +) -> List[tuple]: + """ + Get detailed clash information for a single protein. + + Args: + protein: Protein object + selection: "heavy" or "backbone" + clash_cutoff: Fraction of VDW overlap + min_seq_sep: Minimum sequence separation + device: Computation device + max_num_neighbors: Max neighbors for radius_graph + exclude_disulfides: If True, S-S pairs within 2.5 Å are not counted as clashes + + Returns: + List of tuples: (res_i, atom_name_i, res_j, atom_name_j, distance, overlap) + """ + if protein.n_residues == 0: + return [] + + # Determine atom slots + if selection == "backbone": + n_slots = 4 + coords = protein.coords[:, :4] + mask = protein.mask[:, :4] + atom_names = protein.atom_names[:, :4] + else: + n_slots = MAX_HEAVY_ATOMS + coords = protein.coords + mask = protein.mask + atom_names = protein.atom_names + + N = protein.n_residues + + # Flatten + coords_flat = coords.reshape(-1, 3) + mask_flat = mask.reshape(-1) + + # Create VDW radii + vdw_flat = np.full(N * n_slots, VDW_RADII["default"], dtype=np.float32) + for r in range(N): + for a in range(n_slots): + if mask[r, a]: + atom_name = atom_names[r, a] + element = atom_name[0] if atom_name else "C" + vdw_flat[r * n_slots + a] = VDW_RADII.get(element, VDW_RADII["default"]) + + # Filter to valid atoms + valid_idx = np.where(mask_flat)[0] + coords_valid = coords_flat[valid_idx] + vdw_valid = vdw_flat[valid_idx] + + if len(coords_valid) < 2: + return [] + + # Move to device + coords_t = torch.tensor(coords_valid, dtype=torch.float32, device=device) + vdw_t = torch.tensor(vdw_valid, dtype=torch.float32, device=device) + + # Create index mapping + res_idx = np.arange(N).repeat(n_slots)[valid_idx] + atom_idx = np.tile(np.arange(n_slots), N)[valid_idx] + + # radius_graph + max_vdw_sum = 2 * VDW_RADII["default"] * 2 + + edge_index = radius_graph( + coords_t, + r=max_vdw_sum, + loop=False, + max_num_neighbors=max_num_neighbors, + ) + + if edge_index.shape[1] == 0: + return [] + + src, dst = edge_index[0].cpu().numpy(), edge_index[1].cpu().numpy() + + # Compute distances + d = np.linalg.norm(coords_valid[src] - coords_valid[dst], axis=-1) + + # Get residue/atom indices + ri = res_idx[src] + rj = res_idx[dst] + ai = atom_idx[src] + aj = atom_idx[dst] + + # VDW sum and threshold + vdw_sum = vdw_valid[src] + vdw_valid[dst] + clash_threshold = vdw_sum * (1.0 - clash_cutoff) + + # Filter + seq_sep = np.abs(rj - ri) + order_mask = (ri < rj) | ((ri == rj) & (ai < aj)) + sep_mask = seq_sep >= min_seq_sep + clash_mask = d < clash_threshold + + keep = order_mask & sep_mask & clash_mask + + # Exclude disulfide bonds + if exclude_disulfides: + is_sulfur_src = np.isclose(vdw_valid[src], SULFUR_VDW) + is_sulfur_dst = np.isclose(vdw_valid[dst], SULFUR_VDW) + is_disulfide = is_sulfur_src & is_sulfur_dst & (d <= DISULFIDE_MAX) + keep = keep & ~is_disulfide + + # Build result + clashes = [] + for idx in np.where(keep)[0]: + res_i = int(ri[idx]) + res_j = int(rj[idx]) + atom_i = int(ai[idx]) + atom_j = int(aj[idx]) + distance = float(d[idx]) + overlap = float(vdw_sum[idx] - d[idx]) + + name_i = atom_names[res_i, atom_i] if atom_names[res_i, atom_i] else f"slot{atom_i}" + name_j = atom_names[res_j, atom_j] if atom_names[res_j, atom_j] else f"slot{atom_j}" + + clashes.append(( + protein.res_ids[res_i], + name_i, + protein.res_ids[res_j], + name_j, + distance, + overlap, + )) + + return clashes + + +@torch.no_grad() +def compute_clashes_from_batch( + coords: torch.Tensor, # (B, N, A, 3) + mask: torch.Tensor, # (B, N, A) + vdw_radii: torch.Tensor, # (B, N, A) + selection: Literal["heavy", "backbone"] = "heavy", + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, +) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: + """ + Compute clashes from pre-batched GPU tensors. + + This is the core clash detection kernel optimized for GPU. + + Args: + coords: (B, N, A, 3) coordinates on device + mask: (B, N, A) atom validity mask + vdw_radii: (B, N, A) VDW radii per atom + selection: "heavy" or "backbone" + clash_cutoff: Fraction of VDW overlap + min_seq_sep: Minimum sequence separation + max_num_neighbors: Max neighbors for radius_graph + exclude_disulfides: If True, exclude S-S pairs within DISULFIDE_MAX distance + + Returns: + protein_idx: (E,) protein index for each clash + res_i: (E,) residue index i (within protein) + res_j: (E,) residue index j (within protein) + atom_i: (E,) atom slot i + atom_j: (E,) atom slot j + distances: (E,) inter-atom distances in Angstroms + overlaps: (E,) VDW overlap (vdw_sum - distance) in Angstroms + """ + device = coords.device + B, N, A, _ = coords.shape + + # Apply selection + if selection == "backbone": + coords = coords[:, :, :4, :] + mask = mask[:, :, :4] + vdw_radii = vdw_radii[:, :, :4] + A = 4 + + # Flatten + coords_flat = coords.reshape(B * N * A, 3) + mask_flat = mask.reshape(B * N * A) + vdw_flat = vdw_radii.reshape(B * N * A) + + protein_idx = torch.arange(B, device=device).repeat_interleave(N * A) + res_idx = torch.arange(N, device=device).repeat_interleave(A).repeat(B) + atom_idx = torch.arange(A, device=device).repeat(B * N) + + # Filter valid + valid = mask_flat + coords_valid = coords_flat[valid] + vdw_valid = vdw_flat[valid] + prot_valid = protein_idx[valid] + res_valid = res_idx[valid] + atom_valid = atom_idx[valid] + + if coords_valid.shape[0] < 2: + empty = torch.empty((0,), dtype=torch.long, device=device) + empty_f = torch.empty((0,), dtype=torch.float32, device=device) + return empty, empty, empty, empty, empty, empty_f, empty_f + + # Sort by protein + sort_idx = prot_valid.argsort(stable=True) + coords_sorted = coords_valid[sort_idx] + vdw_sorted = vdw_valid[sort_idx] + prot_sorted = prot_valid[sort_idx] + res_sorted = res_valid[sort_idx] + atom_sorted = atom_valid[sort_idx] + + # radius_graph + max_vdw_sum = 2 * 1.80 * 2 # S-S worst case + + edge_index = radius_graph( + coords_sorted, + r=max_vdw_sum, + batch=prot_sorted, + loop=False, + max_num_neighbors=max_num_neighbors, + ) + + if edge_index.shape[1] == 0: + empty = torch.empty((0,), dtype=torch.long, device=device) + empty_f = torch.empty((0,), dtype=torch.float32, device=device) + return empty, empty, empty, empty, empty, empty_f, empty_f + + src, dst = edge_index[0], edge_index[1] + + # Get attributes + prot_src = prot_sorted[src] + res_src = res_sorted[src] + res_dst = res_sorted[dst] + atom_src = atom_sorted[src] + atom_dst = atom_sorted[dst] + vdw_src = vdw_sorted[src] + vdw_dst = vdw_sorted[dst] + + # Distances + d = torch.linalg.norm(coords_sorted[src] - coords_sorted[dst], dim=-1) + + # Threshold + vdw_sum = vdw_src + vdw_dst + clash_threshold = vdw_sum * (1.0 - clash_cutoff) + + # Filters + seq_sep = torch.abs(res_dst - res_src) + order_mask = (res_src < res_dst) | ((res_src == res_dst) & (atom_src < atom_dst)) + sep_mask = seq_sep >= min_seq_sep + clash_mask = d < clash_threshold + + keep = order_mask & sep_mask & clash_mask + + # Exclude disulfide bonds (S-S pairs within bond distance) + # Sulfur identified by exact VDW radius match (faster than isclose) + if exclude_disulfides: + is_sulfur_src = vdw_src == SULFUR_VDW + is_sulfur_dst = vdw_dst == SULFUR_VDW + is_disulfide = is_sulfur_src & is_sulfur_dst & (d <= DISULFIDE_MAX) + keep = keep & ~is_disulfide + + return ( + prot_src[keep].long(), + res_src[keep].long(), + res_dst[keep].long(), + atom_src[keep].long(), + atom_dst[keep].long(), + d[keep], + (vdw_sum - d)[keep], + ) + + +@torch.no_grad() +def compute_clashing_residues_from_batch( + coords: torch.Tensor, # (B, N, A, 3) + mask: torch.Tensor, # (B, N, A) + vdw_radii: torch.Tensor, # (B, N, A) + selection: Literal["heavy", "backbone"] = "heavy", + clash_cutoff: float = 0.12, + min_seq_sep: int = 3, + max_num_neighbors: int = 128, + exclude_disulfides: bool = True, +) -> tuple[torch.Tensor, torch.Tensor]: + """ + Compute clash counts and clashing residue masks. + + Args: + coords: (B, N, A, 3) coordinates on device + mask: (B, N, A) atom validity mask + vdw_radii: (B, N, A) VDW radii per atom + selection: "heavy" or "backbone" + clash_cutoff: Fraction of VDW overlap + min_seq_sep: Minimum sequence separation + max_num_neighbors: Max neighbors for radius_graph + exclude_disulfides: If True, exclude S-S pairs within DISULFIDE_MAX distance + + Returns: + clash_counts: (B,) number of clashing atom pairs per protein + clashing_residue_mask: (B, N) bool mask of residues involved in clashes + """ + device = coords.device + B, N, _, _ = coords.shape + + prot_idx, res_i, res_j, _, _, _, _ = compute_clashes_from_batch( + coords, mask, vdw_radii, + selection=selection, + clash_cutoff=clash_cutoff, + min_seq_sep=min_seq_sep, + max_num_neighbors=max_num_neighbors, + exclude_disulfides=exclude_disulfides, + ) + + # Count clashes per protein + if prot_idx.numel() == 0: + clash_counts = torch.zeros(B, dtype=torch.long, device=device) + clashing_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + return clash_counts, clashing_mask + + clash_counts = torch.bincount(prot_idx, minlength=B) + + # Mark clashing residues + clashing_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + clashing_mask[prot_idx, res_i] = True + clashing_mask[prot_idx, res_j] = True + + return clash_counts, clashing_mask diff --git a/afdb_integration_kit/gpu/interface.py b/afdb_integration_kit/gpu/interface.py new file mode 100644 index 0000000..6305557 --- /dev/null +++ b/afdb_integration_kit/gpu/interface.py @@ -0,0 +1,234 @@ +# Copyright 2026 Maciej Majewski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +""" +Interface residue detection using CA distances. + +A residue is an interface residue if it has CA within contact_cutoff of +a CA from a different chain. +""" +from __future__ import annotations + +from typing import List, Tuple, Set + +import torch + +from .clashes import radius_graph + +try: + from .batch import ProteinBatch +except ImportError: + from batch import ProteinBatch + + +@torch.no_grad() +def compute_interface_residues( + batch: ProteinBatch, + contact_cutoff: float = 8.0, + max_num_neighbors: int = 64, +) -> List[List[Tuple[int, str]]]: + """ + Compute interface residues for a batch of proteins. + + Interface residues are those with CA within contact_cutoff of a CA + from a different chain. + + Args: + batch: ProteinBatch with CA coordinates and chain IDs on GPU + contact_cutoff: Maximum CA-CA distance to consider a contact (default 8.0 Å) + max_num_neighbors: Maximum neighbors per atom for radius_graph + + Returns: + List of lists, one per protein in batch. Each inner list contains + (res_id, chain_id) tuples for interface residues. + """ + B = batch.batch_size + device = batch.device + + # Flatten CA coordinates + ca_flat = batch.ca_coords.reshape(B * batch.max_residues, 3) + ca_mask_flat = batch.ca_mask.reshape(B * batch.max_residues) + chain_flat = batch.chain_ids.reshape(B * batch.max_residues) + res_id_flat = batch.res_ids.reshape(B * batch.max_residues) + + # Protein index for each residue + protein_idx = torch.arange(B, device=device).repeat_interleave(batch.max_residues) + # Residue index within protein + res_idx = torch.arange(batch.max_residues, device=device).repeat(B) + + # Filter to valid CA atoms + valid = ca_mask_flat + ca_valid = ca_flat[valid] + chain_valid = chain_flat[valid] + res_id_valid = res_id_flat[valid] + protein_valid = protein_idx[valid] + res_idx_valid = res_idx[valid] + + if ca_valid.shape[0] == 0: + return [[] for _ in range(B)] + + # Find neighbors within cutoff + edge_index = radius_graph( + ca_valid, + r=contact_cutoff, + batch=protein_valid, + loop=False, + max_num_neighbors=max_num_neighbors, + ) + + if edge_index.shape[1] == 0: + return [[] for _ in range(B)] + + src, dst = edge_index[0], edge_index[1] + + # Filter to inter-chain contacts (different chain IDs) + chain_src = chain_valid[src] + chain_dst = chain_valid[dst] + inter_chain = chain_src != chain_dst + + src = src[inter_chain] + dst = dst[inter_chain] + + if src.shape[0] == 0: + return [[] for _ in range(B)] + + # Get protein indices for contacts + protein_src = protein_valid[src] + + # Both src and dst residues are interface residues + # Combine them and get unique per protein + interface_protein = torch.cat([protein_src, protein_valid[dst]]) + interface_res_idx = torch.cat([res_idx_valid[src], res_idx_valid[dst]]) + + # Move to CPU for output construction + interface_protein = interface_protein.cpu().numpy() + interface_res_idx = interface_res_idx.cpu().numpy() + + # Build output: unique (res_id, chain_id) per protein + results: List[List[Tuple[int, str]]] = [[] for _ in range(B)] + + # Group by protein + for i in range(B): + protein_mask = interface_protein == i + if not protein_mask.any(): + continue + + # Get unique residue indices for this protein + res_indices = set(interface_res_idx[protein_mask]) + + # Map back to original res_id and chain_id + p = batch.proteins[i] + for r in res_indices: + if r < p.n_residues: + res_id = int(p.res_ids[r]) + chain_id = str(p.chain_ids[r]) + results[i].append((res_id, chain_id)) + + # Sort by chain, then res_id + results[i].sort(key=lambda x: (x[1], x[0])) + + return results + + +@torch.no_grad() +def compute_interface_residues_flat( + ca_coords: torch.Tensor, # (B, N, 3) + ca_mask: torch.Tensor, # (B, N) + chain_ids: torch.Tensor, # (B, N) + contact_cutoff: float = 8.0, + max_num_neighbors: int = 64, +) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: + """ + Compute interface residue masks (pure tensor version). + + Returns boolean mask of interface residues for each protein. + + Args: + ca_coords: (B, N, 3) CA coordinates + ca_mask: (B, N) validity mask + chain_ids: (B, N) chain IDs as integers + contact_cutoff: Maximum CA-CA distance + max_num_neighbors: Maximum neighbors for radius_graph + + Returns: + interface_mask: (B, N) bool mask of interface residues + contact_src: (E,) source residue flat indices for all contacts + contact_dst: (E,) destination residue flat indices for all contacts + contact_distances: (E,) CA-CA distances in Angstroms for all contacts + """ + B, N, _ = ca_coords.shape + device = ca_coords.device + + # Flatten + ca_flat = ca_coords.reshape(B * N, 3) + ca_mask_flat = ca_mask.reshape(B * N) + chain_flat = chain_ids.reshape(B * N) + + protein_idx = torch.arange(B, device=device).repeat_interleave(N) + res_idx = torch.arange(N, device=device).repeat(B) + + # Filter valid + valid = ca_mask_flat + valid_indices = torch.where(valid)[0] + ca_valid = ca_flat[valid] + chain_valid = chain_flat[valid] + protein_valid = protein_idx[valid] + + # Initialize output mask + interface_mask = torch.zeros((B, N), dtype=torch.bool, device=device) + + if ca_valid.shape[0] == 0: + empty = torch.empty((0,), dtype=torch.long, device=device) + empty_dist = torch.empty((0,), dtype=torch.float32, device=device) + return interface_mask, empty, empty, empty_dist + + # Find neighbors + edge_index = radius_graph( + ca_valid, + r=contact_cutoff, + batch=protein_valid, + loop=False, + max_num_neighbors=max_num_neighbors, + ) + + if edge_index.shape[1] == 0: + empty = torch.empty((0,), dtype=torch.long, device=device) + empty_dist = torch.empty((0,), dtype=torch.float32, device=device) + return interface_mask, empty, empty, empty_dist + + src, dst = edge_index[0], edge_index[1] + + # Filter inter-chain + inter_chain = chain_valid[src] != chain_valid[dst] + src = src[inter_chain] + dst = dst[inter_chain] + + if src.shape[0] == 0: + empty = torch.empty((0,), dtype=torch.long, device=device) + empty_dist = torch.empty((0,), dtype=torch.float32, device=device) + return interface_mask, empty, empty, empty_dist + + # Compute CA-CA distances for inter-chain contacts + distances = torch.linalg.norm(ca_valid[src] - ca_valid[dst], dim=-1) + + # Map back to original flat indices + src_orig = valid_indices[src] + dst_orig = valid_indices[dst] + + # Mark interface residues + src_protein = src_orig // N + src_res = src_orig % N + dst_protein = dst_orig // N + dst_res = dst_orig % N + + interface_mask[src_protein, src_res] = True + interface_mask[dst_protein, dst_res] = True + + return interface_mask, src_orig, dst_orig, distances diff --git a/afdb_integration_kit/gpu/parse.py b/afdb_integration_kit/gpu/parse.py new file mode 100644 index 0000000..0762fc6 --- /dev/null +++ b/afdb_integration_kit/gpu/parse.py @@ -0,0 +1,257 @@ +# Copyright 2026 Maciej Majewski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +""" +PDB parser using fastpdb with multi-CPU parallelization. + +Parses PDB files into Protein objects with heavy atoms only (no hydrogens). +Supports parallel parsing of multiple files using ProcessPoolExecutor. +""" +from __future__ import annotations + +from concurrent.futures import ProcessPoolExecutor, as_completed +from functools import partial +from pathlib import Path +from typing import Dict, List, Optional, Sequence, Tuple + +import numpy as np + +import fastpdb +from biotite.structure.atoms import AtomArray + +try: + from .protein import ( + Protein, + empty_protein, + MAX_HEAVY_ATOMS, + AA_ORDER_HEAVY, + AA_ALIASES, + _CANON_AA, + _CANON_OR_ALIAS, + _ATOM_SLOT_LOOKUP, + ) +except ImportError: + from protein import ( + Protein, + empty_protein, + MAX_HEAVY_ATOMS, + AA_ORDER_HEAVY, + AA_ALIASES, + _CANON_AA, + _CANON_OR_ALIAS, + _ATOM_SLOT_LOOKUP, + ) + + +def _read_structure(path: str | Path, model: Optional[int] = 1) -> AtomArray: + """Read PDB file using fastpdb.""" + path = Path(path) + pdb = fastpdb.PDBFile.read(str(path)) + if model is None: + return pdb.get_structure() + else: + return pdb.get_structure(model=model) + + +def parse_protein( + path: str | Path, + model: Optional[int] = 1, +) -> Protein: + """ + Parse a PDB file into a Protein object. + + Args: + path: Path to PDB file + model: Model number (1-indexed) for multi-model files. + Use None to load all models concatenated. + + Returns: + Protein object with heavy atom coordinates + """ + path = Path(path) + + try: + arr = _read_structure(path, model=model) + except Exception as e: + print(f"Warning: Could not parse {path}: {e}") + return empty_protein(str(path)) + + if arr.array_length() == 0: + return empty_protein(str(path)) + + return _parse_atomarray(arr, path) + + +def _parse_atomarray(arr: AtomArray, path: Path) -> Protein: + """Convert biotite AtomArray to Protein object.""" + cats = set(arr.get_annotation_categories()) + n_atoms_total = arr.array_length() + + # Get raw arrays + res_name_raw = arr.res_name + atom_name_all = arr.atom_name + chain_all = arr.chain_id + res_id_all = arr.res_id.astype(np.int32, copy=False) + coord_all = arr.coord + ins_all = arr.ins_code if "ins_code" in cats else np.full(n_atoms_total, "", dtype=" List[Protein]: + """ + Parse multiple PDB files, optionally in parallel. + + Args: + paths: List of paths to PDB files + model: Model number for multi-model files + n_jobs: Number of parallel workers. Use -1 for all CPUs. + + Returns: + List of Protein objects (same order as input paths) + """ + paths = [Path(p) for p in paths] + + if n_jobs == 1: + # Sequential processing + return [parse_protein(p, model=model) for p in paths] + + # Parallel processing + if n_jobs == -1: + import os + n_jobs = os.cpu_count() or 1 + + # Use ProcessPoolExecutor for parallel parsing + proteins = [None] * len(paths) + parse_fn = partial(parse_protein, model=model) + + with ProcessPoolExecutor(max_workers=n_jobs) as executor: + # Submit all jobs + future_to_idx = { + executor.submit(parse_fn, path): i + for i, path in enumerate(paths) + } + + # Collect results + for future in as_completed(future_to_idx): + idx = future_to_idx[future] + try: + proteins[idx] = future.result() + except Exception as e: + print(f"Warning: Failed to parse {paths[idx]}: {e}") + proteins[idx] = empty_protein(str(paths[idx])) + + return proteins diff --git a/afdb_integration_kit/gpu/protein.py b/afdb_integration_kit/gpu/protein.py new file mode 100644 index 0000000..e79fd7b --- /dev/null +++ b/afdb_integration_kit/gpu/protein.py @@ -0,0 +1,128 @@ +# Copyright 2026 Maciej Majewski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +""" +Protein data structure for molecular analysis. + +Simple dataclass to hold protein heavy atom coordinates and metadata. +""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import Dict, Optional, Tuple + +import numpy as np + + +# Maximum heavy atoms per residue +MAX_HEAVY_ATOMS = 14 + +# Amino acid heavy atom ordering (canonical) +AA_ORDER_HEAVY: Dict[str, Tuple[str, ...]] = { + "ALA": ("N", "CA", "C", "O", "CB"), + "ARG": ("N", "CA", "C", "O", "CB", "CG", "CD", "NE", "CZ", "NH1", "NH2"), + "ASN": ("N", "CA", "C", "O", "CB", "CG", "OD1", "ND2"), + "ASP": ("N", "CA", "C", "O", "CB", "CG", "OD1", "OD2"), + "CYS": ("N", "CA", "C", "O", "CB", "SG"), + "GLN": ("N", "CA", "C", "O", "CB", "CG", "CD", "OE1", "NE2"), + "GLU": ("N", "CA", "C", "O", "CB", "CG", "CD", "OE1", "OE2"), + "GLY": ("N", "CA", "C", "O"), + "HIS": ("N", "CA", "C", "O", "CB", "CG", "ND1", "CD2", "CE1", "NE2"), + "ILE": ("N", "CA", "C", "O", "CB", "CG1", "CG2", "CD1"), + "LEU": ("N", "CA", "C", "O", "CB", "CG", "CD1", "CD2"), + "LYS": ("N", "CA", "C", "O", "CB", "CG", "CD", "CE", "NZ"), + "MET": ("N", "CA", "C", "O", "CB", "CG", "SD", "CE"), + "PHE": ("N", "CA", "C", "O", "CB", "CG", "CD1", "CD2", "CE1", "CE2", "CZ"), + "PRO": ("N", "CA", "C", "O", "CB", "CG", "CD"), + "SER": ("N", "CA", "C", "O", "CB", "OG"), + "THR": ("N", "CA", "C", "O", "CB", "OG1", "CG2"), + "TRP": ("N", "CA", "C", "O", "CB", "CG", "CD1", "CD2", "NE1", "CE2", "CE3", "CZ2", "CZ3", "CH2"), + "TYR": ("N", "CA", "C", "O", "CB", "CG", "CD1", "CD2", "CE1", "CE2", "CZ", "OH"), + "VAL": ("N", "CA", "C", "O", "CB", "CG1", "CG2"), +} + +# Amino acid name aliases (non-standard to standard) +AA_ALIASES: Dict[str, str] = { + "HID": "HIS", "HIE": "HIS", "HIP": "HIS", + "CYX": "CYS", "CYM": "CYS", + "ASH": "ASP", "GLH": "GLU", + "LYN": "LYS", "ARGN": "ARG", + "MSE": "MET", "SEC": "CYS", +} + +_CANON_AA = frozenset(AA_ORDER_HEAVY.keys()) +_CANON_OR_ALIAS = _CANON_AA | frozenset(AA_ALIASES.keys()) + +# Pre-build slot lookup: res_type -> {atom_name: slot_index} +_ATOM_SLOT_LOOKUP: Dict[str, Dict[str, int]] = { + res: {atom: i for i, atom in enumerate(atoms)} + for res, atoms in AA_ORDER_HEAVY.items() +} + + +@dataclass(slots=True) +class Protein: + """ + Protein structure with heavy atom coordinates. + + Attributes: + coords: (N_res, MAX_HEAVY_ATOMS, 3) float32 coordinates in Angstroms + mask: (N_res, MAX_HEAVY_ATOMS) bool mask for valid atoms + atom_names: (N_res, MAX_HEAVY_ATOMS) str atom names + res_names: (N_res,) str residue names (3-letter code) + chain_ids: (N_res,) str chain identifiers + res_ids: (N_res,) int residue numbers + path: str source file path + """ + coords: np.ndarray # (N_res, MAX_HEAVY_ATOMS, 3) float32 + mask: np.ndarray # (N_res, MAX_HEAVY_ATOMS) bool + atom_names: np.ndarray # (N_res, MAX_HEAVY_ATOMS) int: + """Number of residues.""" + return self.coords.shape[0] + + @property + def n_atoms(self) -> int: + """Number of valid atoms.""" + return int(self.mask.sum()) + + def get_backbone_coords(self) -> np.ndarray: + """Get backbone coordinates (N, CA, C, O).""" + return self.coords[:, :4, :] + + def get_backbone_mask(self) -> np.ndarray: + """Get backbone atom mask.""" + return self.mask[:, :4] + + def get_ca_coords(self) -> np.ndarray: + """Get CA coordinates.""" + return self.coords[:, 1, :] + + def __repr__(self) -> str: + return f"Protein(n_residues={self.n_residues}, n_atoms={self.n_atoms}, path='{self.path}')" + + +def empty_protein(path: str = "") -> Protein: + """Create an empty protein.""" + return Protein( + coords=np.zeros((0, MAX_HEAVY_ATOMS, 3), dtype=np.float32), + mask=np.zeros((0, MAX_HEAVY_ATOMS), dtype=bool), + atom_names=np.zeros((0, MAX_HEAVY_ATOMS), dtype=" Optional[str]: + """Extract AlphaFold DB identifier from a file path.""" + m = _AF_ID_RE.search(path) + return m.group(1) if m else None + + +def _get_af_id_or_stem(path: str) -> str: + """Return the AF ID if present, otherwise the filename stem.""" + af_id = _extract_af_id(path) + if af_id is not None: + return af_id + from pathlib import Path + return Path(path).stem + + +def _canon_chain_pair(c1: str, c2: str) -> Tuple[str, str]: + """Return chain pair in canonical (alphabetical) order.""" + return (c1, c2) if c1 <= c2 else (c2, c1) + + +_ECO = [{"eco_term": "computational evidence", "eco_code": "ECO_0000246"}] + + +# --------------------------------------------------------------------------- +# Interface schema +# --------------------------------------------------------------------------- + +def _precompute_closest_partners(contacts) -> Dict[Tuple[str, int], dict]: + """Build (chain, res_id) -> closest-partner dict in one pass over contacts.""" + best: Dict[Tuple[str, int], Tuple[float, dict]] = {} + for c in contacts: + # Side 1 -> partner is side 2 + key1 = (c.chain_1, c.res_1_id) + if key1 not in best or c.distance < best[key1][0]: + best[key1] = (c.distance, { + "partner_chain": c.chain_2, + "partner_res_label": str(c.res_2_id), + "partner_aa_type": c.aa_2_type, + "distance_angstrom": round(c.distance, 2), + "method": "ca_ca_distance", + }) + # Side 2 -> partner is side 1 + key2 = (c.chain_2, c.res_2_id) + if key2 not in best or c.distance < best[key2][0]: + best[key2] = (c.distance, { + "partner_chain": c.chain_1, + "partner_res_label": str(c.res_1_id), + "partner_aa_type": c.aa_1_type, + "distance_angstrom": round(c.distance, 2), + "method": "ca_ca_distance", + }) + return {k: v[1] for k, v in best.items()} + + +def result_to_interface_schema( + result: "ProteinAnalysisResult", + interface_cutoff: float = 8.0, + data_resource: str = "AFDB-Integration-Kit", +) -> Optional[dict]: + """ + Build the interface-only FunPDBe schema dict. + + Returns None if there are no interface contacts. + """ + # Group contacts by canonical chain pair + pair_contacts: Dict[Tuple[str, str], list] = defaultdict(list) + for c in result.interface_contacts: + pair = _canon_chain_pair(c.chain_1, c.chain_2) + pair_contacts[pair].append(c) + + if not pair_contacts: + return None + + sites: List[dict] = [] + site_id_counter = 1 + + residue_site_data: Dict[Tuple[str, str], List[dict]] = defaultdict(list) + residue_aa: Dict[Tuple[str, str], str] = {} + residue_annotations: Dict[Tuple[str, str], dict] = {} + + for pair in sorted(pair_contacts.keys()): + contacts = pair_contacts[pair] + site_id = site_id_counter + site_id_counter += 1 + + # Build interactions list + interactions = [] + for c in contacts: + if _canon_chain_pair(c.chain_1, c.chain_2) == (c.chain_1, c.chain_2): + interaction = { + "chain_1": c.chain_1, + "res_1_label": str(c.res_1_id), + "aa_1_type": c.aa_1_type, + "chain_2": c.chain_2, + "res_2_label": str(c.res_2_id), + "aa_2_type": c.aa_2_type, + "distance_angstrom": round(c.distance, 2), + } + else: + interaction = { + "chain_1": c.chain_2, + "res_1_label": str(c.res_2_id), + "aa_1_type": c.aa_2_type, + "chain_2": c.chain_1, + "res_2_label": str(c.res_1_id), + "aa_2_type": c.aa_1_type, + "distance_angstrom": round(c.distance, 2), + } + interactions.append(interaction) + + label = f"{pair[0]}{pair[1]}_interface" + sites.append({ + "site_id": site_id, + "label": label, + "additional_site_annotations": { + "contact_cutoff_angstrom": interface_cutoff, + "description": f"{pair[0]}{pair[1]} interface", + "interactions": interactions, + }, + }) + + # Pre-compute closest partner per residue in one pass (O(C)) + closest = _precompute_closest_partners(contacts) + + # Collect interface residues with partner annotations + seen: set = set() + for c in contacts: + for chain, res_id, aa in [ + (c.chain_1, c.res_1_id, c.aa_1_type), + (c.chain_2, c.res_2_id, c.aa_2_type), + ]: + key = (chain, str(res_id)) + residue_aa[key] = aa + if key not in seen: + seen.add(key) + residue_site_data[key].append({ + "site_id_ref": site_id, + "confidence_classification": "null", + }) + partner = closest.get((chain, res_id)) + if partner and key not in residue_annotations: + residue_annotations[key] = partner + + # Build chains array + chain_residues: Dict[str, List[dict]] = defaultdict(list) + for (chain, res_id_str), sd_list in sorted(residue_site_data.items()): + rd: dict = { + "residue_number": res_id_str, + "aa_type": residue_aa.get((chain, res_id_str), "UNK"), + "site_data": sd_list, + } + ann = residue_annotations.get((chain, res_id_str)) + if ann: + rd["additional_residue_annotations"] = ann + chain_residues[chain].append(rd) + + chains = [] + for cl in sorted(chain_residues.keys()): + residues = chain_residues[cl] + residues.sort(key=lambda r: int(r["residue_number"])) + chains.append({"chain_label": cl, "residues": residues}) + + return { + "data_resource": data_resource, + "af_id": _get_af_id_or_stem(result.path), + "chains": chains, + "sites": sites, + "evidence_code_ontology": _ECO, + } + + +# --------------------------------------------------------------------------- +# Clash schema +# --------------------------------------------------------------------------- + +def _clash_severity(overlap: float) -> str: + """Classify clash severity based on VDW overlap (Angstroms). + + Overlap = vdw_sum - distance. With clash_cutoff=0.12 the minimum + detected overlap is ~0.4 A, giving two tiers: + + >= 0.8 A -> "high" (severe steric clash) + < 0.8 A -> "medium" (moderate clash) + """ + if overlap >= 0.8: + return "high" + return "medium" + + +def _precompute_worst_overlaps( + clash_contacts: List["ClashContact"], +) -> Dict[Tuple[str, int], float]: + """Build (chain, res_id) -> max overlap dict in one pass over contacts.""" + worst: Dict[Tuple[str, int], float] = {} + for c in clash_contacts: + key1 = (c.chain_1, c.res_1_id) + if c.overlap > worst.get(key1, 0.0): + worst[key1] = c.overlap + key2 = (c.chain_2, c.res_2_id) + if c.overlap > worst.get(key2, 0.0): + worst[key2] = c.overlap + return worst + + +def _build_clash_site( + site_id: int, + label: str, + description: str, + n_clashes: int, + clashing_residues: List[dict], + clash_contacts: List["ClashContact"], + residue_site_data: Dict[Tuple[str, str], List[dict]], + residue_aa: Dict[Tuple[str, str], str], +) -> dict: + """Build a single clash site and populate per-residue site_data.""" + # Pre-compute max overlap per residue in one pass (O(C)) + worst_overlaps = _precompute_worst_overlaps(clash_contacts) + + for res in clashing_residues: + key = (res["chain_id"], str(res["res_id"])) + if key not in residue_aa: + residue_aa[key] = res.get("aa_type", "UNK") + + worst = worst_overlaps.get((res["chain_id"], res["res_id"]), 0.0) + residue_site_data[key].append({ + "site_id_ref": site_id, + "raw_score": round(worst, 2), + "raw_score_unit": "vdw_overlap_angstrom", + "confidence_classification": _clash_severity(worst), + }) + + return { + "site_id": site_id, + "label": label, + "additional_site_annotations": { + "description": description, + "n_clashes": n_clashes, + }, + } + + +def result_to_clash_schema( + result: "ProteinAnalysisResult", + data_resource: str = "AFDB-Integration-Kit", +) -> dict: + """ + Build the clash-only FunPDBe schema dict. + + Always includes both backbone_clashes and heavy_atom_clashes sites (with + n_clashes=0 when none found) so that counts are always recorded in the JSON + and downstream enrichment can distinguish "analyzed, 0 clashes" from "not analyzed". + """ + sites: List[dict] = [] + site_id_counter = 1 + + residue_site_data: Dict[Tuple[str, str], List[dict]] = defaultdict(list) + residue_aa: Dict[Tuple[str, str], str] = {} + + site = _build_clash_site( + site_id=site_id_counter, + label="backbone_clashes", + description="Residues with backbone atom clashes (N, CA, C, O)", + n_clashes=result.n_backbone_clashes, + clashing_residues=result.backbone_clashing_residues, + clash_contacts=result.backbone_clash_contacts, + residue_site_data=residue_site_data, + residue_aa=residue_aa, + ) + sites.append(site) + site_id_counter += 1 + + site = _build_clash_site( + site_id=site_id_counter, + label="heavy_atom_clashes", + description="Residues with heavy atom clashes (all non-hydrogen atoms)", + n_clashes=result.n_heavy_clashes, + clashing_residues=result.heavy_clashing_residues, + clash_contacts=result.heavy_clash_contacts, + residue_site_data=residue_site_data, + residue_aa=residue_aa, + ) + sites.append(site) + site_id_counter += 1 + + # Build chains array + chain_residues: Dict[str, List[dict]] = defaultdict(list) + for (chain, res_id_str), sd_list in sorted(residue_site_data.items()): + chain_residues[chain].append({ + "residue_number": res_id_str, + "aa_type": residue_aa.get((chain, res_id_str), "UNK"), + "site_data": sd_list, + }) + + chains = [] + for cl in sorted(chain_residues.keys()): + residues = chain_residues[cl] + residues.sort(key=lambda r: int(r["residue_number"])) + chains.append({"chain_label": cl, "residues": residues}) + + return { + "data_resource": data_resource, + "af_id": _get_af_id_or_stem(result.path), + "chains": chains, + "sites": sites, + "evidence_code_ontology": _ECO, + } diff --git a/afdb_integration_kit/gpu/summary.md b/afdb_integration_kit/gpu/summary.md new file mode 100644 index 0000000..670c1da --- /dev/null +++ b/afdb_integration_kit/gpu/summary.md @@ -0,0 +1,15 @@ +Time to process 10k structures (seconds) +NVIDIA RTX PRO 6000 Blackwell +-------------------------------------------------- +| n_workers | +| BS | 2 | 4 | 8 | 16 | 32 | +|------|--------|--------|--------|--------|--------| +| 8 | | | | 34.49 | | +| 16 | | | | 30.36 | | +| 32 | | | | 28.08 | | +| 64 | | | | 26.09 | | +| 128 | | | | 25.11 | | +| 256 | | | | 24.92 | | +| 512 | 69.72 | 37.02 | 25.77 | 24.03 | 24.07 | +| 1024 | | | | 25.81 | | +-------------------------------------------------- diff --git a/afdb_integration_kit/ipsae/Makefile b/afdb_integration_kit/ipsae/Makefile new file mode 100644 index 0000000..d2ce7c1 --- /dev/null +++ b/afdb_integration_kit/ipsae/Makefile @@ -0,0 +1,81 @@ +# Makefile for ipSAE C++ Implementation +# Used by production_pipeline.py for automatic compilation + +CXX ?= g++ +CXXFLAGS = -O3 -mtune=generic -fopenmp -std=c++17 +LDFLAGS = -fopenmp + +# Eigen is fetched on demand into deps/ (not vendored in the repo). +# Override EIGEN_VERSION or EIGEN_URL to use a different release. +EIGEN_VERSION ?= 3.4.0 +EIGEN_DIR = deps/eigen-$(EIGEN_VERSION) +EIGEN_URL ?= https://gitlab.com/libeigen/eigen/-/archive/$(EIGEN_VERSION)/eigen-$(EIGEN_VERSION).tar.gz + +# Include paths for dependencies +INCLUDES = -I $(EIGEN_DIR) -I deps + +# Source and target +SRC = ipsae_cpp.cpp +TARGET = ipsae_cpp + +# Static linking (creates portable binary ~3MB) +STATIC_FLAGS = -static + +.PHONY: all clean static check help deps + +# Default target - static linking (portable binary ~3MB, no runtime dependencies) +all: $(TARGET) + +$(TARGET): $(SRC) | deps + $(CXX) $(CXXFLAGS) $(STATIC_FLAGS) $(INCLUDES) $< -o $@ $(LDFLAGS) + @echo "Built $(TARGET) (static linking - portable)" + +# Dynamic target - faster compile, requires libs at runtime +dynamic: $(SRC) | deps + $(CXX) $(CXXFLAGS) $(INCLUDES) $< -o $(TARGET) $(LDFLAGS) + @echo "Built $(TARGET) (dynamic linking)" + +# Fetch Eigen header tree if it isn't already present. +deps: $(EIGEN_DIR) + +$(EIGEN_DIR): + @echo "Fetching Eigen $(EIGEN_VERSION) into $(EIGEN_DIR)..." + @mkdir -p deps + @curl -fsSL "$(EIGEN_URL)" -o deps/eigen-$(EIGEN_VERSION).tar.gz + @tar -xzf deps/eigen-$(EIGEN_VERSION).tar.gz -C deps + @rm -f deps/eigen-$(EIGEN_VERSION).tar.gz + @test -d "$(EIGEN_DIR)" || (echo "ERROR: extraction did not produce $(EIGEN_DIR)" && exit 1) + @echo "Eigen $(EIGEN_VERSION) ready." + +# Check that dependencies are available (will not fetch). +check: + @echo "Checking dependencies..." + @test -d $(EIGEN_DIR) || (echo "ERROR: $(EIGEN_DIR) not found. Run 'make deps' to fetch it." && exit 1) + @test -f deps/json.hpp || (echo "ERROR: deps/json.hpp not found" && exit 1) + @echo "All dependencies found." + +# Clean build artifacts (does not delete fetched Eigen tree). +clean: + rm -f $(TARGET) + @echo "Cleaned build artifacts" + +# Help target +help: + @echo "ipSAE C++ Makefile" + @echo "" + @echo "Targets:" + @echo " all - Build static portable binary (default; auto-fetches Eigen if missing)" + @echo " dynamic - Build with dynamic linking" + @echo " deps - Fetch Eigen $(EIGEN_VERSION) into $(EIGEN_DIR)" + @echo " check - Verify dependencies exist (does not fetch)" + @echo " clean - Remove built binary" + @echo " help - Show this help message" + @echo "" + @echo "Variables:" + @echo " EIGEN_VERSION (default: $(EIGEN_VERSION))" + @echo " EIGEN_URL (default: $(EIGEN_URL))" + @echo "" + @echo "Usage:" + @echo " make # Fetch Eigen if needed, then build static binary" + @echo " make dynamic # Build dynamic binary" + @echo " make clean # Remove binary" diff --git a/afdb_integration_kit/ipsae/README.md b/afdb_integration_kit/ipsae/README.md new file mode 100644 index 0000000..867b9df --- /dev/null +++ b/afdb_integration_kit/ipsae/README.md @@ -0,0 +1,49 @@ +## ipSAE C++ implementation + +### Dependencies + +- A C++17 compiler with OpenMP (`g++` recommended) +- `curl` and `tar` (used by `make` to fetch Eigen on first build) +- [Eigen](https://eigen.tuxfamily.org/) **3.4.0** — fetched on demand into `deps/eigen-3.4.0/` (not vendored in this repo) +- [nlohmann/json](https://github.com/nlohmann/json) — vendored as the single-header `deps/json.hpp` + +### Build + +The provided `Makefile` will auto-fetch Eigen on first build: + +```bash +make # fetch Eigen if needed, then build static portable binary +make dynamic # build dynamic-linked binary +make clean # remove built binary (keeps fetched Eigen) +``` + +To fetch Eigen explicitly (e.g., to pre-cache it before an offline build): + +```bash +make deps +``` + +To use a system Eigen install instead of the auto-fetched copy, point `EIGEN_DIR` at it: + +```bash +make EIGEN_DIR=/usr/include/eigen3 +``` + +Or pin a different Eigen release: + +```bash +make EIGEN_VERSION=3.4.0 +``` + +### Manual build (without `make`) + +```bash +# One-time: fetch Eigen +curl -fsSL https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz \ + | tar -xz -C deps + +# Compile (static linking, ~3MB portable binary) +g++ -O3 -mtune=generic -fopenmp -std=c++17 -static \ + -I deps/eigen-3.4.0 -I deps \ + ipsae_cpp.cpp -o ipsae_cpp +``` diff --git a/afdb_integration_kit/ipsae/deps/json.hpp b/afdb_integration_kit/ipsae/deps/json.hpp new file mode 100644 index 0000000..8b72ea6 --- /dev/null +++ b/afdb_integration_kit/ipsae/deps/json.hpp @@ -0,0 +1,24765 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + +/****************************************************************************\ + * Note on documentation: The source files contain links to the online * + * documentation of the public API at https://json.nlohmann.me. This URL * + * contains the most recent documentation and should also be applicable to * + * previous versions; documentation for deprecated functions is not * + * removed, but marked deprecated. See "Generate documentation" section in * + * file docs/README.md. * +\****************************************************************************/ + +#ifndef INCLUDE_NLOHMANN_JSON_HPP_ +#define INCLUDE_NLOHMANN_JSON_HPP_ + +#include // all_of, find, for_each +#include // nullptr_t, ptrdiff_t, size_t +#include // hash, less +#include // initializer_list +#ifndef JSON_NO_IO + #include // istream, ostream +#endif // JSON_NO_IO +#include // random_access_iterator_tag +#include // unique_ptr +#include // string, stoi, to_string +#include // declval, forward, move, pair, swap +#include // vector + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// This file contains all macro definitions affecting or depending on the ABI + +#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK + #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH) + #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 3 + #warning "Already included a different version of the library!" + #endif + #endif +#endif + +#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum) +#define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum) +#define NLOHMANN_JSON_VERSION_PATCH 3 // NOLINT(modernize-macro-to-enum) + +#ifndef JSON_DIAGNOSTICS + #define JSON_DIAGNOSTICS 0 +#endif + +#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0 +#endif + +#if JSON_DIAGNOSTICS + #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag +#else + #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS +#endif + +#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp +#else + #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION + #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0 +#endif + +// Construct the namespace ABI tags component +#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b +#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \ + NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) + +#define NLOHMANN_JSON_ABI_TAGS \ + NLOHMANN_JSON_ABI_TAGS_CONCAT( \ + NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \ + NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON) + +// Construct the namespace version component +#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \ + _v ## major ## _ ## minor ## _ ## patch +#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \ + NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) + +#if NLOHMANN_JSON_NAMESPACE_NO_VERSION +#define NLOHMANN_JSON_NAMESPACE_VERSION +#else +#define NLOHMANN_JSON_NAMESPACE_VERSION \ + NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \ + NLOHMANN_JSON_VERSION_MINOR, \ + NLOHMANN_JSON_VERSION_PATCH) +#endif + +// Combine namespace components +#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b +#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \ + NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) + +#ifndef NLOHMANN_JSON_NAMESPACE +#define NLOHMANN_JSON_NAMESPACE \ + nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \ + NLOHMANN_JSON_ABI_TAGS, \ + NLOHMANN_JSON_NAMESPACE_VERSION) +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN +#define NLOHMANN_JSON_NAMESPACE_BEGIN \ + namespace nlohmann \ + { \ + inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \ + NLOHMANN_JSON_ABI_TAGS, \ + NLOHMANN_JSON_NAMESPACE_VERSION) \ + { +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_END +#define NLOHMANN_JSON_NAMESPACE_END \ + } /* namespace (inline namespace) NOLINT(readability/namespace) */ \ + } // namespace nlohmann +#endif + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // transform +#include // array +#include // forward_list +#include // inserter, front_inserter, end +#include // map +#include // string +#include // tuple, make_tuple +#include // is_arithmetic, is_same, is_enum, underlying_type, is_convertible +#include // unordered_map +#include // pair, declval +#include // valarray + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // nullptr_t +#include // exception +#if JSON_DIAGNOSTICS + #include // accumulate +#endif +#include // runtime_error +#include // to_string +#include // vector + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // array +#include // size_t +#include // uint8_t +#include // string + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // declval, pair +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +// https://en.cppreference.com/w/cpp/experimental/is_detected +struct nonesuch +{ + nonesuch() = delete; + ~nonesuch() = delete; + nonesuch(nonesuch const&) = delete; + nonesuch(nonesuch const&&) = delete; + void operator=(nonesuch const&) = delete; + void operator=(nonesuch&&) = delete; +}; + +template class Op, + class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +template class Op, class... Args> +using is_detected = typename detector::value_t; + +template class Op, class... Args> +struct is_detected_lazy : is_detected { }; + +template class Op, class... Args> +using detected_t = typename detector::type; + +template class Op, class... Args> +using detected_or = detector; + +template class Op, class... Args> +using detected_or_t = typename detected_or::type; + +template class Op, class... Args> +using is_detected_exact = std::is_same>; + +template class Op, class... Args> +using is_detected_convertible = + std::is_convertible, To>; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include + + +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-FileCopyrightText: 2016-2021 Evan Nemerson +// SPDX-License-Identifier: MIT + +/* Hedley - https://nemequ.github.io/hedley + * Created by Evan Nemerson + */ + +#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15) +#if defined(JSON_HEDLEY_VERSION) + #undef JSON_HEDLEY_VERSION +#endif +#define JSON_HEDLEY_VERSION 15 + +#if defined(JSON_HEDLEY_STRINGIFY_EX) + #undef JSON_HEDLEY_STRINGIFY_EX +#endif +#define JSON_HEDLEY_STRINGIFY_EX(x) #x + +#if defined(JSON_HEDLEY_STRINGIFY) + #undef JSON_HEDLEY_STRINGIFY +#endif +#define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x) + +#if defined(JSON_HEDLEY_CONCAT_EX) + #undef JSON_HEDLEY_CONCAT_EX +#endif +#define JSON_HEDLEY_CONCAT_EX(a,b) a##b + +#if defined(JSON_HEDLEY_CONCAT) + #undef JSON_HEDLEY_CONCAT +#endif +#define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b) + +#if defined(JSON_HEDLEY_CONCAT3_EX) + #undef JSON_HEDLEY_CONCAT3_EX +#endif +#define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c + +#if defined(JSON_HEDLEY_CONCAT3) + #undef JSON_HEDLEY_CONCAT3 +#endif +#define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c) + +#if defined(JSON_HEDLEY_VERSION_ENCODE) + #undef JSON_HEDLEY_VERSION_ENCODE +#endif +#define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision)) + +#if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR) + #undef JSON_HEDLEY_VERSION_DECODE_MAJOR +#endif +#define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000) + +#if defined(JSON_HEDLEY_VERSION_DECODE_MINOR) + #undef JSON_HEDLEY_VERSION_DECODE_MINOR +#endif +#define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000) + +#if defined(JSON_HEDLEY_VERSION_DECODE_REVISION) + #undef JSON_HEDLEY_VERSION_DECODE_REVISION +#endif +#define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000) + +#if defined(JSON_HEDLEY_GNUC_VERSION) + #undef JSON_HEDLEY_GNUC_VERSION +#endif +#if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__) + #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) +#elif defined(__GNUC__) + #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0) +#endif + +#if defined(JSON_HEDLEY_GNUC_VERSION_CHECK) + #undef JSON_HEDLEY_GNUC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_GNUC_VERSION) + #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_MSVC_VERSION) + #undef JSON_HEDLEY_MSVC_VERSION +#endif +#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL) + #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100) +#elif defined(_MSC_FULL_VER) && !defined(__ICL) + #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10) +#elif defined(_MSC_VER) && !defined(__ICL) + #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0) +#endif + +#if defined(JSON_HEDLEY_MSVC_VERSION_CHECK) + #undef JSON_HEDLEY_MSVC_VERSION_CHECK +#endif +#if !defined(JSON_HEDLEY_MSVC_VERSION) + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0) +#elif defined(_MSC_VER) && (_MSC_VER >= 1400) + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch))) +#elif defined(_MSC_VER) && (_MSC_VER >= 1200) + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch))) +#else + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor))) +#endif + +#if defined(JSON_HEDLEY_INTEL_VERSION) + #undef JSON_HEDLEY_INTEL_VERSION +#endif +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL) + #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE) +#elif defined(__INTEL_COMPILER) && !defined(__ICL) + #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0) +#endif + +#if defined(JSON_HEDLEY_INTEL_VERSION_CHECK) + #undef JSON_HEDLEY_INTEL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_INTEL_VERSION) + #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #undef JSON_HEDLEY_INTEL_CL_VERSION +#endif +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL) + #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0) +#endif + +#if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK) + #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_PGI_VERSION) + #undef JSON_HEDLEY_PGI_VERSION +#endif +#if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__) + #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) +#endif + +#if defined(JSON_HEDLEY_PGI_VERSION_CHECK) + #undef JSON_HEDLEY_PGI_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_PGI_VERSION) + #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_SUNPRO_VERSION) + #undef JSON_HEDLEY_SUNPRO_VERSION +#endif +#if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10) +#elif defined(__SUNPRO_C) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf) +#elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10) +#elif defined(__SUNPRO_CC) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf) +#endif + +#if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK) + #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_SUNPRO_VERSION) + #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) + #undef JSON_HEDLEY_EMSCRIPTEN_VERSION +#endif +#if defined(__EMSCRIPTEN__) + #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__) +#endif + +#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK) + #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) + #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_ARM_VERSION) + #undef JSON_HEDLEY_ARM_VERSION +#endif +#if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION) + #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100) +#elif defined(__CC_ARM) && defined(__ARMCC_VERSION) + #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100) +#endif + +#if defined(JSON_HEDLEY_ARM_VERSION_CHECK) + #undef JSON_HEDLEY_ARM_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_ARM_VERSION) + #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_IBM_VERSION) + #undef JSON_HEDLEY_IBM_VERSION +#endif +#if defined(__ibmxl__) + #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__) +#elif defined(__xlC__) && defined(__xlC_ver__) + #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff) +#elif defined(__xlC__) + #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0) +#endif + +#if defined(JSON_HEDLEY_IBM_VERSION_CHECK) + #undef JSON_HEDLEY_IBM_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_IBM_VERSION) + #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_VERSION) + #undef JSON_HEDLEY_TI_VERSION +#endif +#if \ + defined(__TI_COMPILER_VERSION__) && \ + ( \ + defined(__TMS470__) || defined(__TI_ARM__) || \ + defined(__MSP430__) || \ + defined(__TMS320C2000__) \ + ) +#if (__TI_COMPILER_VERSION__ >= 16000000) + #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif +#endif + +#if defined(JSON_HEDLEY_TI_VERSION_CHECK) + #undef JSON_HEDLEY_TI_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_VERSION) + #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL2000_VERSION) + #undef JSON_HEDLEY_TI_CL2000_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__) + #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL2000_VERSION) + #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL430_VERSION) + #undef JSON_HEDLEY_TI_CL430_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__) + #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL430_VERSION) + #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_ARMCL_VERSION) + #undef JSON_HEDLEY_TI_ARMCL_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__)) + #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK) + #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_ARMCL_VERSION) + #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL6X_VERSION) + #undef JSON_HEDLEY_TI_CL6X_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__) + #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL6X_VERSION) + #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL7X_VERSION) + #undef JSON_HEDLEY_TI_CL7X_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__C7000__) + #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL7X_VERSION) + #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CLPRU_VERSION) + #undef JSON_HEDLEY_TI_CLPRU_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__PRU__) + #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CLPRU_VERSION) + #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_CRAY_VERSION) + #undef JSON_HEDLEY_CRAY_VERSION +#endif +#if defined(_CRAYC) + #if defined(_RELEASE_PATCHLEVEL) + #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL) + #else + #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0) + #endif +#endif + +#if defined(JSON_HEDLEY_CRAY_VERSION_CHECK) + #undef JSON_HEDLEY_CRAY_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_CRAY_VERSION) + #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_IAR_VERSION) + #undef JSON_HEDLEY_IAR_VERSION +#endif +#if defined(__IAR_SYSTEMS_ICC__) + #if __VER__ > 1000 + #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000)) + #else + #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0) + #endif +#endif + +#if defined(JSON_HEDLEY_IAR_VERSION_CHECK) + #undef JSON_HEDLEY_IAR_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_IAR_VERSION) + #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TINYC_VERSION) + #undef JSON_HEDLEY_TINYC_VERSION +#endif +#if defined(__TINYC__) + #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100) +#endif + +#if defined(JSON_HEDLEY_TINYC_VERSION_CHECK) + #undef JSON_HEDLEY_TINYC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TINYC_VERSION) + #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_DMC_VERSION) + #undef JSON_HEDLEY_DMC_VERSION +#endif +#if defined(__DMC__) + #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf) +#endif + +#if defined(JSON_HEDLEY_DMC_VERSION_CHECK) + #undef JSON_HEDLEY_DMC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_DMC_VERSION) + #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_COMPCERT_VERSION) + #undef JSON_HEDLEY_COMPCERT_VERSION +#endif +#if defined(__COMPCERT_VERSION__) + #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100) +#endif + +#if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK) + #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_COMPCERT_VERSION) + #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_PELLES_VERSION) + #undef JSON_HEDLEY_PELLES_VERSION +#endif +#if defined(__POCC__) + #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0) +#endif + +#if defined(JSON_HEDLEY_PELLES_VERSION_CHECK) + #undef JSON_HEDLEY_PELLES_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_PELLES_VERSION) + #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #undef JSON_HEDLEY_MCST_LCC_VERSION +#endif +#if defined(__LCC__) && defined(__LCC_MINOR__) + #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__) +#endif + +#if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK) + #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_GCC_VERSION) + #undef JSON_HEDLEY_GCC_VERSION +#endif +#if \ + defined(JSON_HEDLEY_GNUC_VERSION) && \ + !defined(__clang__) && \ + !defined(JSON_HEDLEY_INTEL_VERSION) && \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_ARM_VERSION) && \ + !defined(JSON_HEDLEY_CRAY_VERSION) && \ + !defined(JSON_HEDLEY_TI_VERSION) && \ + !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL430_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \ + !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \ + !defined(__COMPCERT__) && \ + !defined(JSON_HEDLEY_MCST_LCC_VERSION) + #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION +#endif + +#if defined(JSON_HEDLEY_GCC_VERSION_CHECK) + #undef JSON_HEDLEY_GCC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_GCC_VERSION) + #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_HAS_ATTRIBUTE +#endif +#if \ + defined(__has_attribute) && \ + ( \ + (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \ + ) +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) +#else +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE +#endif +#if defined(__has_attribute) + #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) +#else + #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE +#endif +#if defined(__has_attribute) + #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) +#else + #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE +#endif +#if \ + defined(__has_cpp_attribute) && \ + defined(__cplusplus) && \ + (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute) +#else + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0) +#endif + +#if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS) + #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS +#endif +#if !defined(__cplusplus) || !defined(__has_cpp_attribute) + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0) +#elif \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_IAR_VERSION) && \ + (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \ + (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0)) + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute) +#else + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE +#endif +#if defined(__has_cpp_attribute) && defined(__cplusplus) + #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) +#else + #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE +#endif +#if defined(__has_cpp_attribute) && defined(__cplusplus) + #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) +#else + #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_BUILTIN) + #undef JSON_HEDLEY_HAS_BUILTIN +#endif +#if defined(__has_builtin) + #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else + #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN) + #undef JSON_HEDLEY_GNUC_HAS_BUILTIN +#endif +#if defined(__has_builtin) + #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) +#else + #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_BUILTIN) + #undef JSON_HEDLEY_GCC_HAS_BUILTIN +#endif +#if defined(__has_builtin) + #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) +#else + #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_FEATURE) + #undef JSON_HEDLEY_HAS_FEATURE +#endif +#if defined(__has_feature) + #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature) +#else + #define JSON_HEDLEY_HAS_FEATURE(feature) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_FEATURE) + #undef JSON_HEDLEY_GNUC_HAS_FEATURE +#endif +#if defined(__has_feature) + #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) +#else + #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_FEATURE) + #undef JSON_HEDLEY_GCC_HAS_FEATURE +#endif +#if defined(__has_feature) + #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) +#else + #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_EXTENSION) + #undef JSON_HEDLEY_HAS_EXTENSION +#endif +#if defined(__has_extension) + #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension) +#else + #define JSON_HEDLEY_HAS_EXTENSION(extension) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION) + #undef JSON_HEDLEY_GNUC_HAS_EXTENSION +#endif +#if defined(__has_extension) + #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) +#else + #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_EXTENSION) + #undef JSON_HEDLEY_GCC_HAS_EXTENSION +#endif +#if defined(__has_extension) + #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) +#else + #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE +#endif +#if defined(__has_declspec_attribute) + #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute) +#else + #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE +#endif +#if defined(__has_declspec_attribute) + #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) +#else + #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE +#endif +#if defined(__has_declspec_attribute) + #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) +#else + #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_WARNING) + #undef JSON_HEDLEY_HAS_WARNING +#endif +#if defined(__has_warning) + #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning) +#else + #define JSON_HEDLEY_HAS_WARNING(warning) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_WARNING) + #undef JSON_HEDLEY_GNUC_HAS_WARNING +#endif +#if defined(__has_warning) + #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) +#else + #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_WARNING) + #undef JSON_HEDLEY_GCC_HAS_WARNING +#endif +#if defined(__has_warning) + #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) +#else + #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ + defined(__clang__) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \ + (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR)) + #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_PRAGMA(value) __pragma(value) +#else + #define JSON_HEDLEY_PRAGMA(value) +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) + #undef JSON_HEDLEY_DIAGNOSTIC_PUSH +#endif +#if defined(JSON_HEDLEY_DIAGNOSTIC_POP) + #undef JSON_HEDLEY_DIAGNOSTIC_POP +#endif +#if defined(__clang__) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) + #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) +#elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#else + #define JSON_HEDLEY_DIAGNOSTIC_PUSH + #define JSON_HEDLEY_DIAGNOSTIC_POP +#endif + +/* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for + HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ +#endif +#if defined(__cplusplus) +# if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat") +# if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions") +# if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions") +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ + _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# endif +# else +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# endif +# endif +#endif +#if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x +#endif + +#if defined(JSON_HEDLEY_CONST_CAST) + #undef JSON_HEDLEY_CONST_CAST +#endif +#if defined(__cplusplus) +# define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast(expr)) +#elif \ + JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +# define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \ + ((T) (expr)); \ + JSON_HEDLEY_DIAGNOSTIC_POP \ + })) +#else +# define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr)) +#endif + +#if defined(JSON_HEDLEY_REINTERPRET_CAST) + #undef JSON_HEDLEY_REINTERPRET_CAST +#endif +#if defined(__cplusplus) + #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast(expr)) +#else + #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr)) +#endif + +#if defined(JSON_HEDLEY_STATIC_CAST) + #undef JSON_HEDLEY_STATIC_CAST +#endif +#if defined(__cplusplus) + #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast(expr)) +#else + #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr)) +#endif + +#if defined(JSON_HEDLEY_CPP_CAST) + #undef JSON_HEDLEY_CPP_CAST +#endif +#if defined(__cplusplus) +# if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast") +# define JSON_HEDLEY_CPP_CAST(T, expr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \ + ((T) (expr)) \ + JSON_HEDLEY_DIAGNOSTIC_POP +# elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0) +# define JSON_HEDLEY_CPP_CAST(T, expr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("diag_suppress=Pe137") \ + JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr)) +# endif +#else +# define JSON_HEDLEY_CPP_CAST(T, expr) (expr) +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445") +#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718") +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)") +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215") +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068)) +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") +#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292)) +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098") +#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wcast-qual") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunused-function") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif + +#if defined(JSON_HEDLEY_DEPRECATED) + #undef JSON_HEDLEY_DEPRECATED +#endif +#if defined(JSON_HEDLEY_DEPRECATED_FOR) + #undef JSON_HEDLEY_DEPRECATED_FOR +#endif +#if \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) +#elif \ + (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) +#elif defined(__cplusplus) && (__cplusplus >= 201402L) + #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]]) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]]) +#elif \ + JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) + #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__)) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated) +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated") + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated") +#else + #define JSON_HEDLEY_DEPRECATED(since) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) +#endif + +#if defined(JSON_HEDLEY_UNAVAILABLE) + #undef JSON_HEDLEY_UNAVAILABLE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since))) +#else + #define JSON_HEDLEY_UNAVAILABLE(available_since) +#endif + +#if defined(JSON_HEDLEY_WARN_UNUSED_RESULT) + #undef JSON_HEDLEY_WARN_UNUSED_RESULT +#endif +#if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG) + #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__)) +#elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]]) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) +#elif defined(_Check_return_) /* SAL */ + #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_ + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_ +#else + #define JSON_HEDLEY_WARN_UNUSED_RESULT + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) +#endif + +#if defined(JSON_HEDLEY_SENTINEL) + #undef JSON_HEDLEY_SENTINEL +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position))) +#else + #define JSON_HEDLEY_SENTINEL(position) +#endif + +#if defined(JSON_HEDLEY_NO_RETURN) + #undef JSON_HEDLEY_NO_RETURN +#endif +#if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_NO_RETURN __noreturn +#elif \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + #define JSON_HEDLEY_NO_RETURN _Noreturn +#elif defined(__cplusplus) && (__cplusplus >= 201103L) + #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]]) +#elif \ + JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) + #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) + #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) +#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) + #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;") +#elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0) + #define JSON_HEDLEY_NO_RETURN __attribute((noreturn)) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0) + #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) +#else + #define JSON_HEDLEY_NO_RETURN +#endif + +#if defined(JSON_HEDLEY_NO_ESCAPE) + #undef JSON_HEDLEY_NO_ESCAPE +#endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(noescape) + #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__)) +#else + #define JSON_HEDLEY_NO_ESCAPE +#endif + +#if defined(JSON_HEDLEY_UNREACHABLE) + #undef JSON_HEDLEY_UNREACHABLE +#endif +#if defined(JSON_HEDLEY_UNREACHABLE_RETURN) + #undef JSON_HEDLEY_UNREACHABLE_RETURN +#endif +#if defined(JSON_HEDLEY_ASSUME) + #undef JSON_HEDLEY_ASSUME +#endif +#if \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_ASSUME(expr) __assume(expr) +#elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume) + #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr) +#elif \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) + #if defined(__cplusplus) + #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr) + #else + #define JSON_HEDLEY_ASSUME(expr) _nassert(expr) + #endif +#endif +#if \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable() +#elif defined(JSON_HEDLEY_ASSUME) + #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) +#endif +#if !defined(JSON_HEDLEY_ASSUME) + #if defined(JSON_HEDLEY_UNREACHABLE) + #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1))) + #else + #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr) + #endif +#endif +#if defined(JSON_HEDLEY_UNREACHABLE) + #if \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) + #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value)) + #else + #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE() + #endif +#else + #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value) +#endif +#if !defined(JSON_HEDLEY_UNREACHABLE) + #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) +#endif + +JSON_HEDLEY_DIAGNOSTIC_PUSH +#if JSON_HEDLEY_HAS_WARNING("-Wpedantic") + #pragma clang diagnostic ignored "-Wpedantic" +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus) + #pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif +#if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0) + #if defined(__clang__) + #pragma clang diagnostic ignored "-Wvariadic-macros" + #elif defined(JSON_HEDLEY_GCC_VERSION) + #pragma GCC diagnostic ignored "-Wvariadic-macros" + #endif +#endif +#if defined(JSON_HEDLEY_NON_NULL) + #undef JSON_HEDLEY_NON_NULL +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) + #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__))) +#else + #define JSON_HEDLEY_NON_NULL(...) +#endif +JSON_HEDLEY_DIAGNOSTIC_POP + +#if defined(JSON_HEDLEY_PRINTF_FORMAT) + #undef JSON_HEDLEY_PRINTF_FORMAT +#endif +#if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check))) +#elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check))) +#elif \ + JSON_HEDLEY_HAS_ATTRIBUTE(format) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check))) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check)) +#else + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) +#endif + +#if defined(JSON_HEDLEY_CONSTEXPR) + #undef JSON_HEDLEY_CONSTEXPR +#endif +#if defined(__cplusplus) + #if __cplusplus >= 201103L + #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr) + #endif +#endif +#if !defined(JSON_HEDLEY_CONSTEXPR) + #define JSON_HEDLEY_CONSTEXPR +#endif + +#if defined(JSON_HEDLEY_PREDICT) + #undef JSON_HEDLEY_PREDICT +#endif +#if defined(JSON_HEDLEY_LIKELY) + #undef JSON_HEDLEY_LIKELY +#endif +#if defined(JSON_HEDLEY_UNLIKELY) + #undef JSON_HEDLEY_UNLIKELY +#endif +#if defined(JSON_HEDLEY_UNPREDICTABLE) + #undef JSON_HEDLEY_UNPREDICTABLE +#endif +#if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable) + #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr)) +#endif +#if \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability)) +# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability)) +# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability)) +# define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 ) +# define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 ) +#elif \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PREDICT(expr, expected, probability) \ + (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))) +# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \ + (__extension__ ({ \ + double hedley_probability_ = (probability); \ + ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \ + })) +# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \ + (__extension__ ({ \ + double hedley_probability_ = (probability); \ + ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \ + })) +# define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1) +# define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0) +#else +# define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)) +# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr)) +# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr)) +# define JSON_HEDLEY_LIKELY(expr) (!!(expr)) +# define JSON_HEDLEY_UNLIKELY(expr) (!!(expr)) +#endif +#if !defined(JSON_HEDLEY_UNPREDICTABLE) + #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5) +#endif + +#if defined(JSON_HEDLEY_MALLOC) + #undef JSON_HEDLEY_MALLOC +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) + #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_MALLOC __declspec(restrict) +#else + #define JSON_HEDLEY_MALLOC +#endif + +#if defined(JSON_HEDLEY_PURE) + #undef JSON_HEDLEY_PURE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PURE __attribute__((__pure__)) +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) +# define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data") +#elif defined(__cplusplus) && \ + ( \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \ + ) +# define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;") +#else +# define JSON_HEDLEY_PURE +#endif + +#if defined(JSON_HEDLEY_CONST) + #undef JSON_HEDLEY_CONST +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(const) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_CONST __attribute__((__const__)) +#elif \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) + #define JSON_HEDLEY_CONST _Pragma("no_side_effect") +#else + #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE +#endif + +#if defined(JSON_HEDLEY_RESTRICT) + #undef JSON_HEDLEY_RESTRICT +#endif +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus) + #define JSON_HEDLEY_RESTRICT restrict +#elif \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ + defined(__clang__) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_RESTRICT __restrict +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus) + #define JSON_HEDLEY_RESTRICT _Restrict +#else + #define JSON_HEDLEY_RESTRICT +#endif + +#if defined(JSON_HEDLEY_INLINE) + #undef JSON_HEDLEY_INLINE +#endif +#if \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ + (defined(__cplusplus) && (__cplusplus >= 199711L)) + #define JSON_HEDLEY_INLINE inline +#elif \ + defined(JSON_HEDLEY_GCC_VERSION) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0) + #define JSON_HEDLEY_INLINE __inline__ +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_INLINE __inline +#else + #define JSON_HEDLEY_INLINE +#endif + +#if defined(JSON_HEDLEY_ALWAYS_INLINE) + #undef JSON_HEDLEY_ALWAYS_INLINE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) +# define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) +# define JSON_HEDLEY_ALWAYS_INLINE __forceinline +#elif defined(__cplusplus) && \ + ( \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \ + ) +# define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) +# define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced") +#else +# define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE +#endif + +#if defined(JSON_HEDLEY_NEVER_INLINE) + #undef JSON_HEDLEY_NEVER_INLINE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) + #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0) + #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline") +#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) + #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never") +#elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0) + #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline)) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0) + #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) +#else + #define JSON_HEDLEY_NEVER_INLINE +#endif + +#if defined(JSON_HEDLEY_PRIVATE) + #undef JSON_HEDLEY_PRIVATE +#endif +#if defined(JSON_HEDLEY_PUBLIC) + #undef JSON_HEDLEY_PUBLIC +#endif +#if defined(JSON_HEDLEY_IMPORT) + #undef JSON_HEDLEY_IMPORT +#endif +#if defined(_WIN32) || defined(__CYGWIN__) +# define JSON_HEDLEY_PRIVATE +# define JSON_HEDLEY_PUBLIC __declspec(dllexport) +# define JSON_HEDLEY_IMPORT __declspec(dllimport) +#else +# if \ + JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ + ( \ + defined(__TI_EABI__) && \ + ( \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \ + ) \ + ) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden"))) +# define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default"))) +# else +# define JSON_HEDLEY_PRIVATE +# define JSON_HEDLEY_PUBLIC +# endif +# define JSON_HEDLEY_IMPORT extern +#endif + +#if defined(JSON_HEDLEY_NO_THROW) + #undef JSON_HEDLEY_NO_THROW +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) + #define JSON_HEDLEY_NO_THROW __declspec(nothrow) +#else + #define JSON_HEDLEY_NO_THROW +#endif + +#if defined(JSON_HEDLEY_FALL_THROUGH) + #undef JSON_HEDLEY_FALL_THROUGH +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__)) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough) + #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]]) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough) + #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]]) +#elif defined(__fallthrough) /* SAL */ + #define JSON_HEDLEY_FALL_THROUGH __fallthrough +#else + #define JSON_HEDLEY_FALL_THROUGH +#endif + +#if defined(JSON_HEDLEY_RETURNS_NON_NULL) + #undef JSON_HEDLEY_RETURNS_NON_NULL +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__)) +#elif defined(_Ret_notnull_) /* SAL */ + #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_ +#else + #define JSON_HEDLEY_RETURNS_NON_NULL +#endif + +#if defined(JSON_HEDLEY_ARRAY_PARAM) + #undef JSON_HEDLEY_ARRAY_PARAM +#endif +#if \ + defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ + !defined(__STDC_NO_VLA__) && \ + !defined(__cplusplus) && \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_TINYC_VERSION) + #define JSON_HEDLEY_ARRAY_PARAM(name) (name) +#else + #define JSON_HEDLEY_ARRAY_PARAM(name) +#endif + +#if defined(JSON_HEDLEY_IS_CONSTANT) + #undef JSON_HEDLEY_IS_CONSTANT +#endif +#if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR) + #undef JSON_HEDLEY_REQUIRE_CONSTEXPR +#endif +/* JSON_HEDLEY_IS_CONSTEXPR_ is for + HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ +#if defined(JSON_HEDLEY_IS_CONSTEXPR_) + #undef JSON_HEDLEY_IS_CONSTEXPR_ +#endif +#if \ + JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr) +#endif +#if !defined(__cplusplus) +# if \ + JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24) +#if defined(__INTPTR_TYPE__) + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*) +#else + #include + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*) +#endif +# elif \ + ( \ + defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \ + !defined(JSON_HEDLEY_SUNPRO_VERSION) && \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_IAR_VERSION)) || \ + (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0) +#if defined(__INTPTR_TYPE__) + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0) +#else + #include + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0) +#endif +# elif \ + defined(JSON_HEDLEY_GCC_VERSION) || \ + defined(JSON_HEDLEY_INTEL_VERSION) || \ + defined(JSON_HEDLEY_TINYC_VERSION) || \ + defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \ + defined(JSON_HEDLEY_TI_CL2000_VERSION) || \ + defined(JSON_HEDLEY_TI_CL6X_VERSION) || \ + defined(JSON_HEDLEY_TI_CL7X_VERSION) || \ + defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \ + defined(__clang__) +# define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \ + sizeof(void) != \ + sizeof(*( \ + 1 ? \ + ((void*) ((expr) * 0L) ) : \ +((struct { char v[sizeof(void) * 2]; } *) 1) \ + ) \ + ) \ + ) +# endif +#endif +#if defined(JSON_HEDLEY_IS_CONSTEXPR_) + #if !defined(JSON_HEDLEY_IS_CONSTANT) + #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr) + #endif + #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1)) +#else + #if !defined(JSON_HEDLEY_IS_CONSTANT) + #define JSON_HEDLEY_IS_CONSTANT(expr) (0) + #endif + #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr) +#endif + +#if defined(JSON_HEDLEY_BEGIN_C_DECLS) + #undef JSON_HEDLEY_BEGIN_C_DECLS +#endif +#if defined(JSON_HEDLEY_END_C_DECLS) + #undef JSON_HEDLEY_END_C_DECLS +#endif +#if defined(JSON_HEDLEY_C_DECL) + #undef JSON_HEDLEY_C_DECL +#endif +#if defined(__cplusplus) + #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" { + #define JSON_HEDLEY_END_C_DECLS } + #define JSON_HEDLEY_C_DECL extern "C" +#else + #define JSON_HEDLEY_BEGIN_C_DECLS + #define JSON_HEDLEY_END_C_DECLS + #define JSON_HEDLEY_C_DECL +#endif + +#if defined(JSON_HEDLEY_STATIC_ASSERT) + #undef JSON_HEDLEY_STATIC_ASSERT +#endif +#if \ + !defined(__cplusplus) && ( \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \ + (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + defined(_Static_assert) \ + ) +# define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message) +#elif \ + (defined(__cplusplus) && (__cplusplus >= 201103L)) || \ + JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) +# define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message)) +#else +# define JSON_HEDLEY_STATIC_ASSERT(expr, message) +#endif + +#if defined(JSON_HEDLEY_NULL) + #undef JSON_HEDLEY_NULL +#endif +#if defined(__cplusplus) + #if __cplusplus >= 201103L + #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr) + #elif defined(NULL) + #define JSON_HEDLEY_NULL NULL + #else + #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0) + #endif +#elif defined(NULL) + #define JSON_HEDLEY_NULL NULL +#else + #define JSON_HEDLEY_NULL ((void*) 0) +#endif + +#if defined(JSON_HEDLEY_MESSAGE) + #undef JSON_HEDLEY_MESSAGE +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") +# define JSON_HEDLEY_MESSAGE(msg) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \ + JSON_HEDLEY_PRAGMA(message msg) \ + JSON_HEDLEY_DIAGNOSTIC_POP +#elif \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg) +#elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg) +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) +#else +# define JSON_HEDLEY_MESSAGE(msg) +#endif + +#if defined(JSON_HEDLEY_WARNING) + #undef JSON_HEDLEY_WARNING +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") +# define JSON_HEDLEY_WARNING(msg) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \ + JSON_HEDLEY_PRAGMA(clang warning msg) \ + JSON_HEDLEY_DIAGNOSTIC_POP +#elif \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) +# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg)) +#else +# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg) +#endif + +#if defined(JSON_HEDLEY_REQUIRE) + #undef JSON_HEDLEY_REQUIRE +#endif +#if defined(JSON_HEDLEY_REQUIRE_MSG) + #undef JSON_HEDLEY_REQUIRE_MSG +#endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if) +# if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat") +# define JSON_HEDLEY_REQUIRE(expr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ + __attribute__((diagnose_if(!(expr), #expr, "error"))) \ + JSON_HEDLEY_DIAGNOSTIC_POP +# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ + __attribute__((diagnose_if(!(expr), msg, "error"))) \ + JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error"))) +# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error"))) +# endif +#else +# define JSON_HEDLEY_REQUIRE(expr) +# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) +#endif + +#if defined(JSON_HEDLEY_FLAGS) + #undef JSON_HEDLEY_FLAGS +#endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion")) + #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) +#else + #define JSON_HEDLEY_FLAGS +#endif + +#if defined(JSON_HEDLEY_FLAGS_CAST) + #undef JSON_HEDLEY_FLAGS_CAST +#endif +#if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0) +# define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("warning(disable:188)") \ + ((T) (expr)); \ + JSON_HEDLEY_DIAGNOSTIC_POP \ + })) +#else +# define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr) +#endif + +#if defined(JSON_HEDLEY_EMPTY_BASES) + #undef JSON_HEDLEY_EMPTY_BASES +#endif +#if \ + (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases) +#else + #define JSON_HEDLEY_EMPTY_BASES +#endif + +/* Remaining macros are deprecated. */ + +#if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK) + #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK +#endif +#if defined(__clang__) + #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0) +#else + #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE +#endif +#define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) + +#if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE +#endif +#define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) + +#if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN) + #undef JSON_HEDLEY_CLANG_HAS_BUILTIN +#endif +#define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin) + +#if defined(JSON_HEDLEY_CLANG_HAS_FEATURE) + #undef JSON_HEDLEY_CLANG_HAS_FEATURE +#endif +#define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature) + +#if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION) + #undef JSON_HEDLEY_CLANG_HAS_EXTENSION +#endif +#define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension) + +#if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE +#endif +#define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) + +#if defined(JSON_HEDLEY_CLANG_HAS_WARNING) + #undef JSON_HEDLEY_CLANG_HAS_WARNING +#endif +#define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning) + +#endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */ + + +// This file contains all internal macro definitions (except those affecting ABI) +// You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them + +// #include + + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + +// C++ language standard detection +// if the user manually specified the used c++ version this is skipped +#if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11) + #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) + #define JSON_HAS_CPP_20 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 + #endif + // the cpp 11 flag is always specified because it is the minimal required version + #define JSON_HAS_CPP_11 +#endif + +#ifdef __has_include + #if __has_include() + #include + #endif +#endif + +#if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM) + #ifdef JSON_HAS_CPP_17 + #if defined(__cpp_lib_filesystem) + #define JSON_HAS_FILESYSTEM 1 + #elif defined(__cpp_lib_experimental_filesystem) + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #elif !defined(__has_include) + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #elif __has_include() + #define JSON_HAS_FILESYSTEM 1 + #elif __has_include() + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #endif + + // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/ + #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support + #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support + #if defined(__clang_major__) && __clang_major__ < 7 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support + #if defined(_MSC_VER) && _MSC_VER < 1914 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before iOS 13 + #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before macOS Catalina + #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + #endif +#endif + +#ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0 +#endif + +#ifndef JSON_HAS_FILESYSTEM + #define JSON_HAS_FILESYSTEM 0 +#endif + +#ifndef JSON_HAS_THREE_WAY_COMPARISON + #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \ + && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L + #define JSON_HAS_THREE_WAY_COMPARISON 1 + #else + #define JSON_HAS_THREE_WAY_COMPARISON 0 + #endif +#endif + +#ifndef JSON_HAS_RANGES + // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error + #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427 + #define JSON_HAS_RANGES 0 + #elif defined(__cpp_lib_ranges) + #define JSON_HAS_RANGES 1 + #else + #define JSON_HAS_RANGES 0 + #endif +#endif + +#ifndef JSON_HAS_STATIC_RTTI + #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0 + #define JSON_HAS_STATIC_RTTI 1 + #else + #define JSON_HAS_STATIC_RTTI 0 + #endif +#endif + +#ifdef JSON_HAS_CPP_17 + #define JSON_INLINE_VARIABLE inline +#else + #define JSON_INLINE_VARIABLE +#endif + +#if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address) + #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] +#else + #define JSON_NO_UNIQUE_ADDRESS +#endif + +// disable documentation warnings on clang +#if defined(__clang__) + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdocumentation" + #pragma clang diagnostic ignored "-Wdocumentation-unknown-command" +#endif + +// allow disabling exceptions +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) + #define JSON_THROW(exception) throw exception + #define JSON_TRY try + #define JSON_CATCH(exception) catch(exception) + #define JSON_INTERNAL_CATCH(exception) catch(exception) +#else + #include + #define JSON_THROW(exception) std::abort() + #define JSON_TRY if(true) + #define JSON_CATCH(exception) if(false) + #define JSON_INTERNAL_CATCH(exception) if(false) +#endif + +// override exception macros +#if defined(JSON_THROW_USER) + #undef JSON_THROW + #define JSON_THROW JSON_THROW_USER +#endif +#if defined(JSON_TRY_USER) + #undef JSON_TRY + #define JSON_TRY JSON_TRY_USER +#endif +#if defined(JSON_CATCH_USER) + #undef JSON_CATCH + #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_CATCH_USER +#endif +#if defined(JSON_INTERNAL_CATCH_USER) + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER +#endif + +// allow overriding assert +#if !defined(JSON_ASSERT) + #include // assert + #define JSON_ASSERT(x) assert(x) +#endif + +// allow to access some private functions (needed by the test suite) +#if defined(JSON_TESTS_PRIVATE) + #define JSON_PRIVATE_UNLESS_TESTED public +#else + #define JSON_PRIVATE_UNLESS_TESTED private +#endif + +/*! +@brief macro to briefly define a mapping between an enum and JSON +@def NLOHMANN_JSON_SERIALIZE_ENUM +@since version 3.4.0 +*/ +#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ + template \ + inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [e](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.first == e; \ + }); \ + j = ((it != std::end(m)) ? it : std::begin(m))->second; \ + } \ + template \ + inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [&j](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.second == j; \ + }); \ + e = ((it != std::end(m)) ? it : std::begin(m))->first; \ + } + +// Ugly macros to avoid uglier copy-paste when specializing basic_json. They +// may be removed in the future once the class is split. + +#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ + template class ObjectType, \ + template class ArrayType, \ + class StringType, class BooleanType, class NumberIntegerType, \ + class NumberUnsignedType, class NumberFloatType, \ + template class AllocatorType, \ + template class JSONSerializer, \ + class BinaryType, \ + class CustomBaseClass> + +#define NLOHMANN_BASIC_JSON_TPL \ + basic_json + +// Macros to simplify conversion from/to types + +#define NLOHMANN_JSON_EXPAND( x ) x +#define NLOHMANN_JSON_GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, NAME,...) NAME +#define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \ + NLOHMANN_JSON_PASTE64, \ + NLOHMANN_JSON_PASTE63, \ + NLOHMANN_JSON_PASTE62, \ + NLOHMANN_JSON_PASTE61, \ + NLOHMANN_JSON_PASTE60, \ + NLOHMANN_JSON_PASTE59, \ + NLOHMANN_JSON_PASTE58, \ + NLOHMANN_JSON_PASTE57, \ + NLOHMANN_JSON_PASTE56, \ + NLOHMANN_JSON_PASTE55, \ + NLOHMANN_JSON_PASTE54, \ + NLOHMANN_JSON_PASTE53, \ + NLOHMANN_JSON_PASTE52, \ + NLOHMANN_JSON_PASTE51, \ + NLOHMANN_JSON_PASTE50, \ + NLOHMANN_JSON_PASTE49, \ + NLOHMANN_JSON_PASTE48, \ + NLOHMANN_JSON_PASTE47, \ + NLOHMANN_JSON_PASTE46, \ + NLOHMANN_JSON_PASTE45, \ + NLOHMANN_JSON_PASTE44, \ + NLOHMANN_JSON_PASTE43, \ + NLOHMANN_JSON_PASTE42, \ + NLOHMANN_JSON_PASTE41, \ + NLOHMANN_JSON_PASTE40, \ + NLOHMANN_JSON_PASTE39, \ + NLOHMANN_JSON_PASTE38, \ + NLOHMANN_JSON_PASTE37, \ + NLOHMANN_JSON_PASTE36, \ + NLOHMANN_JSON_PASTE35, \ + NLOHMANN_JSON_PASTE34, \ + NLOHMANN_JSON_PASTE33, \ + NLOHMANN_JSON_PASTE32, \ + NLOHMANN_JSON_PASTE31, \ + NLOHMANN_JSON_PASTE30, \ + NLOHMANN_JSON_PASTE29, \ + NLOHMANN_JSON_PASTE28, \ + NLOHMANN_JSON_PASTE27, \ + NLOHMANN_JSON_PASTE26, \ + NLOHMANN_JSON_PASTE25, \ + NLOHMANN_JSON_PASTE24, \ + NLOHMANN_JSON_PASTE23, \ + NLOHMANN_JSON_PASTE22, \ + NLOHMANN_JSON_PASTE21, \ + NLOHMANN_JSON_PASTE20, \ + NLOHMANN_JSON_PASTE19, \ + NLOHMANN_JSON_PASTE18, \ + NLOHMANN_JSON_PASTE17, \ + NLOHMANN_JSON_PASTE16, \ + NLOHMANN_JSON_PASTE15, \ + NLOHMANN_JSON_PASTE14, \ + NLOHMANN_JSON_PASTE13, \ + NLOHMANN_JSON_PASTE12, \ + NLOHMANN_JSON_PASTE11, \ + NLOHMANN_JSON_PASTE10, \ + NLOHMANN_JSON_PASTE9, \ + NLOHMANN_JSON_PASTE8, \ + NLOHMANN_JSON_PASTE7, \ + NLOHMANN_JSON_PASTE6, \ + NLOHMANN_JSON_PASTE5, \ + NLOHMANN_JSON_PASTE4, \ + NLOHMANN_JSON_PASTE3, \ + NLOHMANN_JSON_PASTE2, \ + NLOHMANN_JSON_PASTE1)(__VA_ARGS__)) +#define NLOHMANN_JSON_PASTE2(func, v1) func(v1) +#define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2) +#define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3) +#define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4) +#define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5) +#define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6) +#define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7) +#define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8) +#define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9) +#define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10) +#define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) +#define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) +#define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) +#define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) +#define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) +#define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) +#define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) +#define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) +#define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) +#define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) +#define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) +#define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) +#define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) +#define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) +#define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) +#define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) +#define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) +#define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) +#define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) +#define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) +#define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) +#define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) +#define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) +#define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) +#define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) +#define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) +#define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) +#define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) +#define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) +#define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) +#define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) +#define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) +#define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) +#define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) +#define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) +#define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) +#define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) +#define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) +#define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) +#define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) +#define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) +#define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) +#define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) +#define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) +#define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) +#define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) +#define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) +#define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) +#define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) +#define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) +#define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) +#define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) +#define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) + +#define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1; +#define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1); +#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1); + +/*! +@brief macro +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE +@since version 3.9.0 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } + +/*! +@brief macro +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE +@since version 3.9.0 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } + +// inspired from https://stackoverflow.com/a/26745591 +// allows to call any std function as if (e.g. with begin): +// using std::begin; begin(x); +// +// it allows using the detected idiom to retrieve the return type +// of such an expression +#define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \ + namespace detail { \ + using std::std_name; \ + \ + template \ + using result_of_##std_name = decltype(std_name(std::declval()...)); \ + } \ + \ + namespace detail2 { \ + struct std_name##_tag \ + { \ + }; \ + \ + template \ + std_name##_tag std_name(T&&...); \ + \ + template \ + using result_of_##std_name = decltype(std_name(std::declval()...)); \ + \ + template \ + struct would_call_std_##std_name \ + { \ + static constexpr auto const value = ::nlohmann::detail:: \ + is_detected_exact::value; \ + }; \ + } /* namespace detail2 */ \ + \ + template \ + struct would_call_std_##std_name : detail2::would_call_std_##std_name \ + { \ + } + +#ifndef JSON_USE_IMPLICIT_CONVERSIONS + #define JSON_USE_IMPLICIT_CONVERSIONS 1 +#endif + +#if JSON_USE_IMPLICIT_CONVERSIONS + #define JSON_EXPLICIT +#else + #define JSON_EXPLICIT explicit +#endif + +#ifndef JSON_DISABLE_ENUM_SERIALIZATION + #define JSON_DISABLE_ENUM_SERIALIZATION 0 +#endif + +#ifndef JSON_USE_GLOBAL_UDLS + #define JSON_USE_GLOBAL_UDLS 1 +#endif + +#if JSON_HAS_THREE_WAY_COMPARISON + #include // partial_ordering +#endif + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/////////////////////////// +// JSON type enumeration // +/////////////////////////// + +/*! +@brief the JSON type enumeration + +This enumeration collects the different JSON types. It is internally used to +distinguish the stored values, and the functions @ref basic_json::is_null(), +@ref basic_json::is_object(), @ref basic_json::is_array(), +@ref basic_json::is_string(), @ref basic_json::is_boolean(), +@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), +@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), +@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and +@ref basic_json::is_structured() rely on it. + +@note There are three enumeration entries (number_integer, number_unsigned, and +number_float), because the library distinguishes these three types for numbers: +@ref basic_json::number_unsigned_t is used for unsigned integers, +@ref basic_json::number_integer_t is used for signed integers, and +@ref basic_json::number_float_t is used for floating-point numbers or to +approximate integers which do not fit in the limits of their respective type. + +@sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON +value with the default value for a given type + +@since version 1.0.0 +*/ +enum class value_t : std::uint8_t +{ + null, ///< null value + object, ///< object (unordered set of name/value pairs) + array, ///< array (ordered collection of values) + string, ///< string value + boolean, ///< boolean value + number_integer, ///< number value (signed integer) + number_unsigned, ///< number value (unsigned integer) + number_float, ///< number value (floating-point) + binary, ///< binary array (ordered collection of bytes) + discarded ///< discarded by the parser callback function +}; + +/*! +@brief comparison operator for JSON types + +Returns an ordering that is similar to Python: +- order: null < boolean < number < object < array < string < binary +- furthermore, each type is not smaller than itself +- discarded values are not comparable +- binary is represented as a b"" string in python and directly comparable to a + string; however, making a binary array directly comparable with a string would + be surprising behavior in a JSON file. + +@since version 1.0.0 +*/ +#if JSON_HAS_THREE_WAY_COMPARISON + inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD* +#else + inline bool operator<(const value_t lhs, const value_t rhs) noexcept +#endif +{ + static constexpr std::array order = {{ + 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, + 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, + 6 /* binary */ + } + }; + + const auto l_index = static_cast(lhs); + const auto r_index = static_cast(rhs); +#if JSON_HAS_THREE_WAY_COMPARISON + if (l_index < order.size() && r_index < order.size()) + { + return order[l_index] <=> order[r_index]; // *NOPAD* + } + return std::partial_ordering::unordered; +#else + return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; +#endif +} + +// GCC selects the built-in operator< over an operator rewritten from +// a user-defined spaceship operator +// Clang, MSVC, and ICC select the rewritten candidate +// (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200) +#if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__) +inline bool operator<(const value_t lhs, const value_t rhs) noexcept +{ + return std::is_lt(lhs <=> rhs); // *NOPAD* +} +#endif + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/*! +@brief replace all occurrences of a substring by another string + +@param[in,out] s the string to manipulate; changed so that all + occurrences of @a f are replaced with @a t +@param[in] f the substring to replace with @a t +@param[in] t the string to replace @a f + +@pre The search string @a f must not be empty. **This precondition is +enforced with an assertion.** + +@since version 2.0.0 +*/ +template +inline void replace_substring(StringType& s, const StringType& f, + const StringType& t) +{ + JSON_ASSERT(!f.empty()); + for (auto pos = s.find(f); // find first occurrence of f + pos != StringType::npos; // make sure f was found + s.replace(pos, f.size(), t), // replace with t, and + pos = s.find(f, pos + t.size())) // find next occurrence of f + {} +} + +/*! + * @brief string escaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to escape + * @return escaped string + * + * Note the order of escaping "~" to "~0" and "/" to "~1" is important. + */ +template +inline StringType escape(StringType s) +{ + replace_substring(s, StringType{"~"}, StringType{"~0"}); + replace_substring(s, StringType{"/"}, StringType{"~1"}); + return s; +} + +/*! + * @brief string unescaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to unescape + * @return unescaped string + * + * Note the order of escaping "~1" to "/" and "~0" to "~" is important. + */ +template +static void unescape(StringType& s) +{ + replace_substring(s, StringType{"~1"}, StringType{"/"}); + replace_substring(s, StringType{"~0"}, StringType{"~"}); +} + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // size_t + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/// struct to capture the start position of the current token +struct position_t +{ + /// the total number of characters read + std::size_t chars_read_total = 0; + /// the number of characters read in the current line + std::size_t chars_read_current_line = 0; + /// the number of lines read + std::size_t lines_read = 0; + + /// conversion to size_t to preserve SAX interface + constexpr operator size_t() const + { + return chars_read_total; + } +}; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-FileCopyrightText: 2018 The Abseil Authors +// SPDX-License-Identifier: MIT + + + +#include // array +#include // size_t +#include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type +#include // index_sequence, make_index_sequence, index_sequence_for + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +template +using uncvref_t = typename std::remove_cv::type>::type; + +#ifdef JSON_HAS_CPP_14 + +// the following utilities are natively available in C++14 +using std::enable_if_t; +using std::index_sequence; +using std::make_index_sequence; +using std::index_sequence_for; + +#else + +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + +// The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h +// which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0. + +//// START OF CODE FROM GOOGLE ABSEIL + +// integer_sequence +// +// Class template representing a compile-time integer sequence. An instantiation +// of `integer_sequence` has a sequence of integers encoded in its +// type through its template arguments (which is a common need when +// working with C++11 variadic templates). `absl::integer_sequence` is designed +// to be a drop-in replacement for C++14's `std::integer_sequence`. +// +// Example: +// +// template< class T, T... Ints > +// void user_function(integer_sequence); +// +// int main() +// { +// // user_function's `T` will be deduced to `int` and `Ints...` +// // will be deduced to `0, 1, 2, 3, 4`. +// user_function(make_integer_sequence()); +// } +template +struct integer_sequence +{ + using value_type = T; + static constexpr std::size_t size() noexcept + { + return sizeof...(Ints); + } +}; + +// index_sequence +// +// A helper template for an `integer_sequence` of `size_t`, +// `absl::index_sequence` is designed to be a drop-in replacement for C++14's +// `std::index_sequence`. +template +using index_sequence = integer_sequence; + +namespace utility_internal +{ + +template +struct Extend; + +// Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency. +template +struct Extend, SeqSize, 0> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >; +}; + +template +struct Extend, SeqSize, 1> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >; +}; + +// Recursion helper for 'make_integer_sequence'. +// 'Gen::type' is an alias for 'integer_sequence'. +template +struct Gen +{ + using type = + typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type; +}; + +template +struct Gen +{ + using type = integer_sequence; +}; + +} // namespace utility_internal + +// Compile-time sequences of integers + +// make_integer_sequence +// +// This template alias is equivalent to +// `integer_sequence`, and is designed to be a drop-in +// replacement for C++14's `std::make_integer_sequence`. +template +using make_integer_sequence = typename utility_internal::Gen::type; + +// make_index_sequence +// +// This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`, +// and is designed to be a drop-in replacement for C++14's +// `std::make_index_sequence`. +template +using make_index_sequence = make_integer_sequence; + +// index_sequence_for +// +// Converts a typename pack into an index sequence of the same length, and +// is designed to be a drop-in replacement for C++14's +// `std::index_sequence_for()` +template +using index_sequence_for = make_index_sequence; + +//// END OF CODE FROM GOOGLE ABSEIL + +#endif + +// dispatch utility (taken from ranges-v3) +template struct priority_tag : priority_tag < N - 1 > {}; +template<> struct priority_tag<0> {}; + +// taken from ranges-v3 +template +struct static_const +{ + static JSON_INLINE_VARIABLE constexpr T value{}; +}; + +#ifndef JSON_HAS_CPP_17 + template + constexpr T static_const::value; +#endif + +template +inline constexpr std::array make_array(Args&& ... args) +{ + return std::array {{static_cast(std::forward(args))...}}; +} + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // numeric_limits +#include // false_type, is_constructible, is_integral, is_same, true_type +#include // declval +#include // tuple +#include // char_traits + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // random_access_iterator_tag + +// #include + +// #include + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +template +struct iterator_types {}; + +template +struct iterator_types < + It, + void_t> +{ + using difference_type = typename It::difference_type; + using value_type = typename It::value_type; + using pointer = typename It::pointer; + using reference = typename It::reference; + using iterator_category = typename It::iterator_category; +}; + +// This is required as some compilers implement std::iterator_traits in a way that +// doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341. +template +struct iterator_traits +{ +}; + +template +struct iterator_traits < T, enable_if_t < !std::is_pointer::value >> + : iterator_types +{ +}; + +template +struct iterator_traits::value>> +{ + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = T*; + using reference = T&; +}; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN + +NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin); + +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN + +NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end); + +NLOHMANN_JSON_NAMESPACE_END + +// #include + +// #include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + +#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_ + #define INCLUDE_NLOHMANN_JSON_FWD_HPP_ + + #include // int64_t, uint64_t + #include // map + #include // allocator + #include // string + #include // vector + + // #include + + + /*! + @brief namespace for Niels Lohmann + @see https://github.com/nlohmann + @since version 1.0.0 + */ + NLOHMANN_JSON_NAMESPACE_BEGIN + + /*! + @brief default JSONSerializer template argument + + This serializer ignores the template arguments and uses ADL + ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl)) + for serialization. + */ + template + struct adl_serializer; + + /// a class to store JSON values + /// @sa https://json.nlohmann.me/api/basic_json/ + template class ObjectType = + std::map, + template class ArrayType = std::vector, + class StringType = std::string, class BooleanType = bool, + class NumberIntegerType = std::int64_t, + class NumberUnsignedType = std::uint64_t, + class NumberFloatType = double, + template class AllocatorType = std::allocator, + template class JSONSerializer = + adl_serializer, + class BinaryType = std::vector, // cppcheck-suppress syntaxError + class CustomBaseClass = void> + class basic_json; + + /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document + /// @sa https://json.nlohmann.me/api/json_pointer/ + template + class json_pointer; + + /*! + @brief default specialization + @sa https://json.nlohmann.me/api/json/ + */ + using json = basic_json<>; + + /// @brief a minimal map-like container that preserves insertion order + /// @sa https://json.nlohmann.me/api/ordered_map/ + template + struct ordered_map; + + /// @brief specialization that maintains the insertion order of object keys + /// @sa https://json.nlohmann.me/api/ordered_json/ + using ordered_json = basic_json; + + NLOHMANN_JSON_NAMESPACE_END + +#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_ + + +NLOHMANN_JSON_NAMESPACE_BEGIN +/*! +@brief detail namespace with internal helper functions + +This namespace collects functions that should not be exposed, +implementations of some @ref basic_json methods, and meta-programming helpers. + +@since version 2.1.0 +*/ +namespace detail +{ + +///////////// +// helpers // +///////////// + +// Note to maintainers: +// +// Every trait in this file expects a non CV-qualified type. +// The only exceptions are in the 'aliases for detected' section +// (i.e. those of the form: decltype(T::member_function(std::declval()))) +// +// In this case, T has to be properly CV-qualified to constraint the function arguments +// (e.g. to_json(BasicJsonType&, const T&)) + +template struct is_basic_json : std::false_type {}; + +NLOHMANN_BASIC_JSON_TPL_DECLARATION +struct is_basic_json : std::true_type {}; + +// used by exceptions create() member functions +// true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t +// false_type otherwise +template +struct is_basic_json_context : + std::integral_constant < bool, + is_basic_json::type>::type>::value + || std::is_same::value > +{}; + +////////////////////// +// json_ref helpers // +////////////////////// + +template +class json_ref; + +template +struct is_json_ref : std::false_type {}; + +template +struct is_json_ref> : std::true_type {}; + +////////////////////////// +// aliases for detected // +////////////////////////// + +template +using mapped_type_t = typename T::mapped_type; + +template +using key_type_t = typename T::key_type; + +template +using value_type_t = typename T::value_type; + +template +using difference_type_t = typename T::difference_type; + +template +using pointer_t = typename T::pointer; + +template +using reference_t = typename T::reference; + +template +using iterator_category_t = typename T::iterator_category; + +template +using to_json_function = decltype(T::to_json(std::declval()...)); + +template +using from_json_function = decltype(T::from_json(std::declval()...)); + +template +using get_template_function = decltype(std::declval().template get()); + +// trait checking if JSONSerializer::from_json(json const&, udt&) exists +template +struct has_from_json : std::false_type {}; + +// trait checking if j.get is valid +// use this trait instead of std::is_constructible or std::is_convertible, +// both rely on, or make use of implicit conversions, and thus fail when T +// has several constructors/operator= (see https://github.com/nlohmann/json/issues/958) +template +struct is_getable +{ + static constexpr bool value = is_detected::value; +}; + +template +struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +// This trait checks if JSONSerializer::from_json(json const&) exists +// this overload is used for non-default-constructible user-defined-types +template +struct has_non_default_from_json : std::false_type {}; + +template +struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +// This trait checks if BasicJsonType::json_serializer::to_json exists +// Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion. +template +struct has_to_json : std::false_type {}; + +template +struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +template +using detect_key_compare = typename T::key_compare; + +template +struct has_key_compare : std::integral_constant::value> {}; + +// obtains the actual object key comparator +template +struct actual_object_comparator +{ + using object_t = typename BasicJsonType::object_t; + using object_comparator_t = typename BasicJsonType::default_object_comparator_t; + using type = typename std::conditional < has_key_compare::value, + typename object_t::key_compare, object_comparator_t>::type; +}; + +template +using actual_object_comparator_t = typename actual_object_comparator::type; + +///////////////// +// char_traits // +///////////////// + +// Primary template of char_traits calls std char_traits +template +struct char_traits : std::char_traits +{}; + +// Explicitly define char traits for unsigned char since it is not standard +template<> +struct char_traits : std::char_traits +{ + using char_type = unsigned char; + using int_type = uint64_t; + + // Redefine to_int_type function + static int_type to_int_type(char_type c) noexcept + { + return static_cast(c); + } + + static char_type to_char_type(int_type i) noexcept + { + return static_cast(i); + } + + static constexpr int_type eof() noexcept + { + return static_cast(EOF); + } +}; + +// Explicitly define char traits for signed char since it is not standard +template<> +struct char_traits : std::char_traits +{ + using char_type = signed char; + using int_type = uint64_t; + + // Redefine to_int_type function + static int_type to_int_type(char_type c) noexcept + { + return static_cast(c); + } + + static char_type to_char_type(int_type i) noexcept + { + return static_cast(i); + } + + static constexpr int_type eof() noexcept + { + return static_cast(EOF); + } +}; + +/////////////////// +// is_ functions // +/////////////////// + +// https://en.cppreference.com/w/cpp/types/conjunction +template struct conjunction : std::true_type { }; +template struct conjunction : B { }; +template +struct conjunction +: std::conditional(B::value), conjunction, B>::type {}; + +// https://en.cppreference.com/w/cpp/types/negation +template struct negation : std::integral_constant < bool, !B::value > { }; + +// Reimplementation of is_constructible and is_default_constructible, due to them being broken for +// std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367). +// This causes compile errors in e.g. clang 3.5 or gcc 4.9. +template +struct is_default_constructible : std::is_default_constructible {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + +template +struct is_constructible : std::is_constructible {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_iterator_traits : std::false_type {}; + +template +struct is_iterator_traits> +{ + private: + using traits = iterator_traits; + + public: + static constexpr auto value = + is_detected::value && + is_detected::value && + is_detected::value && + is_detected::value && + is_detected::value; +}; + +template +struct is_range +{ + private: + using t_ref = typename std::add_lvalue_reference::type; + + using iterator = detected_t; + using sentinel = detected_t; + + // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator + // and https://en.cppreference.com/w/cpp/iterator/sentinel_for + // but reimplementing these would be too much work, as a lot of other concepts are used underneath + static constexpr auto is_iterator_begin = + is_iterator_traits>::value; + + public: + static constexpr bool value = !std::is_same::value && !std::is_same::value && is_iterator_begin; +}; + +template +using iterator_t = enable_if_t::value, result_of_begin())>>; + +template +using range_value_t = value_type_t>>; + +// The following implementation of is_complete_type is taken from +// https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/ +// and is written by Xiang Fan who agreed to using it in this library. + +template +struct is_complete_type : std::false_type {}; + +template +struct is_complete_type : std::true_type {}; + +template +struct is_compatible_object_type_impl : std::false_type {}; + +template +struct is_compatible_object_type_impl < + BasicJsonType, CompatibleObjectType, + enable_if_t < is_detected::value&& + is_detected::value >> +{ + using object_t = typename BasicJsonType::object_t; + + // macOS's is_constructible does not play well with nonesuch... + static constexpr bool value = + is_constructible::value && + is_constructible::value; +}; + +template +struct is_compatible_object_type + : is_compatible_object_type_impl {}; + +template +struct is_constructible_object_type_impl : std::false_type {}; + +template +struct is_constructible_object_type_impl < + BasicJsonType, ConstructibleObjectType, + enable_if_t < is_detected::value&& + is_detected::value >> +{ + using object_t = typename BasicJsonType::object_t; + + static constexpr bool value = + (is_default_constructible::value && + (std::is_move_assignable::value || + std::is_copy_assignable::value) && + (is_constructible::value && + std::is_same < + typename object_t::mapped_type, + typename ConstructibleObjectType::mapped_type >::value)) || + (has_from_json::value || + has_non_default_from_json < + BasicJsonType, + typename ConstructibleObjectType::mapped_type >::value); +}; + +template +struct is_constructible_object_type + : is_constructible_object_type_impl {}; + +template +struct is_compatible_string_type +{ + static constexpr auto value = + is_constructible::value; +}; + +template +struct is_constructible_string_type +{ + // launder type through decltype() to fix compilation failure on ICPC +#ifdef __INTEL_COMPILER + using laundered_type = decltype(std::declval()); +#else + using laundered_type = ConstructibleStringType; +#endif + + static constexpr auto value = + conjunction < + is_constructible, + is_detected_exact>::value; +}; + +template +struct is_compatible_array_type_impl : std::false_type {}; + +template +struct is_compatible_array_type_impl < + BasicJsonType, CompatibleArrayType, + enable_if_t < + is_detected::value&& + is_iterator_traits>>::value&& +// special case for types like std::filesystem::path whose iterator's value_type are themselves +// c.f. https://github.com/nlohmann/json/pull/3073 + !std::is_same>::value >> +{ + static constexpr bool value = + is_constructible>::value; +}; + +template +struct is_compatible_array_type + : is_compatible_array_type_impl {}; + +template +struct is_constructible_array_type_impl : std::false_type {}; + +template +struct is_constructible_array_type_impl < + BasicJsonType, ConstructibleArrayType, + enable_if_t::value >> + : std::true_type {}; + +template +struct is_constructible_array_type_impl < + BasicJsonType, ConstructibleArrayType, + enable_if_t < !std::is_same::value&& + !is_compatible_string_type::value&& + is_default_constructible::value&& +(std::is_move_assignable::value || + std::is_copy_assignable::value)&& +is_detected::value&& +is_iterator_traits>>::value&& +is_detected::value&& +// special case for types like std::filesystem::path whose iterator's value_type are themselves +// c.f. https://github.com/nlohmann/json/pull/3073 +!std::is_same>::value&& + is_complete_type < + detected_t>::value >> +{ + using value_type = range_value_t; + + static constexpr bool value = + std::is_same::value || + has_from_json::value || + has_non_default_from_json < + BasicJsonType, + value_type >::value; +}; + +template +struct is_constructible_array_type + : is_constructible_array_type_impl {}; + +template +struct is_compatible_integer_type_impl : std::false_type {}; + +template +struct is_compatible_integer_type_impl < + RealIntegerType, CompatibleNumberIntegerType, + enable_if_t < std::is_integral::value&& + std::is_integral::value&& + !std::is_same::value >> +{ + // is there an assert somewhere on overflows? + using RealLimits = std::numeric_limits; + using CompatibleLimits = std::numeric_limits; + + static constexpr auto value = + is_constructible::value && + CompatibleLimits::is_integer && + RealLimits::is_signed == CompatibleLimits::is_signed; +}; + +template +struct is_compatible_integer_type + : is_compatible_integer_type_impl {}; + +template +struct is_compatible_type_impl: std::false_type {}; + +template +struct is_compatible_type_impl < + BasicJsonType, CompatibleType, + enable_if_t::value >> +{ + static constexpr bool value = + has_to_json::value; +}; + +template +struct is_compatible_type + : is_compatible_type_impl {}; + +template +struct is_constructible_tuple : std::false_type {}; + +template +struct is_constructible_tuple> : conjunction...> {}; + +template +struct is_json_iterator_of : std::false_type {}; + +template +struct is_json_iterator_of : std::true_type {}; + +template +struct is_json_iterator_of : std::true_type +{}; + +// checks if a given type T is a template specialization of Primary +template