diff --git a/dlib/matrix/matrix.h b/dlib/matrix/matrix.h index 84a71f2dc..f7dad950d 100644 --- a/dlib/matrix/matrix.h +++ b/dlib/matrix/matrix.h @@ -16,6 +16,9 @@ #include "matrix_assign_fwd.h" #include "matrix_op.h" #include +#ifdef DLIB_HAS_RVALUE_REFERENCES +#include +#endif #ifdef MATLAB_MEX_FILE #include @@ -1112,6 +1115,55 @@ namespace dlib } #ifdef DLIB_HAS_RVALUE_REFERENCES + matrix(const std::initializer_list& l) + { + if (NR*NC != 0) + { + DLIB_ASSERT(l.size() == NR*NC, + "\t matrix::matrix(const std::initializer_list& l)" + << "\n\t You are trying to initialize a statically sized matrix with a list that doesn't have a matching size." + << "\n\t l.size(): "<< l.size() + << "\n\t NR*NC: "<< NR*NC); + + data.set_size(NR, NC); + } + else if (NR!=0) + { + DLIB_ASSERT(l.size()%NR == 0, + "\t matrix::matrix(const std::initializer_list& l)" + << "\n\t You are trying to initialize a statically sized matrix with a list that doesn't have a compatible size." + << "\n\t l.size(): "<< l.size() + << "\n\t NR: "<< NR); + + if (l.size() != 0) + data.set_size(NR, l.size()/NR); + } + else if (NC!=0) + { + DLIB_ASSERT(l.size()%NC == 0, + "\t matrix::matrix(const std::initializer_list& l)" + << "\n\t You are trying to initialize a statically sized matrix with a list that doesn't have a compatible size." + << "\n\t l.size(): "<< l.size() + << "\n\t NC: "<< NC); + + if (l.size() != 0) + data.set_size(l.size()/NC, NC); + } + else if (l.size() != 0) + { + data.set_size(l.size(),1); + } + + if (l.size() != 0) + { + T* d = &data(0,0); + for (auto&& v : l) + *d++ = v; + } + + } + + matrix(matrix&& item) { #ifdef MATLAB_MEX_FILE diff --git a/dlib/matrix/matrix_abstract.h b/dlib/matrix/matrix_abstract.h index ce3384486..5385c2338 100644 --- a/dlib/matrix/matrix_abstract.h +++ b/dlib/matrix/matrix_abstract.h @@ -336,6 +336,30 @@ namespace dlib - #aliases(*this) == true - #ref().aliases(*this) == true !*/ + + matrix( + const std::initializer_list& l + ); + /*! + requires + - This matrix is capable of having a size() == l.size(). Therefore, if + NR*NC != 0 then l.size() must equal NR*NC. Alternatively, if NR or NC is + != 0 then l.size() must be a multiple of the non-zero NR or NC. + ensures + - #size() == l.size() + - The contents of l are enumerated and read into the matrix in row major order. + - if (NR != 0) then + - #nr() == NR + - #nc() == l.size()/NR + - if (NC != 0) then + - #nr() == l.size()/NC + - #nc() == NC + - if (NR*NC==0) then + - #nr() == l.size() + - #nc() == 1 + - #aliases(*this) == true + - #ref().aliases(*this) == true + !*/ T& operator() ( long r,