View Issue Details

IDProjectCategoryView StatusLast Update
0003534OpenFOAMBugpublic2020-08-18 14:13
Reporterpeltonp1Assigned Tohenry 
PrioritynormalSeveritymajorReproducibilityalways
Status resolvedResolutionfixed 
PlatformGNU/LinuxOSUbuntuOS Version14.04
Product Version7 
Fixed in Versiondev 
Summary0003534: The UList::reverse_iterator does not behave like a reverse iterator
DescriptionThe operator ++ and -- for the UList::reverse_iterator shifts the pointer to the wrong direction causing confusing behaviour. For instance, this disables the use of many of the std::algorithms (such as find_if, count_if, count etc) with reverse iterators since they run into an infinite loop. For a reverse iterator it would be more natural that the ++ operator shifts the pointer to the opposite direction as in the forward_iterator.

Steps To Reproduceusing namespace Foam;

    std::vector<int> v_std = {1, 10, 3, 5};
    DynamicList<int> v_of(4, int());

    std::copy(v_std.begin(), v_std.end(), v_of.begin());
    CHECK(v_of[1] == 10);

   //this prints the std::vector in a reverse order
    for (auto it = v_std.rbegin(); it != v_std.rend(); it++){
        Info << *it << endl;
    }
 
    //this is an infinite loop as the pointer is always shifted right
    for (auto it = v_of.rbegin(); it != v_of.rend(); it++){
        Info << *it << endl;
    }
    
    //this fails because of an infinite loop
    int number_of_ones = std::count(v_of.rbegin(), v_of.rend(), 1);
    Info << number_of_ones << endl;

Additional InformationFix: Return a std::reverse_iterator from the UList::rbegin/rend (and possibly some other containers)
TagsNo tags attached.

Activities

henry

2020-08-17 10:25

manager   ~0011459

> For a reverse iterator it would be more natural that the ++ operator shifts the pointer to the opposite direction as in the forward_iterator.

This doesn't seem logical to me, the -- operator should be used to reduce the pointer.

Note that I wrote the containers in OpenFOAM before STL was developed and while there has been some attempt to update them over the years to make them STL compliant this is a lot of work without funding and hence not complete.

> Fix: Return a std::reverse_iterator from the UList::rbegin/rend (and possibly some other containers)

Did you try this approach? It is not clear to me how it would work, could you send the version of UList you have developed which returns std::reverse_iterator?

peltonp1

2020-08-17 10:51

reporter   ~0011460

I understand that the actual operation ++ shifting pointer to left is counter intuitive, but imo it would be more safe to not provide a reverse iterator at all if it behaves like a forward iterator. I actually encountered this while trying to call std::find_if on a array I knew was sorted with a reverse iterator. I did not make any adjustments to UList, instead I call the algorithms like this:
///
///@brief Converts an iterator to a reverse iterator
///
///@tparam InputIt input iterator type
///@param i the iterator to convert
///@return std::reverse_iterator<Iter> a reverse iterator
/// with ++ and -- operators shifting to left and right, respectively.
///
template<class InputIt>
std::reverse_iterator<InputIt> make_reverse(InputIt i){
    return std::reverse_iterator<InputIt>(i);
}
auto rbegin = make_reverse(problems.end());
auto rend = make_reverse(problems.begin());
//... do stuff



For a fix in the UList, without trying, I guess just defining the reverse iterator as:

// STL reverse_iterator (not really -Petteri)

        //- Reverse iterator for reverse traversal of UList
       // typedef T* reverse_iterator; //old one
       typedef std::reverse_iterator<iterator> reverse_iterator; //new one

and implemeting the functions as:

template<class T>
inline typename Foam::UList<T>::reverse_iterator
Foam::UList<T>::rbegin()
{
    return reverse_iterator(end());
}

would fix the issue.

henry

2020-08-17 11:05

manager   ~0011461

Last edited: 2020-08-17 11:18

View 2 revisions

I just quickly tested your proposal and it works, many thanks for the idea. I will check the other components as well, const iterators rend etc.

henry

2020-08-17 11:38

manager   ~0011462

Last edited: 2020-08-17 11:42

View 2 revisions

I got const_reverse_iterator to work


        //- Reverse iterator for reverse traversal of constant UList
        typedef std::reverse_iterator<const_iterator> const_reverse_iterator;


template<class T>
inline typename Foam::UList<T>::const_reverse_iterator
Foam::UList<T>::crbegin() const
{
    return const_reverse_iterator(cend());
}

template<class T>
inline typename Foam::UList<T>::const_reverse_iterator
Foam::UList<T>::crend() const
{
    return const_reverse_iterator(cbegin());
}

template<class T>
inline typename Foam::UList<T>::const_reverse_iterator
Foam::UList<T>::rbegin() const
{
    return const_reverse_iterator(cend());
}

template<class T>
inline typename Foam::UList<T>::const_reverse_iterator
Foam::UList<T>::rend() const
{
    return const_reverse_iterator(cbegin());
}

peltonp1

2020-08-17 12:14

reporter   ~0011463

Very good! Thanks for the quick reply and patch.

henry

2020-08-17 14:06

manager   ~0011464

Try this:

commit 17c40d2aa57b1f922cdddc315d8cf3471c9bc600 (HEAD -> master, origin/master, origin/HEAD)
Author: Henry Weller <http://cfd.direct>
Date: Mon Aug 17 14:04:42 2020 +0100

    UList,FixedList: Updated reverse_iterators for STL compliance
    
    Resolves bug-report https://bugs.openfoam.org/view.php?id=3534

peltonp1

2020-08-18 11:13

reporter   ~0011465

I tested the fix and it appears to be working. The applications/test/List/Test-List.C I used is attached. It fails before the aforementioned commit^.

Test-List.C (6,370 bytes)

henry

2020-08-18 14:13

manager   ~0011466

Resolved by commit 17c40d2aa57b1f922cdddc315d8cf3471c9bc600

Issue History

Date Modified Username Field Change
2020-08-17 08:59 peltonp1 New Issue
2020-08-17 10:25 henry Note Added: 0011459
2020-08-17 10:51 peltonp1 Note Added: 0011460
2020-08-17 11:05 henry Note Added: 0011461
2020-08-17 11:18 henry Note Edited: 0011461 View Revisions
2020-08-17 11:38 henry Note Added: 0011462
2020-08-17 11:42 henry Note Edited: 0011462 View Revisions
2020-08-17 12:14 peltonp1 Note Added: 0011463
2020-08-17 14:06 henry Note Added: 0011464
2020-08-18 11:13 peltonp1 File Added: Test-List.C
2020-08-18 11:13 peltonp1 Note Added: 0011465
2020-08-18 14:13 henry Assigned To => henry
2020-08-18 14:13 henry Status new => resolved
2020-08-18 14:13 henry Resolution open => fixed
2020-08-18 14:13 henry Fixed in Version => dev
2020-08-18 14:13 henry Note Added: 0011466