Skip to content

Commit

Permalink
GH-35941: [Dev][MATLAB] Add clang-format configuration to pre-commit (#…
Browse files Browse the repository at this point in the history
…40588)

### Rationale for this change

If we use formatter, we don't need to check style manually.

### What changes are included in this PR?

* Add clang-format configuration to pre-commit
* This uses the same style as C++/Python/R uses

We can format by `pre-commit run -a`.
 
### Are these changes tested?

Yes.

### Are there any user-facing changes?

No.
* GitHub Issue: #35941

Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Kevin Gurney <[email protected]>
  • Loading branch information
kou authored Mar 19, 2024
1 parent 1ee3da0 commit 8ec32aa
Show file tree
Hide file tree
Showing 85 changed files with 3,000 additions and 2,797 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ repos:
name: C/GLib Format
files: >-
^c_glib/
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
name: MATLAB (C++) Format
files: >-
^matlab/src/cpp/
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.13
hooks:
Expand Down
2 changes: 1 addition & 1 deletion matlab/src/cpp/arrow/matlab/api/visibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
#else
#define ARROW_MATLAB_EXPORT __declspec(dllimport)
#endif
#else // Not Windows
#else // Not Windows
#define ARROW_MATLAB_EXPORT __attribute__((visibility("default")))
#endif
266 changes: 133 additions & 133 deletions matlab/src/cpp/arrow/matlab/array/proxy/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,150 +32,150 @@

