/usr/include/xtensor/xstrides.hpp is in xtensor-dev 0.10.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | /***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XSTRIDES_HPP
#define XSTRIDES_HPP
#include <cstddef>
#include <functional>
#include <numeric>
#include "xexception.hpp"
#include "xtensor_forward.hpp"
namespace xt
{
template <class shape_type>
auto compute_size(const shape_type& shape) noexcept;
/***************
* data offset *
***************/
template <class size_type, class S, size_t dim = 0>
size_type data_offset(const S& strides) noexcept;
template <class size_type, class S, size_t dim = 0, class Arg, class... Args>
size_type data_offset(const S& strides, Arg arg, Args... args) noexcept;
template <class size_type, class S, class It>
size_type element_offset(const S& strides, It first, It last) noexcept;
/*******************
* strides builder *
*******************/
template <class shape_type, class strides_type>
std::size_t compute_strides(const shape_type& shape, layout_type l, strides_type& strides);
template <class shape_type, class strides_type, class backstrides_type>
std::size_t compute_strides(const shape_type& shape, layout_type l,
strides_type& strides, backstrides_type& backstrides);
template <class shape_type, class strides_type>
void adapt_strides(const shape_type& shape, strides_type& strides) noexcept;
template <class shape_type, class strides_type, class backstrides_type>
void adapt_strides(const shape_type& shape, strides_type& strides,
backstrides_type& backstrides) noexcept;
/***********************
* broadcast functions *
***********************/
template <class S1, class S2>
bool broadcast_shape(const S1& input, S2& output);
template <class S1, class S2>
bool broadcastable(const S1& s1, S2& s2);
/********************************************
* utility functions for strided containers *
********************************************/
template <class C, class It>
It strided_data_end(const C& c, It end, layout_type l)
{
using strides_type = std::decay_t<decltype(c.strides())>;
if (c.dimension() == 0)
{
return end;
}
else
{
auto leading_stride = (l == layout_type::row_major ? c.strides().back() : c.strides().front());
leading_stride = std::max(leading_stride, typename strides_type::value_type(1));
return end - 1 + leading_stride;
}
}
/******************
* Implementation *
******************/
template <class shape_type>
inline auto compute_size(const shape_type& shape) noexcept
{
using size_type = std::decay_t<typename shape_type::value_type>;
return std::accumulate(shape.cbegin(), shape.cend(), size_type(1), std::multiplies<size_type>());
}
namespace detail
{
template <class size_type, class S, std::size_t dim>
inline size_type raw_data_offset(const S&) noexcept
{
return 0;
}
template <class size_type, class S, std::size_t dim, class Arg, class... Args>
inline size_type raw_data_offset(const S& strides, Arg arg, Args... args) noexcept
{
return arg * strides[dim] + raw_data_offset<size_type, S, dim + 1>(strides, args...);
}
}
template <class size_type, class S, std::size_t dim>
inline size_type data_offset(const S&) noexcept
{
return 0;
}
template <class size_type, class S, std::size_t dim, class Arg, class... Args>
inline size_type data_offset(const S& strides, Arg arg, Args... args) noexcept
{
constexpr std::size_t nargs = sizeof...(Args) + dim + 1;
if (nargs == strides.size())
{
// Correct number of arguments: iterate
return detail::raw_data_offset<size_type, S, dim, Arg, Args...>(strides, arg, args...);
}
else if (nargs > strides.size())
{
// Too many arguments: drop the first
return data_offset<size_type, S, dim>(strides, args...);
}
else
{
// Too few arguments: right to left scalar product
auto view = strides.cend() - nargs;
return detail::raw_data_offset<size_type, const typename S::const_iterator, dim, Arg, Args...>(view, arg, args...);
}
}
template <class size_type, class S, class It>
inline size_type element_offset(const S& strides, It first, It last) noexcept
{
auto size = std::min(static_cast<typename S::size_type>(std::distance(first, last)), strides.size());
return std::inner_product(last - size, last, strides.cend() - size, size_type(0));
}
namespace detail
{
template <class shape_type, class strides_type, class bs_ptr>
inline void adapt_strides(const shape_type& shape, strides_type& strides,
bs_ptr backstrides, typename strides_type::size_type i) noexcept
{
if (shape[i] == 1)
strides[i] = 0;
(*backstrides)[i] = strides[i] * (shape[i] - 1);
}
template <class shape_type, class strides_type>
inline void adapt_strides(const shape_type& shape, strides_type& strides,
std::nullptr_t, typename strides_type::size_type i) noexcept
{
if (shape[i] == 1)
strides[i] = 0;
}
template <class shape_type, class strides_type, class bs_ptr>
inline std::size_t compute_strides(const shape_type& shape, layout_type l,
strides_type& strides, bs_ptr bs)
{
std::size_t data_size = 1;
if (l == layout_type::row_major)
{
for (std::size_t i = strides.size(); i != 0; --i)
{
strides[i - 1] = data_size;
data_size = strides[i - 1] * shape[i - 1];
adapt_strides(shape, strides, bs, i - 1);
}
}
else
{
for (std::size_t i = 0; i < strides.size(); ++i)
{
strides[i] = data_size;
data_size = strides[i] * shape[i];
adapt_strides(shape, strides, bs, i);
}
}
return data_size;
}
}
template <class shape_type, class strides_type>
inline std::size_t compute_strides(const shape_type& shape, layout_type l, strides_type& strides)
{
return detail::compute_strides(shape, l, strides, nullptr);
}
template <class shape_type, class strides_type, class backstrides_type>
inline std::size_t compute_strides(const shape_type& shape, layout_type l,
strides_type& strides,
backstrides_type& backstrides)
{
return detail::compute_strides(shape, l, strides, &backstrides);
}
template <class shape_type, class strides_type>
inline void adapt_strides(const shape_type& shape, strides_type& strides) noexcept
{
for (typename shape_type::size_type i = 0; i < shape.size(); ++i)
{
detail::adapt_strides(shape, strides, nullptr, i);
}
}
template <class shape_type, class strides_type, class backstrides_type>
inline void adapt_strides(const shape_type& shape, strides_type& strides,
backstrides_type& backstrides) noexcept
{
for (typename shape_type::size_type i = 0; i < shape.size(); ++i)
{
detail::adapt_strides(shape, strides, &backstrides, i);
}
}
template <class S1, class S2>
inline bool broadcast_shape(const S1& input, S2& output)
{
bool trivial_broadcast = (input.size() == output.size());
auto input_iter = input.crbegin();
auto output_iter = output.rbegin();
for (; input_iter != input.crend(); ++input_iter, ++output_iter)
{
if (*output_iter == 1)
{
*output_iter = *input_iter;
}
else if ((*input_iter != 1) && (*output_iter != *input_iter))
{
throw broadcast_error(output, input);
}
trivial_broadcast = trivial_broadcast && (*output_iter == *input_iter);
}
return trivial_broadcast;
}
template <class S1, class S2>
inline bool broadcastable(const S1& s1, const S2& s2)
{
auto iter1 = s1.crbegin();
auto iter2 = s2.crbegin();
for (; iter1 != s1.crend() && iter2 != s2.crend(); ++iter1, ++iter2)
{
if ((*iter2 != 1) && (*iter1 != 1) && (*iter2 != *iter1))
{
return false;
}
}
return true;
}
}
#endif
|