26 #ifndef MXNET_CPP_SHAPE_H_
27 #define MXNET_CPP_SHAPE_H_
45 Shape() : ndim_(0), num_heap_allocated_(0), data_heap_(nullptr) {}
50 explicit Shape(
const std::vector<index_t>& v) : ndim_(v.size()) {
51 if (ndim_ <= kStackCache) {
53 num_heap_allocated_ = 0;
54 std::copy(v.begin(), v.end(), data_stack_);
56 data_heap_ =
new index_t[ndim_];
57 num_heap_allocated_ = ndim_;
58 std::copy(v.begin(), v.end(), data_heap_);
66 if (ndim_ <= kStackCache) {
68 num_heap_allocated_ = 0;
71 data_heap_ =
new index_t[ndim_];
72 num_heap_allocated_ = ndim_;
82 if (ndim_ <= kStackCache) {
84 num_heap_allocated_ = 0;
88 data_heap_ =
new index_t[ndim_];
89 num_heap_allocated_ = ndim_;
101 if (ndim_ <= kStackCache) {
102 data_heap_ =
nullptr;
103 num_heap_allocated_ = 0;
108 data_heap_ =
new index_t[ndim_];
109 num_heap_allocated_ = ndim_;
123 if (ndim_ <= kStackCache) {
124 data_heap_ =
nullptr;
125 num_heap_allocated_ = 0;
131 data_heap_ =
new index_t[ndim_];
132 num_heap_allocated_ = ndim_;
148 if (ndim_ <= kStackCache) {
149 data_heap_ =
nullptr;
150 num_heap_allocated_ = 0;
157 data_heap_ =
new index_t[ndim_];
158 num_heap_allocated_ = ndim_;
171 if (ndim_ <= kStackCache) {
172 data_heap_ =
nullptr;
173 num_heap_allocated_ = 0;
174 std::copy(s.data_stack_, s.data_stack_ + ndim_, data_stack_);
176 data_heap_ =
new index_t[ndim_];
177 num_heap_allocated_ = ndim_;
178 std::copy(s.data_heap_, s.data_heap_ + ndim_, data_heap_);
187 : ndim_(s.ndim_), num_heap_allocated_(s.num_heap_allocated_), data_heap_(s.data_heap_) {
188 if (ndim_ <= kStackCache) {
189 std::copy(s.data_stack_, s.data_stack_ + ndim_, data_stack_);
192 s.data_heap_ =
nullptr;
206 template <
typename RandomAccessIterator>
207 inline void CopyFrom(RandomAccessIterator begin, RandomAccessIterator end) {
208 this->SetDim(end - begin);
209 std::copy(begin, end,
data());
217 this->SetDim(shape.ndim_);
219 std::copy(src, src + ndim_,
data());
228 this->
CopyFrom(shape.begin(), shape.end());
233 return ndim_ <= kStackCache ? data_stack_ : data_heap_;
237 return ndim_ <= kStackCache ? data_stack_ : data_heap_;
260 inline size_t Size(
void)
const {
263 for (
index_t i = 0; i < ndim_; ++i) {
273 if (ndim_ != s.ndim_)
275 if (ndim_ <= kStackCache) {
276 for (
index_t i = 0; i < ndim_; ++i) {
277 if (data_stack_[i] != s.data_stack_[i])
281 for (
index_t i = 0; i < ndim_; ++i) {
282 if (data_heap_[i] != s.data_heap_[i])
293 return !(*
this == s);
304 static const index_t kStackCache = 5;
310 index_t data_stack_[kStackCache];
317 inline void SetDim(
index_t dim) {
318 if (dim > kStackCache && dim > num_heap_allocated_) {
322 num_heap_allocated_ = dim;
339 os << static_cast<int>(shape[i]);
342 if (shape.
ndim() == 1)
361 is.setstate(std::ios::failbit);
366 std::vector<index_t> tmp;
388 }
else if (ch ==
')') {
391 is.setstate(std::ios::failbit);
395 shape.
CopyFrom(tmp.begin(), tmp.end());
402 #endif // MXNET_CPP_SHAPE_H_