24 #ifndef TVM_RUNTIME_CONTAINER_ARRAY_H_ 25 #define TVM_RUNTIME_CONTAINER_ARRAY_H_ 41 size_t size()
const {
return this->size_; }
57 void clear() { ShrinkBy(size_); }
73 int64_t
size = from->size_;
74 ICHECK_GE(cap, size) <<
"ValueError: not enough capacity";
79 for (int64_t& i = p->size_ = 0; i < size; ++i) {
92 int64_t
size = from->size_;
93 ICHECK_GE(cap, size) <<
"ValueError: not enough capacity";
98 for (int64_t& i = p->size_ = 0; i < size; ++i) {
99 new (write++)
ObjectRef(std::move(*read++));
114 for (int64_t& i = p->size_ = 0; i < n; ++i) {
126 size_t GetSize()
const {
return this->size_; }
134 ObjectRef* MutableEnd()
const {
return MutableBegin() + size_; }
157 template <
typename IterType>
158 ArrayNode* InitRange(int64_t idx, IterType first, IterType last) {
160 for (; first != last; ++first) {
174 ArrayNode* MoveElementsLeft(int64_t dst, int64_t src_begin, int64_t src_end) {
175 ObjectRef* from = MutableBegin() + src_begin;
177 while (src_begin++ != src_end) {
178 *to++ = std::move(*from++);
190 ArrayNode* MoveElementsRight(int64_t dst, int64_t src_begin, int64_t src_end) {
191 ObjectRef* from = MutableBegin() + src_end;
192 ObjectRef* to = MutableBegin() + (src_end - src_begin + dst);
193 while (src_begin++ != src_end) {
194 *--to = std::move(*--from);
207 while (delta-- > 0) {
221 while (delta-- > 0) {
235 static constexpr int64_t kInitSize = 4;
238 static constexpr int64_t kIncFactor = 2;
244 template <
typename,
typename>
268 template <
typename T,
269 typename =
typename std::enable_if<std::is_base_of<ObjectRef, T>::value>::type>
277 Array() { data_ = ArrayNode::Empty(); }
284 data_ = std::move(other.data_);
307 template <
typename IterType>
308 Array(IterType first, IterType last) {
316 Array(std::initializer_list<T> init) {
317 Assign(init.begin(), init.end());
325 Assign(init.begin(), init.end());
341 data_ = std::move(other.data_);
392 ICHECK(p !=
nullptr) <<
"ValueError: cannot index a null array";
393 ICHECK(0 <= i && i < p->size_)
394 <<
"IndexError: indexing " << i <<
" on an array of size " << p->size_;
395 return DowncastNoCheck<T>(*(p->
begin() + i));
401 return p ==
nullptr ? 0 : GetArrayNode()->size_;
407 return p ==
nullptr ? 0 : GetArrayNode()->capacity_;
416 ICHECK(p !=
nullptr) <<
"ValueError: cannot index a null array";
417 ICHECK_GT(p->size_, 0) <<
"IndexError: cannot index an empty array";
418 return DowncastNoCheck<T>(*(p->
begin()));
424 ICHECK(p !=
nullptr) <<
"ValueError: cannot index a null array";
425 ICHECK_GT(p->size_, 0) <<
"IndexError: cannot index an empty array";
426 return DowncastNoCheck<T>(*(p->
end() - 1));
447 ICHECK(data_ !=
nullptr) <<
"ValueError: cannot insert a null array";
448 int64_t idx = std::distance(
begin(), position);
449 int64_t
size = GetArrayNode()->size_;
450 auto addr = CopyOnWrite(1)
452 ->MoveElementsRight(idx + 1, idx, size)
463 template <
typename IterType>
468 ICHECK(data_ !=
nullptr) <<
"ValueError: cannot insert a null array";
469 int64_t idx = std::distance(
begin(), position);
470 int64_t
size = GetArrayNode()->size_;
471 int64_t numel = std::distance(first, last);
474 ->MoveElementsRight(idx + numel, idx, size)
475 ->InitRange(idx, first, last);
480 ICHECK(data_ !=
nullptr) <<
"ValueError: cannot pop_back because array is null";
481 int64_t
size = GetArrayNode()->size_;
482 ICHECK_GT(size, 0) <<
"ValueError: cannot pop_back because array is empty";
483 CopyOnWrite()->ShrinkBy(1);
491 ICHECK(data_ !=
nullptr) <<
"ValueError: cannot erase a null array";
492 int64_t st = std::distance(
begin(), position);
493 int64_t
size = GetArrayNode()->size_;
494 ICHECK(0 <= st && st < size) <<
"ValueError: cannot erase at index " << st
495 <<
", because Array size is " <<
size;
497 ->MoveElementsLeft(st, st + 1, size)
510 ICHECK(data_ !=
nullptr) <<
"ValueError: cannot erase a null array";
511 int64_t
size = GetArrayNode()->size_;
512 int64_t st = std::distance(
begin(), first);
513 int64_t ed = std::distance(
begin(), last);
514 ICHECK_LT(st, ed) <<
"ValueError: cannot erase array in range [" << st <<
", " << ed <<
")";
515 ICHECK(0 <= st && st <= size && 0 <= ed && ed <= size)
516 <<
"ValueError: cannot erase array in range [" << st <<
", " << ed <<
")" 517 <<
", because array size is " <<
size;
519 ->MoveElementsLeft(st, ed, size)
528 ICHECK_GE(n, 0) <<
"ValueError: cannot resize an Array to negative size";
529 if (data_ ==
nullptr) {
533 int64_t
size = GetArrayNode()->size_;
535 CopyOnWrite(n - size)->EnlargeBy(n - size);
536 }
else if (size > n) {
537 CopyOnWrite()->ShrinkBy(size - n);
546 if (data_ ==
nullptr || n > GetArrayNode()->capacity_) {
553 if (data_ !=
nullptr) {
567 void Set(int64_t i, T value) {
569 ICHECK(0 <= i && i < p->size_)
570 <<
"IndexError: indexing " << i <<
" on an array of size " << p->size_;
571 *(p->MutableBegin() + i) = std::move(value);
583 template <
typename F>
585 if (data_ ==
nullptr) {
594 std::unique_ptr<StackFrame> s = std::make_unique<StackFrame>();
595 s->p = GetArrayNode();
596 s->itr = s->p->MutableBegin();
598 s->size = s->p->size_;
599 if (!data_.unique()) {
603 for (; s->i < s->size; ++s->i, ++s->itr) {
604 T new_elem = fmutate(DowncastNoCheck<T>(*s->itr));
606 if (new_elem.same_as(*s->itr)) {
612 s->itr = copy->MutableBegin() + (s->i++);
613 *s->itr++ = std::move(new_elem);
614 data_ = std::move(copy);
622 for (; s->i < s->size; ++s->i, ++s->itr) {
623 *s->itr = std::move(fmutate(std::move(DowncastNoCheck<T>(std::move(*s->itr)))));
633 template <
typename IterType>
634 void Assign(IterType first, IterType last) {
635 int64_t cap = std::distance(first, last);
636 ICHECK_GE(cap, 0) <<
"ValueError: cannot construct an Array of negative size";
638 if (p !=
nullptr && data_.
unique() && p->capacity_ >= cap) {
643 data_ = ArrayNode::Empty(cap);
648 for (int64_t& i = p->size_ = 0; i < cap; ++i, ++first, ++itr) {
662 if (data_ ==
nullptr) {
663 return SwitchContainer(ArrayNode::kInitSize);
665 if (!data_.unique()) {
666 return SwitchContainer(capacity());
668 return static_cast<ArrayNode*
>(data_.get());
680 ArrayNode* CopyOnWrite(int64_t reserve_extra) {
684 const int64_t kInitSize = ArrayNode::kInitSize;
685 return SwitchContainer(
std::max(kInitSize, reserve_extra));
687 if (p->capacity_ >= p->size_ + reserve_extra) {
688 return CopyOnWrite();
690 int64_t cap = p->capacity_ * ArrayNode::kIncFactor;
691 cap =
std::max(cap, p->size_ + reserve_extra);
692 return SwitchContainer(cap);
699 ArrayNode* SwitchContainer(int64_t capacity) {
700 if (data_ ==
nullptr) {
701 data_ = ArrayNode::Empty(capacity);
702 }
else if (data_.unique()) {
707 return static_cast<ArrayNode*
>(data_.get());
717 template <
typename T,
718 typename =
typename std::enable_if<std::is_base_of<ObjectRef, T>::value>::type>
720 for (
const auto& x : rhs) {
723 return std::move(lhs);
729 return ArrayNode::Empty();
739 #endif // TVM_RUNTIME_CONTAINER_ARRAY_H_ void reserve(int64_t n)
Make sure the list has the capacity of at least n.
Definition: array.h:545
static constexpr const uint32_t _type_index
Definition: array.h:120
array node content in array
Definition: array.h:38
const T operator[](int64_t i) const
Immutably read i-th element from array.
Definition: array.h:390
Managed reference to IterSplitExprNode.
Definition: iter_affine_map.h:187
A custom smart pointer for Object.
Definition: object.h:358
void insert(iterator position, IterType first, IterType last)
Insert a range of elements into the given position.
Definition: array.h:464
Array(Array< T > &&other)
move constructor
Definition: array.h:283
TVM_DECLARE_FINAL_OBJECT_INFO(ArrayNode, Object)
T ResultType
Definition: array.h:358
void SetItem(int64_t i, ObjectRef item)
Set i-th element of the array in-place.
Definition: array.h:64
const T back() const
Definition: array.h:422
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
static ObjectPtr< ArrayNode > CopyFrom(int64_t cap, ArrayNode *from)
Constructs a container and copy from another.
Definition: array.h:72
bool empty() const
Definition: array.h:411
iterator adapter that adapts TIter to return another type.
Definition: base.h:241
void resize(int64_t n)
Resize the array.
Definition: array.h:527
size_t size() const
Definition: array.h:41
Base utilities for common POD(plain old data) container types.
const ObjectRef * end() const
Definition: array.h:54
Array(const size_t n, const T &val)
Constructs a container with n elements. Each element is a copy of val.
Definition: array.h:333
friend ObjectPtr< ArrayNode > make_object()
Definition: array.h:728
ArrayNode * GetArrayNode() const
Definition: array.h:575
base class of all object containers.
Definition: object.h:167
Array()
default constructor
Definition: array.h:277
void MutateByApply(F fmutate)
Helper function to apply fmutate to mutate an array.
Definition: array.h:584
void Set(int64_t i, T value)
set i-th element of the array.
Definition: array.h:567
Array< T > & operator=(const Array< T > &other)
copy assign operator
Definition: array.h:350
void push_back(const T &item)
push a new item to the back of the list
Definition: array.h:436
Base template for classes with array like memory layout.
Definition: base.h:100
const ObjectRef * begin() const
Definition: array.h:51
void insert(iterator position, const T &val)
Insert an element into the given position.
Definition: array.h:446
size_t size() const
Definition: array.h:399
const T front() const
Definition: array.h:414
void erase(iterator position)
Erase an element on the given position.
Definition: array.h:490
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
bool unique() const
Definition: object.h:862
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:574
Array(const Array< T > &other)
copy constructor
Definition: array.h:291
void erase(iterator first, iterator last)
Erase a given range of elements.
Definition: array.h:506
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
void clear()
Release reference to all the elements.
Definition: array.h:57
reverse_iterator rbegin() const
Definition: array.h:372
void * AddressOf(size_t idx) const
Return the raw pointer to the element at idx.
Definition: base.h:169
Array(IterType first, IterType last)
Constructor from iterator.
Definition: array.h:308
iterator end() const
Definition: array.h:369
Base class of all object reference.
Definition: object.h:511
Array(const std::vector< T > &init)
constructor from vector
Definition: array.h:324
iterator begin() const
Definition: array.h:366
ArrayNode * 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.
Definition: array.h:661
Array< T > & operator=(Array< T > &&other)
move assign operator
Definition: array.h:340
iterator adapter that adapts TIter to return another type.
Definition: base.h:188
static constexpr const char * _type_key
Definition: array.h:121
runtime::Array.
Definition: object.h:68
const ObjectRef at(int64_t i) const
Read i-th element from array.
Definition: array.h:48
size_t capacity() const
Definition: array.h:405
const ObjectRef & operator[](size_t idx) const
Access element at index.
Definition: base.h:107
void pop_back()
Remove the last item of the list.
Definition: array.h:479
reverse_iterator rend() const
Definition: array.h:378
void Assign(IterType first, IterType last)
reset the array to content from iterator.
Definition: array.h:634
static T convert(const ObjectRef &n)
Definition: array.h:359
Array(std::initializer_list< T > init)
constructor from initializer list
Definition: array.h:316
void EmplaceInit(size_t idx, Args &&... args)
Construct a value in place with the arguments.
Definition: base.h:149
static ObjectPtr< ArrayNode > CreateRepeated(int64_t n, const ObjectRef &val)
Constructs a container with n elements. Each element is a copy of val.
Definition: array.h:111
Array(ObjectPtr< Object > n)
constructor from pointer
Definition: array.h:299
Array< T > Concat(Array< T > lhs, const Array< T > &rhs)
Concat two Arrays.
Definition: array.h:719
void clear()
Release reference to all the elements.
Definition: array.h:552
static ObjectPtr< ArrayNode > MoveFrom(int64_t cap, ArrayNode *from)
Constructs a container and move from another.
Definition: array.h:91