namespace arrow::matlab::array::proxy {

Array::Array(std::shared_ptr<arrow::Array> array) : array{std::move(array)} {

// Register Proxy methods.
REGISTER_METHOD(Array, toString);
REGISTER_METHOD(Array, getNumElements);
REGISTER_METHOD(Array, getValid);
REGISTER_METHOD(Array, getType);
REGISTER_METHOD(Array, isEqual);
REGISTER_METHOD(Array, slice);
}

std::shared_ptr<arrow::Array> Array::unwrap() {
return array;
}

void Array::toString(libmexclass::proxy::method::Context& context) {
::matlab::data::ArrayFactory factory;

auto opts = arrow::PrettyPrintOptions::Defaults();
opts.window = 3;
opts.indent = 4;
opts.indent_size = 4;

const auto type_id = array->type()->id();
if (arrow::is_primitive(type_id) || arrow::is_string(type_id)) {
/*
* Display primitive and string types horizontally without
* opening and closing delimiters. Use " | " as the delimiter
* between elements. Below is an example Int32Array display:
*
* 1 | 2 | 3 | ... | 6 | 7 | 8
*/
opts.skip_new_lines = true;
opts.array_delimiters.open = "";
opts.array_delimiters.close = "";
opts.array_delimiters.element = " | ";
}

std::stringstream ss;
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(arrow::PrettyPrint(*array, opts, &ss), context, error::ARRAY_PRETTY_PRINT_FAILED);

const auto str_utf8 = opts.skip_new_lines ? " " + ss.str() : ss.str();

MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(const auto str_utf16, arrow::util::UTF8StringToUTF16(str_utf8), context, error::UNICODE_CONVERSION_ERROR_ID);
auto str_mda = factory.createScalar(str_utf16);
context.outputs[0] = str_mda;
}

void Array::getNumElements(libmexclass::proxy::method::Context& context) {
::matlab::data::ArrayFactory factory;
auto length_mda = factory.createScalar(array->length());
context.outputs[0] = length_mda;
}

void Array::getValid(libmexclass::proxy::method::Context& context) {
auto array_length = static_cast<size_t>(array->length());

// If the Arrow array has no null values, then return a MATLAB
// logical array that is all "true" for the validity bitmap.
if (array->null_count() == 0) {
::matlab::data::ArrayFactory factory;
auto validity_buffer = factory.createBuffer<bool>(array_length);
auto validity_buffer_ptr = validity_buffer.get();
std::fill(validity_buffer_ptr, validity_buffer_ptr + array_length, true);
auto valid_elements_mda = factory.createArrayFromBuffer<bool>({array_length, 1}, std::move(validity_buffer));
context.outputs[0] = valid_elements_mda;
return;
}

auto validity_bitmap = array->null_bitmap();
auto valid_elements_mda = bit::unpack(validity_bitmap, array_length, array->offset());
context.outputs[0] = valid_elements_mda;
}

void Array::getType(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
Array::Array(std::shared_ptr<arrow::Array> array) : array{std::move(array)} {
// Register Proxy methods.
REGISTER_METHOD(Array, toString);
REGISTER_METHOD(Array, getNumElements);
REGISTER_METHOD(Array, getValid);
REGISTER_METHOD(Array, getType);
REGISTER_METHOD(Array, isEqual);
REGISTER_METHOD(Array, slice);
}

mda::ArrayFactory factory;
std::shared_ptr<arrow::Array> Array::unwrap() { return array; }

void Array::toString(libmexclass::proxy::method::Context& context) {
::matlab::data::ArrayFactory factory;

auto opts = arrow::PrettyPrintOptions::Defaults();
opts.window = 3;
opts.indent = 4;
opts.indent_size = 4;

const auto type_id = array->type()->id();
if (arrow::is_primitive(type_id) || arrow::is_string(type_id)) {
/*
* Display primitive and string types horizontally without
* opening and closing delimiters. Use " | " as the delimiter
* between elements. Below is an example Int32Array display:
*
* 1 | 2 | 3 | ... | 6 | 7 | 8
*/
opts.skip_new_lines = true;
opts.array_delimiters.open = "";
opts.array_delimiters.close = "";
opts.array_delimiters.element = " | ";
}

std::stringstream ss;
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(arrow::PrettyPrint(*array, opts, &ss), context,
error::ARRAY_PRETTY_PRINT_FAILED);

const auto str_utf8 = opts.skip_new_lines ? " " + ss.str() : ss.str();

MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(const auto str_utf16,
arrow::util::UTF8StringToUTF16(str_utf8), context,
error::UNICODE_CONVERSION_ERROR_ID);
auto str_mda = factory.createScalar(str_utf16);
context.outputs[0] = str_mda;
}

MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto type_proxy,
type::proxy::wrap(array->type()),
context,
error::ARRAY_FAILED_TO_CREATE_TYPE_PROXY);
void Array::getNumElements(libmexclass::proxy::method::Context& context) {
::matlab::data::ArrayFactory factory;
auto length_mda = factory.createScalar(array->length());
context.outputs[0] = length_mda;
}

const auto type_id = static_cast<int32_t>(type_proxy->unwrap()->id());
const auto proxy_id = libmexclass::proxy::ProxyManager::manageProxy(type_proxy);
void Array::getValid(libmexclass::proxy::method::Context& context) {
auto array_length = static_cast<size_t>(array->length());

// If the Arrow array has no null values, then return a MATLAB
// logical array that is all "true" for the validity bitmap.
if (array->null_count() == 0) {
::matlab::data::ArrayFactory factory;
auto validity_buffer = factory.createBuffer<bool>(array_length);
auto validity_buffer_ptr = validity_buffer.get();
std::fill(validity_buffer_ptr, validity_buffer_ptr + array_length, true);
auto valid_elements_mda = factory.createArrayFromBuffer<bool>(
{array_length, 1}, std::move(validity_buffer));
context.outputs[0] = valid_elements_mda;
return;
}

auto validity_bitmap = array->null_bitmap();
auto valid_elements_mda = bit::unpack(validity_bitmap, array_length, array->offset());
context.outputs[0] = valid_elements_mda;
}

mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", "TypeID"});
output[0]["ProxyID"] = factory.createScalar(proxy_id);
output[0]["TypeID"] = factory.createScalar(type_id);
context.outputs[0] = output;
}
void Array::getType(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;

void Array::isEqual(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;

const mda::TypedArray<uint64_t> array_proxy_ids = context.inputs[0];

bool is_equal = true;
const auto equals_options = arrow::EqualOptions::Defaults();
for (const auto& array_proxy_id : array_proxy_ids) {
// Retrieve the Array proxy from the ProxyManager
auto proxy = libmexclass::proxy::ProxyManager::getProxy(array_proxy_id);
auto array_proxy = std::static_pointer_cast<proxy::Array>(proxy);
auto array_to_compare = array_proxy->unwrap();

if (!array->Equals(array_to_compare, equals_options)) {
is_equal = false;
break;
}
}
mda::ArrayFactory factory;
context.outputs[0] = factory.createScalar(is_equal);
}
mda::ArrayFactory factory;

void Array::slice(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto type_proxy, type::proxy::wrap(array->type()),
context, error::ARRAY_FAILED_TO_CREATE_TYPE_PROXY);

mda::StructArray opts = context.inputs[0];
const mda::TypedArray<int64_t> offset_mda = opts[0]["Offset"];
const mda::TypedArray<int64_t> length_mda = opts[0]["Length"];
const auto type_id = static_cast<int32_t>(type_proxy->unwrap()->id());
const auto proxy_id = libmexclass::proxy::ProxyManager::manageProxy(type_proxy);

const auto matlab_offset = int64_t(offset_mda[0]);
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(arrow::matlab::index::validateSliceOffset(matlab_offset),
context, error::ARRAY_SLICE_NON_POSITIVE_OFFSET);
mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", "TypeID"});
output[0]["ProxyID"] = factory.createScalar(proxy_id);
output[0]["TypeID"] = factory.createScalar(type_id);
context.outputs[0] = output;
}

