Commit 1d2ecc9d authored by Davis King's avatar Davis King

Changed the comma based matrix assignment to use operator= instead

of operator<<.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402717
parent 4c0d826b
...@@ -1434,47 +1434,68 @@ namespace dlib ...@@ -1434,47 +1434,68 @@ namespace dlib
operator<< form of matrix assignment. operator<< form of matrix assignment.
*/ */
literal_assign_helper(const literal_assign_helper& item) : m(item.m), r(item.r), c(item.c), is_copy(true) {} literal_assign_helper(const literal_assign_helper& item) : m(item.m), r(item.r), c(item.c), has_been_used(false) {}
literal_assign_helper(matrix* m_): m(m_), r(0), c(0),is_copy(false) {} literal_assign_helper(matrix* m_): m(m_), r(0), c(0),has_been_used(false) {next();}
~literal_assign_helper() ~literal_assign_helper()
{ {
DLIB_CASSERT(!is_copy || r == m->nr(), DLIB_CASSERT(!has_been_used || r == m->nr(),
"You have used the matrix operator<< assignment incorrectly by failing to\n" "You have used the matrix comma based assignment incorrectly by failing to\n"
"supply a full set of values for every element of a matrix object.\n"); "supply a full set of values for every element of a matrix object.\n");
} }
template <typename U>
const literal_assign_helper& operator, ( const literal_assign_helper& operator, (
const U& val const T& val
) const ) const
{ {
DLIB_CASSERT(r < m->nr() && c < m->nc(), DLIB_CASSERT(r < m->nr() && c < m->nc(),
"You have used the matrix operator<< assignment incorrectly by attempting to\n" << "You have used the matrix comma based assignment incorrectly by attempting to\n" <<
"supply more values than there are elements in the matrix object being assigned to.\n\n" << "supply more values than there are elements in the matrix object being assigned to.\n\n" <<
"Did you forget to call set_size()?\n"); "Did you forget to call set_size()?\n");
(*m)(r,c) = static_cast<T>(val); (*m)(r,c) = val;
next();
has_been_used = true;
return *this;
}
private:
void next (
) const
{
++c; ++c;
if (c == m->nc()) if (c == m->nc())
{ {
c = 0; c = 0;
++r; ++r;
} }
return *this;
} }
matrix* m; matrix* m;
mutable long r; mutable long r;
mutable long c; mutable long c;
bool is_copy; mutable bool has_been_used;
}; };
public: public:
template <typename U> const literal_assign_helper operator = (
const literal_assign_helper operator << ( const T& val
const U& val )
) { return literal_assign_helper(this) , val; } {
// assign the given value to every spot in this matrix
for (long r = 0; r < nr(); ++r)
{
for (long c = 0; c < nc(); ++c)
{
data(r,c) = val;
}
}
// Now return the literal_assign_helper so that the user
// can use the overloaded comma notation to initialize
// the matrix if they want to.
return literal_assign_helper(this);
}
private: private:
......
...@@ -750,25 +750,28 @@ namespace dlib ...@@ -750,25 +750,28 @@ namespace dlib
- returns *this - returns *this
!*/ !*/
template <typename U> const literal_assign_helper operator = (
const literal_assign_helper operator << ( const T& val
const U& val
); );
/*! /*!
This function is somewhat different than all the others defined in this file. This function is somewhat different than all the others defined in this file.
The purpose of this function is to enable you to easily initialize a matrix object. The purpose of this function is to enable you to easily initialize a matrix object.
For example: For example:
matrix<double> m(2,3); matrix<double> m(2,3);
m << 1,2,3, m = 1,2,3,
4,5,6; 4,5,6;
The above code creates a matrix m with 2 rows and 3 columns and sets it so that The above code creates a matrix m with 2 rows and 3 columns and sets it so that
it contains the matrix | 1 2 3 | it contains the matrix | 1 2 3 |
| 4 5 6 | | 4 5 6 |
To use this method of assignment it is required that you supply exactly m.size() You can also use this function to assign to all elements of a matrix. So
values so that the matrix is fully initialized. Supplying fewer or more than saying m = 3; would assign all elements of m equal to 3.
that is an error that will cause a dlib::fatal_error to be thrown.
Note that to use this method of assignment it is required that you supply
exactly m.size() or 1 values so that the matrix is fully initialized. Supplying
fewer or more than that is an error that will cause a dlib::fatal_error to be
thrown.
!*/ !*/
void swap ( void swap (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment