Sierra Toolkit Version of the Day
Part.cpp
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 #include <algorithm>
00010 #include <stk_mesh/base/Types.hpp>
00011 #include <stk_mesh/base/Part.hpp>
00012 #include <stk_util/util/string_case_compare.hpp>
00013 
00014 
00015 
00016 namespace stk {
00017 namespace mesh {
00018   
00019 //----------------------------------------------------------------------
00020 
00021 Part * find( const PartVector & parts , const std::string & name )
00022 {
00023   PartVector::const_iterator i = parts.begin();
00024 
00025   while ( i != parts.end() && not_equal_case((*i)->name(),name) ) { ++i ; }
00026 
00027   return i != parts.end() ? *i : NULL ;
00028 }
00029 
00030 //----------------------------------------------------------------------
00031 
00032 std::ostream &
00033 print( std::ostream & os , const char * const lead , const Part & p )
00034 {
00035   const PartVector & supersets = p.supersets();
00036   const PartVector & subsets   = p.subsets();
00037   const PartVector & intersection = p.intersection_of();
00038 
00039   std::vector<Part*>::const_iterator i ;
00040 
00041   if ( lead != NULL ) { os << lead ; }
00042   os << "Part[ " ;
00043   os << p.name() ;
00044   os << " , " ;
00045   os << p.mesh_meta_data_ordinal() ;
00046   os << " ] {" ;
00047   os << std::endl ;
00048 
00049   if ( lead != NULL ) { os << lead ; }
00050   os << "  Supersets {" ;
00051   for ( i = supersets.begin() ; i != supersets.end() ; ++i ) {
00052     const std::string & n = (*i)->name() ; os << " " << n ;
00053   }
00054   os << " }" << std::endl ;
00055 
00056   if ( lead != NULL ) { os << lead ; }
00057   os << "  Intersection_Of {" ;
00058   for ( i = intersection.begin() ; i != intersection.end() ; ++i ) {
00059     const std::string & n = (*i)->name() ; os << " " << n ;
00060   }
00061   os << " } }" << std::endl ;
00062 
00063   if ( lead != NULL ) { os << lead ; }
00064   os << "  Subsets {" ;
00065   for ( i = subsets.begin() ; i != subsets.end() ; ++i ) {
00066     const std::string & n = (*i)->name() ; os << " " << n ;
00067   }
00068   os << " }" << std::endl ;
00069 
00070   return os ;
00071 }
00072 
00073 //----------------------------------------------------------------------
00074 
00075 void order( PartVector & v )
00076 {
00077   PartVector::iterator ev = v.end();
00078   PartVector::iterator iv = v.begin();
00079   std::sort( iv , ev , PartLess() );
00080   iv = std::unique( iv , ev );
00081   v.erase( iv , ev );
00082 }
00083 
00084 bool insert( PartVector & v , Part & part )
00085 {
00086   const PartVector::iterator e = v.end();
00087         PartVector::iterator i = v.begin();
00088 
00089   i = std::lower_bound( i , e , part , PartLess() );
00090 
00091   const bool new_member = i == e || *i != & part ;
00092 
00093   if ( new_member ) { v.insert( i , &part ); }
00094   return new_member ;
00095 }
00096 
00097 void remove( PartVector & v , Part & part )
00098 {
00099   const PartVector::iterator e = v.end();
00100         PartVector::iterator i = v.begin();
00101 
00102   i = std::lower_bound( i , e , part , PartLess() );
00103 
00104   if ( i != e && *i == & part ) { v.erase( i ); }
00105 }
00106 
00107 bool contain( const PartVector & v , const Part & part )
00108 {
00109   const PartVector::const_iterator e = v.end();
00110         PartVector::const_iterator i = v.begin();
00111 
00112   i = std::lower_bound( i , e , part , PartLess() );
00113 
00114   return i != e && *i == & part ;
00115 }
00116 
00117 bool contain( const PartVector & super , const PartVector & sub )
00118 {
00119   bool result = ( ! sub.empty() ) && ( sub.size() <= super.size() );
00120 
00121   if ( result ) {
00122     PartLess comp ;
00123 
00124     const PartVector::const_iterator ev = super.end();
00125           PartVector::const_iterator iv = super.begin();
00126 
00127     const PartVector::const_iterator ep = sub.end();
00128           PartVector::const_iterator ip = sub.begin();
00129 
00130     while ( result && ip != ep ) {
00131       Part * const q = *ip ; ++ip ;
00132       iv = std::lower_bound( iv , ev , q , comp );
00133       result = iv != ev && *iv == q ;
00134     }
00135   }
00136 
00137   return result ;
00138 }
00139 
00140 size_t intersect( const PartVector & v , const PartVector & p )
00141 {
00142   // Both lists must be sorted, assume v.size() > p.size()
00143 
00144   const PartVector::const_iterator ev = v.end();
00145         PartVector::const_iterator iv = v.begin();
00146 
00147   const PartVector::const_iterator ep = p.end();
00148         PartVector::const_iterator ip = p.begin();
00149 
00150   size_t count = 0 ;
00151 
00152   for ( ; ip != ep && iv != ev ; ++ip ) {
00153     Part * const q = *ip ;
00154     iv = std::lower_bound( iv , ev , q , PartLess() );
00155     if ( iv != ev && *iv == q ) { ++count ; }
00156   }
00157 
00158   return count ;
00159 }
00160 
00161 size_t intersect( const PartVector & v , const PartVector & p , PartVector & r )
00162 {
00163   // Both lists must be sorted, assume v.size() > p.size()
00164 
00165   const PartVector::const_iterator ev = v.end();
00166         PartVector::const_iterator iv = v.begin();
00167 
00168   const PartVector::const_iterator ep = p.end();
00169         PartVector::const_iterator ip = p.begin();
00170 
00171   for ( ; ip != ep && iv != ev ; ++ip ) {
00172     Part * const q = *ip ;
00173     iv = std::lower_bound( iv , ev , q , PartLess() );
00174     if ( iv != ev && *iv == q ) { r.push_back( q ); }
00175   }
00176 
00177   return r.size() ;
00178 }
00179 
00180 bool intersect( const Part & a , const Part & b )
00181 {
00182   const PartVector & a_sub = a.subsets();
00183   const PartVector & b_sub = b.subsets();
00184   return contain( a_sub , b ) ||
00185          contain( b_sub , a ) ||
00186          intersect( b_sub , a_sub );
00187 }
00188 
00189 //----------------------------------------------------------------------
00190 //----------------------------------------------------------------------
00191 
00192 
00193 } // namespace mesh
00194 } // namespace stk
00195 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends