| 126 | |
| 127 | template <typename ValueType, typename ValueCastFn> |
| 128 | std::pair<std::string, std::string> serialize_sparse_vector( |
| 129 | const py::dict &sparse_dict, ValueCastFn &&value_caster) { |
| 130 | const size_t n = sparse_dict.size(); |
| 131 | if (n == 0) return {{}, {}}; |
| 132 | |
| 133 | std::string indices_buf; |
| 134 | indices_buf.resize(n * sizeof(uint32_t)); |
| 135 | auto *indices_ptr = reinterpret_cast<uint32_t *>(indices_buf.data()); |
| 136 | |
| 137 | std::string values_buf; |
| 138 | values_buf.resize(n * sizeof(ValueType)); |
| 139 | auto *values_ptr = reinterpret_cast<ValueType *>(values_buf.data()); |
| 140 | |
| 141 | size_t i = 0; |
| 142 | for (const auto &[py_key, py_val] : sparse_dict) { |
| 143 | indices_ptr[i] = checked_cast<uint32_t>(py_key, "Sparse indices", "UINT32"); |
| 144 | values_ptr[i] = value_caster(py_val, i); |
| 145 | ++i; |
| 146 | } |
| 147 | return {std::move(indices_buf), std::move(values_buf)}; |
| 148 | } |
| 149 | |
| 150 | void ZVecPyParams::Initialize(pybind11::module_ &parent) { |
| 151 | auto m = |