ConcatIterator.h 3.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file ConcatIterator.h
//! \ingroup core
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//! \author Christoph Rettinger <christoph.rettinger@fau.de>
//! \author Sebastian Eibl <sebastian.eibl@fau.de>
//======================================================================================================================

#pragma once

#include <iterator>

namespace walberla {

template< typename T >
30
class ConcatIterator
31
32
{
public:
33
34
35
36
37
38
    using iterator_category = std::input_iterator_tag;
    using value_type = typename T::value_type;
    using difference_type = typename T::difference_type;
    using pointer = typename T::pointer;
    using reference = typename T::reference;

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    /// default constructed iterator points to end()
    ConcatIterator( )
        : ended_( true )
    {}

    ConcatIterator( const T& beginFirst,
                    const T& endFirst,
                    const T& beginSecond,
                    const T& endSecond )
        : it_(beginFirst)
        , itEndFirst_(endFirst)
        , itBeginSecond_(beginSecond)
        , itEndSecond_(endSecond)
        , first_(true)
        , ended_(false)
    {
        checkStateAndAdapt();
    }

    ConcatIterator& operator++()    { ++it_; checkStateAndAdapt(); return *this; }            // prefix ++X
    ConcatIterator  operator++(int) { ConcatIterator it( *this ); operator++(); return it; }; // postfix X++

    bool operator==( const ConcatIterator& rhs ) const
    {
        if (ended_ || rhs.ended_)
        {
           if (ended_ == rhs.ended_)
           {
              return true;
           }
           else
           {
              return false;
           }
        }

        return it_ == rhs.it_;
    }
    bool operator!=( const ConcatIterator & rhs ) const { return !(*this == rhs); }

    typename T::reference  operator*()  { return it_.operator*(); }
    typename T::pointer    operator->() { return it_.operator->(); }

private:

    /// checks if iterator has to jump to second range
    void checkStateAndAdapt()
    {
        if( first_ && it_ == itEndFirst_ )
        {
           it_ = itBeginSecond_;
           first_ = false;
        }

        if( !first_ && it_ == itEndSecond_ )
        {
            ended_ = true;
        }
    }

    /// current position
    T it_;
    /// end of first range
    const T itEndFirst_;
    /// begin of second range
    const T itBeginSecond_;
    /// end of second range
    const T itEndSecond_;

    ///located in first range?
    bool first_;

    /// end reached?
    bool ended_;
};

} // namespace walberla