Commit ee4e7aea authored by Davis King's avatar Davis King

Added STL style begin() and end() methods to matrix and matrix_exp.

parent 634f4db4
...@@ -996,6 +996,8 @@ namespace dlib ...@@ -996,6 +996,8 @@ namespace dlib
const static long NR = matrix_traits<matrix>::NR; const static long NR = matrix_traits<matrix>::NR;
const static long NC = matrix_traits<matrix>::NC; const static long NC = matrix_traits<matrix>::NC;
const static long cost = matrix_traits<matrix>::cost; const static long cost = matrix_traits<matrix>::cost;
typedef T* iterator;
typedef const T* const_iterator;
matrix () matrix ()
{ {
...@@ -1551,6 +1553,39 @@ namespace dlib ...@@ -1551,6 +1553,39 @@ namespace dlib
const matrix_exp<U>& const matrix_exp<U>&
) const { return false; } ) const { return false; }
iterator begin()
{
if (size() != 0)
return &data(0,0);
else
return 0;
}
iterator end()
{
if (size() != 0)
return &data(0,0)+size();
else
return 0;
}
const_iterator begin() const
{
if (size() != 0)
return &data(0,0);
else
return 0;
}
const_iterator end() const
{
if (size() != 0)
return &data(0,0)+size();
else
return 0;
}
private: private:
struct literal_assign_helper struct literal_assign_helper
{ {
......
...@@ -253,6 +253,8 @@ namespace dlib ...@@ -253,6 +253,8 @@ namespace dlib
const static long NR = num_rows; const static long NR = num_rows;
const static long NC = num_cols; const static long NC = num_cols;
const static long cost = 1; const static long cost = 1;
typedef T* iterator;
typedef const T* const_iterator;
matrix ( matrix (
); );
...@@ -601,6 +603,44 @@ namespace dlib ...@@ -601,6 +603,44 @@ namespace dlib
ensures ensures
- swaps *this and item - swaps *this and item
!*/ !*/
iterator begin(
);
/*!
ensures
- returns a random access iterator pointing to the first element in this
matrix.
- The iterator will iterate over the elements of the matrix in row major
order if layout is row_major_layout or in column major order if layout is
column_major_layout.
!*/
iterator end(
);
/*!
ensures
- returns a random access iterator pointing to one past the end of the last
element in this matrix.
!*/
const_iterator begin(
) const;
/*!
ensures
- returns a random access iterator pointing to the first element in this
matrix.
- The iterator will iterate over the elements of the matrix in row major
order if layout is row_major_layout or in column major order if layout is
column_major_layout.
!*/
const_iterator end(
) const;
/*!
ensures
- returns a random access iterator pointing to one past the end of the last
element in this matrix.
!*/
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -52,6 +52,62 @@ namespace dlib ...@@ -52,6 +52,62 @@ namespace dlib
const static long cost = EXP::cost; const static long cost = EXP::cost;
}; };
// ----------------------------------------------------------------------------------------
template <typename EXP> class matrix_exp;
template <typename EXP>
class matrix_exp_iterator
{
friend class matrix_exp<EXP>;
matrix_exp_iterator(const EXP& m_, long r_, long c_)
{
r = r_;
c = c_;
nc = m_.nc();
m = &m_;
}
public:
matrix_exp_iterator() : r(0), c(0), nc(0), m(0) {}
typedef typename matrix_traits<EXP>::type type;
typedef type value_type;
typedef typename matrix_traits<EXP>::const_ret_type const_ret_type;
bool operator == ( const matrix_exp_iterator& itr) const
{ return r == itr.r && c == itr.c; }
bool operator != ( const matrix_exp_iterator& itr) const
{ return !(*this == itr); }
matrix_exp_iterator& operator++()
{
++c;
if (c==nc)
{
c = 0;
++r;
}
return *this;
}
matrix_exp_iterator operator++(int)
{
matrix_exp_iterator temp(*this);
++(*this);
return temp;
}
const_ret_type operator* () const { return (*m)(r,c); }
private:
long r, c;
long nc;
const EXP* m;
};
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
...@@ -77,6 +133,8 @@ namespace dlib ...@@ -77,6 +133,8 @@ namespace dlib
typedef matrix<type,NR,NC,mem_manager_type,layout_type> matrix_type; typedef matrix<type,NR,NC,mem_manager_type,layout_type> matrix_type;
typedef EXP exp_type; typedef EXP exp_type;
typedef matrix_exp_iterator<EXP> iterator;
typedef matrix_exp_iterator<EXP> const_iterator;
inline const_ret_type operator() ( inline const_ret_type operator() (
long r, long r,
...@@ -165,6 +223,9 @@ namespace dlib ...@@ -165,6 +223,9 @@ namespace dlib
return temp(0); return temp(0);
} }
const_iterator begin() const { return matrix_exp_iterator<EXP>(ref(),0,0); }
const_iterator end() const { return matrix_exp_iterator<EXP>(ref(),nr(),0); }
protected: protected:
matrix_exp() {} matrix_exp() {}
matrix_exp(const matrix_exp& ) {} matrix_exp(const matrix_exp& ) {}
......
...@@ -57,6 +57,8 @@ namespace dlib ...@@ -57,6 +57,8 @@ namespace dlib
const static long NC = EXP::NC; const static long NC = EXP::NC;
typedef matrix<type,NR,NC, mem_manager_type,layout_type> matrix_type; typedef matrix<type,NR,NC, mem_manager_type,layout_type> matrix_type;
typedef EXP exp_type; typedef EXP exp_type;
typedef matrix_exp_iterator<EXP> iterator;
typedef matrix_exp_iterator<EXP> const_iterator;
const_ret_type operator() ( const_ret_type operator() (
long r, long r,
...@@ -167,6 +169,27 @@ namespace dlib ...@@ -167,6 +169,27 @@ namespace dlib
(i.e. returns *static_cast<const exp_type*>(this) ) (i.e. returns *static_cast<const exp_type*>(this) )
!*/ !*/
const_iterator begin(
) const;
/*!
ensures
- returns a forward access iterator pointing to the first element in this
matrix expression.
- Since matrix_exp objects represent immutable views of a matrix, the
returned iterator does not allow the user to modify the matrix
expression's elements.
- The iterator will iterate over the elements of the matrix in row major
order.
!*/
const_iterator end(
) const;
/*!
ensures
- returns a forward access iterator pointing to one past the end of the
last element in this matrix expression.
!*/
protected: protected:
// Only derived classes of matrix_exp may call the matrix_exp constructors. // Only derived classes of matrix_exp may call the matrix_exp constructors.
......
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