Skip to content

Commit 811952f

Browse files
CopilotCrazyDubya
andcommitted
Optimize C++ code generation: Fix function names and tuple assignments
Co-authored-by: CrazyDubya <97849040+CrazyDubya@users.noreply.github.com>
1 parent ddd99c9 commit 811952f

File tree

24 files changed

+1058
-16
lines changed

24 files changed

+1058
-16
lines changed

generated_improved/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(pytocpp_generated)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
8+
9+
# Add pybind11
10+
find_package(pybind11 REQUIRED)
11+
12+
# Add main executable
13+
add_executable(${PROJECT_NAME}
14+
main.cpp
15+
generated.cpp
16+
)
17+
18+
# Add Python module
19+
pybind11_add_module(cpp_impl
20+
wrapper.cpp
21+
generated.cpp
22+
)
23+
24+
target_include_directories(${PROJECT_NAME} PRIVATE
25+
${CMAKE_CURRENT_SOURCE_DIR}
26+
)
27+
28+
target_include_directories(cpp_impl PRIVATE
29+
${CMAKE_CURRENT_SOURCE_DIR}
30+
)

generated_improved/generated.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "generated.hpp"
2+
#include <vector>
3+
#include <map>
4+
#include <set>
5+
#include <tuple>
6+
#include <optional>
7+
#include <variant>
8+
#include <stdexcept>
9+
#include <algorithm>
10+
#include <numeric>
11+
#include <iostream>
12+
#include <string>
13+
#include <sstream>
14+
#include <cmath>
15+
16+
namespace pytocpp {
17+
18+
int function_calculate_fibonacci(int n) {
19+
if (n <= 1) {
20+
return n;
21+
}
22+
int a = 0;
23+
int b = 1;
24+
for (int i = 2; i < (n + 1); i += 1) {
25+
a = b;
26+
b = (a + b);
27+
}
28+
return b;}
29+
30+
void function_main() {
31+
std::vector<int> numbers = std::vector<int>{5, 10, 15};
32+
std::vector<int> results = std::vector<int>{};
33+
for (auto num : numbers) {
34+
auto result = calculate_fibonacci(num);
35+
results.push_back(result);
36+
std::cout << "Fibonacci(" + std::to_string(num) + ") = " + result << std::endl;
37+
}}
38+
39+
} // namespace pytocpp

generated_improved/generated.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
#include <vector>
6+
#include <map>
7+
#include <set>
8+
#include <tuple>
9+
#include <optional>
10+
#include <variant>
11+
#include <stdexcept>
12+
#include <algorithm>
13+
#include <numeric>
14+
#include <cmath>
15+
16+
namespace pytocpp {
17+
18+
int function_calculate_fibonacci(int n);
19+
20+
void function_main();
21+
22+
} // namespace pytocpp

generated_improved/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "generated.hpp"
2+
#include <iostream>
3+
#include <vector>
4+
5+
int main() {
6+
std::cout << "Generated C++ code" << std::endl;
7+
8+
return 0;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Python wrapper for optimized C++ implementations.
3+
This module provides both pure Python and C++ implementations,
4+
allowing you to choose based on your needs.
5+
"""
6+
7+
from typing import List, Dict, Union, Optional, Type, TypeVar, Any
8+
import numpy as np
9+
from . import cpp_impl

generated_improved/setup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name="optimized_numerical",
5+
version="0.1.0",
6+
packages=find_packages(),
7+
install_requires=[
8+
"numpy",
9+
],
10+
author="PyToCpp",
11+
description="Optimized numerical operations using C++",
12+
)

generated_improved/wrapper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/stl.h>
3+
#include "generated.hpp"
4+
5+
namespace py = pybind11;
6+
7+
PYBIND11_MODULE(cpp_impl, m) {
8+
m.doc() = "C++ implementations for optimized numerical operations";
9+
10+
m.def("function_calculate_fibonacci", &pytocpp::function_calculate_fibonacci, "");
11+
m.def("function_main", &pytocpp::function_main, "");
12+
}

generated_optimized/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(pytocpp_generated)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
8+
9+
# Add pybind11
10+
find_package(pybind11 REQUIRED)
11+
12+
# Add main executable
13+
add_executable(${PROJECT_NAME}
14+
main.cpp
15+
generated.cpp
16+
)
17+
18+
# Add Python module
19+
pybind11_add_module(cpp_impl
20+
wrapper.cpp
21+
generated.cpp
22+
)
23+
24+
target_include_directories(${PROJECT_NAME} PRIVATE
25+
${CMAKE_CURRENT_SOURCE_DIR}
26+
)
27+
28+
target_include_directories(cpp_impl PRIVATE
29+
${CMAKE_CURRENT_SOURCE_DIR}
30+
)

generated_optimized/generated.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "generated.hpp"
2+
#include <vector>
3+
#include <map>
4+
#include <set>
5+
#include <tuple>
6+
#include <optional>
7+
#include <variant>
8+
#include <stdexcept>
9+
#include <algorithm>
10+
#include <numeric>
11+
#include <iostream>
12+
#include <string>
13+
#include <sstream>
14+
#include <cmath>
15+
16+
namespace pytocpp {
17+
18+
int calculate_fibonacci(int n) {
19+
if (n <= 1) {
20+
return n;
21+
}
22+
int a = 0;
23+
int b = 1;
24+
for (int i = 2; i < (n + 1); i += 1) {
25+
int temp_1 = (a + b);
26+
a = b;
27+
b = temp_1;
28+
}
29+
return b;}
30+
31+
void main() {
32+
// Create shapes list
33+
std::vector<std::variant<Rectangle, Circle>> shapes = {
34+
Rectangle(5.0, 4.0, "blue"),
35+
Circle(3.0, "red"),
36+
Rectangle(2.5, 3.0, "green")
37+
};
38+
39+
// Calculate total area
40+
double total_area = 0.0;
41+
for (const auto& shape : shapes) {
42+
std::visit([&total_area](auto&& s) {
43+
total_area += s.area();
44+
}, shape);
45+
}
46+
std::cout << "Total area of all shapes: " << total_area << std::endl;
47+
48+
// Get info about each shape
49+
for (const auto& shape : shapes) {
50+
std::map<std::string, std::variant<double, std::string>> info = get_shape_info(shape);
51+
std::cout << "Shape info: [area=" << std::get<double>(info["area"]) << ", description=" << std::get<std::string>(info["description"]) << "]" << std::endl;
52+
}
53+
54+
// Optional shape
55+
std::optional<std::variant<Rectangle, Circle>> optional_shape;
56+
if (total_area > 50) {
57+
optional_shape = Rectangle(1.0, 1.0, "white");
58+
}
59+
60+
if (optional_shape) {
61+
double area = 0.0;
62+
std::visit([&area](auto&& s) {
63+
area = s.area();
64+
}, *optional_shape);
65+
std::cout << "Optional shape area: " << area << std::endl;
66+
}
67+
else {
68+
std::cout << "No optional shape created" << std::endl;
69+
}
70+
}
71+
72+
} // namespace pytocpp

generated_optimized/generated.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
#include <vector>
6+
#include <map>
7+
#include <set>
8+
#include <tuple>
9+
#include <optional>
10+
#include <variant>
11+
#include <stdexcept>
12+
#include <algorithm>
13+
#include <numeric>
14+
#include <cmath>
15+
16+
namespace pytocpp {
17+
18+
int calculate_fibonacci(int n);
19+
20+
void main();
21+
22+
} // namespace pytocpp

0 commit comments

Comments
 (0)