28 #include <type_traits> 50 template<
typename ValueType>
70 inline Tuple(std::initializer_list<ValueType> init) {
71 this->
assign(init.begin(), init.end());
77 inline Tuple(std::vector<ValueType> init) {
78 this->
assign(init.begin(), init.end());
94 template<
typename RandomAccessIterator>
96 RandomAccessIterator
end) {
105 template<
typename RandomAccessIterator>
107 RandomAccessIterator
end) {
108 this->
SetDim(end - begin);
109 std::copy(begin, end, this->
begin());
145 this->
assign(init.begin(), init.end());
161 return !(*
this == s);
164 inline const ValueType *
begin()
const {
172 inline const ValueType*
end()
const {
204 std::vector<ValueType> tmp(
begin(),
end());
212 std::vector<ValueType> tmp;
214 this->
assign(tmp.begin(), tmp.end());
222 friend std::ostream &operator<<(std::ostream &os, const Tuple<ValueType> &t) {
224 const ValueType*
begin = t.begin();
225 const ValueType*
end = t.end();
226 for (
const ValueType* it = begin; it !=
end; ++it) {
227 if (it != begin) os <<
',';
243 if (
isdigit(ch) || ch ==
'-') {
251 if (ch ==
'(' || ch ==
'[')
break;
253 is.setstate(std::ios::failbit);
261 if (is.peek() ==
')' || is.peek() ==
']') {
267 std::vector<ValueType> tmp;
274 if (std::is_integral<ValueType>::value && ch ==
'L') {
283 if (ch ==
')' || ch ==
']') {
288 if (ch ==
')' || ch ==
']')
break;
289 }
else if (ch ==
')' || ch ==
']') {
292 is.setstate(std::ios::failbit);
296 t.
assign(tmp.begin(), tmp.end());
305 template<
typename DType = ValueType,
typename TStream>
306 inline void Save(TStream *strm)
const;
314 template<
typename DType = ValueType,
typename TStream>
315 inline bool Load(TStream *strm);
330 if (ndim > kStackCache &&
353 std::fill_n(
begin(), ndim, 1);
366 inline TShape(std::initializer_list<dim_t> init) {
367 this->
assign(init.begin(), init.end());
382 template<
typename RandomAccessIterator>
384 RandomAccessIterator
end) {
408 const dim_t* start =
begin(), *fin =
end();
409 for (
const dim_t* it = start; it != fin; ++it) {
419 inline size_t ProdShape(
int dimstart,
int dimend)
const {
421 const dim_t *d = this->data();
422 for (
int i = dimstart; i < dimend; ++i) {
428 inline const dim_t *
data()
const {
435 #ifdef MSHADOW_XINLINE 463 CHECK_EQ(dim, static_cast<int>(
ndim()))
464 <<
"dimension do not match target dimension " << dim <<
" vs " <<
ndim();
465 const dim_t *d = this->data();
467 for (
int i = 0; i < dim; ++i) {
479 const dim_t *d = this->data();
482 for (
size_t i = 1; i <
ndim(); ++i) {
495 CHECK(axis_end >= axis_begin);
498 const dim_t *d = this->data();
503 for (
size_t i = 0; i < axis_begin; ++i) {
506 for (
size_t i = axis_begin; i <= axis_end; ++i) {
509 for (
size_t i = axis_end + 1; i <
ndim(); ++i) {
520 return FlatTo3D(axis, axis);
523 if (
ndim() != s.
ndim())
return false;
527 return !(*
this == s);
536 if (
ndim_ != dim)
return false;
538 for (
size_t i = 0; i < dim; ++i) {
539 if (d[i] != s.
shape_[i])
return false;
550 return !(*
this == s);
556 template<
typename SrcIter,
typename DstIter>
560 typedef typename std::iterator_traits<SrcIter>::value_type SrcDType;
561 typedef typename std::iterator_traits<DstIter>::value_type DstDType;
562 auto cast = [](
const SrcDType& dim) {
return static_cast<DstDType
>(dim); };
563 return std::transform(begin, end, dst_begin, cast);
567 template<
typename SrcIter>
569 size_t ndim = std::distance(begin, end);
576 template<
typename ValueType>
577 template<
typename DType,
typename TStream>
580 if (
typeid(DType) ==
typeid(ValueType)) {
581 strm->Write(
begin(),
sizeof(ValueType) *
ndim_);
583 std::vector<DType> buffer(
ndim_);
585 strm->Write(buffer.data(),
sizeof(DType) *
ndim_);
590 template<
typename ValueType>
591 template<
typename DType,
typename TStream>
595 size_t nread =
sizeof(DType) *
ndim_;
596 if (
typeid(DType) ==
typeid(ValueType)) {
597 if (strm->Read(
begin(), nread) != nread)
return false;
599 std::vector<DType> buffer(
ndim_);
600 if (strm->Read(buffer.data(), nread) != nread)
return false;
614 std::hash<uint32_t> hash_uint;
615 size_t res = hash_uint(val.
ndim());
616 for (uint32_t i = 0; i < val.
ndim(); ++i) {
628 std::hash<uint32_t> hash_uint;
629 size_t res = hash_uint(val.
ndim());
630 for (uint32_t i = 0; i < val.
ndim(); ++i) {
642 #if !defined(_MSC_VER) 646 return "tuple of <" + type_name<T>() +
">";
651 #endif // NNVM_TUPLE_H_ #define DMLC_DECLARE_TYPE_NAME(Type, Name)
macro to quickly declare traits information
Definition: type_traits.h:133
helper class to construct a string that represents type name
Definition: type_traits.h:86
Tuple< ValueType > & operator=(Tuple< ValueType > &&src)
assignment from rvalue of another tuple.
Definition: tuple.h:135
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:51
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:327
uint32_t ndim_
number of dimension of the tuple
Definition: tuple.h:321
const ValueType & operator[](size_t i) const
get corresponding index
Definition: tuple.h:196
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:392
dim_t * data()
Definition: tuple.h:432
bool operator==(const Tuple< ValueType > &s) const
Definition: tuple.h:152
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:325
int64_t dim_t
data type to store dim size
Definition: tuple.h:38
size_t operator()(const nnvm::TShape &val) const
hash a TShape into unsigned int
Definition: tuple.h:627
Definition: optional.h:241
Tuple()=default
default constructor
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:343
TShape(uint32_t ndim)
Definition: tuple.h:351
TShape(std::initializer_list< dim_t > init)
constructor from initializer list
Definition: tuple.h:366
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:138
ValueType & operator[](size_t i)
get corresponding index
Definition: tuple.h:188
size_t Size() const
Definition: tuple.h:406
void SetDim(uint32_t ndim)
Definition: tuple.h:329
ValueType * end()
Definition: tuple.h:176
const dim_t * data() const
Definition: tuple.h:428
size_t HashCombine(size_t key, const T &value)
hash an object and combines the key with previous keys
Definition: common.h:37
bool operator!=(const Tuple< ValueType > &s) const
Definition: tuple.h:160
Lightweight JSON Reader to read any STL compositions and structs. The user need to know the schema of...
Definition: json.h:44
bool isspace(char c)
Inline implementation of isspace(). Tests whether the given character is a whitespace letter...
Definition: strtonum.h:26
TShape & operator=(Tuple< dim_t > &&src)
move assignment function from tshape
Definition: tuple.h:401
static const uint32_t kStackCache
Definition: tuple.h:319
DstIter ShapeTypeCast(const SrcIter begin, const SrcIter end, DstIter dst_begin)
helper function to cast type of container elements
Definition: tuple.h:557
namespace for dmlc
Definition: array_view.h:12
size_t operator()(const nnvm::Tuple< T > &val) const
hash a Tuple into unsigned int
Definition: tuple.h:613
TShape(Tuple< dim_t > &&s)
move constructor.
Definition: tuple.h:373
Tuple< ValueType > & operator=(std::initializer_list< ValueType > init)
assignment from initializer list
Definition: tuple.h:144
void Write(const ValueType &value)
Write value to json.
ValueType * begin()
Definition: tuple.h:168
friend std::istream & operator>>(std::istream &is, Tuple< ValueType > &t)
read tuple from the istream
Definition: tuple.h:239
void Save(dmlc::JSONWriter *writer) const
Save Tuple to JSON.
Definition: tuple.h:203
int num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:390
TShape & operator=(const Tuple< dim_t > &src)
assignment function from tshape
Definition: tuple.h:392
TShape(const Tuple< dim_t > &s)
copy constructor of TShape
Definition: tuple.h:359
const ValueType * end() const
Definition: tuple.h:210
const ValueType * begin() const
Definition: tuple.h:164
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:76
Tuple(const Tuple< ValueType > &s)
copy constructor from another tuple
Definition: tuple.h:63
void Read(ValueType *out_value)
Read next ValueType.
MSHADOW_XINLINE Shape< 2 > Shape2(index_t s0, index_t s1)
construct a two dimension shape, stride will equal s0
Definition: tensor.h:217
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:58
Tuple(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:95
Tuple(std::vector< ValueType > init)
constructor from vector
Definition: tuple.h:77
const ValueType * end() const
Definition: tuple.h:172
const ValueType * begin() const
Definition: tuple.h:202
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:106
int ndim_
number of dimension of the tuple
Definition: tuple.h:388
Tuple< ValueType > & operator=(const Tuple< ValueType > &src)
assignment from another tuple.
Definition: tuple.h:126
Tuple(std::initializer_list< ValueType > init)
constructor from initializer list
Definition: tuple.h:70
~Tuple()
destructor
Definition: tuple.h:56
static std::string value()
Definition: tuple.h:645
MSHADOW_XINLINE Shape< 3 > Shape3(index_t s0, index_t s1, index_t s2)
construct a three dimension shape, stride will equal s0
Definition: tensor.h:228
size_t ProdShape(int dimstart, int dimend) const
Definition: tuple.h:419
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:394
void Load(dmlc::JSONReader *reader)
Load Tuple from JSON.
Definition: tuple.h:211
Tuple(Tuple< ValueType > &&src)
move constructor from Tuple
Definition: tuple.h:85
uint32_t ndim() const
Definition: tuple.h:180
uint32_t num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:323
bool isdigit(char c)
Inline implementation of isdigit(). Tests whether the given character is a decimal digit...
Definition: strtonum.h:46
void swap(Tuple< ValueType > &other)
Swap current object with other.
Definition: tuple.h:115
Configuration of nnvm as well as basic data structure.
TShape(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:383
Lightweight json to write any STL compositions.
Definition: json.h:189