|
Sierra Toolkit
Version of the Day
|
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_EntityKey_hpp 00010 #define stk_mesh_EntityKey_hpp 00011 00012 #include <stdint.h> 00013 #include <limits> 00014 #include <boost/functional/hash.hpp> 00015 00016 #include <stk_mesh/base/Types.hpp> 00017 00018 namespace stk_classic { 00019 namespace mesh { 00020 00021 00022 // Note: EntityRank and EntityId typedefs are defined in Types.hpp 00023 00028 //---------------------------------------------------------------------- 00063 union EntityKey { 00064 public: 00065 typedef uint64_t raw_key_type ; 00066 00067 enum { rank_digits = 8 }; 00068 00069 private: 00070 00071 enum { 00072 invalid_key = ~raw_key_type(0) , 00073 raw_digits = std::numeric_limits<raw_key_type>::digits , 00074 id_digits = raw_digits - rank_digits , 00075 id_mask = ~raw_key_type(0) >> rank_digits 00076 }; 00077 00078 raw_key_type key ; 00079 00080 struct { 00081 raw_key_type id : id_digits ; 00082 raw_key_type rank : rank_digits ; 00083 } normal_view ; 00084 00085 struct { 00086 raw_key_type rank : rank_digits ; 00087 raw_key_type id : id_digits ; 00088 } reverse_view ; 00089 00090 public: 00092 ~EntityKey() {} 00093 00097 EntityKey() : key(invalid_key) { } 00098 00099 EntityKey( const EntityKey & rhs ) : key( rhs.key ) {} 00100 00101 EntityKey & operator = ( const EntityKey & rhs ) 00102 { key = rhs.key ; return *this ; } 00103 00115 EntityKey( EntityRank entity_rank, raw_key_type entity_id ); 00116 00117 raw_key_type id() const { return key & id_mask ; } 00118 00119 EntityRank rank() const { return key >> id_digits ; } 00120 00121 EntityRank type() const { return rank(); } 00122 00123 bool operator==(const EntityKey &rhs) const { 00124 return key == rhs.key; 00125 } 00126 00127 bool operator!=(const EntityKey &rhs) const { 00128 return !(key == rhs.key); 00129 } 00130 00131 bool operator<(const EntityKey &rhs) const { 00132 return key < rhs.key; 00133 } 00134 00135 bool operator>(const EntityKey &rhs) const { 00136 return rhs.key < key; 00137 } 00138 00139 bool operator<=(const EntityKey &rhs) const { 00140 return !(key < rhs.key); 00141 } 00142 00143 bool operator>=(const EntityKey &rhs) const { 00144 return !(rhs.key < key); 00145 } 00146 00147 //------------------------------ 00148 // As safe and explict a conversion 00149 // as possible between the raw_key_type and value. 00150 00151 explicit EntityKey( const raw_key_type * const value ) 00152 : key( *value ) {} 00153 00154 raw_key_type raw_key() const { return key ; } 00155 }; 00156 00157 // Functions for encoding / decoding entity keys. 00158 00160 inline 00161 EntityRank entity_rank( const EntityKey & key ) { 00162 return key.rank(); 00163 } 00164 00166 inline 00167 EntityId entity_id( const EntityKey & key ) { 00168 return key.id(); 00169 } 00170 00172 inline 00173 bool entity_key_valid( const EntityKey & key ) { 00174 return key != EntityKey(); 00175 } 00176 00177 inline 00178 bool entity_id_valid( EntityKey::raw_key_type id ) { 00179 return 0 < id && id <= EntityKey().id(); 00180 } 00181 00182 inline 00183 size_t hash_value( EntityKey key) { 00184 return boost::hash_value(key.raw_key()); 00185 } 00186 00187 00188 00189 } // namespace mesh 00190 } // namespace stk_classic 00191 00192 #endif /* stk_mesh_EntityKey_hpp */ 00193