Skip to content

Commit 3c60c95

Browse files
committed
refactor(python): refactor bindings
1 parent 00a1d17 commit 3c60c95

12 files changed

+290
-623
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: 0 additions & 1 deletion
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

binding/algorithms_binding.cpp

Lines changed: 48 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -3,129 +3,70 @@
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>
813
#include <pybind11/stl.h>
914

1015
#include "algorithms/stl.hpp"
1116
#include "containers/container.hpp"
1217

1318
namespace py = pybind11;
19+
using namespace cpp_features::algorithms;
20+
using cpp_features::containers::Container;
1421

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");
22+
namespace {
2323

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

32-
m.def(
33-
"sort_container", [](std::vector<int> &container) { SortContainer(container); },
34-
"Sort an integer vector");
30+
template <std::ranges::input_range Range>
31+
auto TransformToVectorWrapper(
32+
const Range &range,
33+
const std::function<std::ranges::range_value_t<Range>(std::ranges::range_value_t<Range>)>
34+
&transform) {
35+
return TransformToVector(range, transform);
36+
}
3537

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

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

4449
// 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");
50+
m.def("count_if", &CountIfWrapper<Container<int>>);
51+
m.def("count_if", &CountIfWrapper<Container<double>>);
52+
m.def("count_if", &CountIfWrapper<Container<std::string>>);
53+
m.def("count_if", &CountIfWrapper<std::vector<int>>);
54+
m.def("count_if", &CountIfWrapper<std::vector<double>>);
55+
m.def("count_if", &CountIfWrapper<std::vector<std::string>>);
7856

7957
// 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");
58+
m.def("transform", &TransformToVectorWrapper<Container<int>>);
59+
m.def("transform", &TransformToVectorWrapper<Container<double>>);
60+
m.def("transform", &TransformToVectorWrapper<Container<std::string>>);
61+
m.def("transform", &TransformToVectorWrapper<std::vector<int>>);
62+
m.def("transform", &TransformToVectorWrapper<std::vector<double>>);
63+
m.def("transform", &TransformToVectorWrapper<std::vector<std::string>>);
11464

11565
// 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");
66+
m.def("find_min_max", &FindMinMax<Container<int>>);
67+
m.def("find_min_max", &FindMinMax<Container<double>>);
68+
m.def("find_min_max", &FindMinMax<Container<std::string>>);
69+
m.def("find_min_max", &FindMinMax<std::vector<int>>);
70+
m.def("find_min_max", &FindMinMax<std::vector<double>>);
71+
m.def("find_min_max", &FindMinMax<std::vector<std::string>>);
13172
}

0 commit comments

Comments
 (0)