1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #19967 from HattrickGenerator:master

* Adding functions rbegin() and rend() functions to matrix class.
This is important to be more standard compliant with C++ and an ever increasing number of people using standard algorithms for better code readability- and maintainability.

The functions are copy pated from their counterparts (even though they should probably call the counterparts but this gave me some troube).
They return iterators using std::reverse_iterators

Follow up of an open feature request:
https://github.com/opencv/opencv/issues/4641

* Fix rbegin() and rend() and provide tests for them

* Removing unnecessary whitespaces

* Adding rbegin and rend to Mat_ class with the right parameters so we don't need to repeat the template argument.
An instantiating cv::Mat_<int> for example can call it's rbegin() function and doesn't need rbegin<int>() with this convience addition.

Follows what is done for forward iterators

* static cast the vector size (return size_t) to an int (that is required for opencv mat constructor)

Co-authored-by: Stefan <stefan.gerl@tum.de>
This commit is contained in:
HattrickGenerator
2021-05-20 21:21:34 +02:00
committed by GitHub
parent c4df8989e9
commit 115e471515
3 changed files with 159 additions and 0 deletions
+17
View File
@@ -2011,6 +2011,11 @@ public:
template<typename _Tp> MatIterator_<_Tp> begin();
template<typename _Tp> MatConstIterator_<_Tp> begin() const;
/** @brief Same as begin() but for inverse traversal
*/
template<typename _Tp> std::reverse_iterator<MatIterator_<_Tp>> rbegin();
template<typename _Tp> std::reverse_iterator<MatConstIterator_<_Tp>> rbegin() const;
/** @brief Returns the matrix iterator and sets it to the after-last matrix element.
The methods return the matrix read-only or read-write iterators, set to the point following the last
@@ -2019,6 +2024,12 @@ public:
template<typename _Tp> MatIterator_<_Tp> end();
template<typename _Tp> MatConstIterator_<_Tp> end() const;
/** @brief Same as end() but for inverse traversal
*/
template<typename _Tp> std::reverse_iterator< MatIterator_<_Tp>> rend();
template<typename _Tp> std::reverse_iterator< MatConstIterator_<_Tp>> rend() const;
/** @brief Runs the given functor over all matrix elements in parallel.
The operation passed as argument has to be a function pointer, a function object or a lambda(C++11).
@@ -2250,6 +2261,12 @@ public:
const_iterator begin() const;
const_iterator end() const;
//reverse iterators
std::reverse_iterator<iterator> rbegin();
std::reverse_iterator<iterator> rend();
std::reverse_iterator<const_iterator> rbegin() const;
std::reverse_iterator<const_iterator> rend() const;
//! template methods for for operation over all matrix elements.
// the operations take care of skipping gaps in the end of rows (if any)
template<typename Functor> void forEach(const Functor& operation);
@@ -1015,6 +1015,17 @@ MatConstIterator_<_Tp> Mat::begin() const
return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat::rbegin() const
{
if (empty())
return std::reverse_iterator<MatConstIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
it += total();
return std::reverse_iterator<MatConstIterator_<_Tp>> (it);
}
template<typename _Tp> inline
MatConstIterator_<_Tp> Mat::end() const
{
@@ -1026,6 +1037,15 @@ MatConstIterator_<_Tp> Mat::end() const
return it;
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat::rend() const
{
if (empty())
return std::reverse_iterator<MatConstIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
return std::reverse_iterator<MatConstIterator_<_Tp>>((const Mat_<_Tp>*)this);
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat::begin()
{
@@ -1035,6 +1055,17 @@ MatIterator_<_Tp> Mat::begin()
return MatIterator_<_Tp>((Mat_<_Tp>*)this);
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat::rbegin()
{
if (empty())
return std::reverse_iterator<MatIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
MatIterator_<_Tp> it((Mat_<_Tp>*)this);
it += total();
return std::reverse_iterator<MatIterator_<_Tp>>(it);
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat::end()
{
@@ -1046,6 +1077,15 @@ MatIterator_<_Tp> Mat::end()
return it;
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat::rend()
{
if (empty())
return std::reverse_iterator<MatIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
return std::reverse_iterator<MatIterator_<_Tp>>(MatIterator_<_Tp>((Mat_<_Tp>*)this));
}
template<typename _Tp, typename Functor> inline
void Mat::forEach(const Functor& operation) {
this->forEach_impl<_Tp>(operation);
@@ -1713,24 +1753,48 @@ MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
return Mat::begin<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat_<_Tp>::rbegin() const
{
return Mat::rbegin<_Tp>();
}
template<typename _Tp> inline
MatConstIterator_<_Tp> Mat_<_Tp>::end() const
{
return Mat::end<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat_<_Tp>::rend() const
{
return Mat::rend<_Tp>();
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat_<_Tp>::begin()
{
return Mat::begin<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat_<_Tp>::rbegin()
{
return Mat::rbegin<_Tp>();
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat_<_Tp>::end()
{
return Mat::end<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat_<_Tp>::rend()
{
return Mat::rend<_Tp>();
}
template<typename _Tp> template<typename Functor> inline
void Mat_<_Tp>::forEach(const Functor& operation) {
Mat::forEach<_Tp, Functor>(operation);