24 #ifndef MXNET_NODE_CONTAINER_H_ 25 #define MXNET_NODE_CONTAINER_H_ 29 #include <type_traits> 31 #include <initializer_list> 32 #include <unordered_map> 42 std::vector<ObjectRef>
data;
53 template<
typename Converter,
59 using pointer =
typename Converter::ResultType*;
72 template<
typename T = IterAdapter>
73 typename std::enable_if<std::is_same<iterator_category, std::random_access_iterator_tag>::value,
74 typename T::difference_type>::type
76 return iter_ - rhs.iter_;
80 return iter_ == other.iter_;
83 return !(*
this == other);
86 return Converter::convert(*iter_);
102 typename =
typename std::enable_if<std::is_base_of<ObjectRef, T>::value>::type >
109 data_ = make_object<ArrayNode>();
116 data_ = std::move(other.data_);
123 data_ = std::move(other.data_);
136 template<
typename IterType>
137 Array(IterType begin, IterType end) {
144 Array(std::initializer_list<T> init) {
145 assign(init.begin(), init.end());
152 assign(init.begin(), init.end());
159 explicit Array(
size_t n,
const T& val) {
160 auto tmp_node = make_object<ArrayNode>();
161 for (
size_t i = 0; i < n; ++i) {
162 tmp_node->data.push_back(val);
164 data_ = std::move(tmp_node);
172 data_ = std::move(other.data_);
190 template<
typename IterType>
191 void assign(IterType begin, IterType end) {
192 auto n = make_object<ArrayNode>();
193 for (IterType it = begin; it != end; ++it) {
194 n->data.push_back(T(*it));
196 data_ = std::move(n);
204 return DowncastNoCheck<T>(
209 if (data_.get() ==
nullptr)
return 0;
210 return static_cast<const ArrayNode*
>(data_.get())->
data.size();
221 if (data_.get() ==
nullptr || !data_.unique()) {
226 return static_cast<ArrayNode*
>(data_.get());
234 n->
data.push_back(item);
242 n->
data.resize(size);
249 inline void Set(
size_t i,
const T& value) {
266 if (ptr ==
nullptr)
return;
267 if (data_.unique()) {
270 for (
size_t i = 0; i < ptr->
data.size(); ++i) {
274 T old_elem = DowncastNoCheck<T>(std::move(ptr->
data[i]));
275 T new_elem = fmutate(std::move(old_elem));
276 ptr->
data[i] = std::move(new_elem);
281 for (
size_t i = 0; i < ptr->
data.size(); ++i) {
282 T old_elem = DowncastNoCheck<T>(ptr->
data[i]);
283 T new_elem = fmutate(old_elem);
284 if (!new_elem.same_as(ptr->
data[i])) {
286 if (copy ==
nullptr) {
287 copy = runtime::make_object<ArrayNode>(*ptr);
289 copy->data[i] = std::move(new_elem);
293 if (copy !=
nullptr) {
294 data_ = std::move(copy);
305 return DowncastNoCheck<T>(n);
309 std::vector<ObjectRef>::const_iterator>;
313 std::vector<ObjectRef>::const_reverse_iterator>;
317 return iterator(static_cast<const ArrayNode*>(data_.get())->
data.begin());
321 return iterator(static_cast<const ArrayNode*>(data_.get())->
data.end());
334 #endif // MXNET_NODE_CONTAINER_H_ bool operator==(IterAdapter other) const
Definition: container.h:79
IterAdapter(TIter iter)
Definition: container.h:63
Array< T > & operator=(const Array< T > &other)
copy assign operator
Definition: container.h:180
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: container.h:220
Array(std::initializer_list< T > init)
constructor from initializer list
Definition: container.h:144
typename Converter::ResultType & reference
Definition: container.h:60
bool operator!=(IterAdapter other) const
Definition: container.h:82
std::vector< ObjectRef > data
the data content
Definition: container.h:42
namespace of mxnet
Definition: api_registry.h:33
iterator end() const
Definition: container.h:320
typename Converter::ResultType * pointer
Definition: container.h:59
static T convert(const ObjectRef &n)
Definition: container.h:304
MXNET_DECLARE_FINAL_OBJECT_INFO(ArrayNode, Object)
reverse_iterator rend() const
Definition: container.h:328
A custom smart pointer for Object.
Definition: object.h:345
bool empty() const
Definition: container.h:254
IterAdapter & operator++()
Definition: container.h:64
Definition: container.h:302
reverse_iterator rbegin() const
Definition: container.h:324
void Set(size_t i, const T &value)
set i-th element of the array.
Definition: container.h:249
typename Converter::ResultType value_type
Definition: container.h:58
Array(size_t n, const T &val)
Constructs a container with n elements. Each element is a copy of val.
Definition: container.h:159
IterAdapter operator+(difference_type offset) const
Definition: container.h:68
std::enable_if< std::is_same< iterator_category, std::random_access_iterator_tag >::value, typename T::difference_type >::type operator-(const IterAdapter &rhs) const
Definition: container.h:75
BinaryMapExp< OP, TA, ScalarExp< MSHADOW_SCALAR_ >, MSHADOW_SCALAR_,(ta|type::kMapper)> F(const Exp< TA, MSHADOW_SCALAR_, ta > &lhs, const ScalarExp< MSHADOW_SCALAR_ > &rhs)
operator overload for const
Definition: expr_scalar-inl.h:72
Array< T > & operator=(Array< T > &&other)
move assign operator
Definition: container.h:171
const value_type operator*() const
Definition: container.h:85
void resize(size_t size)
Resize the array.
Definition: container.h:240
void push_back(const T &item)
push a new item to the back of the list
Definition: container.h:232
array node content in array
Definition: container.h:39
Array(const Array< T > &other)
copy constructor
Definition: container.h:122
typename std::iterator_traits< TIter >::difference_type difference_type
Definition: container.h:57
T ResultType
Definition: container.h:303
void MutateByApply(F fmutate)
Helper function to apply fmutate to mutate an array.
Definition: container.h:264
iterator begin() const
Definition: container.h:316
Array container of NodeRef in DSL graph. Array implements copy on write semantics, which means array is mutable but copy will happen when array is referenced in more than two places.
Definition: container.h:103
typename std::iterator_traits< TIter >::iterator_category iterator_category
Definition: container.h:61
const T operator[](size_t i) const
Read i-th element from array.
Definition: container.h:203
Array(Array< T > &&other)
move constructor
Definition: container.h:115
Array()
default constructor
Definition: container.h:108
size_t size() const
Definition: container.h:208
static constexpr const char * _type_key
Definition: container.h:44
iterator adapter that adapts TIter to return another type.
Definition: container.h:55
Array(const std::vector< T > &init)
constructor from vector
Definition: container.h:151
Array(runtime::ObjectPtr< Object > n)
constructor from pointer
Definition: container.h:129
Array(IterType begin, IterType end)
constructor from iterator
Definition: container.h:137
void assign(IterType begin, IterType end)
reset the array to content from iterator.
Definition: container.h:191