Skip to content

Commit

Permalink
unchecked method implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed May 30, 2018
1 parent 3ea5ad7 commit f90820a
Show file tree
Hide file tree
Showing 24 changed files with 648 additions and 15 deletions.
29 changes: 29 additions & 0 deletions include/xtensor/xbroadcast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ namespace xt
template <class... Args>
const_reference at(Args... args) const;

template <class... Args>
const_reference unchecked(Args... args) const;

template <class S>
disable_integral_t<S, const_reference> operator[](const S& index) const;
template <class I>
Expand Down Expand Up @@ -280,6 +283,32 @@ namespace xt
return this->operator()(args...);
}

/**
* Returns a constant reference to the element at the specified position in the expression.
* @param args a list of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the expression, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have
* undefined behavior (see parameters), operator() should be prefered whenever
* it is possible.
* @warning This method is NOT compatible with broadcasting, meaning the following
* code has undefined behavior:
* \code{.cpp}
* xt::xarray<double> a = {{0, 1}, {2, 3}};
* xt::xarray<double> b = {0, 1};
* auto fd = a + b;
* double res = fd.uncheked(0, 1);
* \endcode
*/
template <class CT, class X>
template <class... Args>
inline auto xbroadcast<CT, X>::unchecked(Args... args) const -> const_reference
{
return this->operator()(args...);
}

/**
* Returns a constant reference to the element at the specified position in the expression.
* @param index a sequence of indices specifying the position in the function. Indices
Expand Down
60 changes: 60 additions & 0 deletions include/xtensor/xcontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ namespace xt
template <class... Args>
const_reference at(Args... args) const;

template <class... Args>
reference unchecked(Args... args);

template <class... Args>
const_reference unchecked(Args... args) const;

template <class S>
disable_integral_t<S, reference> operator[](const S& index);
template <class I>
Expand Down Expand Up @@ -596,6 +602,60 @@ namespace xt
return this->operator()(args...);
}

/**
* Returns a reference to the element at the specified position in the container.
* @param args a list of indices specifying the position in the container. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the container, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have
* undefined behavior (see parameters), operator() should be prefered whenever
* it is possible.
* @warning This method is NOT compatible with broadcasting, meaning the following
* code has undefined behavior:
* \code{.cpp}
* xt::xarray<double> a = {{0, 1}, {2, 3}};
* xt::xarray<double> b = {0, 1};
* auto fd = a + b;
* double res = fd.uncheked(0, 1);
* \endcode
*/
template <class D>
template <class... Args>
inline auto xcontainer<D>::unchecked(Args... args) -> reference
{
size_type index = xt::unchecked_data_offset<size_type>(strides(), static_cast<size_type>(args)...);
return storage()[index];
}

/**
* Returns a constant reference to the element at the specified position in the container.
* @param args a list of indices specifying the position in the container. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the container, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have
* undefined behavior (see parameters), operator() should be prefered whenever
* it is possible.
* @warning This method is NOT compatible with broadcasting, meaning the following
* code has undefined behavior:
* \code{.cpp}
* xt::xarray<double> a = {{0, 1}, {2, 3}};
* xt::xarray<double> b = {0, 1};
* auto fd = a + b;
* double res = fd.uncheked(0, 1);
* \endcode
*/
template <class D>
template <class... Args>
inline auto xcontainer<D>::unchecked(Args... args) const -> const_reference
{
size_type index = xt::unchecked_data_offset<size_type>(strides(), static_cast<size_type>(args)...);
return storage()[index];
}

