Sierra Toolkit Version of the Day
Bucket.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 #ifndef stk_mesh_Bucket_hpp
00010 #define stk_mesh_Bucket_hpp
00011 
00012 //----------------------------------------------------------------------
00013 
00014 #include <iosfwd>
00015 #include <vector>
00016 #include <algorithm>
00017 
00018 #include <stk_util/environment/ReportHandler.hpp>
00019 
00020 #include <stk_mesh/baseImpl/BucketImpl.hpp>
00021 
00022 #include <stk_mesh/base/Types.hpp>
00023 #include <stk_mesh/base/Field.hpp>
00024 #include <stk_mesh/base/Part.hpp>
00025 #include <stk_mesh/base/Entity.hpp>
00026 
00027 #include <boost/iterator/transform_iterator.hpp>
00028 
00029 //----------------------------------------------------------------------
00030 
00031 namespace stk {
00032 namespace mesh {
00033 
00034 namespace impl {
00035 class BucketRepository;
00036 } // namespace impl
00037 
00038 
00046 std::ostream & operator << ( std::ostream & , const Bucket & );
00047 
00049 std::ostream &
00050 print( std::ostream & , const std::string & indent , const Bucket & );
00051 
00052 // The part count and parts are equal
00053 bool bucket_part_equal( const unsigned * lhs , const unsigned * rhs );
00054 
00055 //----------------------------------------------------------------------
00059 bool has_superset( const Bucket & ,  const Part & p );
00060 
00064 bool has_superset( const Bucket & ,  const unsigned & ordinal );
00065 
00069 bool has_superset( const Bucket & , const PartVector & );
00070 
00071 //----------------------------------------------------------------------
00076 class BucketIterator : public std::iterator<std::random_access_iterator_tag,Entity > {
00077 private:
00078   const Bucket * m_bucket_ptr;
00079   size_t         m_current_entity;
00080 
00081   inline Entity & entity( const size_t ) const ;
00082 
00083   template< class field_type >
00084     friend
00085     typename FieldTraits< field_type >::data_type *
00086     field_data( const field_type & f , const BucketIterator &i );
00087 
00088   template< class field_type > friend struct BucketArray ;
00089 
00090 public:
00091 
00096   template< typename intType >
00097   BucketIterator(const Bucket * const bucket_ptr, intType offset) {
00098     m_bucket_ptr = bucket_ptr;
00099     m_current_entity = offset;
00100   }
00101 
00103   BucketIterator() {
00104     m_bucket_ptr = NULL;
00105     m_current_entity = 0;
00106   }
00107 
00109   BucketIterator(const BucketIterator &i) {
00110     m_bucket_ptr = i.m_bucket_ptr;
00111     m_current_entity = i.m_current_entity;
00112   }
00113 
00115   BucketIterator & operator=(const BucketIterator &i) {
00116     m_bucket_ptr = i.m_bucket_ptr;
00117     m_current_entity = i.m_current_entity;
00118     return *this;
00119   }
00120 
00124   inline Entity & operator*() const { return entity(0); }
00125 
00129   inline Entity * operator->() const { return & entity(0); }
00130 
00132   inline BucketIterator & operator++() {
00133     ++m_current_entity;
00134     return *this;
00135   }
00136 
00138   inline BucketIterator & operator--() {
00139     --m_current_entity;
00140     return *this;
00141   }
00142 
00144   inline BucketIterator operator++(int) {
00145     BucketIterator temp = *this;
00146     ++m_current_entity;
00147     return temp;
00148   }
00149 
00151   inline BucketIterator operator--(int) {
00152     BucketIterator temp = *this;
00153     --m_current_entity;
00154     return temp;
00155   }
00156 
00158   inline bool operator<(const BucketIterator &i) const {
00159     ThrowAssertMsg(m_bucket_ptr == i.m_bucket_ptr,
00160                    "operator < given iterator from different bucket");
00161     return (m_current_entity < i.m_current_entity);
00162   }
00163 
00165   inline bool operator<=(const BucketIterator &i) const {
00166     ThrowAssertMsg(m_bucket_ptr == i.m_bucket_ptr,
00167                    "operator <= given iterator from different bucket");
00168     return (m_current_entity <= i.m_current_entity);
00169   }
00170 
00172   inline bool operator>(const BucketIterator &i) const {
00173     ThrowAssertMsg(m_bucket_ptr == i.m_bucket_ptr,
00174                    "operator > given iterator from different bucket");
00175     return (m_current_entity > i.m_current_entity);
00176   }
00177 
00179   inline bool operator>=(const BucketIterator &i) const {
00180     ThrowAssertMsg(m_bucket_ptr == i.m_bucket_ptr,
00181                    "operator >= given iterator from different bucket");
00182     return (m_current_entity >= i.m_current_entity);
00183   }
00184 
00186   inline bool operator==(const BucketIterator &i) const {
00187     ThrowAssertMsg(m_bucket_ptr == i.m_bucket_ptr,
00188                    "operator == given iterator from different bucket");
00189     return (m_current_entity == i.m_current_entity);
00190   }
00191 
00193   inline bool operator!=(const BucketIterator &i) const {
00194     ThrowAssertMsg(m_bucket_ptr == i.m_bucket_ptr,
00195                    "operator != given iterator from different bucket");
00196     return (m_current_entity != i.m_current_entity);
00197   }
00198 
00199   inline BucketIterator & operator+=(int n) {
00200     m_current_entity += n;
00201     return *this;
00202   }
00203 
00204   inline BucketIterator & operator-=(int n) {
00205     m_current_entity -= n;
00206     return *this;
00207   }
00208 
00209   inline BucketIterator operator+(int n) const {
00210     return BucketIterator(m_bucket_ptr, m_current_entity + n);
00211   }
00212 
00213   inline BucketIterator operator-(int n) const {
00214     return BucketIterator(m_bucket_ptr, m_current_entity - n);
00215   }
00216 
00218   inline ptrdiff_t operator-(const BucketIterator &i) const {
00219     ThrowAssertMsg(m_bucket_ptr == i.m_bucket_ptr,
00220                    "operator - given iterator from different bucket");
00221     return static_cast<ptrdiff_t>(m_current_entity - i.m_current_entity);
00222   }
00223 
00224   template< typename intType >
00225   inline Entity & operator[]( const intType & n ) const { return entity(n); }
00226 
00227 }; // class BucketIterator
00228 
00229 struct To_Ptr : std::unary_function<Entity&, Entity*>
00230 {
00231   Entity* operator()(Entity& entity) const
00232   {
00233     return &entity;
00234   }
00235 };
00236 
00237 // Sometimes, we want a bucket-iterator to dereference to an Entity*
00238 typedef boost::transform_iterator<To_Ptr, BucketIterator> BucketPtrIterator;
00239 
00240 //----------------------------------------------------------------------
00248 class Bucket {
00249 private:
00250   friend class impl::BucketRepository ;
00251   friend class impl::BucketImpl ;
00252 
00253   impl::BucketImpl       m_bucketImpl ;
00254 
00255 public:
00256 
00257   //--------------------------------
00258   // Container-like types and methods:
00259 
00260   typedef BucketIterator iterator ;
00261 
00263   inline iterator begin() const { return iterator(this,0); }
00264 
00266   inline iterator end() const { return iterator(this,size()); }
00267 
00269   size_t size() const { return m_bucketImpl.size() ; }
00270 
00272   size_t capacity() const { return m_bucketImpl.capacity() ; }
00273 
00275   Entity & operator[] ( size_t i ) const { return m_bucketImpl[i] ; }
00276 
00278   unsigned field_data_size(const FieldBase & field) const
00279   { return m_bucketImpl.field_data_size(field); }
00280 
00282   const FieldBase::Restriction::size_type * field_data_stride( const FieldBase & field ) const
00283   { return m_bucketImpl.field_data_stride(field); }
00284 
00286   unsigned char * field_data_location( const FieldBase & field, const Entity & entity ) const
00287   { return m_bucketImpl.field_data_location(field,entity); }
00288 
00290   unsigned char * field_data_location( const FieldBase & field ) const
00291   { return m_bucketImpl.field_data_location(field); }
00292 
00294   template< class field_type >
00295   typename FieldTraits< field_type >::data_type *
00296   field_data( const field_type & field , const Entity & entity ) const
00297   { return m_bucketImpl.field_data(field,entity.bucket_ordinal()); }
00298 
00299   //--------------------------------
00303   BulkData & mesh() const { return m_bucketImpl.mesh(); }
00304 
00306   unsigned entity_rank() const { return m_bucketImpl.entity_rank(); }
00307 
00309   void supersets( PartVector & ) const ;
00310 
00311   //--------------------------------
00313   bool member( const Part & ) const ;
00314 
00316   bool member_all( const std::vector<Part*> & ) const ;
00317 
00319   bool member_any( const std::vector<Part*> & ) const ;
00320 
00321   //--------------------------------
00323   std::pair<const unsigned *, const unsigned *>
00324     superset_part_ordinals() const { return m_bucketImpl.superset_part_ordinals() ; }
00325 
00328   bool equivalent( const Bucket& b ) const {
00329     return m_bucketImpl.equivalent(b.m_bucketImpl);
00330   }
00331 
00332 #ifndef DOXYGEN_COMPILE
00333   const unsigned * key() const { return m_bucketImpl.key() ; }
00334 #endif /* DOXYGEN_COMPILE */
00335 
00337   unsigned allocation_size() const { return m_bucketImpl.allocation_size() ; }
00338 
00339 private:
00343   BulkData & bulk_data() const { return m_bucketImpl.mesh(); }
00344 
00345   ~Bucket();
00346   Bucket();
00347   Bucket( const Bucket & );
00348   Bucket & operator = ( const Bucket & );
00349 
00350   Bucket( BulkData        & arg_mesh ,
00351           EntityRank        arg_entity_rank ,
00352           const unsigned  * arg_key ,
00353           size_t            arg_alloc_size ,
00354           size_t            arg_capacity ,
00355           impl::BucketImpl::DataMap * arg_field_map ,
00356           Entity         ** arg_entity_array );
00357 
00358   friend class ::stk::mesh::BulkData;
00359 };
00360 
00361 
00362 
00363 struct BucketLess {
00364   bool operator()( const Bucket * lhs_bucket , const unsigned * rhs ) const ;
00365   bool operator()( const unsigned * lhs , const Bucket * rhs_bucket ) const ;
00366 };
00367 
00368 
00369 inline
00370 std::vector<Bucket*>::iterator
00371 lower_bound( std::vector<Bucket*> & v , const unsigned * key )
00372 { return std::lower_bound( v.begin() , v.end() , key , BucketLess() ); }
00373 
00374 
00375 
00378 } // namespace mesh
00379 } // namespace stk
00380 
00381 //----------------------------------------------------------------------
00382 //----------------------------------------------------------------------
00383 
00384 namespace stk {
00385 namespace mesh {
00386 
00387 inline Entity & BucketIterator::entity( const size_t i ) const
00388 {
00389   ThrowAssert( m_bucket_ptr );
00390   return (*m_bucket_ptr)[ m_current_entity + i ] ;
00391 }
00392 
00393 }
00394 }
00395 
00396 #endif
00397 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends