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
 30
 31
 32
 33
 34
 35
 36
 37
 38
 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef MOAB_MB_ITER_HPP
#define MOAB_MB_ITER_HPP

#define IS_BUILDING_MB
#include "Internals.hpp"
#include "moab/Range.hpp"
#include "moab/Core.hpp"
#include <vector>
#include <algorithm>

struct iBase_EntityArrIterator_Private
{
  protected:
    iBase_EntityType entType;
    iMesh_EntityTopology entTopo;
    EntityHandle entSet;
    int arrSize;
    bool isRecursive;

  public:
    iBase_EntityArrIterator_Private( iBase_EntityType type,
                                     iMesh_EntityTopology topology,
                                     EntityHandle set,
                                     int array_sz,
                                     bool recursive = false )
        : entType( type ), entTopo( topology ), entSet( set ), arrSize( array_sz ), isRecursive( recursive )
    {
    }

    virtual ~iBase_EntityArrIterator_Private() {}

    int array_size() const
    {
        return arrSize;
    }

    virtual ErrorCode step( int num_steps, bool& at_end ) = 0;<--- Virtual function in base class

    // NOTE: input array must be at least arrLen long
    virtual void get_entities( Core* mb, EntityHandle* array, int& count_out ) = 0;<--- Virtual function in base class

    virtual ErrorCode reset( Interface* mb ) = 0;<--- Virtual function in base class

    class IsType
    {
      private:
        EntityType type;

      public:
        IsType( EntityType t ) : type( t ) {}
        bool operator()( EntityHandle h )
        {
            return TYPE_FROM_HANDLE( h ) == type;
        }
    };

    void remove_type( std::vector< EntityHandle >& vect, EntityType t )
    {
        vect.erase( std::remove_if( vect.begin(), vect.end(), IsType( t ) ), vect.end() );
    }

    void remove_type( Range& range, EntityType t )
    {
        std::pair< Range::iterator, Range::iterator > p = range.equal_range( t );
        range.erase( p.first, p.second );
    }
};

// step_iterator will safely step forward N steps in a iterator. We specialize
// for random-access iterators (vectors and Ranges) so that they perform better.

template < typename T >
inline ErrorCode step_iterator( T& curr, const T& end, int num_steps, bool& at_end )
{
    if( 0 > num_steps ) return MB_FAILURE;

    while( num_steps && curr != end )
    {
        num_steps--;
        curr++;
    }
    at_end = ( curr == end );
    return MB_SUCCESS;
}

template < typename T >
inline ErrorCode step_iterator( typename std::vector< T >::const_iterator& curr,
                                const typename std::vector< T >::const_iterator& end,
                                int num_steps,
                                bool& at_end )
{
    if( 0 > num_steps ) return MB_FAILURE;

    assert( curr <= end );  // Sanity check
    at_end = ( end - curr <= num_steps );

    if( at_end )
        curr = end;
    else
        curr += num_steps;
    return MB_SUCCESS;
}

inline ErrorCode step_iterator( Range::const_iterator& curr,
                                const Range::const_iterator& end,
                                int num_steps,
                                bool& at_end )
{
    if( 0 > num_steps ) return MB_FAILURE;

    at_end = ( end - curr <= num_steps );

    if( at_end )
        curr = end;
    else
        curr += num_steps;
    return MB_SUCCESS;
}

template < class Container >
class MBIter : public iBase_EntityArrIterator_Private
{
  protected:
    Container iterData;
    typename Container::const_iterator iterPos;

  public:
    MBIter( iBase_EntityType type,
            iMesh_EntityTopology topology,
            EntityHandle set,
            int arr_size,
            bool recursive = false )
        : iBase_EntityArrIterator_Private( type, topology, set, arr_size, recursive ), iterPos( iterData.end() )
    {
    }

    ~MBIter() {}

    typename Container::const_iterator position() const
    {
        return iterPos;
    };

    typename Container::const_iterator end() const
    {
        return iterData.end();
    };

    ErrorCode step( int num_steps, bool& at_end )<--- Function in derived class
    {
        return step_iterator( iterPos, end(), num_steps, at_end );
    }

    void get_entities( Core* mb, EntityHandle* array, int& count )<--- Function in derived class
    {
        for( count = 0; count < arrSize && iterPos != iterData.end(); ++iterPos )
            if( mb->is_valid( *iterPos ) ) array[count++] = *iterPos;
    }

    virtual ErrorCode reset( Interface* mb )<--- Function in derived class
    {
        ErrorCode result;
        iterData.clear();
        if( entTopo != iMesh_ALL_TOPOLOGIES )
        {
            if( entTopo == iMesh_SEPTAHEDRON )
                result = MB_SUCCESS;
            else
                result = mb->get_entities_by_type( entSet, mb_topology_table[entTopo], iterData, isRecursive );
        }
        else if( entType != iBase_ALL_TYPES )
        {
            result = mb->get_entities_by_dimension( entSet, entType, iterData, isRecursive );
            if( entType == iBase_REGION ) remove_type( iterData, MBKNIFE );
        }
        else
        {
            result = mb->get_entities_by_handle( entSet, iterData, isRecursive );
            remove_type( iterData, MBENTITYSET );
            remove_type( iterData, MBKNIFE );
        }
        iterPos = iterData.begin();
        return result;
    }
};

typedef MBIter< std::vector< EntityHandle > > MBListIter;
typedef MBIter< moab::Range > MBRangeIter;

#endif