# passing nested std::vector print('Test 1') src = r''' #include <iostream> #include <numeric> #include <vector> std::vector<std::vector<int>> two_dim_vector_test(std::vector<std::vector<int>> v) { for (auto &r : v) { std::partial_sum(std::begin(r), std::end(r), std::begin(r)); } return v; }''' two_dim_vector_test = cpp.generate('two_dim_vector_test', 'std::vector<std::vector<int>>(std::vector<std::vector<int>>)', src, config=config) print(two_dim_vector_test([[0, 1, 2], [3, 4, 5], [6, 7, 8]])) print('') # passing std::vector dested 3 deep print('Test 2') src = r''' #include <iostream> #include <numeric> #include <vector> std::vector<std::vector<std::vector<int>>> three_dim_vector_test(std::vector<std::vector<std::vector<int>>> v) { for (auto &r : v) { for (auto &c : r)
# passing and returning a std::stack print('Test 1') src = r''' #include <iostream> #include <numeric> #include <stack> std::stack<int> stack_test(std::stack<int> v) { v.pop(); v.pop(); v.push(42); return v; }''' stack_test = cpp.generate('stack_test', 'std::stack<int>(std::stack<int>)', src, config=config) print(stack_test([1, 2, 3, 4])) print('') # passing and returning a std::queue print('Test 2') src = r''' #include <iostream> #include <numeric> #include <queue> std::queue<int> queue_test(std::queue<int> v) { v.pop(); v.pop(); v.push(42);
import sys import copperhead as cpp extra_compile_args = "'/std:c++14'" if sys.version.split('[')[1].startswith( 'MSC') else "'-std=c++14'" config = {'extra_compile_args': extra_compile_args} # passing and returning a std::map print('Test 1') src = r''' #include <iostream> #include <map> #include <string> std::map<std::string, int> map_test(std::map<std::string, int> m) { m["foo2"] = 10; m["bar2"] = 20; return m; }''' map_test = cpp.generate( 'map_test', 'std::map<std::string, int>(std::map<std::string, int>)', src, config=config) print(map_test({'foo': 1, 'bar': 2})) print('')
const int max_iter {100}; int i {0}; std::complex<double> z {0.0, 0.0}; while (std::abs(z) < 2.0 && i < max_iter) { z = z * z + c; ++i; } return max_iter - i; } void compute(std::string filename, double x, double y, double h) { const auto n {std::lround(2.5 / h)}; std::ofstream f(filename); for (long yidx {0}; yidx < n; ++yidx) { for (long xidx {0}; xidx < n; ++xidx) { f << mandelbrot(std::complex<double>(x, y)) << " "; x += h; } f << "\n"; y -= h; x -= 2.5; } }''' compute = cpp.generate('compute', 'void(std::string, double, double, double)', mandelbrot_cpp, config=config) compute('fractal.dat', -2.0, 1.25, 0.005)
import sys import copperhead as cpp extra_compile_args = "'/std:c++14'" if sys.version.split('[')[1].startswith( 'MSC') else "'-std=c++14'" config = {'extra_compile_args': extra_compile_args} # testing passing strings print('Test 1') src = ''' #include <iostream> std::string greet(std::string name) { std::string str = "Hello " + name; return str; }''' greet = cpp.generate('greet', 'std::string(std::string)', src, config=config) print(greet('bob')) print('')
import sys import copperhead as cpp extra_compile_args = "'/std:c++14'" if sys.version.split('[')[1].startswith( 'MSC') else "'-std=c++14'" config = {'extra_compile_args': extra_compile_args} # simple output print('Test 1') src = ''' #include <iostream> void hello_world() { std::cout << "Hello World!" << std::endl; }''' hello_world = cpp.generate('hello_world', 'void()', src, config=config) hello_world() print('') # passing and returning primitives print('Test 2') src = ''' int fibonacci(int i) { int f = 1; while (i > 1) { f *= (i--); } return f; }'''
src = r''' #include <iostream> #include <numeric> #include <vector> std::vector<int> vector_test(std::vector<int> v) { std::partial_sum(std::begin(v), std::end(v), std::begin(v)); for (auto i : v) { std::cout << i << " "; } std::cout << std::endl; return v; }''' vector_test = cpp.generate('vector_test', 'std::vector<int>(std::vector<int>)', src, config=config) vector_test([1, 2, 3, 4]) print('') # passing and returning a std::deque print('Test 2') src = r''' #include <deque> #include <iostream> #include <numeric> std::deque<int> deque_test(std::deque<int> v) { std::partial_sum(std::begin(v), std::end(v), std::begin(v)); for (auto i : v) {
import os import copperhead as cpp this_dir = os.path.dirname(__file__) source_file = os.path.join(this_dir, 'file_imports', 'hello_world.cpp') hello_world = cpp.generate('hello_world', 'void()', block_file=source_file) hello_world()