| 31 | |
| 32 | template <typename ArrowType, enable_if_time<ArrowType> = true> |
| 33 | libmexclass::proxy::MakeResult make_time_array( |
| 34 | const libmexclass::proxy::FunctionArguments& constructor_arguments) { |
| 35 | namespace mda = ::matlab::data; |
| 36 | using namespace arrow::matlab::type; |
| 37 | using MatlabBuffer = arrow::matlab::buffer::MatlabBuffer; |
| 38 | using TimeArray = arrow::NumericArray<ArrowType>; |
| 39 | using TimeArrayProxy = proxy::NumericArray<ArrowType>; |
| 40 | using CType = typename arrow::TypeTraits<ArrowType>::CType; |
| 41 | |
| 42 | mda::StructArray opts = constructor_arguments[0]; |
| 43 | |
| 44 | const mda::TypedArray<CType> time_mda = opts[0]["MatlabArray"]; |
| 45 | const mda::TypedArray<bool> validity_bitmap_mda = opts[0]["Valid"]; |
| 46 | const mda::TypedArray<mda::MATLABString> time_unit_mda = opts[0]["TimeUnit"]; |
| 47 | |
| 48 | // extract the time unit |
| 49 | const std::u16string& time_unit_utf16 = time_unit_mda[0]; |
| 50 | MATLAB_ASSIGN_OR_ERROR(const auto time_unit, timeUnitFromString(time_unit_utf16), |
| 51 | error::UNKNOWN_TIME_UNIT_ERROR_ID); |
| 52 | |
| 53 | MATLAB_ERROR_IF_NOT_OK(validateTimeUnit<ArrowType>(time_unit), |
| 54 | error::INVALID_TIME_UNIT); |
| 55 | |
| 56 | // create the ArrowType |
| 57 | const auto data_type = std::make_shared<ArrowType>(time_unit); |
| 58 | |
| 59 | auto array_length = static_cast<size_t>(time_mda.getNumberOfElements()); |
| 60 | auto data_buffer = std::make_shared<MatlabBuffer>(time_mda); |
| 61 | |
| 62 | // Pack the validity bitmap values. |
| 63 | MATLAB_ASSIGN_OR_ERROR(auto packed_validity_bitmap, bit::packValid(validity_bitmap_mda), |
| 64 | error::BITPACK_VALIDITY_BITMAP_ERROR_ID); |
| 65 | |
| 66 | auto array_data = arrow::ArrayData::Make(data_type, array_length, |
| 67 | {packed_validity_bitmap, data_buffer}); |
| 68 | auto time_array = std::static_pointer_cast<TimeArray>(arrow::MakeArray(array_data)); |
| 69 | return std::make_shared<TimeArrayProxy>(std::move(time_array)); |
| 70 | } |
| 71 | } // namespace arrow::matlab::array::proxy |