|
3 | 3 | * @brief Python bindings for the algorithms module
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -#include <pybind11/functional.h> |
| 6 | +#include <cstddef> |
| 7 | +#include <functional> |
| 8 | +#include <ranges> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
7 | 12 | #include <pybind11/pybind11.h>
|
8 | 13 | #include <pybind11/stl.h>
|
9 | 14 |
|
10 | 15 | #include "algorithms/stl.hpp"
|
11 | 16 | #include "containers/container.hpp"
|
12 | 17 |
|
13 | 18 | namespace py = pybind11;
|
| 19 | +using namespace cpp_features::algorithms; |
| 20 | +using cpp_features::containers::Container; |
14 | 21 |
|
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 { |
23 | 23 |
|
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 | +} |
31 | 29 |
|
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 | +} |
35 | 37 |
|
36 |
| - m.def( |
37 |
| - "sort_container", [](std::vector<double> &container) { SortContainer(container); }, |
38 |
| - "Sort a double vector"); |
| 38 | +} // namespace |
39 | 39 |
|
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>>); |
43 | 48 |
|
44 | 49 | // 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>>); |
78 | 56 |
|
79 | 57 | // 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>>); |
114 | 64 |
|
115 | 65 | // 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>>); |
131 | 72 | }
|
0 commit comments