Skip to content

Commit baf3b6f

Browse files
committed
refactor(python): refactor bindings
1 parent b658788 commit baf3b6f

13 files changed

+308
-649
lines changed

binding/.clang-tidy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
InheritParentConfig: true
3+
Checks: >
4+
bugprone-*,
5+
-bugprone-exception-escape,
6+
cert-*,
7+
clang-analyzer-*,
8+
google-*,
9+
-google-build-using-namespace,
10+
misc-*,
11+
-misc-const-correctness,
12+
-misc-use-internal-linkage,
13+
modernize-*,
14+
performance-*,
15+
readability-*,
16+
-readability-identifier-length,
17+
-readability-magic-numbers

binding/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pybind11_add_module(cpp_features
1414
algorithms_binding.cpp
1515
containers_binding.cpp
1616
exceptions_binding.cpp
17-
memory_binding.cpp
1817
random_binding.cpp
1918
shapes_binding.cpp
2019
timing_binding.cpp
@@ -46,7 +45,7 @@ set_target_properties(
4645
)
4746

4847
# Compile definitions
49-
target_compile_definitions(cpp_features PRIVATE VERSION_INFO=${PROJECT_VERSION})
48+
target_compile_definitions(cpp_features PRIVATE VERSION_INFO="${PROJECT_VERSION}")
5049

5150
# Install the Python module
5251
install(TARGETS cpp_features COMPONENT python LIBRARY DESTINATION .)

binding/algorithms_binding.cpp

Lines changed: 52 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -3,129 +3,74 @@
33
* @brief Python bindings for the algorithms module
44
*/
55

6-
#include <pybind11/functional.h>
6+
#include <cstddef>
7+
#include <functional>
8+
#include <ranges>
9+
#include <string>
10+
#include <vector>
11+
712
#include <pybind11/pybind11.h>
8-
#include <pybind11/stl.h>
913

1014
#include "algorithms/stl.hpp"
1115
#include "containers/container.hpp"
1216

1317
namespace py = pybind11;
18+
using namespace cpp_features::algorithms;
19+
using cpp_features::containers::Container;
1420

15-
void bind_algorithms(py::module &m) {
16-
using namespace cpp_features::algorithms;
17-
using namespace cpp_features::containers;
18-
19-
// Bind sort functions
20-
m.def(
21-
"sort_container", [](Container<int> &container) { SortContainer(container); },
22-
"Sort an integer container");
21+
namespace {
2322

24-
m.def(
25-
"sort_container", [](Container<double> &container) { SortContainer(container); },
26-
"Sort a double container");
23+
template <std::ranges::input_range Range>
24+
auto CountIfWrapper(const Range &range,
25+
const std::function<bool(std::ranges::range_value_t<Range>)> &predicate) {
26+
return CountIf(range, predicate);
27+
}
2728

28-
m.def(
29-
"sort_container", [](Container<std::string> &container) { SortContainer(container); },
30-
"Sort a string container");
29+
template <std::ranges::input_range Range>
30+
auto TransformToVectorWrapper(
31+
const Range &range,
32+
const std::function<std::ranges::range_value_t<Range>(std::ranges::range_value_t<Range>)>
33+
&transform) {
34+
return TransformToVector(range, transform);
35+
}
3136

32-
m.def(
33-
"sort_container", [](std::vector<int> &container) { SortContainer(container); },
34-
"Sort an integer vector");
37+
template <std::ranges::input_range Range>
38+
auto FindMinMaxWrapper(const Range &range) {
39+
return FindMinMax(range);
40+
}
3541

36-
m.def(
37-
"sort_container", [](std::vector<double> &container) { SortContainer(container); },
38-
"Sort a double vector");
42+
} // namespace
3943

40-
m.def(
41-
"sort_container", [](std::vector<std::string> &container) { SortContainer(container); },
42-
"Sort a string vector");
44+
void BindAlgorithms(py::module &m) {
45+
// Bind sort functions
46+
m.def("sort", &SortContainer<Container<int>>);
47+
m.def("sort", &SortContainer<Container<double>>);
48+
m.def("sort", &SortContainer<Container<std::string>>);
49+
m.def("sort", &SortContainer<std::vector<int>>);
50+
m.def("sort", &SortContainer<std::vector<double>>);
51+
m.def("sort", &SortContainer<std::vector<std::string>>);
4352

4453
// Bind count_if functions
45-
m.def(
46-
"count_if",
47-
[](const std::vector<int> &range, std::function<bool(int)> predicate) {
48-
return CountIf(range, predicate);
49-
},
50-
"Count elements matching predicate in integer vector");
51-
52-
m.def(
53-
"count_if",
54-
[](const std::vector<double> &range, std::function<bool(double)> predicate) {
55-
return CountIf(range, predicate);
56-
},
57-
"Count elements matching predicate in double vector");
58-
59-
m.def(
60-
"count_if",
61-
[](const std::vector<std::string> &range,
62-
std::function<bool(const std::string &)> predicate) { return CountIf(range, predicate); },
63-
"Count elements matching predicate in string vector");
64-
65-
m.def(
66-
"count_if",
67-
[](const Container<int> &range, std::function<bool(int)> predicate) {
68-
return CountIf(range, predicate);
69-
},
70-
"Count elements matching predicate in integer container");
71-
72-
m.def(
73-
"count_if",
74-
[](const Container<double> &range, std::function<bool(double)> predicate) {
75-
return CountIf(range, predicate);
76-
},
77-
"Count elements matching predicate in double container");
54+
m.def("count_if", &CountIfWrapper<Container<int>>);
55+
m.def("count_if", &CountIfWrapper<Container<double>>);
56+
m.def("count_if", &CountIfWrapper<Container<std::string>>);
57+
m.def("count_if", &CountIfWrapper<std::vector<int>>);
58+
m.def("count_if", &CountIfWrapper<std::vector<double>>);
59+
m.def("count_if", &CountIfWrapper<std::vector<std::string>>);
7860

7961
// Bind transform functions
80-
m.def(
81-
"transform_to_vector",
82-
[](const std::vector<int> &range, std::function<int(int)> transform) {
83-
return TransformToVector(range, transform);
84-
},
85-
"Transform integer vector to new vector");
86-
87-
m.def(
88-
"transform_to_vector",
89-
[](const std::vector<double> &range, std::function<double(double)> transform) {
90-
return TransformToVector(range, transform);
91-
},
92-
"Transform double vector to new vector");
93-
94-
m.def(
95-
"transform_to_vector",
96-
[](const std::vector<int> &range, std::function<double(int)> transform) {
97-
return TransformToVector(range, transform);
98-
},
99-
"Transform integer vector to double vector");
100-
101-
m.def(
102-
"transform_to_vector",
103-
[](const Container<int> &range, std::function<int(int)> transform) {
104-
return TransformToVector(range, transform);
105-
},
106-
"Transform integer container to vector");
107-
108-
m.def(
109-
"transform_to_vector",
110-
[](const Container<double> &range, std::function<double(double)> transform) {
111-
return TransformToVector(range, transform);
112-
},
113-
"Transform double container to vector");
62+
m.def("transform", &TransformToVectorWrapper<Container<int>>);
63+
m.def("transform", &TransformToVectorWrapper<Container<double>>);
64+
m.def("transform", &TransformToVectorWrapper<Container<std::string>>);
65+
m.def("transform", &TransformToVectorWrapper<std::vector<int>>);
66+
m.def("transform", &TransformToVectorWrapper<std::vector<double>>);
67+
m.def("transform", &TransformToVectorWrapper<std::vector<std::string>>);
11468

11569
// Bind find_min_max functions
116-
m.def(
117-
"find_min_max", [](const std::vector<int> &range) { return FindMinMax(range); },
118-
"Find minimum and maximum in integer vector");
119-
120-
m.def(
121-
"find_min_max", [](const std::vector<double> &range) { return FindMinMax(range); },
122-
"Find minimum and maximum in double vector");
123-
124-
m.def(
125-
"find_min_max", [](const Container<int> &range) { return FindMinMax(range); },
126-
"Find minimum and maximum in integer container");
127-
128-
m.def(
129-
"find_min_max", [](const Container<double> &range) { return FindMinMax(range); },
130-
"Find minimum and maximum in double container");
70+
m.def("find_min_max", &FindMinMaxWrapper<Container<int>>);
71+
m.def("find_min_max", &FindMinMaxWrapper<Container<double>>);
72+
m.def("find_min_max", &FindMinMaxWrapper<Container<std::string>>);
73+
m.def("find_min_max", &FindMinMaxWrapper<std::vector<int>>);
74+
m.def("find_min_max", &FindMinMaxWrapper<std::vector<double>>);
75+
m.def("find_min_max", &FindMinMaxWrapper<std::vector<std::string>>);
13176
}

0 commit comments

Comments
 (0)