From 9e23fbe01787d164605ee2ad40d4cda6e0a3e81a Mon Sep 17 00:00:00 2001 From: vera-rubin <322284620+vera-rubin@users.noreply.github.com> Date: Sat, 17 Oct 2026 12:20:52 +0000 Subject: [PATCH] Fix argsort for views with nonzero data offsets --- include/xtensor/misc/xsort.hpp | 2 +- test/test_xsort.cpp | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/xtensor/misc/xsort.hpp b/include/xtensor/misc/xsort.hpp index 196ec45cd..1de56f240 100644 --- a/include/xtensor/misc/xsort.hpp +++ b/include/xtensor/misc/xsort.hpp @@ -101,7 +101,7 @@ namespace xt const auto begin1 = e1.data(); const auto end1 = begin1 + n_iters * secondary_stride1; - const auto begin2 = e2.data(); + const auto begin2 = e2.data() + e2.data_offset(); const auto end2 = begin2 + n_iters * secondary_stride2; auto iter1 = begin1; auto iter2 = begin2; diff --git a/test/test_xsort.cpp b/test/test_xsort.cpp index bbe928d4d..7a53918f3 100644 --- a/test/test_xsort.cpp +++ b/test/test_xsort.cpp @@ -17,6 +17,7 @@ #include "xtensor/io/xio.hpp" #include "xtensor/misc/xsort.hpp" #include "xtensor/views/xslice.hpp" +#include "xtensor/views/xstrided_view.hpp" #include "xtensor/views/xview.hpp" #include "test_common_macros.hpp" @@ -70,6 +71,27 @@ namespace xt } } + TEST(xsort, argsort_view_offset) + { + xarray a = {{1, 2, 3}, {3, 2, 1}}; + auto row = view(a, range(1, 2), all()); + auto strided_row = strided_view(a, {range(1, 2), all()}); + xarray expected_row = {{2, 1, 0}}; + + xarray b = {{1, 3}, {2, 2}, {3, 1}}; + auto column = view(b, all(), range(1, 2)); + auto strided_column = strided_view(b, {all(), range(1, 2)}); + xtensor expected_column = {{2}, {1}, {0}}; + + for (auto method : {sorting_method::quick, sorting_method::stable}) + { + EXPECT_EQ(expected_row, argsort(row, 1, method)); + EXPECT_EQ(expected_row, argsort(strided_row, 1, method)); + EXPECT_EQ(expected_column, argsort(column, 0, method)); + EXPECT_EQ(expected_column, argsort(strided_column, 0, method)); + } + } + TEST(xsort, argsort_zero_stride) { {