diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index 990da8729f577fbe979028ac68f17272680d650c..22aa159469d6fc2de4e212dd73ab850a2c2a9e7e 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -105,6 +105,22 @@ int main(int argc, char *argv[]) }; 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); Info<< "list1.append(list2): " << list1 << endl; diff --git a/src/OpenFOAM/include/stdFoam.H b/src/OpenFOAM/include/stdFoam.H index 3141c38351233067ba8c00706920a6e13dc8bceb..ac2653a779a63452b78eb1bc2767326172980c75 100644 --- a/src/OpenFOAM/include/stdFoam.H +++ b/src/OpenFOAM/include/stdFoam.H @@ -34,13 +34,15 @@ Description control over which definitions are used within the OpenFOAM code-base. SeeAlso - - http://en.cppreference.com/w/cpp/iterator/end - 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 -#define StdFoam_H +#ifndef stdFoam_H +#define stdFoam_H #include <initializer_list> #include <utility> @@ -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 template<class C> constexpr auto begin(C& c) -> decltype(c.begin()) @@ -60,7 +64,7 @@ constexpr auto begin(C& c) -> decltype(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 template<class C> 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 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 template<class C> 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 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 template<class C> constexpr auto end(C& c) -> decltype(c.end()) @@ -84,7 +88,7 @@ constexpr auto end(C& c) -> decltype(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 template<class C> 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 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 template<class C> 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 @@ -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 // \par Usage // \code @@ -169,6 +261,8 @@ constexpr auto cend(const C& c) -> decltype(c.end()) 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 // of type \a Container. // \par Usage