sgilmore10 commented on code in PR #35479: URL: https://github.com/apache/arrow/pull/35479#discussion_r1188711309
########## matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h: ########## @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include "arrow/array.h" +#include "arrow/builder.h" +#include "arrow/type_traits.h" + +#include "arrow/matlab/array/proxy/array.h" + +#include "libmexclass/proxy/Proxy.h" + +namespace arrow::matlab::array::proxy { + +template<typename CType> +class NumericArray : public arrow::matlab::array::proxy::Array { + public: + NumericArray(const libmexclass::proxy::FunctionArguments& constructor_arguments) + : arrow::matlab::array::proxy::Array(constructor_arguments) { + using ArrowType = typename arrow::CTypeTraits<CType>::ArrowType; + using BuilderType = typename arrow::CTypeTraits<CType>::BuilderType; + + // Get the mxArray from constructor arguments + const ::matlab::data::TypedArray<CType> numeric_mda = constructor_arguments[0]; + const ::matlab::data::TypedArray<bool> make_copy = constructor_arguments[1]; + + // Get raw pointer of mxArray + auto it(numeric_mda.cbegin()); + auto dt = it.operator->(); + + const bool make_deep_copy = make_copy[0]; + + if (make_deep_copy) { + BuilderType builder; + auto st = builder.AppendValues(dt, numeric_mda.getNumberOfElements()); + + // TODO: handle error case + if (st.ok()) { + auto maybe_array = builder.Finish(); + if (maybe_array.ok()) { + array = *maybe_array; + } + } + } else { + // Pass pointer to Arrow array constructor that takes a buffer + // Do not make a copy when creating arrow::Buffer + std::shared_ptr<arrow::Buffer> buffer( + new arrow::Buffer(reinterpret_cast<const uint8_t*>(dt), + sizeof(CType) * numeric_mda.getNumberOfElements())); + + // Construct arrow::NumericArray specialization using arrow::Buffer. + // pass in nulls information...we could compute and provide the number of nulls here too + std::shared_ptr<arrow::Array> array_wrapper( + new arrow::NumericArray<ArrowType>(numeric_mda.getNumberOfElements(), buffer, + nullptr, // TODO: fill validity bitmap with data + -1)); Review Comment: Hi @kou, That's a good idea. Just pushed a change to use `ArrayData` and `MakeArary()`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
