diff --git a/mdio/dataset_test.cc b/mdio/dataset_test.cc index b7b433a..a97f0fb 100644 --- a/mdio/dataset_test.cc +++ b/mdio/dataset_test.cc @@ -583,6 +583,19 @@ TEST(Dataset, selRange) { ASSERT_TRUE(sliceRes.ok()) << sliceRes.status(); } +TEST(Dataset, legacySliceDescriptor) { + std::string path = "zarrs/selTester.mdio"; + auto dsRes = makePopulated(path); + ASSERT_TRUE(dsRes.ok()) << dsRes.status(); + auto ds = dsRes.value(); + + mdio::SliceDescriptor ilRange = {"inline", 2, 5, 1}; + + // ds.sel does not, and will not, support the legacy slice descriptor + auto sliceRes = ds.isel(ilRange); + ASSERT_TRUE(sliceRes.ok()) << sliceRes.status(); +} + TEST(Dataset, selRangeFlippedStartStop) { std::string path = "zarrs/selTester.mdio"; auto dsRes = makePopulated(path); diff --git a/mdio/variable.h b/mdio/variable.h index fcd0e54..34d16b2 100644 --- a/mdio/variable.h +++ b/mdio/variable.h @@ -62,6 +62,24 @@ struct extract_descriptor_Ttype; template struct outer_type; +/** + * @brief A descriptor for slicing a Variable or Dataset. + * @tparam T The type of the range. Default is `Index` for `isel` based slicing. + * @param label The label of the dimension to slice. The recommended is to use a + * label instead of an index. + * @param start The start index or value of the slice. + * @param stop The stop index or value of the slice. + * @param step The step index or value of the slice. Default is 1. + */ +template +struct RangeDescriptor { + using type = T; + DimensionIdentifier label; + T start; + T stop; + Index step = 1; +}; + /** * @brief A descriptor for slicing a Variable. * A struct representing how to slice a Variable or Dataset. @@ -88,24 +106,9 @@ struct SliceDescriptor { Index start; Index stop; Index step; -}; -/** - * @brief A descriptor for slicing a Variable or Dataset. - * @tparam T The type of the range. Default is `Index` for `isel` based slicing. - * @param label The label of the dimension to slice. The recommended is to use a - * label instead of an index. - * @param start The start index or value of the slice. - * @param stop The stop index or value of the slice. - * @param step The step index or value of the slice. Default is 1. - */ -template -struct RangeDescriptor { - using type = T; - DimensionIdentifier label; - T start; - T stop; - Index step = 1; + // Implicit conversion to RangeDescriptor + operator RangeDescriptor() const { return {label, start, stop, step}; } }; /** diff --git a/mdio/variable_test.cc b/mdio/variable_test.cc index 860d748..44f8cd3 100644 --- a/mdio/variable_test.cc +++ b/mdio/variable_test.cc @@ -548,6 +548,28 @@ TEST(Variable, slice) { std::filesystem::remove_all("name"); } +TEST(Variable, legacySliceDescriptor) { + auto variable = + mdio::Variable<>::Open(json_good, mdio::constants::kCreateClean).value(); + + // compile time example + mdio::SliceDescriptor desc1 = {"x", 0, 5, 1}; + mdio::SliceDescriptor desc2 = {"y", 5, 11, 1}; + + auto result = variable.slice(desc2, desc1); + ASSERT_TRUE(result.ok()); + + auto domain = result->dimensions(); + + EXPECT_THAT(domain.labels(), ::testing::ElementsAre("x", "y")); + + EXPECT_THAT(domain.origin(), ::testing::ElementsAre(0, 5)); + + EXPECT_THAT(domain.shape(), ::testing::ElementsAre(5, 6)); + + std::filesystem::remove_all("name"); +} + TEST(Variable, sliceTyped) { auto variable = mdio::Variable::Open( json_good, mdio::constants::kCreateClean)