From 6ff26745b33245f66461b906472f0008785dc574 Mon Sep 17 00:00:00 2001 From: Drew Hubley Date: Mon, 1 Jan 2024 18:28:51 -0400 Subject: [PATCH] Updated concatenate access --- include/xtensor/xbuilder.hpp | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/include/xtensor/xbuilder.hpp b/include/xtensor/xbuilder.hpp index 6be2775c3..e124d764c 100644 --- a/include/xtensor/xbuilder.hpp +++ b/include/xtensor/xbuilder.hpp @@ -497,20 +497,44 @@ namespace xt template inline value_type access(const tuple_type& t, size_type axis, It first, It last) const { - xindex index(first, last); - auto match = [&index, axis](auto& arr) + // trim off extra indices if provided to match behavior of containers + auto dim_offset = std::distance(first, last) - std::get<0>(t).dimension(); + size_t axis_dim = *(first + axis + dim_offset); + auto match = [&](auto& arr) { - if (index[axis] >= arr.shape()[axis]) + if (axis_dim >= arr.shape()[axis]) { - index[axis] -= arr.shape()[axis]; + axis_dim -= arr.shape()[axis]; return false; } return true; }; - auto get = [&index](auto& arr) + auto get = [&](auto& arr) { - return arr[index]; + size_t offset = 0; + const size_t end = arr.dimension(); + for (size_t i = 0; i < end; i++) + { + const auto& shape = arr.shape(); + const size_t stride = std::accumulate( + shape.begin() + i + 1, + shape.end(), + 1, + std::multiplies() + ); + if (i == axis) + { + offset += axis_dim * stride; + } + else + { + const auto len = (*(first + i + dim_offset)); + offset += len * stride; + } + } + const auto element = arr.begin() + offset; + return *element; }; size_type i = 0;