Added a matrix constructor that takes an initializer list.

This commit is contained in:
Davis King 2016-04-18 17:35:02 -04:00
parent 13bc3880e1
commit e9fa65398f
2 changed files with 76 additions and 0 deletions

View File

@ -16,6 +16,9 @@
#include "matrix_assign_fwd.h"
#include "matrix_op.h"
#include <utility>
#ifdef DLIB_HAS_RVALUE_REFERENCES
#include <initializer_list>
#endif
#ifdef MATLAB_MEX_FILE
#include <mex.h>
@ -1112,6 +1115,55 @@ namespace dlib
}
#ifdef DLIB_HAS_RVALUE_REFERENCES
matrix(const std::initializer_list<T>& 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

View File

@ -336,6 +336,30 @@ namespace dlib
- #aliases(*this) == true
- #ref().aliases(*this) == true
!*/
matrix(
const std::initializer_list<T>& 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,