Go to the documentation of this file.
30 #include <type_traits>
51 template <
typename ValueType>
67 inline Tuple(std::initializer_list<ValueType> init) { this->
assign(init.begin(), init.end()); }
72 inline Tuple(std::vector<ValueType> init) {
73 this->
assign(init.begin(), init.end());
89 template <
typename RandomAccessIterator>
99 template <
typename RandomAccessIterator>
138 this->
assign(init.begin(), init.end());
159 inline const ValueType*
end()
const {
185 std::vector<ValueType> tmp(
begin(),
end());
193 std::vector<ValueType> tmp;
195 this->
assign(tmp.begin(), tmp.end());
206 const ValueType*
end = t.
end();
207 for (
const ValueType* it =
begin; it !=
end; ++it) {
208 if (it !=
begin) os <<
',';
224 if (
isdigit(ch) || ch ==
'-') {
232 if (ch ==
'(' || ch ==
'[')
break;
234 is.setstate(std::ios::failbit);
242 if (is.peek() ==
')' || is.peek() ==
']') {
248 std::vector<ValueType> tmp;
255 if (std::is_integral<ValueType>::value && ch ==
'L') {
265 if (ch ==
')' || ch ==
']') {
271 if (ch ==
')' || ch ==
']')
break;
272 }
else if (ch ==
')' || ch ==
']') {
275 is.setstate(std::ios::failbit);
279 t.
assign(tmp.begin(), tmp.end());
288 template <
typename DType = ValueType,
typename TStream>
289 inline void Save(TStream* strm)
const;
297 template <
typename DType = ValueType,
typename TStream>
298 inline bool Load(TStream* strm);
348 inline TShape(std::initializer_list<dim_t> init) { this->
assign(init.begin(), init.end()); }
362 template <
typename RandomAccessIterator>
381 TShape(std::move(src)).swap(*
this);
388 for (
const dim_t* it = start; it != fin; ++it) {
398 inline size_t ProdShape(
int dimstart,
int dimend)
const {
401 for (
int i = dimstart; i < dimend; ++i) {
410 #ifdef MSHADOW_XINLINE
418 this->
assign(s.shape_, s.shape_ + dim);
438 CHECK_EQ(dim,
static_cast<int>(
ndim()))
439 <<
"dimension do not match target dimension " << dim <<
" vs " <<
ndim();
442 for (
int i = 0; i < dim; ++i) {
457 for (
size_t i = 1; i <
ndim(); ++i) {
470 CHECK(axis_end >= axis_begin);
478 for (
size_t i = 0; i < axis_begin; ++i) {
481 for (
size_t i = axis_begin; i <= axis_end; ++i) {
484 for (
size_t i = axis_end + 1; i <
ndim(); ++i) {
494 inline mshadow::Shape<3> FlatTo3D(
size_t axis)
const {
return FlatTo3D(axis, axis); }
496 if (
ndim() != s.ndim())
return false;
497 return std::equal(
begin(),
end(), s.begin());
507 if (
ndim_ != dim)
return false;
509 for (
size_t i = 0; i < dim; ++i) {
510 if (d[i] != s.
shape_[i])
return false;
521 return !(*
this == s);
527 template <
typename SrcIter,
typename DstIter>
528 inline DstIter
ShapeTypeCast(
const SrcIter begin,
const SrcIter end, DstIter dst_begin) {
529 typedef typename std::iterator_traits<SrcIter>::value_type SrcDType;
530 typedef typename std::iterator_traits<DstIter>::value_type DstDType;
531 auto cast = [](
const SrcDType& dim) {
return static_cast<DstDType
>(dim); };
532 return std::transform(begin, end, dst_begin, cast);
536 template <
typename SrcIter>
538 size_t ndim = std::distance(begin, end);
545 template <
typename ValueType>
546 template <
typename DType,
typename TStream>
548 strm->Write(&ndim_,
sizeof(ndim_));
549 if (
typeid(DType) ==
typeid(ValueType)) {
550 strm->Write(begin(),
sizeof(ValueType) * ndim_);
552 std::vector<DType> buffer(ndim_);
554 strm->Write(buffer.data(),
sizeof(DType) * ndim_);
559 template <
typename ValueType>
560 template <
typename DType,
typename TStream>
562 if (strm->Read(&ndim_,
sizeof(ndim_)) !=
sizeof(ndim_))
return false;
564 size_t nread =
sizeof(DType) * ndim_;
565 if (
typeid(DType) ==
typeid(ValueType)) {
566 if (strm->Read(begin(), nread) != nread)
return false;
568 std::vector<DType> buffer(ndim_);
569 if (strm->Read(buffer.data(), nread) != nread)
return false;
579 template <
typename T>
583 std::hash<uint32_t> hash_uint;
584 size_t res = hash_uint(val.
ndim());
585 for (uint32_t i = 0; i < val.
ndim(); ++i) {
597 std::hash<uint32_t> hash_uint;
598 size_t res = hash_uint(val.
ndim());
599 for (uint32_t i = 0; i < val.
ndim(); ++i) {
611 #if !defined(_MSC_VER)
612 template <
typename T>
614 static inline std::string
value() {
return "tuple of <" + type_name<T>() +
">"; }
618 #endif // NNVM_TUPLE_H_
const ValueType * begin() const
Definition: tuple.h:201
void Read(ValueType *out_value)
Read next ValueType.
size_t operator()(const nnvm::TShape &val) const
hash a TShape into unsigned int
Definition: tuple.h:596
helper class to construct a string that represents type name
Definition: type_traits.h:86
int ndim_
number of dimension of the tuple
Definition: tuple.h:392
const ValueType * end() const
Definition: tuple.h:209
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:57
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:100
void Load(dmlc::JSONReader *reader)
Load Tuple from JSON.
Definition: tuple.h:192
void Write(const ValueType &value)
Write value to json.
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:52
TShape(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:363
bool isspace(char c)
Inline implementation of isspace(). Tests whether the given character is a whitespace letter.
Definition: strtonum.h:26
TShape & operator=(const Tuple< dim_t > &src)
assignment function from tshape
Definition: tuple.h:371
TShape(Tuple< dim_t > &&s)
move constructor.
Definition: tuple.h:353
namespace for dmlc
Definition: array_view.h:12
Tuple()=default
default constructor
TShape(uint32_t ndim)
Definition: tuple.h:333
TShape & operator=(Tuple< dim_t > &&src)
move assignment function from tshape
Definition: tuple.h:380
friend std::ostream & operator<<(std::ostream &os, const Tuple< ValueType > &t)
allow output string of tuple to ostream
Definition: tuple.h:203
Tuple(std::initializer_list< ValueType > init)
constructor from initializer list
Definition: tuple.h:67
dim_t * data()
Definition: tuple.h:409
bool isdigit(char c)
Inline implementation of isdigit(). Tests whether the given character is a decimal digit.
Definition: strtonum.h:46
const ValueType * begin() const
Definition: tuple.h:155
uint32_t num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:306
~Tuple()
destructor
Definition: tuple.h:57
Configuration of nnvm as well as basic data structure.
DMLC_DECLARE_TYPE_NAME(optional< int >, "int or None")
description for optional int
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:396
ValueType & operator[](size_t i)
get corresponding index
Definition: tuple.h:173
static const uint32_t kStackCache
Definition: tuple.h:302
uint32_t ndim() const
Definition: tuple.h:167
size_t HashCombine(size_t key, const T &value)
hash an object and combines the key with previous keys
Definition: common.h:37
Tuple(Tuple< ValueType > &&src)
move constructor from Tuple
Definition: tuple.h:80
bool operator!=(const Tuple< ValueType > &s) const
Definition: tuple.h:153
size_t Size() const
Definition: tuple.h:385
int64_t dim_t
data type to store dim size
Definition: c_api.h:69
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:86
Tuple(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:90
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:325
TShape()=default
default constructor
friend std::istream & operator>>(std::istream &is, Tuple< ValueType > &t)
read tuple from the istream
Definition: tuple.h:220
int num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:394
const dim_t * data() const
Definition: tuple.h:407
TShape(const Tuple< dim_t > &s)
copy constructor of TShape
Definition: tuple.h:341
void swap(Tuple< ValueType > &other)
Swap current object with other.
Definition: tuple.h:108
ValueType * end()
Definition: tuple.h:163
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:310
bool operator==(const Tuple< ValueType > &s) const
Definition: tuple.h:145
int64_t dim_t
data type to store dim size
Definition: tuple.h:39
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:308
Lightweight json to write any STL compositions.
Definition: json.h:190
const ValueType & operator[](size_t i) const
get corresponding index
Definition: tuple.h:179
MSHADOW_XINLINE Shape< 2 > Shape2(index_t s0, index_t s1)
construct a two dimension shape, stride will equal s0
Definition: tensor.h:230
Definition: optional.h:251
const ValueType * end() const
Definition: tuple.h:159
DstIter ShapeTypeCast(const SrcIter begin, const SrcIter end, DstIter dst_begin)
helper function to cast type of container elements
Definition: tuple.h:528
Tuple< ValueType > & operator=(Tuple< ValueType > &&src)
assignment from rvalue of another tuple.
Definition: tuple.h:128
Tuple(const Tuple< ValueType > &s)
copy constructor from another tuple
Definition: tuple.h:62
size_t ProdShape(int dimstart, int dimend) const
Definition: tuple.h:398
Tuple< ValueType > & operator=(const Tuple< ValueType > &src)
assignment from another tuple.
Definition: tuple.h:119
uint32_t ndim_
number of dimension of the tuple
Definition: tuple.h:304
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:136
void Save(dmlc::JSONWriter *writer) const
Save Tuple to JSON.
Definition: tuple.h:184
Tuple(std::vector< ValueType > init)
constructor from vector
Definition: tuple.h:72
ValueType * begin()
Definition: tuple.h:157
Lightweight JSON Reader to read any STL compositions and structs. The user need to know the schema of...
Definition: json.h:44
void SetDim(uint32_t ndim)
Definition: tuple.h:312
TShape(std::initializer_list< dim_t > init)
constructor from initializer list
Definition: tuple.h:348
Tuple< ValueType > & operator=(std::initializer_list< ValueType > init)
assignment from initializer list
Definition: tuple.h:137
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:398
void swap(Tuple< ValueType > &other)
Swap current object with other.
Definition: tuple.h:145
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:241
size_t operator()(const nnvm::Tuple< T > &val) const
hash a Tuple into unsigned int
Definition: tuple.h:582
static std::string value()
Definition: tuple.h:614