Sierra Toolkit  Version of the Day
base/EntityKey.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef stk_mesh_EntityKey_hpp
10 #define stk_mesh_EntityKey_hpp
11 
12 #include <stdint.h>
13 #include <limits>
14 #include <boost/functional/hash.hpp>
15 
16 #include <stk_mesh/base/Types.hpp>
17 
18 namespace stk_classic {
19 namespace mesh {
20 
21 
22 // Note: EntityRank and EntityId typedefs are defined in Types.hpp
23 
28 //----------------------------------------------------------------------
63 union EntityKey {
64 public:
65  typedef uint64_t raw_key_type ;
66 
67  enum { rank_digits = 8 };
68 
69 private:
70 
71  enum {
72  invalid_key = ~raw_key_type(0) ,
73  raw_digits = std::numeric_limits<raw_key_type>::digits ,
74  id_digits = raw_digits - rank_digits ,
75  id_mask = ~raw_key_type(0) >> rank_digits
76  };
77 
78  raw_key_type key ;
79 
80  struct {
81  raw_key_type id : id_digits ;
82  raw_key_type rank : rank_digits ;
83  } normal_view ;
84 
85  struct {
86  raw_key_type rank : rank_digits ;
87  raw_key_type id : id_digits ;
88  } reverse_view ;
89 
90 public:
93 
97  EntityKey() : key(invalid_key) { }
98 
99  EntityKey( const EntityKey & rhs ) : key( rhs.key ) {}
100 
101  EntityKey & operator = ( const EntityKey & rhs )
102  { key = rhs.key ; return *this ; }
103 
115  EntityKey( EntityRank entity_rank, raw_key_type entity_id );
116 
117  raw_key_type id() const { return key & id_mask ; }
118 
119  EntityRank rank() const { return key >> id_digits ; }
120 
121  EntityRank type() const { return rank(); }
122 
123  bool operator==(const EntityKey &rhs) const {
124  return key == rhs.key;
125  }
126 
127  bool operator!=(const EntityKey &rhs) const {
128  return !(key == rhs.key);
129  }
130 
131  bool operator<(const EntityKey &rhs) const {
132  return key < rhs.key;
133  }
134 
135  bool operator>(const EntityKey &rhs) const {
136  return rhs.key < key;
137  }
138 
139  bool operator<=(const EntityKey &rhs) const {
140  return !(key < rhs.key);
141  }
142 
143  bool operator>=(const EntityKey &rhs) const {
144  return !(rhs.key < key);
145  }
146 
147  //------------------------------
148  // As safe and explict a conversion
149  // as possible between the raw_key_type and value.
150 
151  explicit EntityKey( const raw_key_type * const value )
152  : key( *value ) {}
153 
154  raw_key_type raw_key() const { return key ; }
155 };
156 
157 // Functions for encoding / decoding entity keys.
158 
160 inline
161 EntityRank entity_rank( const EntityKey & key ) {
162  return key.rank();
163 }
164 
166 inline
167 EntityId entity_id( const EntityKey & key ) {
168  return key.id();
169 }
170 
172 inline
173 bool entity_key_valid( const EntityKey & key ) {
174  return key != EntityKey();
175 }
176 
177 inline
178 bool entity_id_valid( EntityKey::raw_key_type id ) {
179  return 0 < id && id <= EntityKey().id();
180 }
181 
182 inline
183 size_t hash_value( EntityKey key) {
184  return boost::hash_value(key.raw_key());
185 }
186 
187 
188 
189 } // namespace mesh
190 } // namespace stk_classic
191 
192 #endif /* stk_mesh_EntityKey_hpp */
193 
bool entity_key_valid(const EntityKey &key)
Query if an entity key is valid.
Integer type for the entity keys, which is an encoding of the entity type and entity identifier...
EntityId entity_id(const EntityKey &key)
Given an entity key, return the identifier for the entity.
Sierra Toolkit.
EntityRank entity_rank(const EntityKey &key)
Given an entity key, return an entity type (rank).