// Note: MATLAB uses 1-based indexing, so subtract 1.
const int64_t offset = matlab_offset - 1;
const int64_t length = int64_t(length_mda[0]);
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(arrow::matlab::index::validateSliceLength(length),
context, error::ARRAY_SLICE_NEGATIVE_LENGTH);
void Array::isEqual(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;

auto sliced_array = array->Slice(offset, length);
const auto type_id = static_cast<int32_t>(sliced_array->type_id());
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto sliced_array_proxy,
array::proxy::wrap(sliced_array),
context, error::ARRAY_SLICE_FAILED_TO_CREATE_ARRAY_PROXY);
const mda::TypedArray<uint64_t> array_proxy_ids = context.inputs[0];

const auto proxy_id = libmexclass::proxy::ProxyManager::manageProxy(sliced_array_proxy);
bool is_equal = true;
const auto equals_options = arrow::EqualOptions::Defaults();
for (const auto& array_proxy_id : array_proxy_ids) {
// Retrieve the Array proxy from the ProxyManager
auto proxy = libmexclass::proxy::ProxyManager::getProxy(array_proxy_id);
auto array_proxy = std::static_pointer_cast<proxy::Array>(proxy);
auto array_to_compare = array_proxy->unwrap();

mda::ArrayFactory factory;
mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", "TypeID"});
output[0]["ProxyID"] = factory.createScalar(proxy_id);
output[0]["TypeID"] = factory.createScalar(type_id);
context.outputs[0] = output;
if (!array->Equals(array_to_compare, equals_options)) {
is_equal = false;
break;
}
}
mda::ArrayFactory factory;
context.outputs[0] = factory.createScalar(is_equal);
}

void Array::slice(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;

mda::StructArray opts = context.inputs[0];
const mda::TypedArray<int64_t> offset_mda = opts[0]["Offset"];
const mda::TypedArray<int64_t> length_mda = opts[0]["Length"];

const auto matlab_offset = int64_t(offset_mda[0]);
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(
arrow::matlab::index::validateSliceOffset(matlab_offset), context,
error::ARRAY_SLICE_NON_POSITIVE_OFFSET);

// Note: MATLAB uses 1-based indexing, so subtract 1.
const int64_t offset = matlab_offset - 1;
const int64_t length = int64_t(length_mda[0]);
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(arrow::matlab::index::validateSliceLength(length),
context, error::ARRAY_SLICE_NEGATIVE_LENGTH);

auto sliced_array = array->Slice(offset, length);
const auto type_id = static_cast<int32_t>(sliced_array->type_id());
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto sliced_array_proxy,
array::proxy::wrap(sliced_array), context,
error::ARRAY_SLICE_FAILED_TO_CREATE_ARRAY_PROXY);

const auto proxy_id = libmexclass::proxy::ProxyManager::manageProxy(sliced_array_proxy);

mda::ArrayFactory factory;
mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", "TypeID"});
output[0]["ProxyID"] = factory.createScalar(proxy_id);
output[0]["TypeID"] = factory.createScalar(type_id);
context.outputs[0] = output;
}
} // namespace arrow::matlab::array::proxy
27 changes: 13 additions & 14 deletions matlab/src/cpp/arrow/matlab/array/proxy/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,27 @@
namespace arrow::matlab::array::proxy {

class Array : public libmexclass::proxy::Proxy {
public:
Array(std::shared_ptr<arrow::Array> array);

virtual ~Array() {}
public:
Array(std::shared_ptr<arrow::Array> array);

std::shared_ptr<arrow::Array> unwrap();
virtual ~Array() {}

protected:
std::shared_ptr<arrow::Array> unwrap();

void toString(libmexclass::proxy::method::Context& context);
protected:
void toString(libmexclass::proxy::method::Context& context);

void getNumElements(libmexclass::proxy::method::Context& context);
void getNumElements(libmexclass::proxy::method::Context& context);

void getValid(libmexclass::proxy::method::Context& context);
void getValid(libmexclass::proxy::method::Context& context);

void getType(libmexclass::proxy::method::Context& context);
void getType(libmexclass::proxy::method::Context& context);

void isEqual(libmexclass::proxy::method::Context& context);
void isEqual(libmexclass::proxy::method::Context& context);

void slice(libmexclass::proxy::method::Context& context);
void slice(libmexclass::proxy::method::Context& context);

std::shared_ptr<arrow::Array> array;
std::shared_ptr<arrow::Array> array;
};

}
} // namespace arrow::matlab::array::proxy
Loading

0 comments on commit 8ec32aa

Please sign in to comment.