MOAB: Mesh Oriented datABase  (version 5.3.1)
MBIter.hpp
Go to the documentation of this file.
00001 #ifndef MOAB_MB_ITER_HPP
00002 #define MOAB_MB_ITER_HPP
00003 
00004 #define IS_BUILDING_MB
00005 #include "Internals.hpp"
00006 #include "moab/Range.hpp"
00007 #include "moab/Core.hpp"
00008 #include <vector>
00009 #include <algorithm>
00010 
00011 struct iBase_EntityArrIterator_Private
00012 {
00013   protected:
00014     iBase_EntityType entType;
00015     iMesh_EntityTopology entTopo;
00016     EntityHandle entSet;
00017     int arrSize;
00018     bool isRecursive;
00019 
00020   public:
00021     iBase_EntityArrIterator_Private( iBase_EntityType type, iMesh_EntityTopology topology, EntityHandle set,
00022                                      int array_sz, bool recursive = false )
00023         : entType( type ), entTopo( topology ), entSet( set ), arrSize( array_sz ), isRecursive( recursive )
00024     {
00025     }
00026 
00027     virtual ~iBase_EntityArrIterator_Private() {}
00028 
00029     int array_size() const
00030     {
00031         return arrSize;
00032     }
00033 
00034     virtual ErrorCode step( int num_steps, bool& at_end ) = 0;
00035 
00036     // NOTE: input array must be at least arrLen long
00037     virtual void get_entities( Core* mb, EntityHandle* array, int& count_out ) = 0;
00038 
00039     virtual ErrorCode reset( Interface* mb ) = 0;
00040 
00041     class IsType
00042     {
00043       private:
00044         EntityType type;
00045 
00046       public:
00047         IsType( EntityType t ) : type( t ) {}
00048         bool operator()( EntityHandle h )
00049         {
00050             return TYPE_FROM_HANDLE( h ) == type;
00051         }
00052     };
00053 
00054     void remove_type( std::vector< EntityHandle >& vect, EntityType t )
00055     {
00056         vect.erase( std::remove_if( vect.begin(), vect.end(), IsType( t ) ), vect.end() );
00057     }
00058 
00059     void remove_type( Range& range, EntityType t )
00060     {
00061         std::pair< Range::iterator, Range::iterator > p = range.equal_range( t );
00062         range.erase( p.first, p.second );
00063     }
00064 };
00065 
00066 // step_iterator will safely step forward N steps in a iterator. We specialize
00067 // for random-access iterators (vectors and Ranges) so that they perform better.
00068 
00069 template < typename T >
00070 inline ErrorCode step_iterator( T& curr, const T& end, int num_steps, bool& at_end )
00071 {
00072     if( 0 > num_steps ) return MB_FAILURE;
00073 
00074     while( num_steps && curr != end )
00075     {
00076         num_steps--;
00077         curr++;
00078     }
00079     at_end = ( curr == end );
00080     return MB_SUCCESS;
00081 }
00082 
00083 template < typename T >
00084 inline ErrorCode step_iterator( typename std::vector< T >::const_iterator& curr,
00085                                 const typename std::vector< T >::const_iterator& end, int num_steps, bool& at_end )
00086 {
00087     if( 0 > num_steps ) return MB_FAILURE;
00088 
00089     assert( curr <= end );  // Sanity check
00090     at_end = ( end - curr <= num_steps );
00091 
00092     if( at_end )
00093         curr = end;
00094     else
00095         curr += num_steps;
00096     return MB_SUCCESS;
00097 }
00098 
00099 inline ErrorCode step_iterator( Range::const_iterator& curr, const Range::const_iterator& end, int num_steps,
00100                                 bool& at_end )
00101 {
00102     if( 0 > num_steps ) return MB_FAILURE;
00103 
00104     at_end = ( end - curr <= num_steps );
00105 
00106     if( at_end )
00107         curr = end;
00108     else
00109         curr += num_steps;
00110     return MB_SUCCESS;
00111 }
00112 
00113 template < class Container >
00114 class MBIter : public iBase_EntityArrIterator_Private
00115 {
00116   protected:
00117     Container iterData;
00118     typename Container::const_iterator iterPos;
00119 
00120   public:
00121     MBIter( iBase_EntityType type, iMesh_EntityTopology topology, EntityHandle set, int arr_size,
00122             bool recursive = false )
00123         : iBase_EntityArrIterator_Private( type, topology, set, arr_size, recursive ), iterPos( iterData.end() )
00124     {
00125     }
00126 
00127     ~MBIter() {}
00128 
00129     typename Container::const_iterator position() const
00130     {
00131         return iterPos;
00132     };
00133 
00134     typename Container::const_iterator end() const
00135     {
00136         return iterData.end();
00137     };
00138 
00139     ErrorCode step( int num_steps, bool& at_end )
00140     {
00141         return step_iterator( iterPos, end(), num_steps, at_end );
00142     }
00143 
00144     void get_entities( Core* mb, EntityHandle* array, int& count )
00145     {
00146         for( count = 0; count < arrSize && iterPos != iterData.end(); ++iterPos )
00147             if( mb->is_valid( *iterPos ) ) array[count++] = *iterPos;
00148     }
00149 
00150     virtual ErrorCode reset( Interface* mb )
00151     {
00152         ErrorCode result;
00153         iterData.clear();
00154         if( entTopo != iMesh_ALL_TOPOLOGIES )
00155         {
00156             if( entTopo == iMesh_SEPTAHEDRON )
00157                 result = MB_SUCCESS;
00158             else
00159                 result = mb->get_entities_by_type( entSet, mb_topology_table[entTopo], iterData, isRecursive );
00160         }
00161         else if( entType != iBase_ALL_TYPES )
00162         {
00163             result = mb->get_entities_by_dimension( entSet, entType, iterData, isRecursive );
00164             if( entType == iBase_REGION ) remove_type( iterData, MBKNIFE );
00165         }
00166         else
00167         {
00168             result = mb->get_entities_by_handle( entSet, iterData, isRecursive );
00169             remove_type( iterData, MBENTITYSET );
00170             remove_type( iterData, MBKNIFE );
00171         }
00172         iterPos = iterData.begin();
00173         return result;
00174     }
00175 };
00176 
00177 typedef MBIter< std::vector< EntityHandle > > MBListIter;
00178 typedef MBIter< moab::Range > MBRangeIter;
00179 
00180 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines