25 #ifndef MXNET_RUNTIME_CONTAINER_H_ 26 #define MXNET_RUNTIME_CONTAINER_H_ 27 #include <dmlc/logging.h> 31 #include <initializer_list> 32 #include <type_traits> 79 template <
typename ArrayType,
typename ElemType>
88 size_t size = Self()->GetSize();
89 CHECK_LT(idx, size) <<
"Index " << idx <<
" out of bounds " << size <<
"\n";
90 return *(
reinterpret_cast<ElemType*
>(AddressOf(idx)));
99 size_t size = Self()->GetSize();
100 CHECK_LT(idx, size) <<
"Index " << idx <<
" out of bounds " << size <<
"\n";
101 return *(
reinterpret_cast<ElemType*
>(AddressOf(idx)));
108 if (!(std::is_standard_layout<ElemType>::value &&
109 std::is_trivial<ElemType>::value)) {
110 size_t size = Self()->GetSize();
111 for (
size_t i = 0; i < size; ++i) {
112 ElemType* fp =
reinterpret_cast<ElemType*
>(AddressOf(i));
113 fp->ElemType::~ElemType();
130 template <
typename... Args>
132 void* field_ptr = AddressOf(idx);
133 new (field_ptr) ElemType(std::forward<Args>(args)...);
142 inline ArrayType* Self()
const {
152 void* AddressOf(
size_t idx)
const {
153 static_assert(
alignof(ArrayType) %
alignof(ElemType) == 0 &&
154 sizeof(ArrayType) %
alignof(ElemType) == 0,
155 "The size and alignment of ArrayType should respect " 156 "ElemType's alignment.");
158 size_t kDataStart =
sizeof(ArrayType);
159 ArrayType*
self = Self();
160 char* data_start =
reinterpret_cast<char*
>(
self) + kDataStart;
161 return data_start + idx *
sizeof(ElemType);
175 static constexpr
const char* _type_key =
"MXNet.ADT";
182 size_t GetSize()
const {
return size; }
191 template <
typename Iterator>
192 void Init(Iterator begin, Iterator end) {
193 size_t num_elems = std::distance(begin, end);
196 for (
size_t i = 0; i < num_elems; ++i) {
216 ADT(uint32_t tag, std::vector<ObjectRef> fields)
217 :
ADT(tag, fields.begin(), fields.end()){};
226 template <
typename Iterator>
227 ADT(uint32_t tag, Iterator begin, Iterator end) {
228 size_t num_elems = std::distance(begin, end);
229 auto ptr = make_inplace_array_object<ADTObj, ObjectRef>(num_elems);
231 ptr->Init(begin, end);
232 data_ = std::move(ptr);
241 ADT(uint32_t tag, std::initializer_list<ObjectRef> init)
242 :
ADT(tag, init.begin(), init.end()){};
251 return operator->()->operator[](idx);
257 size_t tag()
const {
return operator->()->tag; }
262 size_t size()
const {
return operator->()->size; }
271 template <
typename... Args>
273 return ADT(0, std::forward<Args>(args)...);
282 #endif // MXNET_RUNTIME_CONTAINER_H_ ElemType & operator[](size_t idx)
Access element at index.
Definition: container.h:98
ADT(uint32_t tag, Iterator begin, Iterator end)
construct an ADT object reference.
Definition: container.h:227
#define MXNET_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:686
Runtime memory management.
namespace of mxnet
Definition: api_registry.h:33
~InplaceArrayBase()
Destroy the Inplace Array Base object.
Definition: container.h:107
static ADT Tuple(Args &&...args)
Construct a tuple object.
Definition: container.h:272
ADT(uint32_t tag, std::vector< ObjectRef > fields)
construct an ADT object reference.
Definition: container.h:216
An object representing a structure or enumeration.
Definition: container.h:166
Base class of all object reference.
Definition: object.h:499
uint32_t tag
The tag representing the constructor used.
Definition: container.h:169
const ObjectRef & operator[](size_t idx) const
Access element at index.
Definition: container.h:250
ADT(uint32_t tag, std::initializer_list< ObjectRef > init)
construct an ADT object reference.
Definition: container.h:241
base class of all object containers.
Definition: object.h:149
void EmplaceInit(size_t idx, Args &&...args)
Construct a value in place with the arguments.
Definition: container.h:131
size_t size() const
Return the number of fields.
Definition: container.h:262
size_t tag() const
Return the ADT tag.
Definition: container.h:257
A managed object in MXNet runtime.
const ElemType & operator[](size_t idx) const
Access element at index.
Definition: container.h:87
A builder class that helps to incrementally build ADT.
Definition: ffi_helper.h:106
Base template for classes with array like memory layout.
Definition: container.h:80
reference to algebraic data type objects.
Definition: container.h:208
#define MXNET_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:669