/**
* Returns a reference to the element at the specified position in the container.
* @param index a sequence of indices specifying the position in the container. Indices
Expand Down
41 changes: 41 additions & 0 deletions include/xtensor/xfunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ namespace xt
template <class... Args>
const_reference at(Args... args) const;

template <class... Args>
const_reference unchecked(Args... args) const;

template <class S>
disable_integral_t<S, const_reference> operator[](const S& index) const;
template <class I>
Expand Down Expand Up @@ -312,6 +315,9 @@ namespace xt

template <std::size_t... I, class... Args>
const_reference access_impl(std::index_sequence<I...>, Args... args) const;

template <std::size_t... I, class... Args>
const_reference unchecked_impl(std::index_sequence<I...>, Args... args) const;

template <std::size_t... I, class It>
const_reference element_access_impl(std::index_sequence<I...>, It first, It last) const;
Expand Down Expand Up @@ -627,6 +633,34 @@ namespace xt
return this->operator()(args...);
}

/**
* Returns a constant reference to the element at the specified position in the expression.
* @param args a list of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the expression, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have
* undefined behavior (see parameters), operator() should be prefered whenever
* it is possible.
* @warning This method is NOT compatible with broadcasting, meaning the following
* code has undefined behavior:
* \code{.cpp}
* xt::xarray<double> a = {{0, 1}, {2, 3}};
* xt::xarray<double> b = {0, 1};
* auto fd = a + b;
* double res = fd.uncheked(0, 1);
* \endcode
*/
template <class F, class R, class... CT>
template <class... Args>
inline auto xfunction_base<F, R, CT...>::unchecked(Args... args) const -> const_reference
{
// The static cast prevents the compiler from instantiating the template methods with signed integers,
// leading to warning about signed/unsigned conversions in the deeper layers of the access methods
return unchecked_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
}

template <class F, class R, class... CT>
template <class S>
inline auto xfunction_base<F, R, CT...>::operator[](const S& index) const
Expand Down Expand Up @@ -819,6 +853,13 @@ namespace xt
XTENSOR_CHECK_DIMENSION(shape(), args...);
return m_f(std::get<I>(m_e)(args...)...);
}

template <class F, class R, class... CT>
template <std::size_t... I, class... Args>
inline auto xfunction_base<F, R, CT...>::unchecked_impl(std::index_sequence<I...>, Args... args) const -> const_reference
{
return m_f(std::get<I>(m_e).unchecked(args...)...);
}

template <class F, class R, class... CT>
template <std::size_t... I, class It>
Expand Down
58 changes: 58 additions & 0 deletions include/xtensor/xfunctor_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ namespace xt
template <class... Args>
reference at(Args... args);

template <class... Args>
reference unchecked(Args... args);

template <class S>
disable_integral_t<S, reference> operator[](const S& index);
template <class I>
Expand All @@ -180,6 +183,9 @@ namespace xt
template <class... Args>
const_reference operator()(Args... args) const;

template <class... Args>
const_reference unchecked(Args... args) const;

template <class... Args>
const_reference at(Args... args) const;

Expand Down Expand Up @@ -542,6 +548,32 @@ namespace xt
return this->operator()(args...);
}

/**
* Returns a reference to the element at the specified position in the expression.
* @param args a list of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the expression, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have
* undefined behavior (see parameters), operator() should be prefered whenever
* it is possible.
* @warning This method is NOT compatible with broadcasting, meaning the following
* code has undefined behavior:
* \code{.cpp}
* xt::xarray<double> a = {{0, 1}, {2, 3}};
* xt::xarray<double> b = {0, 1};
* auto fd = a + b;
* double res = fd.uncheked(0, 1);
* \endcode
*/
template <class F, class CT>
template <class... Args>
inline auto xfunctor_view<F, CT>::unchecked(Args... args) -> reference
{
return m_functor(m_e.unchecked(args...));
}

