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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
.. 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.
Implementation classes
======================
Requirements
~~~~~~~~~~~~
An implementation class in *xtensor* is a final class that models a specific
kind of expression. It must inherit (either directly or indirectly) from
:cpp:type:`xt::xexpression` and define (or inherit from classes that define) the following
types:
**container types**
.. code::
value_type;
reference;
const_reference;
pointer;
const_pointer;
size_type;
difference_type;
shape_type;
**iterator types**
.. code::
iterator;
const_iterator;
reverse_iterator;
const_reverse_iterator;
template <class S, layout_type L>
broadcast_iterator<S, L>;
template <class S, layout_type L>
const_broadcast_iterator<S, L>;
template <class S, layout_type L>
reverse_broadcast_iterator<S, L>;
template <class S, layout_type L>
const_reverse_broadcast_iterator<S, L>;
linear_iterator;
const_linear_iterator;
reverse_linear_iterator;
const_reverse_linear_iterator;
**layout data**
.. code::
static layout_type static_layout;
static bool contiguous_layout;
It must also provide the following methods, either by defining them
itself, or by inheriting from classes that define them, partially or
totally:
**shape methods**
.. code::
size_type size() const noexcept;
size_type dimension() const noexcept;
const inner_shape_type& shape() const noexcept;
**broadcasting methods**
.. code::
template <class S>
bool broadcast_shape(const S& shape) const;
**data access methods**
.. code::
template <class... Args>
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 S>
disable_integral_t<S, const_reference> operator[](const S& index) const;
template <class I>
const_reference operator[](std::initializer_list<I> index) const;
template <class It>
const_reference element(It first, It last) const;
const storage_type& storage() const;
**iteration methods**
These methods are usually provided by inheriting from ``xconst_iterable`` or ``xiterable``.
See :ref:`iterating-expression-label` for more details.
If the expression is mutable, it must also define the non-const counterparts of the data access
methods, and inherits from a semantic class to provide assignment operators.
List of available expression classes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*xtensor* provides the following expression classes:
**Containers**
- ``xarray_container`` : N-dimensional array with dynamic shape
- ``xarray_adaptor`` : N-dimensional array adaptor for STL-like containers or C arrays
- ``xtensor_container``: N-dimensional array with static number of dimensions
- ``xtensor_adaptor`` : N-dimensional tensor adaptor for STL-like containers or C arrays
- ``xfixed_container`` : N-dimensional array with static shape
- ``xfixed_adaptor`` : N-dimensoinal fixed tensor adaptor for STL-like containers or C arrays
Most of the methods of these classes are implemented in the base class ``xcontainer``, the
inheriting classes only provide constructors and assignment operators for the value semantic.
The container classes are generally used through type aliases which set many of the template
arguments:
- :cpp:type:`xt::xarray`
- :cpp:type:`xt::xtensor`
- ``xfixed_tensor``
The classes for adaptors can be instantiated through the many overloads of ``xt::adapt`` function,
so that their templates parameters are deduced.
**Scalar**
*xtensor* provides the ``xscalar`` class to adapt scalar values and give them the required API.
**Optional containers**
- ``xoptional_assembly`` : N-dimensional array holding optional values.
- ``xoptional_assembly_adaptor``: N-dimensional adaptor holding optional values.
Most of the mehtods of these classes are defined in their base class ``xoptional_assembly_base``.
**Views**
- :cpp:type:`xt::xview`: N-dimensional view with static number of slices, supporting all kind of slices
- ``xstrided_view``: N-dimensional view with dynamic number of slices, supporting strided slices only (see below)
- ``xdynamic_view``: N-dimensional view with dynamic number of slices, supporting all kind of slices
- ``xfunctor_view``: N-dimensional view applying a functor to its underlying elements (e.g. ``imag``, ``real``)
- ``xindex_view`` : Flat (1D) view yielding the values at the indices of its index array
- ``xmasked_view`` : View on optional expression hiding values depending on a mask
When the index of an element in the underlying expression of a view can be computed thanks to a strided scheme,
the slice used in this view is said to be a strided slice. *xtensor* provides the following strided slices:
- ``xrange``
- ``xstepped_range``
- ``xall``
- ``xnewaxis``
The following slices are not strided, and thus incompatible with ``xstrided_view``:
- ``xkeep_slice``
- ``xdrop_slice``
**Functional expressions**
Contrary to containers and views, the functional expressions are immutable.
- ``xbroadcast``: Broadcasts an expression to a specific shape
- ``xfunction`` : N-dimensional function operating on tensor expressions
- ``xgenerator``: N-dimensional function operating on indices
- ``xreducer`` : Reducing function operating over specified axes
xarray and xtensor
~~~~~~~~~~~~~~~~~~
Although they represent different concepts, :cpp:type:`xt::xarray` and :cpp:type:`xt::xtensor` have really similar
implementations so only :cpp:type:`xt::xarray` will be covered.
:cpp:type:`xt::xarray` is a strided array expression that can be assigned to. Everything :cpp:type:`xt::xarray` needs
is already defined in classes modeling :ref:`concepts-label`, so :cpp:type:`xt::xarray` only has to inherit
from these classes and define constructors and assignment operators:
.. image:: xarray_uml.svg
Besides implementing the methods that define value semantic, :cpp:type:`xt::xarray` and :cpp:type:`xt::xtensor` hold
the data container. Since the ``xcontainer`` base class implements all the logic for accessing
the data, it must me able to access the data container. This is achieved by requiring that
every class inheriting from ``xcontainer`` provides the following methods:
.. code::
storage_type& storage_impl() noexcept;
const storage_type& storage_impl() const noexcept;
These are the implementation methods of the ``storage()`` interface methods defined in ``xcontainer``,
and thus are defined in the private section of :cpp:type:`xt::xarray` and :cpp:type:`xt::xtensor`. In order to grant access
to ``xcontainer``, this last one is declared as ``friend``:
.. code::
template <class EC, layout_type L, class SC, class Tag>
class xarray : public xstrided_container<xarray<EC, L, SC, Tag>,
public xcontainer_semantic<xarray<EC, L, SC, Tag>>
{
public:
// ....
private:
storage_type m_storage;
storage_type& storage() noexcept;
const storage_type& storage() const noexcept;
friend class xcontainer<xarray<EC, L, SC, Tag>>;
};
This pattern is similar to the template method pattern used in hierarchy of classes with
entity semantic (see virtuality_).
Inner types definition
~~~~~~~~~~~~~~~~~~~~~~
Although the base classes use the types defined in the Requirement section, they cannot
define them; first because different base classes may need the same types and we want
to avoid duplication of type definitions. The second reason is that most of the types
may rely on other types specific to the implementation classes. For instance,
``value_type``, ``reference``, etc, of :cpp:type:`xt::xarray` are simply the types defined in the
container type hold by :cpp:type:`xt::xarray`:
.. code::
using value_type = typename storage_type::value_type;
using reference = typename storage_type::reference;
using const_reference = typename storage_type::const_reference;
...
Moreover, CRTP base classes cannot access inner types defined in CRTP leaf classes, because
a CRTP leaf class is only declared, not defined, when the CRTP base class is being defined.
The solution is to define those types in an external structure that is specialized for
each CRTP leaf class:
.. code::
// Declaration only, no generic definition
template <class C>
struct xcontainer_inner_types;
In ``xarray.hpp``
.. code::
template <class EC, layout_type L, class SC, class Tag>
struct xcontainer_inner_types<xarray<EC, L,SC, Tag>>
{
// Definition of types required by CRTP bases
};
In order to avoid a lot of boilerplate, the CRTP base classes expect only a few types to be defined
in this structure, and then compute the other types, based on these former definitions. The requirements
on types definition regarding the base classes is detailed below.
**xsemantic**
The semantic classes only expect the following type: ``temporary_type``.
**xcontainer**
``xcontainer`` and ``xstrided_container`` expect the following types to be defined:
.. code::
storage_type;
shape_type;
strides_type;
backstrides_type;
inner_shape_type;
inner_strides_type;
inner_backstrides_type;
layout_type;
.. _xiterable-inner-label:
**xiterable**
Since many expressions are not containers, the definition of types required by the iterable concept is
done in a dedicated structure following the same pattern as ``xcontainer_inner_types``, i.e. a sturcture
declared and specialized for each final class:
.. code::
template <class C>
struct xiterable_inner_types;
The following types must be defined in each specialization:
.. code::
inner_shape_type;
const_stepper;
stepper;
More detail about the stepper types is given in :ref:`iterating-expression-label`.
.. _virtuality: http://www.gotw.ca/publications/mill18.htm
|