Template Class Array#
Defined in File array.h
Inheritance Relationships#
Base Type#
public tvm::ffi::ObjectRef
(Class ObjectRef)
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
Public Functions
-
inline explicit Array(UnsafeInit tag)#
Construct an Array with UnsafeInit.
-
inline Array()#
default constructor
-
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 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 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 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.
-
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 typeT
, 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.