Skip to content
Snippets Groups Projects
Commit d6b32094 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: add reverse iteration macros

- forAllReverseIters and forAllReverseConstIters macros
- stdFoam::rbegin(), stdFoam::rend()
  stdFoam::crbegin(), stdFoam::crend()
parent 15ac4e5c
No related branches found
No related tags found
No related merge requests found
...@@ -105,6 +105,22 @@ int main(int argc, char *argv[]) ...@@ -105,6 +105,22 @@ int main(int argc, char *argv[])
}; };
Info<< "list2: " << list2 << endl; Info<< "list2: " << list2 << endl;
Info<< "forAllConstIters(list2): ";
forAllConstIters(list2, iter) { Info<< " " << *iter; }
Info<< endl;
Info<< "forAllReverseConstIters(list2): ";
forAllReverseConstIters(list2, iter) { Info<< " " << *iter; }
Info<< endl;
Info<< "forAllConstIters(list2): ";
forAllIters(list2, iter) { *iter *= 2; Info<< " " << *iter; }
Info<< endl;
Info<< "forAllReverseConstIters(list2): ";
forAllReverseIters(list2, iter) { *iter *= 0.5; Info<< " " << *iter; }
Info<< endl;
list1.append(list2); list1.append(list2);
Info<< "list1.append(list2): " << list1 << endl; Info<< "list1.append(list2): " << list1 << endl;
......
...@@ -34,13 +34,15 @@ Description ...@@ -34,13 +34,15 @@ Description
control over which definitions are used within the OpenFOAM code-base. control over which definitions are used within the OpenFOAM code-base.
SeeAlso SeeAlso
- http://en.cppreference.com/w/cpp/iterator/end
- http://en.cppreference.com/w/cpp/iterator/begin - http://en.cppreference.com/w/cpp/iterator/begin
- http://en.cppreference.com/w/cpp/iterator/end
- http://en.cppreference.com/w/cpp/iterator/rbegin
- http://en.cppreference.com/w/cpp/iterator/rend
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef StdFoam_H #ifndef stdFoam_H
#define StdFoam_H #define stdFoam_H
#include <initializer_list> #include <initializer_list>
#include <utility> #include <utility>
...@@ -52,7 +54,9 @@ namespace stdFoam ...@@ -52,7 +54,9 @@ namespace stdFoam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return iterator to the beginning of the container \a c or array. // Forward iteration
//- Return iterator to the beginning of the container \a c.
// Definition as per std::begin C++17 // Definition as per std::begin C++17
template<class C> template<class C>
constexpr auto begin(C& c) -> decltype(c.begin()) constexpr auto begin(C& c) -> decltype(c.begin())
...@@ -60,7 +64,7 @@ constexpr auto begin(C& c) -> decltype(c.begin()) ...@@ -60,7 +64,7 @@ constexpr auto begin(C& c) -> decltype(c.begin())
return c.begin(); return c.begin();
} }
//- Return const_iterator to the beginning of the container \a c or array. //- Return const_iterator to the beginning of the container \a c.
// Definition as per std::begin C++17 // Definition as per std::begin C++17
template<class C> template<class C>
constexpr auto begin(const C& c) -> decltype(c.begin()) constexpr auto begin(const C& c) -> decltype(c.begin())
...@@ -68,7 +72,7 @@ constexpr auto begin(const C& c) -> decltype(c.begin()) ...@@ -68,7 +72,7 @@ constexpr auto begin(const C& c) -> decltype(c.begin())
return c.begin(); return c.begin();
} }
//- Return const_iterator to the beginning of the container \a c or array. //- Return const_iterator to the beginning of the container \a c.
// Definition as per std::cbegin C++17 // Definition as per std::cbegin C++17
template<class C> template<class C>
constexpr auto cbegin(const C& c) -> decltype(c.begin()) constexpr auto cbegin(const C& c) -> decltype(c.begin())
...@@ -76,7 +80,7 @@ constexpr auto cbegin(const C& c) -> decltype(c.begin()) ...@@ -76,7 +80,7 @@ constexpr auto cbegin(const C& c) -> decltype(c.begin())
return c.begin(); return c.begin();
} }
//- Return iterator to the end of the container \a c or array. //- Return iterator to the end of the container \a c array.
// Definition as per std::end C++17 // Definition as per std::end C++17
template<class C> template<class C>
constexpr auto end(C& c) -> decltype(c.end()) constexpr auto end(C& c) -> decltype(c.end())
...@@ -84,7 +88,7 @@ constexpr auto end(C& c) -> decltype(c.end()) ...@@ -84,7 +88,7 @@ constexpr auto end(C& c) -> decltype(c.end())
return c.end(); return c.end();
} }
//- Return const_iterator to the end of the container \a c or array. //- Return const_iterator to the end of the container \a c array.
// Definition as per std::end C++17 // Definition as per std::end C++17
template<class C> template<class C>
constexpr auto end(const C& c) -> decltype(c.end()) constexpr auto end(const C& c) -> decltype(c.end())
...@@ -92,7 +96,7 @@ constexpr auto end(const C& c) -> decltype(c.end()) ...@@ -92,7 +96,7 @@ constexpr auto end(const C& c) -> decltype(c.end())
return c.end(); return c.end();
} }
//- Return const_iterator to the end of the container \a c or array. //- Return const_iterator to the end of the container \a c.
// Definition as per std::cend C++17 // Definition as per std::cend C++17
template<class C> template<class C>
constexpr auto cend(const C& c) -> decltype(c.end()) constexpr auto cend(const C& c) -> decltype(c.end())
...@@ -101,6 +105,57 @@ constexpr auto cend(const C& c) -> decltype(c.end()) ...@@ -101,6 +105,57 @@ constexpr auto cend(const C& c) -> decltype(c.end())
} }
// Reverse iteration
//- Return reverse_iterator to the reverse-begin of container \a c.
// Definition as per std::rbegin C++17
template<class C>
constexpr auto rbegin(C& c) -> decltype(c.rbegin())
{
return c.rbegin();
}
//- Return const_reverse_iterator to the reverse-begin of container \a c.
// Definition as per std::rbegin C++17
template<class C>
constexpr auto rbegin(const C& c) -> decltype(c.rbegin())
{
return c.rbegin();
}
//- Return const_reverse_iterator to the reverse-begin of container \a c.
// Definition as per std::crbegin C++17
template<class C>
constexpr auto crbegin(const C& c) -> decltype(c.rbegin())
{
return c.rbegin();
}
//- Return reverse_iterator to reverse-end of container \a c.
// Definition as per std::rend C++17
template<class C>
constexpr auto rend(C& c) -> decltype(c.rend())
{
return c.rend();
}
//- Return const_reverse_iterator to reverse-end of container \a c.
// Definition as per std::rend C++17
template<class C>
constexpr auto rend(const C& c) -> decltype(c.rend())
{
return c.rend();
}
//- Return const_reverse_iterator to reverse-end of container \a c.
// Definition as per std::crend C++17
template<class C>
constexpr auto crend(const C& c) -> decltype(c.rend())
{
return c.rend();
}
} // End namespace stdFoam } // End namespace stdFoam
...@@ -143,6 +198,43 @@ constexpr auto cend(const C& c) -> decltype(c.end()) ...@@ -143,6 +198,43 @@ constexpr auto cend(const C& c) -> decltype(c.end())
) )
//- Rverse iterate across elements in the \a container object of type
// \a Container.
// \par Usage
// \code
// forAllReverseIters(container, iter)
// {
// statements;
// }
// \endcode
// \sa forAllReverseConstIters
#define forAllReverseIters(container,iter) \
for \
( \
auto iter = stdFoam::rbegin(container); \
iter != stdFoam::rend(container); \
--iter \
)
//- Reverse iterate across elements of \a container object with const access.
// \par Usage
// \code
// forAllReverseConstIters(container, iter)
// {
// statements;
// }
// \endcode
// \sa forAllReverseIters
#define forAllReverseConstIters(container,iter) \
for \
( \
auto iter = stdFoam::crbegin(container); \
iter != stdFoam::crend(container); \
--iter \
)
//- Loop across all elements in \a list //- Loop across all elements in \a list
// \par Usage // \par Usage
// \code // \code
...@@ -169,6 +261,8 @@ constexpr auto cend(const C& c) -> decltype(c.end()) ...@@ -169,6 +261,8 @@ constexpr auto cend(const C& c) -> decltype(c.end())
for (Foam::label i=(list).size()-1; i>=0; --i) for (Foam::label i=(list).size()-1; i>=0; --i)
// Compatibility macros for pre C++11
//- Iterate across all elements in the \a container object //- Iterate across all elements in the \a container object
// of type \a Container. // of type \a Container.
// \par Usage // \par Usage
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment