| 35 | } |
| 36 | |
| 37 | libmexclass::proxy::MakeResult StructArray::make( |
| 38 | const libmexclass::proxy::FunctionArguments& constructor_arguments) { |
| 39 | namespace mda = ::matlab::data; |
| 40 | using libmexclass::proxy::ProxyManager; |
| 41 | |
| 42 | mda::StructArray opts = constructor_arguments[0]; |
| 43 | const mda::TypedArray<uint64_t> arrow_array_proxy_ids = opts[0]["ArrayProxyIDs"]; |
| 44 | const mda::StringArray field_names_mda = opts[0]["FieldNames"]; |
| 45 | const mda::TypedArray<bool> validity_bitmap_mda = opts[0]["Valid"]; |
| 46 | |
| 47 | std::vector<std::shared_ptr<arrow::Array>> arrow_arrays; |
| 48 | arrow_arrays.reserve(arrow_array_proxy_ids.getNumberOfElements()); |
| 49 | |
| 50 | // Retrieve all of the Arrow Array Proxy instances from the libmexclass ProxyManager. |
| 51 | for (const auto& arrow_array_proxy_id : arrow_array_proxy_ids) { |
| 52 | auto proxy = ProxyManager::getProxy(arrow_array_proxy_id); |
| 53 | auto arrow_array_proxy = std::static_pointer_cast<proxy::Array>(proxy); |
| 54 | auto arrow_array = arrow_array_proxy->unwrap(); |
| 55 | arrow_arrays.push_back(arrow_array); |
| 56 | } |
| 57 | |
| 58 | // Convert the utf-16 encoded field names into utf-8 encoded strings |
| 59 | std::vector<std::string> field_names; |
| 60 | field_names.reserve(field_names_mda.getNumberOfElements()); |
| 61 | for (const auto& field_name : field_names_mda) { |
| 62 | const auto field_name_utf16 = std::u16string(field_name); |
| 63 | MATLAB_ASSIGN_OR_ERROR(const auto field_name_utf8, |
| 64 | arrow::util::UTF16StringToUTF8(field_name_utf16), |
| 65 | error::UNICODE_CONVERSION_ERROR_ID); |
| 66 | field_names.push_back(field_name_utf8); |
| 67 | } |
| 68 | |
| 69 | // Pack the validity bitmap values. |
| 70 | MATLAB_ASSIGN_OR_ERROR(auto validity_bitmap_buffer, bit::packValid(validity_bitmap_mda), |
| 71 | error::BITPACK_VALIDITY_BITMAP_ERROR_ID); |
| 72 | |
| 73 | // Create the StructArray |
| 74 | MATLAB_ASSIGN_OR_ERROR( |
| 75 | auto array, |
| 76 | arrow::StructArray::Make(arrow_arrays, field_names, validity_bitmap_buffer), |
| 77 | error::STRUCT_ARRAY_MAKE_FAILED); |
| 78 | |
| 79 | // Construct the StructArray Proxy |
| 80 | auto struct_array = std::static_pointer_cast<arrow::StructArray>(array); |
| 81 | return std::make_shared<proxy::StructArray>(std::move(struct_array)); |
| 82 | } |
| 83 | |
| 84 | void StructArray::getNumFields(libmexclass::proxy::method::Context& context) { |
| 85 | namespace mda = ::matlab::data; |