Skip to content

Commit d48f2d6

Browse files
authored
Fix squeeze offset for sliced views (#2933)
Squeezing a sliced view now retains its data offset instead of reading from the start of the underlying tensor. Both squeeze overloads reuse the existing offset helper; expressions without a data interface keep their zero-offset fallback. Fixes #2834. Validation in Docker (Ubuntu 24.04, clang 18.1.3, C++20): the added regression failed before the fix with `{1,2}` instead of `{2,3}`. After the fix, the focused squeeze tests passed in both row-major and column-major configurations (2 test cases, 12 assertions each). The modified header and test passed `clang-format-18 --dry-run --Werror`. Co-authored-by: Tyagiquamar <Tyagiquamar@users.noreply.github.com>
1 parent c9125c9 commit d48f2d6

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎include/xtensor/misc/xmanipulation.hpp‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,13 @@ namespace xt
536536
}
537537
);
538538

539-
return strided_view(std::forward<E>(e), std::move(new_shape), std::move(new_strides), 0, e.layout());
539+
return strided_view(
540+
std::forward<E>(e),
541+
std::move(new_shape),
542+
std::move(new_strides),
543+
detail::get_offset<XTENSOR_DEFAULT_LAYOUT>(e),
544+
e.layout()
545+
);
540546
}
541547

542548
namespace detail
@@ -559,7 +565,13 @@ namespace xt
559565
}
560566
}
561567

562-
return strided_view(std::forward<E>(e), std::move(new_shape), std::move(new_strides), 0, e.layout());
568+
return strided_view(
569+
std::forward<E>(e),
570+
std::move(new_shape),
571+
std::move(new_strides),
572+
detail::get_offset<XTENSOR_DEFAULT_LAYOUT>(e),
573+
e.layout()
574+
);
563575
}
564576

565577
template <class E, class S>

‎test/test_xmanipulation.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ namespace xt
300300
EXPECT_EQ(sq3(2, 2, 0, 1, 0, 2), b(2, 2, 0, 0, 1, 0, 2));
301301
}
302302

303+
TEST(xmanipulation, squeeze_offset)
304+
{
305+
xtensor<int, 2> a = {{1, 2, 3}};
306+
xtensor<int, 1> expected = {2, 3};
307+
308+
EXPECT_EQ(squeeze(view(a, all(), range(1, 3))), expected);
309+
EXPECT_EQ(squeeze(view(a, all(), range(1, 3)), 0, check_policy::full()), expected);
310+
311+
auto expression = view(a, all(), range(1, 3)) + 0;
312+
EXPECT_EQ(squeeze(expression), expected);
313+
EXPECT_EQ(squeeze(expression, 0, check_policy::full()), expected);
314+
}
315+
303316
TEST(xmanipulation, expand_dims)
304317
{
305318
auto b = xt::xarray<double>::from_shape({3, 3});

0 commit comments

Comments
 (0)