Template Class ArrayView

Class Documentation

template<typename T, size_t Dim>
class ArrayView

Multi-dimensional view into contiguous memory.

ArrayView provides lightweight indexing and slicing functionality over memory without owning it. It supports both read-only and mutable access. Shape and strides are passed at construction to define the logical layout.

Template Parameters:
  • T – Element type (e.g., float, int, const T for read-only)

  • Dim – Number of dimensions (e.g., 1D, 2D, etc.)

Public Functions

template<typename Indexable1, typename Indexable2>
inline ArrayView(T *ptr, const Indexable1 &shape, const Indexable2 &strides)

Construct from any types with subscript operator, i.e.

vector/array

host only due to cuda/stl incompatibility

Parameters:
  • ptr[in] – pointer to data

  • shape[in] – arbitrary container of shape dimensions

  • strides[in] – arbitrary container of stride offsets

template<typename ContainerT>
inline ArrayView(T *ptr, const ContainerT &shape)

Construct from any container type, deducing strides.

host only due to cuda/stl incompatibility

Parameters:
  • ptr[in] – pointer to data

  • shape[in] – arbitrary container of shape dimensions

inline OSDK_FN ArrayView(T *ptr, const int (&shape)[Dim], const int (&strides)[Dim])

Construct from C arrays.

device, host

Parameters:
  • ptr[in] – pointer to data

  • shape[in] – C array of shape dimensions

  • strides[in] – C array of stride offsets

template<typename IntT>
inline OSDK_FN ArrayView(T *ptr, std::initializer_list<IntT> shape)

Construct from initializer list, deducing strides.

device, host

Parameters:
  • ptr[in] – pointer to data

  • shape[in] – initializer_list of shape dimensions

inline OSDK_FN ArrayView(T *ptr, const int (&shape)[Dim])

Construct from C array, deducing strides.

device, host

Parameters:
  • ptr[in] – pointer to data

  • shape[in] – C array of shape dimensions

template<typename OtherT, typename = std::enable_if_t<std::is_same<T, const OtherT>::VALUE, void>>
inline OSDK_FN ArrayView(const ArrayView<OtherT, Dim> &other)

Below allows conversion from ArrayView to ConstArrayView but not the other way around.

/code ArrayView4<int> a; ConstArrayView4<int> b = a; // compiles ArrayView4<int> c = b; // does not compile /endcode

Parameters:

other[in]ArrayView

template<typename... Args> inline OSDK_FN const T & operator() (Args &&... idx) const

Retrieve reference to value with subscript operator.

Parameters:

idx[in] – indices

Returns:

reference to value

template<typename... Args> inline OSDK_FN T & operator() (Args &&... idx)

Retrieve reference to value with subscript operator.

Parameters:

idx[in] – indices

Returns:

reference to value

template<typename... Args> inline OSDK_FN ArrayView< T, subview_dim< Args... >()> subview (Args... idx) const

Get a subview.

Operates similarly to numpy ndarray bracket operator, returning a sliced, potentially sparse subview

The following two snippets are functionally equivalent

std::vector<int> data(100*100*100);
ArrayView3<int> view{data.data(), {100,100,100};
// get a slice of all elements in the first dimension with second
// and third pinned to 10 and 20 respectively
ArrayView1<int> subview = view.subview(keep(), 10, 20);

import numpy as np
arr = np.ndarray(shape=(100,100,100), dtype=np.int32)
subview = arr[:,10,20]
Parameters:

idx[in] – pack of int indices or idx_range (keep())

Returns:

subview with different dimensions

template<typename ...Args>
inline ArrayView<T, sizeof...(Args)> reshape(Args... dims) const

Reshape array view to a different set of dimensions.

Throws:
  • std::invalid_argument – on trying to reshape a sparse ArrayView

  • std::invalid_argument – on flattened dimension size not matching the original view size

Parameters:

dims[in] new dimensions

Returns:

reshaped ArrayView with new dimensions

inline OSDK_FN bool sparse () const

Check if the ArrayView is not contiguous.

Returns:

true if sparse

inline const T *begin() const

Iterator functions below return basic pointers to data, which only works if the current array is not sparse.

Because we have no way of throwing if the array is sparse, these are explicitly host side at the moment, until we get to implementing sparse iterators

Throws:

std::logic_error – if the view is sparse, since iterators are not supported for non-contiguous memory layouts.

Returns:

pointer to the first element in the view.

inline const T *end() const

Returns a pointer to one past the last element in the view.

Provides a raw pointer that acts as the end iterator for the array view, which can be used in standard iterator-based loops. Only supported for non-sparse arrays.

Throws:

std::logic_error – if the view is sparse, as iterators are not supported for sparse memory layouts.

Returns:

pointer to one past the last element.

inline T *begin()

Iterator functions below return basic pointers to data, which only works if the current array is not sparse.

Because we have no way of throwing if the array is sparse, these are explicitly host side at the moment, until we get to implementing sparse iterators

Throws:

std::logic_error – if the view is sparse, since iterators are not supported for non-contiguous memory layouts.

Returns:

pointer to the first element in the view.

inline T *end()

Returns a pointer to one past the last element in the view.

Provides a raw pointer that acts as the end iterator for the array view, which can be used in standard iterator-based loops. Only supported for non-sparse arrays.

Throws:

std::logic_error – if the view is sparse, as iterators are not supported for sparse memory layouts.

Returns:

pointer to one past the last element.

inline const T *data() const

Get the underlying data pointer.

Returns:

pointer to data

inline T *data()

Get the underlying data pointer.

Returns:

pointer to data

inline size_t size() const

Get total size of the elements in the array view.

Returns:

size of the array view

Public Members

const int32_t strides[Dim]

C array of stride offsets.

const uint32_t shape[Dim]

C array of shape dimensions.

Public Static Functions

template<typename... Args> static inline OSDK_FN constexpr int subview_dim ()

Templated utility to calculate subview dimensions at compile time.

Template Parameters:

Args – parameter pack of indices or idx_range

Returns:

subview dimension