Template Class Array#

Inheritance Relationships#

Base Type#

Class Documentation#

template<typename T, typename = typename std::enable_if_t<details::storage_enabled_v<T>>>
class Array : public tvm::ffi::ObjectRef#

Array, container representing a contiguous sequence of ObjectRefs.

Array implements in-place copy-on-write semantics.

As in typical copy-on-write, a method which would typically mutate the array instead opaquely copies the underlying container, and then acts on its copy.

If the array has reference count equal to one, we directly update the container in place without copying. This is optimization is sound because when the reference count is equal to one this reference is guranteed to be the sole pointer to the container.

operator[] only provides const access, use Set to mutate the content.

Template Parameters:

T – The content Value type, must be compatible with tvm::ffi::Any

Public Types

using value_type = T#

The value type of the array.

using iterator = details::IterAdapter<ValueConverter, const Any*>#

The iterator type of the array.

using reverse_iterator = details::ReverseIterAdapter<ValueConverter, const Any*>#

The reverse iterator type of the array.

using ContainerType = ArrayObj#

specify container node

Public Functions

inline explicit Array(UnsafeInit tag)#

Construct an Array with UnsafeInit.

inline Array()#

default constructor

inline Array(Array<T> &&other)#

Move constructor.

Parameters:

other – The other array

inline Array(const Array<T> &other)#

Copy constructor.

Parameters:

other – The other array

template<typename U, typename = std::enable_if_t<details::type_contains_v<T, U>>>
inline Array(Array<U> &&other)#

Constructor from another array.

Parameters:

other – The other array

Template Parameters:

U – The value type of the other array

template<typename U, typename = std::enable_if_t<details::type_contains_v<T, U>>>
inline Array(const Array<U> &other)#

Constructor from another array.

Parameters:

other – The other array

Template Parameters:

U – The value type of the other array

inline explicit Array(ObjectPtr<Object> n)#

Constructor from pointer.

Parameters:

n – the container pointer

template<typename IterType>
inline Array(IterType first, IterType last)#

Constructor from iterator.

Parameters:
  • first – begin of iterator

  • last – end of iterator

Template Parameters:

IterType – The type of iterator

inline Array(std::initializer_list<T> init)#

constructor from initializer list

Parameters:

init – The initializer list

inline Array(const std::vector<T> &init)#

constructor from vector

Parameters:

init – The vector

inline explicit Array(const size_t n, const T &val)#

Constructs a container with n elements. Each element is a copy of val.

Parameters:
  • n – The size of the container

  • val – The init value

inline iterator begin() const#
Returns:

begin iterator

inline iterator end() const#
Returns:

end iterator

inline reverse_iterator rbegin() const#
Returns:

rbegin iterator

inline reverse_iterator rend() const#
Returns:

rend iterator

inline size_t size() const#
Returns:

The size of the array

inline size_t capacity() const#
Returns:

The capacity of the array

inline bool empty() const#
Returns:

Whether array is empty

inline const T front() const#
Returns:

The first element of the array

inline const T back() const#
Returns:

The last element of the array

inline void push_back(const T &item)#

push a new item to the back of the list

Parameters:

item – The item to be pushed.

template<typename ...Args>
inline void emplace_back(Args&&... args)#

Emplace a new element at the back of the array.

Parameters:

args – The arguments to construct the new element

inline void insert(iterator position, const T &val)#

Insert an element into the given position.

Parameters:
  • position – An iterator pointing to the insertion point

  • val – The element to insert

template<typename IterType>
inline void insert(iterator position, IterType first, IterType last)#

Insert a range of elements into the given position.

Parameters:
  • position – An iterator pointing to the insertion point

  • first – The begin iterator of the range

  • last – The end iterator of the range

inline void pop_back()#

Remove the last item of the list.

inline void erase(iterator position)#

Erase an element on the given position.

Parameters:

position – An iterator pointing to the element to be erased

inline void erase(iterator first, iterator last)#

Erase a given range of elements.

Parameters:
  • first – The begin iterator of the range

  • last – The end iterator of the range

inline void resize(int64_t n)#

Resize the array.

Parameters:

n – The new size.

inline void reserve(int64_t n)#

Make sure the list has the capacity of at least n.

Parameters:

n – lower bound of the capacity

inline void clear()#

Release reference to all the elements.

inline void Set(int64_t i, T value)#

set i-th element of the array.

Parameters:
  • i – The index

  • value – The value to be setted.

inline ArrayObj *GetArrayObj() const#
Returns:

The underlying ArrayObj

template<typename F, typename U = std::invoke_result_t<F, T>>
inline Array<U> Map(F fmap) const#

Helper function to apply a map function onto the array.

Note

This function performs copy on write optimization. If fmap returns an object of type T, and all elements of the array are mapped to themselves, then the returned array will be the same as the original, and reference counts of the elements in the array will not be incremented.

Parameters:

fmap – The transformation function T -> U.

Template Parameters:
  • F – The type of the mutation function.

  • U – The type of the returned array, inferred from the return type of F. If overridden by the user, must be something that is convertible from the return type of F.

Returns:

The transformed array.

template<typename F, typename = std::enable_if_t<std::is_same_v<T, std::invoke_result_t<F, T>>>>
inline void MutateByApply(F fmutate)#

Helper function to apply fmutate to mutate an array.

Note

This function performs copy on write optimization.

Parameters:

fmutate – The transformation function T -> T.

Template Parameters:

F – the type of the mutation function.

template<typename IterType>
inline void Assign(IterType first, IterType last)#

reset the array to content from iterator.

Parameters:
  • first – begin of iterator

  • last – end of iterator

Template Parameters:

IterType – The type of iterator

inline ArrayObj *CopyOnWrite()#

Copy on write semantics Do nothing if current handle is the unique copy of the array. Otherwise make a new copy of the array to ensure the current handle hold a unique copy.

Returns:

Handle to the internal node container(which ganrantees to be unique)

Public Static Functions

template<typename ...Args>
static inline Array<T> Agregate(Args... args)#

Agregate arguments into a single Array<T>

Parameters:

args – sequence of T or Array<T> elements

Returns:

Agregated Array<T>