MLIR 23.0.0git
mlir::Sliceable< Derived, ElementTy > Class Template Reference

A CRTP base class for pseudo-containers willing to support Python-type slicing access on top of indexed access. More...

#include "mlir/Bindings/Python/NanobindUtils.h"

Inheritance diagram for mlir::Sliceable< Derived, ElementTy >:

Classes

struct  has_maybe_downcast
 Trait to check if T provides a maybeDownCast method. More...
struct  has_maybe_downcast< T, std::void_t< decltype(&T::maybeDownCast)> >

Public Member Functions

 Sliceable (intptr_t startIndex, intptr_t length, intptr_t step)
ElementTy getElement (intptr_t index)
 Returns the index-th element in the slice, supports negative indices.
intptr_t size ()
 Returns the size of slice.
std::vector< ElementTy > dunderAdd (Derived &other)
 Returns a new vector (mapped to Python list) containing elements from two slices.

Static Public Member Functions

static void bind (nanobind::module_ &m)
 Binds the indexing and length methods in the Python class.
static void bindDerived (ClassTy &)
 Hook for derived classes willing to bind more methods.

Public Attributes

intptr_t startIndex
intptr_t length
intptr_t step

Protected Types

using ClassTy = nanobind::class_<Derived>

Protected Member Functions

intptr_t wrapIndex (intptr_t index)
 Transforms index into a legal value to access the underlying sequence.
intptr_t linearizeIndex (intptr_t index)
 Computes the linear index given the current slice properties.
nanobind::typed< nanobind::object, ElementTy > getItem (intptr_t index)
 Returns the element at the given slice index.
nanobind::object getItemSlice (PyObject *slice)
 Returns a new instance of the pseudo-container restricted to the given slice.

Detailed Description

template<typename Derived, typename ElementTy>
class mlir::Sliceable< Derived, ElementTy >

A CRTP base class for pseudo-containers willing to support Python-type slicing access on top of indexed access.

Calling bind on this class will define __len__ as well as __getitem__ with integer and slice arguments.

This is intended for pseudo-containers that can refer to arbitrary slices of underlying storage indexed by a single integer. Indexing those with an integer produces an instance of ElementTy. Indexing those with a slice produces a new instance of Derived, which can be sliced further.

A derived class must provide the following:

  • a static const char *pyClassName field containing the name of the Python class to bind;
  • an instance method intptr_t getRawNumElements() that returns the number of elements in the backing container (NOT that of the slice);
  • an instance method ElementTy getRawElement(intptr_t) that returns a single element at the given linear index (NOT slice index);
  • an instance method Derived slice(intptr_t, intptr_t, intptr_t) that constructs a new instance of the derived pseudo-container with the given slice parameters (to be forwarded to the Sliceable constructor).

The getRawNumElements() and getRawElement(intptr_t) callbacks must not throw.

A derived class may additionally define:

  • a static void bindDerived(ClassTy &) method to bind additional methods the python class.

Definition at line 285 of file NanobindUtils.h.

Member Typedef Documentation

◆ ClassTy

template<typename Derived, typename ElementTy>
using mlir::Sliceable< Derived, ElementTy >::ClassTy = nanobind::class_<Derived>
protected

Definition at line 287 of file NanobindUtils.h.

Constructor & Destructor Documentation

◆ Sliceable()

template<typename Derived, typename ElementTy>
mlir::Sliceable< Derived, ElementTy >::Sliceable ( intptr_t startIndex,
intptr_t length,
intptr_t step )
inlineexplicit

Definition at line 351 of file NanobindUtils.h.

Member Function Documentation

◆ bind()

template<typename Derived, typename ElementTy>
void mlir::Sliceable< Derived, ElementTy >::bind ( nanobind::module_ & m)
inlinestatic

Binds the indexing and length methods in the Python class.

Definition at line 387 of file NanobindUtils.h.

◆ bindDerived()

template<typename Derived, typename ElementTy>
void mlir::Sliceable< Derived, ElementTy >::bindDerived ( ClassTy & )
inlinestatic

Hook for derived classes willing to bind more methods.

Definition at line 446 of file NanobindUtils.h.

◆ dunderAdd()

template<typename Derived, typename ElementTy>
std::vector< ElementTy > mlir::Sliceable< Derived, ElementTy >::dunderAdd ( Derived & other)
inline

Returns a new vector (mapped to Python list) containing elements from two slices.

The new vector is necessary because slices may not be contiguous or even come from the same original sequence.

Definition at line 374 of file NanobindUtils.h.

Referenced by mlir::Sliceable< PyOpResultList, PyOpResult >::bind().

◆ getElement()

template<typename Derived, typename ElementTy>
ElementTy mlir::Sliceable< Derived, ElementTy >::getElement ( intptr_t index)
inline

Returns the index-th element in the slice, supports negative indices.

Throws if the index is out of bounds.

Definition at line 358 of file NanobindUtils.h.

Referenced by mlir::Sliceable< PyOpResultList, PyOpResult >::dunderAdd().

◆ getItem()

template<typename Derived, typename ElementTy>
nanobind::typed< nanobind::object, ElementTy > mlir::Sliceable< Derived, ElementTy >::getItem ( intptr_t index)
inlineprotected

Returns the element at the given slice index.

Supports negative indices by taking elements in inverse order. Returns a nullptr object if out of bounds.

Definition at line 320 of file NanobindUtils.h.

◆ getItemSlice()

template<typename Derived, typename ElementTy>
nanobind::object mlir::Sliceable< Derived, ElementTy >::getItemSlice ( PyObject * slice)
inlineprotected

Returns a new instance of the pseudo-container restricted to the given slice.

Returns a nullptr object on failure.

Definition at line 339 of file NanobindUtils.h.

◆ linearizeIndex()

template<typename Derived, typename ElementTy>
intptr_t mlir::Sliceable< Derived, ElementTy >::linearizeIndex ( intptr_t index)
inlineprotected

Computes the linear index given the current slice properties.

Definition at line 300 of file NanobindUtils.h.

Referenced by mlir::Sliceable< PyOpResultList, PyOpResult >::getElement(), and mlir::Sliceable< PyOpResultList, PyOpResult >::getItem().

◆ size()

template<typename Derived, typename ElementTy>
intptr_t mlir::Sliceable< Derived, ElementTy >::size ( )
inline

Returns the size of slice.

Definition at line 369 of file NanobindUtils.h.

◆ wrapIndex()

template<typename Derived, typename ElementTy>
intptr_t mlir::Sliceable< Derived, ElementTy >::wrapIndex ( intptr_t index)
inlineprotected

Transforms index into a legal value to access the underlying sequence.

Returns <0 on failure.

Definition at line 291 of file NanobindUtils.h.

Referenced by mlir::Sliceable< PyOpResultList, PyOpResult >::getElement(), and mlir::Sliceable< PyOpResultList, PyOpResult >::getItem().

Member Data Documentation

◆ length

template<typename Derived, typename ElementTy>
intptr_t mlir::Sliceable< Derived, ElementTy >::length

Definition at line 449 of file NanobindUtils.h.

◆ startIndex

template<typename Derived, typename ElementTy>
intptr_t mlir::Sliceable< Derived, ElementTy >::startIndex

Definition at line 448 of file NanobindUtils.h.

◆ step

template<typename Derived, typename ElementTy>
intptr_t mlir::Sliceable< Derived, ElementTy >::step

Definition at line 450 of file NanobindUtils.h.


The documentation for this class was generated from the following file: