Skip to content

Commit 021afca

Browse files
committed
fix write array error
1 parent 8f56e6d commit 021afca

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/nixdataarray.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,18 @@ namespace nixdataarray {
5252
nix::DataArray da = input.entity<nix::DataArray>(1);
5353
nix::DataType dtype = input.dtype(2);
5454

55-
nix::NDSize count = input.ndsize(2);
56-
nix::NDSize offset(0);
55+
const mxArray *arr = input.get_mx_array(2);
56+
57+
mwSize ndims = mxGetNumberOfDimensions(arr);
58+
const mwSize *dims = mxGetDimensions(arr);
59+
60+
nix::NDSize count(ndims);
61+
62+
for (mwSize i = 0; i < ndims; i++) {
63+
count[(ndims - i) - 1] = static_cast<nix::ndsize_t>(dims[i]);
64+
}
65+
66+
nix::NDSize offset(count.size(), 0);
5767
double *ptr = input.get_raw(2);
5868

5969
if (dtype == nix::DataType::String) {

src/utils/arguments.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class argument_helper {
1818

1919
bool check_size(size_t pos, bool fatal = false) const {
2020
bool res = pos + 1 > number;
21-
if (!res && fatal) {
22-
throw std::out_of_range("argument position is out of bounds");
23-
}
21+
if (!res && fatal) {
22+
throw std::out_of_range("argument position is out of bounds");
23+
}
2424
return res;
2525
}
2626

@@ -49,7 +49,7 @@ class argument_helper {
4949
size_t number;
5050
};
5151

52-
class extractor : public argument_helper<const mxArray> {
52+
class extractor : public argument_helper < const mxArray > {
5353
public:
5454
extractor(const mxArray **arr, int n) : argument_helper(arr, n) { }
5555

@@ -76,7 +76,7 @@ class extractor : public argument_helper<const mxArray> {
7676
nix::NDSize ndsize(size_t pos) const {
7777
return mx_to_ndsize(array[pos]);
7878
}
79-
79+
8080
nix::DataType dtype(size_t pos) const {
8181
return dtype_mex2nix(array[pos]);
8282
}
@@ -97,7 +97,7 @@ class extractor : public argument_helper<const mxArray> {
9797
}
9898

9999
handle hdl(size_t pos) const {
100-
handle h = handle(num<uint64_t>(pos));
100+
handle h = handle(num<uint64_t>(pos));
101101
return h;
102102
}
103103

@@ -114,24 +114,28 @@ class extractor : public argument_helper<const mxArray> {
114114
return mxGetPr(array[pos]);
115115
}
116116

117+
const mxArray *get_mx_array(size_t pos) const {
118+
return array[pos];
119+
}
120+
117121
private:
118122
};
119123

120124
template<>
121125
inline std::vector<std::string> extractor::vec(size_t pos) const {
122-
return mx_to_strings(array[pos]);
126+
return mx_to_strings(array[pos]);
123127
}
124128

125129

126130
class infusor : public argument_helper<mxArray> {
127131
public:
128132
infusor(mxArray **arr, int n) : argument_helper(arr, n) { }
129133

130-
template<typename T>
131-
void set(size_t pos, T &&value) {
132-
mxArray *array = make_mx_array(std::forward<T>(value));
133-
set(pos, array);
134-
}
134+
template<typename T>
135+
void set(size_t pos, T &&value) {
136+
mxArray *array = make_mx_array(std::forward<T>(value));
137+
set(pos, array);
138+
}
135139

136140
void set(size_t pos, mxArray *arr) {
137141
array[pos] = arr;

0 commit comments

Comments
 (0)