| 247 | |
| 248 | template <typename Backend, typename DataType, int sample_ndim> |
| 249 | struct TensorListViewBase { |
| 250 | using element_type = DataType; |
| 251 | |
| 252 | /** |
| 253 | * @brief Return non-owning View to sample at specified index |
| 254 | */ |
| 255 | TensorView<Backend, DataType, sample_ndim> operator[](int sample) const { |
| 256 | return { tensor_data(sample), tensor_shape(sample) }; |
| 257 | } |
| 258 | |
| 259 | template <int other_sample_ndim> |
| 260 | TensorView<Backend, DataType, other_sample_ndim> tensor_view(int sample) const { |
| 261 | static_assert(other_sample_ndim == sample_ndim || sample_ndim == DynamicDimensions |
| 262 | || other_sample_ndim == DynamicDimensions, "Cannot convert to other static ndim"); |
| 263 | return { data[sample], shape.template tensor_shape<other_sample_ndim>(sample)}; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @brief Number of samples |
| 268 | */ |
| 269 | int size() const noexcept { return shape.size(); } |
| 270 | int num_samples() const noexcept { return size(); } |
| 271 | ptrdiff_t num_elements() const { |
| 272 | return shape.num_elements(); |
| 273 | } |
| 274 | int sample_dim() const { return shape.sample_dim(); } |
| 275 | |
| 276 | void resize(int num_samples) { |
| 277 | shape.resize(num_samples); |
| 278 | data.resize(num_samples); |
| 279 | } |
| 280 | |
| 281 | void resize(int num_samples, int dim) { |
| 282 | shape.resize(num_samples, dim); |
| 283 | data.resize(num_samples); |
| 284 | } |
| 285 | |
| 286 | bool empty() const { |
| 287 | return data.empty(); |
| 288 | } |
| 289 | |
| 290 | explicit operator bool() const { |
| 291 | return !empty(); |
| 292 | } |
| 293 | |
| 294 | template <int other_sample_ndim> |
| 295 | TensorListView<Backend, DataType, other_sample_ndim> to_static() const & { |
| 296 | static_assert(other_sample_ndim != DynamicDimensions, |
| 297 | "Conversion to static only allowed for static shape"); |
| 298 | return { data, shape.template to_static<other_sample_ndim>() }; |
| 299 | } |
| 300 | |
| 301 | template <int other_sample_ndim> |
| 302 | TensorListView<Backend, DataType, other_sample_ndim> to_static() && { |
| 303 | static_assert(other_sample_ndim != DynamicDimensions, |
| 304 | "Conversion to static only allowed for static shape"); |
| 305 | return { std::move(data), std::move(shape).template to_static<other_sample_ndim>() }; |
| 306 | } |