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