Sierra Toolkit Version of the Day
Selector.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 
00010 #ifndef stk_mesh_Selector_hpp
00011 #define stk_mesh_Selector_hpp
00012 
00013 #include <iosfwd>
00014 #include <algorithm>
00015 #include <stk_mesh/base/Types.hpp>
00016 #include <string>
00017 
00018 namespace stk {
00019 namespace mesh {
00020 
00021 //An operator to obtain a part-ordinal from a part-iterator.
00022 //This general template handles cases where the part-iterator
00023 //iterates either stk::mesh::Part or Fmwk::MeshPart objects.
00024 //Specializations for part-ordinal-pointers follow below.
00025 template<typename PartIterator>
00026 struct GetPartIterOrdinal {
00027 unsigned operator()(PartIterator p_it) const
00028 { return (*p_it)->mesh_meta_data_ordinal(); }
00029 };
00030 
00031 template<>
00032 struct GetPartIterOrdinal<const unsigned*> {
00033 unsigned operator()(const unsigned* p_it) const
00034 { return *p_it; }
00035 };
00036 
00037 template<>
00038 struct GetPartIterOrdinal<unsigned*> {
00039 unsigned operator()(unsigned* p_it) const
00040 { return *p_it; }
00041 };
00042 
00043 
00044 struct PartOrdLess {
00045 
00046 bool operator()(unsigned lhs, unsigned rhs) const
00047 { return lhs < rhs; }
00048 
00049 };
00050 
00051 //Function to determine whether a specified part-ordinal is present
00052 //in a given range of parts.
00053 //Caller-provided comparison-operator compares a part-ordinal with
00054 //one obtained from whatever a PartIterator dereferences to.
00055 template<typename PartIterator, class Compare>
00056 bool part_is_present(unsigned part_ord,
00057                      const std::pair<PartIterator, PartIterator>& part_range,
00058                      Compare comp)
00059 {
00060   GetPartIterOrdinal<PartIterator> get_part_ordinal;
00061 
00062   // Search for 'part_ord' in the bucket's list of sorted integer part ords
00063   PartIterator p_it = std::lower_bound(part_range.first, part_range.second, part_ord, comp);
00064   return (p_it != part_range.second && get_part_ordinal(p_it) == part_ord);
00065 }
00066 
00067 enum Op{
00068         INVALID = 0,
00069         COMPOUND = 1,
00070         PART_ID = 2
00071        };
00072 
00073 struct OpType {
00074   unsigned       m_part_id ; 
00075   unsigned short m_unary ;   
00076   unsigned short m_count ;   
00077   Op             m_op      ; 
00078 
00079   OpType() : m_part_id(0), m_unary(0), m_count(0), m_op(INVALID) {}
00080   OpType( unsigned part_id , unsigned unary , unsigned count, Op op=INVALID )
00081     : m_part_id( part_id ), m_unary( unary ), m_count( count ), m_op(op)  {}
00082 
00083   bool operator == (const OpType & opType ) const
00084   {
00085     return m_part_id == opType.m_part_id &&
00086            m_unary == opType.m_unary &&
00087            m_count == opType.m_count &&
00088            m_op == opType.m_op;
00089   }
00090   bool operator != (const OpType & opType ) const
00091   { return !(*this == opType); }
00092 };
00093 
00112 class Selector {
00113 public:
00115   Selector();
00116 
00117   bool operator == (const Selector & rhs) const
00118   { return m_op == rhs.m_op; }
00119 
00120   bool operator != (const Selector & rhs) const
00121   { return m_op != rhs.m_op; }
00122 
00124   Selector( const Part & part);
00125 
00127   Selector & operator &= ( const Selector & selector);
00128 
00130   Selector & operator |= ( const Selector & selector);
00131 
00135   Selector & complement();
00136 
00138   Selector operator ! () const
00139     { Selector S( *this ); return S.complement(); }
00140 
00144   bool operator()( const Part & part ) const;
00145 
00149   bool operator()( const Bucket & candidate ) const;
00150 
00154   bool operator()( const Bucket * candidate ) const;
00155 
00159   bool operator()( const Entity & candidate ) const;
00160 
00164   template<typename PartIterator, class Compare>
00165   bool apply(const std::pair<PartIterator,PartIterator>& part_range, Compare comp) const
00166   { return apply(m_op.begin(), m_op.end(), part_range, comp); }
00167 
00169 #ifndef SWIG
00170   friend std::ostream & operator << ( std::ostream & out, const Selector & selector);
00171 #endif
00172 
00173   const std::vector<OpType>& get_ops() const { return m_op; }
00174   void set_ops(const std::vector<OpType>& ops) { m_op = ops; }
00175 
00177   void compoundAll();
00178 
00179 private:
00180 
00182   const MetaData * m_mesh_meta_data ;
00183 
00185   std::vector< OpType > m_op ;
00186 
00188   void verify_compatible( const Selector & B ) const;
00189 
00191   void verify_compatible( const Bucket & B ) const;
00192 
00194   template<typename PartIterator, class Compare>
00195   bool apply(
00196       std::vector<OpType>::const_iterator i,
00197       std::vector<OpType>::const_iterator j,
00198       const std::pair<PartIterator,PartIterator>& part_range,
00199       Compare comp) const
00200   {
00201     bool result = i != j ;
00202     while ( result && i != j ) {
00203       const unsigned statement_length = i->m_count;
00204       if ( statement_length > 0 ) { // Check if compound statement
00205         result = i->m_unary ^ apply( i + 1 , i + statement_length , part_range , comp );
00206         i += statement_length;
00207       }
00208       else { // Test for containment of bucket in this part, or not in
00209         result = i->m_unary ^ part_is_present( i->m_part_id , part_range , comp );
00210         ++i ;
00211       }
00212     }
00213     return result ;
00214   }
00215 
00217   std::string printExpression(
00218       const std::vector<OpType>::const_iterator start,
00219       const std::vector<OpType>::const_iterator finish
00220       ) const;
00221 
00222 };
00223 
00224 class Part;
00225 
00226 #ifndef SWIG
00227 std::ostream & operator<<( std::ostream & out, const Selector & selector);
00228 #endif
00229 
00233 Selector operator & ( const Part & A , const Part & B );
00234 
00238 Selector operator & ( const Part & A , const Selector & B );
00239 
00243 Selector operator & ( const Selector & A, const Part & B );
00244 
00248 Selector operator & ( const Selector & A, const Selector & B );
00249 
00253 Selector operator | ( const Part & A , const Part & B );
00254 
00258 Selector operator | ( const Part & A , const Selector & B );
00259 
00263 Selector operator | ( const Selector & A, const Part & B  );
00264 
00268 Selector operator | ( const Selector & A , const Selector & B );
00269 
00273 Selector operator ! ( const Part & A );
00274 
00275 
00279 Selector selectUnion( const PartVector& union_part_vector );
00280 
00284 Selector selectIntersection( const PartVector& intersection_part_vector );
00285 
00289 Selector selectField( const FieldBase& field );
00290 
00293 } // namespace mesh
00294 } // namespace stk
00295 
00296 #endif // stk_mesh_Selector_hpp
00297 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines