29 #include <type_traits> 51 template<
typename ValueType>
71 inline Tuple(std::initializer_list<ValueType> init) {
72 this->
assign(init.begin(), init.end());
78 inline Tuple(std::vector<ValueType> init) {
79 this->
assign(init.begin(), init.end());
95 template<
typename RandomAccessIterator>
97 RandomAccessIterator
end) {
106 template<
typename RandomAccessIterator>
108 RandomAccessIterator
end) {
109 this->
SetDim(end - begin);
110 std::copy(begin, end, this->
begin());
146 this->
assign(init.begin(), init.end());
162 return !(*
this == s);
165 inline const ValueType *
begin()
const {
173 inline const ValueType*
end()
const {
205 std::vector<ValueType> tmp(
begin(),
end());
213 std::vector<ValueType> tmp;
215 this->
assign(tmp.begin(), tmp.end());
223 friend std::ostream &operator<<(std::ostream &os, const Tuple<ValueType> &t) {
225 const ValueType*
begin = t.begin();
226 const ValueType*
end = t.end();
227 for (
const ValueType* it = begin; it !=
end; ++it) {
228 if (it != begin) os <<
',';
244 if (
isdigit(ch) || ch ==
'-') {
252 if (ch ==
'(' || ch ==
'[')
break;
254 is.setstate(std::ios::failbit);
262 if (is.peek() ==
')' || is.peek() ==
']') {
268 std::vector<ValueType> tmp;
275 if (std::is_integral<ValueType>::value && ch ==
'L') {
284 if (ch ==
')' || ch ==
']') {
289 if (ch ==
')' || ch ==
']')
break;
290 }
else if (ch ==
')' || ch ==
']') {
293 is.setstate(std::ios::failbit);
297 t.
assign(tmp.begin(), tmp.end());
306 template<
typename DType = ValueType,
typename TStream>
307 inline void Save(TStream *strm)
const;
315 template<
typename DType = ValueType,
typename TStream>
316 inline bool Load(TStream *strm);
331 if (ndim > kStackCache &&
354 std::fill_n(
begin(), ndim, 1);
367 inline TShape(std::initializer_list<dim_t> init) {
368 this->
assign(init.begin(), init.end());
383 template<
typename RandomAccessIterator>
385 RandomAccessIterator
end) {
409 const dim_t* start =
begin(), *fin =
end();
410 for (
const dim_t* it = start; it != fin; ++it) {
420 inline size_t ProdShape(
int dimstart,
int dimend)
const {
422 const dim_t *d = this->data();
423 for (
int i = dimstart; i < dimend; ++i) {
429 inline const dim_t *
data()
const {
436 #ifdef MSHADOW_XINLINE 464 CHECK_EQ(dim, static_cast<int>(
ndim()))
465 <<
"dimension do not match target dimension " << dim <<
" vs " <<
ndim();
466 const dim_t *d = this->data();
468 for (
int i = 0; i < dim; ++i) {
480 const dim_t *d = this->data();
483 for (
size_t i = 1; i <
ndim(); ++i) {
496 CHECK(axis_end >= axis_begin);
499 const dim_t *d = this->data();
504 for (
size_t i = 0; i < axis_begin; ++i) {
507 for (
size_t i = axis_begin; i <= axis_end; ++i) {
510 for (
size_t i = axis_end + 1; i <
ndim(); ++i) {
521 return FlatTo3D(axis, axis);
524 if (
ndim() != s.
ndim())
return false;
528 return !(*
this == s);
537 if (
ndim_ != dim)
return false;
539 for (
size_t i = 0; i < dim; ++i) {
540 if (d[i] != s.
shape_[i])
return false;
551 return !(*
this == s);
557 template<
typename SrcIter,
typename DstIter>
561 typedef typename std::iterator_traits<SrcIter>::value_type SrcDType;
562 typedef typename std::iterator_traits<DstIter>::value_type DstDType;
563 auto cast = [](
const SrcDType& dim) {
return static_cast<DstDType
>(dim); };
564 return std::transform(begin, end, dst_begin, cast);
568 template<
typename SrcIter>
570 size_t ndim = std::distance(begin, end);
577 template<
typename ValueType>
578 template<
typename DType,
typename TStream>
581 if (
typeid(DType) ==
typeid(ValueType)) {
582 strm->Write(
begin(),
sizeof(ValueType) *
ndim_);
584 std::vector<DType> buffer(
ndim_);
586 strm->Write(buffer.data(),
sizeof(DType) *
ndim_);
591 template<
typename ValueType>
592 template<
typename DType,
typename TStream>
596 size_t nread =
sizeof(DType) *
ndim_;
597 if (
typeid(DType) ==
typeid(ValueType)) {
598 if (strm->Read(
begin(), nread) != nread)
return false;
600 std::vector<DType> buffer(
ndim_);
601 if (strm->Read(buffer.data(), nread) != nread)
return false;
615 std::hash<uint32_t> hash_uint;
616 size_t res = hash_uint(val.
ndim());
617 for (uint32_t i = 0; i < val.
ndim(); ++i) {
629 std::hash<uint32_t> hash_uint;
630 size_t res = hash_uint(val.
ndim());
631 for (uint32_t i = 0; i < val.
ndim(); ++i) {
643 #if !defined(_MSC_VER) 647 return "tuple of <" + type_name<T>() +
">";
652 #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:136
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:52
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:328
uint32_t ndim_
number of dimension of the tuple
Definition: tuple.h:322
const ValueType & operator[](size_t i) const
get corresponding index
Definition: tuple.h:197
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:367
dim_t * data()
Definition: tuple.h:433
bool operator==(const Tuple< ValueType > &s) const
Definition: tuple.h:153
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:326
int64_t dim_t
data type to store dim size
Definition: tuple.h:39
size_t operator()(const nnvm::TShape &val) const
hash a TShape into unsigned int
Definition: tuple.h:628
Definition: optional.h:241
Tuple()=default
default constructor
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:344
TShape(uint32_t ndim)
Definition: tuple.h:352
TShape(std::initializer_list< dim_t > init)
constructor from initializer list
Definition: tuple.h:367
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:113
ValueType & operator[](size_t i)
get corresponding index
Definition: tuple.h:189
size_t Size() const
Definition: tuple.h:407
void SetDim(uint32_t ndim)
Definition: tuple.h:330
ValueType * end()
Definition: tuple.h:177
const dim_t * data() const
Definition: tuple.h:429
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:161
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:402
static const uint32_t kStackCache
Definition: tuple.h:320
DstIter ShapeTypeCast(const SrcIter begin, const SrcIter end, DstIter dst_begin)
helper function to cast type of container elements
Definition: tuple.h:558
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:614
TShape(Tuple< dim_t > &&s)
move constructor.
Definition: tuple.h:374
Tuple< ValueType > & operator=(std::initializer_list< ValueType > init)
assignment from initializer list
Definition: tuple.h:145
void Write(const ValueType &value)
Write value to json.
ValueType * begin()
Definition: tuple.h:169
friend std::istream & operator>>(std::istream &is, Tuple< ValueType > &t)
read tuple from the istream
Definition: tuple.h:240
void Save(dmlc::JSONWriter *writer) const
Save Tuple to JSON.
Definition: tuple.h:204
int num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:365
TShape & operator=(const Tuple< dim_t > &src)
assignment function from tshape
Definition: tuple.h:393
TShape(const Tuple< dim_t > &s)
copy constructor of TShape
Definition: tuple.h:360
const ValueType * end() const
Definition: tuple.h:185
const ValueType * begin() const
Definition: tuple.h:165
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:57
Tuple(const Tuple< ValueType > &s)
copy constructor from another tuple
Definition: tuple.h:64
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:198
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:54
Tuple(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:96
Tuple(std::vector< ValueType > init)
constructor from vector
Definition: tuple.h:78
const ValueType * end() const
Definition: tuple.h:173
const ValueType * begin() const
Definition: tuple.h:177
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:107
int ndim_
number of dimension of the tuple
Definition: tuple.h:363
Tuple< ValueType > & operator=(const Tuple< ValueType > &src)
assignment from another tuple.
Definition: tuple.h:127
Tuple(std::initializer_list< ValueType > init)
constructor from initializer list
Definition: tuple.h:71
~Tuple()
destructor
Definition: tuple.h:57
static std::string value()
Definition: tuple.h:646
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:209
size_t ProdShape(int dimstart, int dimend) const
Definition: tuple.h:420
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:369
void Load(dmlc::JSONReader *reader)
Load Tuple from JSON.
Definition: tuple.h:212
Tuple(Tuple< ValueType > &&src)
move constructor from Tuple
Definition: tuple.h:86
uint32_t ndim() const
Definition: tuple.h:181
uint32_t num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:324
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:116
Configuration of nnvm as well as basic data structure.
TShape(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:384
Lightweight json to write any STL compositions.
Definition: json.h:189