File indexing completed on 2025-01-18 10:17:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <pybind11/numpy.h>
0011 #include <pybind11/stl_bind.h>
0012
0013 #include "pybind11_tests.h"
0014
0015 #include <deque>
0016 #include <map>
0017 #include <unordered_map>
0018
0019 class El {
0020 public:
0021 El() = delete;
0022 explicit El(int v) : a(v) {}
0023
0024 int a;
0025 };
0026
0027 std::ostream &operator<<(std::ostream &s, El const &v) {
0028 s << "El{" << v.a << '}';
0029 return s;
0030 }
0031
0032
0033 class E_nc {
0034 public:
0035 explicit E_nc(int i) : value{i} {}
0036 E_nc(const E_nc &) = delete;
0037 E_nc &operator=(const E_nc &) = delete;
0038 E_nc(E_nc &&) = default;
0039 E_nc &operator=(E_nc &&) = default;
0040
0041 int value;
0042 };
0043
0044 template <class Container>
0045 Container *one_to_n(int n) {
0046 auto *v = new Container();
0047 for (int i = 1; i <= n; i++) {
0048 v->emplace_back(i);
0049 }
0050 return v;
0051 }
0052
0053 template <class Map>
0054 Map *times_ten(int n) {
0055 auto *m = new Map();
0056 for (int i = 1; i <= n; i++) {
0057 m->emplace(int(i), E_nc(10 * i));
0058 }
0059 return m;
0060 }
0061
0062 template <class NestMap>
0063 NestMap *times_hundred(int n) {
0064 auto *m = new NestMap();
0065 for (int i = 1; i <= n; i++) {
0066 for (int j = 1; j <= n; j++) {
0067 (*m)[i].emplace(int(j * 10), E_nc(100 * j));
0068 }
0069 }
0070 return m;
0071 }
0072
0073 TEST_SUBMODULE(stl_binders, m) {
0074
0075 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
0076
0077
0078 py::class_<El>(m, "El").def(py::init<int>());
0079 py::bind_vector<std::vector<El>>(m, "VectorEl");
0080 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
0081
0082
0083 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
0084 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
0085
0086
0087 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
0088 py::bind_map<std::unordered_map<std::string, double const>>(m,
0089 "UnorderedMapStringDoubleConst");
0090
0091 py::class_<E_nc>(m, "ENC").def(py::init<int>()).def_readwrite("value", &E_nc::value);
0092
0093
0094 py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
0095 m.def("get_vnc", &one_to_n<std::vector<E_nc>>);
0096 py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
0097 m.def("get_dnc", &one_to_n<std::deque<E_nc>>);
0098 py::bind_map<std::map<int, E_nc>>(m, "MapENC");
0099 m.def("get_mnc", ×_ten<std::map<int, E_nc>>);
0100 py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
0101 m.def("get_umnc", ×_ten<std::unordered_map<int, E_nc>>);
0102
0103 py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
0104 m.def("get_nvnc", [](int n) {
0105 auto *m = new std::map<int, std::vector<E_nc>>();
0106 for (int i = 1; i <= n; i++) {
0107 for (int j = 1; j <= n; j++) {
0108 (*m)[i].emplace_back(j);
0109 }
0110 }
0111 return m;
0112 });
0113 py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
0114 m.def("get_nmnc", ×_hundred<std::map<int, std::map<int, E_nc>>>);
0115 py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
0116 m.def("get_numnc", ×_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>);
0117
0118
0119 py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
0120
0121 struct VUndeclStruct {
0122 bool w;
0123 uint32_t x;
0124 double y;
0125 bool z;
0126 };
0127 m.def("create_undeclstruct", [m]() mutable {
0128 py::bind_vector<std::vector<VUndeclStruct>>(
0129 m, "VectorUndeclStruct", py::buffer_protocol());
0130 });
0131
0132
0133 try {
0134 py::module_::import("numpy");
0135 } catch (...) {
0136 return;
0137 }
0138
0139
0140 struct VStruct {
0141 bool w;
0142 uint32_t x;
0143 double y;
0144 bool z;
0145 };
0146 PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
0147 py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
0148 py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
0149 m.def("get_vectorstruct", [] {
0150 return std::vector<VStruct>{{false, 5, 3.0, true}, {true, 30, -1e4, false}};
0151 });
0152 }