Skip to content

Commit c651861

Browse files
committed
[c++/m] Add DataArray getDimensionByIndex
Closes #123
1 parent c326c16 commit c651861

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

+nix/DataArray.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@
7373
dim = nix.RangeDimension(nix_mx(func_name, obj.nix_handle));
7474
end
7575

76+
function dim = open_dimension_idx(obj, idx)
77+
% Getting the dimension by index starts with 1
78+
% instead of 0 compared to all other index functions.
79+
func_name = strcat(obj.alias, '::openDimensionIdx');
80+
ret = nix_mx(func_name, obj.nix_handle, idx);
81+
switch(ret.dimension_type)
82+
case 'set'
83+
dim = nix.SetDimension(ret.handle);
84+
case 'sampled'
85+
dim = nix.SampledDimension(ret.handle);
86+
case 'range'
87+
dim = nix.RangeDimension(ret.handle);
88+
end;
89+
end
90+
7691
function delCheck = delete_dimensions(obj)
7792
func_name = strcat(obj.alias, '::deleteDimensions');
7893
delCheck = nix_mx(func_name, obj.nix_handle);

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ void mexFunction(int nlhs,
247247
methods->add("DataArray::dataType", nixdataarray::dataType);
248248
methods->add("DataArray::setDataExtent", nixdataarray::setDataExtent);
249249
methods->add("DataArray::openSourceIdx", nixdataarray::openSourceIdx);
250+
methods->add("DataArray::openDimensionIdx", nixdataarray::openDimensionIdx);
250251

251252
classdef<nix::Source>("Source", methods)
252253
.desc(&nixsource::describe)

src/nixdataarray.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// LICENSE file in the root of the Project.
88

99
#include "nixdataarray.h"
10+
#include "nixdimensions.h"
1011
#include "mkarray.h"
1112
#include "mex.h"
1213

@@ -142,4 +143,31 @@ namespace nixdataarray {
142143
output.set(0, currObj.getSource(idx));
143144
}
144145

146+
void openDimensionIdx(const extractor &input, infusor &output) {
147+
nix::DataArray currObj = input.entity<nix::DataArray>(1);
148+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
149+
150+
nix::Dimension d = currObj.getDimension(idx);
151+
152+
struct_builder sb({ 1 }, { "dimension_type", "handle" });
153+
154+
switch (d.dimensionType()) {
155+
case nix::DimensionType::Set:
156+
sb.set("set");
157+
sb.set(d.asSetDimension());
158+
break;
159+
case nix::DimensionType::Sample:
160+
sb.set("sampled");
161+
sb.set(d.asSampledDimension());
162+
break;
163+
case nix::DimensionType::Range:
164+
sb.set("range");
165+
sb.set(d.asRangeDimension());
166+
break;
167+
default: throw std::invalid_argument("unkown dimension type");
168+
}
169+
170+
output.set(0, sb.array());
171+
}
172+
145173
} // namespace nixdataarray

src/nixdataarray.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ namespace nixdataarray {
4343

4444
void openSourceIdx(const extractor &input, infusor &output);
4545

46+
void openDimensionIdx(const extractor &input, infusor &output);
47+
4648
} // namespace nixdataarray
4749

4850
#endif

tests/TestDataArray.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
funcs{end+1} = @test_has_source;
2929
funcs{end+1} = @test_source_count;
3030
funcs{end+1} = @test_dimensions;
31+
funcs{end+1} = @test_open_dimension_idx;
3132
funcs{end+1} = @test_dimension_count;
3233
funcs{end+1} = @test_datatype;
3334
funcs{end+1} = @test_set_data_extent;
@@ -510,6 +511,24 @@
510511
assert(isequal(daAliasWa.dataExtent, [3 3]));
511512
end
512513

514+
function [] = test_open_dimension_idx( varargin )
515+
%% Test: Open dimension by index
516+
fileName = fullfile(pwd, 'tests', 'testRW.h5');
517+
f = nix.File(fileName, nix.FileMode.Overwrite);
518+
b = f.create_block('daTestBlock', 'test nixBlock');
519+
da = b.create_data_array('daTest', 'test nixDataArray', nix.DataType.Double, [1 2]);
520+
521+
da.append_set_dimension();
522+
da.append_sampled_dimension(200);
523+
da.append_range_dimension([1, 2, 3, 4]);
524+
525+
% for some weird reason getting the dimension by index starts with 1
526+
% instead of 0 compared to all other index functions.
527+
assert(strcmp(da.open_dimension_idx(1).dimensionType, 'set'));
528+
assert(strcmp(da.open_dimension_idx(2).dimensionType, 'sample'));
529+
assert(strcmp(da.open_dimension_idx(3).dimensionType, 'range'));
530+
end
531+
513532
%% Test: Dimension count
514533
function [] = test_dimension_count( varargin )
515534
testFile = fullfile(pwd, 'tests', 'testRW.h5');

0 commit comments

Comments
 (0)