/**
* Returns a reference to the element at the specified position in the expression.
* @param index a sequence of indices specifying the position in the function. Indices
Expand Down Expand Up @@ -617,6 +649,32 @@ namespace xt
return this->operator()(args...);
}

/**
* Returns a constant reference to the element at the specified position in the expression.
* @param args a list of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the expression, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have
* undefined behavior (see parameters), operator() should be prefered whenever
* it is possible.
* @warning This method is NOT compatible with broadcasting, meaning the following
* code has undefined behavior:
* \code{.cpp}
* xt::xarray<double> a = {{0, 1}, {2, 3}};
* xt::xarray<double> b = {0, 1};
* auto fd = a + b;
* double res = fd.uncheked(0, 1);
* \endcode
*/
template <class F, class CT>
template <class... Args>
inline auto xfunctor_view<F, CT>::unchecked(Args... args) const -> const_reference
{
return m_functor(m_e.unchecked(args...));
}

/**
* Returns a constant reference to the element at the specified position in the expression.
* @param index a sequence of indices specifying the position in the function. Indices
Expand Down
28 changes: 28 additions & 0 deletions include/xtensor/xgenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace xt
const_reference operator()(Args... args) const;
template <class... Args>
const_reference at(Args... args) const;
template <class... Args>
const_reference unchecked(Args... args) const;
template <class OS>
disable_integral_t<OS, const_reference> operator[](const OS& index) const;
template <class I>
Expand Down Expand Up @@ -220,6 +222,32 @@ namespace xt
return this->operator()(args...);
}

/**
* Returns a constant reference to the element at the specified position in the expression.
* @param args a list of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices must be equal to the number of
* dimensions of the expression, else the behavior is undefined.
*
* @warning This method is meant for performance, for expressions with a dynamic
* number of dimensions (i.e. not known at compile time). Since it may have
* undefined behavior (see parameters), operator() should be prefered whenever
* it is possible.
* @warning This method is NOT compatible with broadcasting, meaning the following
* code has undefined behavior:
* \code{.cpp}
* xt::xarray<double> a = {{0, 1}, {2, 3}};
* xt::xarray<double> b = {0, 1};
* auto fd = a + b;
* double res = fd.uncheked(0, 1);
* \endcode
*/
template <class F, class R, class S>
template <class... Args>
inline auto xgenerator<F, R, S>::unchecked(Args... args) const -> const_reference
{
return m_f(args...);
}

template <class F, class R, class S>
template <class OS>
inline auto xgenerator<F, R, S>::operator[](const OS& index) const
Expand Down
22 changes: 22 additions & 0 deletions include/xtensor/xindex_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace xt
reference operator()(size_type idx = size_type(0));
template <class... Args>
reference operator()(size_type idx0, size_type idx1, Args... args);
reference unchecked(size_type idx);
template <class S>
disable_integral_t<S, reference> operator[](const S& index);
template <class OI>
Expand All @@ -122,6 +123,7 @@ namespace xt
const_reference operator()(size_type idx = size_type(0)) const;
template <class... Args>
const_reference operator()(size_type idx0, size_type idx1, Args... args) const;
const_reference unchecked(size_type idx) const;
template <class S>
disable_integral_t<S, const_reference> operator[](const S& index) const;
template <class OI>
Expand Down Expand Up @@ -333,6 +335,16 @@ namespace xt
return this->operator()(idx1, args...);
}

/**
* Returns a reference to the element at the specified position in the xindex_view.
* @param idx index specifying the position in the index_view.
*/
template <class CT, class I>
inline auto xindex_view<CT, I>::unchecked(size_type idx) -> reference
{
return this->operator()(idx);
}

template <class CT, class I>
inline auto xindex_view<CT, I>::operator()(size_type idx) const -> const_reference
{
Expand All @@ -352,6 +364,16 @@ namespace xt
return this->operator()(idx1, args...);
}

/**
* Returns a constant reference to the element at the specified position in the xindex_view.
* @param idx index specifying the position in the index_view.
*/
template <class CT, class I>
inline auto xindex_view<CT, I>::unchecked(size_type idx) const -> const_reference
{
return this->operator()(idx);
}

/**
* Returns a reference to the element at the specified position in the container.
* @param index a sequence of indices specifying the position in the container. Indices
Expand Down

0 comments on commit f90820a

Please sign in to comment.