Metadata-Version: 2.4
Name: xycpplib
Version: 0.1.0
Summary: Run C++ programs from Python through the local C++ compiler
Author: xycpplib contributors
License-Expression: MIT
Project-URL: Homepage, https://github.com/ahigherdesire/xycpplib
Project-URL: Issues, https://github.com/ahigherdesire/xycpplib/issues
Project-URL: Repository, https://github.com/ahigherdesire/xycpplib
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: C++
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# xycpplib

`xycpplib` lets a Python program compile and execute C++ source using the C++
compiler installed on the machine.  It is useful for running competitive
programming solutions, including GCC conveniences such as
`#include <bits/stdc++.h>`, from Python.

It is not a source-to-source C++-to-Python translator: arbitrary C++ cannot be
faithfully translated to Python.  Instead, the original C++ is compiled, so
programs work to the extent that the selected local compiler supports them.

## Install

```powershell
python -m pip install -e .
```

`g++`, `clang++`, or `c++` must be available on `PATH`.  Set `XYCPPLIB_CXX` to
choose another compiler explicitly.

## Use in a Python file

Python cannot contain bare C++ after an `import`: the Python parser rejects it
before the import can run. Put the C++ in a string and call `main` to make the
Python file behave like the C++ executable:

```python
from xycpplib import main

main(r"""
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int d,t; cin>>d>>t;
    int cnt=0;
    vector<int> a(d+1,0);
    vector<int> b(d+1,0);
    while(t--){
        int x,y; cin>>x>>y;
        a[x]--;
        if(a[x]<b[x]) b[x]=a[x];
        a[y]++;
    }
    for(int i:b) cnt+=(0-i);
    cout<<cnt;
}
""")
```

Run it normally and pipe input just as you would with a C++ binary:

```powershell
Get-Content input.txt | python solution.py
```

For code that needs the output as a Python value, use `run`:

```python
import xycpplib

answer = xycpplib.run(CPP_SOURCE, input="3 4\n1 2\n1 2\n2 3\n3 2\n")
assert answer == "2"
```

Executables are cached by source, compiler arguments, and selected compiler in
the user's cache directory. Use `xycpplib.clear_cache()` to remove this
library's cache.

## Create a portable Python launcher

```python
import xycpplib

xycpplib.write_python("solution.py", CPP_SOURCE)
```

The generated file embeds the C++ source and requires `xycpplib` plus a C++
compiler when it is run.

