Sierra Toolkit  Version of the Day
Selector.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 
10 #ifndef stk_mesh_Selector_hpp
11 #define stk_mesh_Selector_hpp
12 
13 #include <iosfwd>
14 #include <algorithm>
15 #include <stk_mesh/base/Types.hpp>
16 #include <string>
17 
18 namespace stk_classic {
19 namespace mesh {
20 
21 //An operator to obtain a part-ordinal from a part-iterator.
22 //This general template handles cases where the part-iterator
23 //iterates either stk_classic::mesh::Part or Fmwk::MeshPart objects.
24 //Specializations for part-ordinal-pointers follow below.
25 template<typename PartIterator>
26 struct GetPartIterOrdinal {
27 unsigned operator()(PartIterator p_it) const
28 { return (*p_it)->mesh_meta_data_ordinal(); }
29 };
30 
31 template<>
32 struct GetPartIterOrdinal<const unsigned*> {
33 unsigned operator()(const unsigned* p_it) const
34 { return *p_it; }
35 };
36 
37 template<>
38 struct GetPartIterOrdinal<unsigned*> {
39 unsigned operator()(unsigned* p_it) const
40 { return *p_it; }
41 };
42 
43 
44 struct PartOrdLess {
45 
46 bool operator()(unsigned lhs, unsigned rhs) const
47 { return lhs < rhs; }
48 
49 };
50 
51 //Function to determine whether a specified part-ordinal is present
52 //in a given range of parts.
53 //Caller-provided comparison-operator compares a part-ordinal with
54 //one obtained from whatever a PartIterator dereferences to.
55 template<typename PartIterator, class Compare>
56 bool part_is_present(unsigned part_ord,
57  const std::pair<PartIterator, PartIterator>& part_range,
58  Compare comp)
59 {
60  GetPartIterOrdinal<PartIterator> get_part_ordinal;
61 
62  // Search for 'part_ord' in the bucket's list of sorted integer part ords
63  PartIterator p_it = std::lower_bound(part_range.first, part_range.second, part_ord, comp);
64  return (p_it != part_range.second && get_part_ordinal(p_it) == part_ord);
65 }
66 
67 enum Op{
68  INVALID = 0,
69  COMPOUND = 1,
70  PART_ID = 2
71  };
72 
73 struct OpType {
74  unsigned m_part_id ;
75  unsigned short m_unary ;
76  unsigned short m_count ;
77  Op m_op ;
78 
79  OpType() : m_part_id(0), m_unary(0), m_count(0), m_op(INVALID) {}
80  OpType( unsigned part_id , unsigned unary , unsigned count, Op op=INVALID )
81  : m_part_id( part_id ), m_unary( unary ), m_count( count ), m_op(op) {}
82 
83  bool operator == (const OpType & opType ) const
84  {
85  return m_part_id == opType.m_part_id &&
86  m_unary == opType.m_unary &&
87  m_count == opType.m_count &&
88  m_op == opType.m_op;
89  }
90  bool operator != (const OpType & opType ) const
91  { return !(*this == opType); }
92 };
93 
112 class Selector {
113 public:
115  Selector();
116 
117  bool operator == (const Selector & rhs) const
118  { return m_op == rhs.m_op; }
119 
120  bool operator != (const Selector & rhs) const
121  { return m_op != rhs.m_op; }
122 
124  Selector( const Part & part);
125 
127  Selector & operator &= ( const Selector & selector);
128 
130  Selector & operator |= ( const Selector & selector);
131 
135  Selector & complement();
136 
139  { Selector S( *this ); return S.complement(); }
140 
144  bool operator()( const Part & part ) const;
145 
149  bool operator()( const Bucket & candidate ) const;
150 
154  bool operator()( const Bucket * candidate ) const;
155 
159  bool operator()( const Entity & candidate ) const;
160 
164  template<typename PartIterator, class Compare>
165  bool apply(const std::pair<PartIterator,PartIterator>& part_range, Compare comp) const
166  { return apply(m_op.begin(), m_op.end(), part_range, comp); }
167 
169 #ifndef SWIG
170  friend std::ostream & operator << ( std::ostream & out, const Selector & selector);
171 #endif
172 
173  const std::vector<OpType>& get_ops() const { return m_op; }
174  void set_ops(const std::vector<OpType>& ops) { m_op = ops; }
175 
177  void compoundAll();
178 
179 private:
180 
182  const MetaData * m_mesh_meta_data ;
183 
185  std::vector< OpType > m_op ;
186 
188  void verify_compatible( const Selector & B ) const;
189 
191  void verify_compatible( const Bucket & B ) const;
192 
194  template<typename PartIterator, class Compare>
195  bool apply(
196  std::vector<OpType>::const_iterator i,
197  std::vector<OpType>::const_iterator j,
198  const std::pair<PartIterator,PartIterator>& part_range,
199  Compare comp) const
200  {
201  bool result = i != j ;
202  while ( result && i != j ) {
203  const unsigned statement_length = i->m_count;
204  if ( statement_length > 0 ) { // Check if compound statement
205  result = i->m_unary ^ apply( i + 1 , i + statement_length , part_range , comp );
206  i += statement_length;
207  }
208  else { // Test for containment of bucket in this part, or not in
209  result = i->m_unary ^ part_is_present( i->m_part_id , part_range , comp );
210  ++i ;
211  }
212  }
213  return result ;
214  }
215 
217  std::string printExpression(
218  const std::vector<OpType>::const_iterator start,
219  const std::vector<OpType>::const_iterator finish
220  ) const;
221 
222 };
223 
224 class Part;
225 
226 #ifndef SWIG
227 std::ostream & operator<<( std::ostream & out, const Selector & selector);
228 #endif
229 
233 Selector operator & ( const Part & A , const Part & B );
234 
238 Selector operator & ( const Part & A , const Selector & B );
239 
243 Selector operator & ( const Selector & A, const Part & B );
244 
248 Selector operator & ( const Selector & A, const Selector & B );
249 
253 Selector operator | ( const Part & A , const Part & B );
254 
258 Selector operator | ( const Part & A , const Selector & B );
259 
263 Selector operator | ( const Selector & A, const Part & B );
264 
268 Selector operator | ( const Selector & A , const Selector & B );
269 
273 Selector operator ! ( const Part & A );
274 
275 
279 Selector selectUnion( const PartVector& union_part_vector );
280 
284 Selector selectIntersection( const PartVector& intersection_part_vector );
285 
289 Selector selectField( const FieldBase& field );
290 
293 } // namespace mesh
294 } // namespace stk_classic
295 
296 #endif // stk_mesh_Selector_hpp
297 
Selector & operator&=(const Selector &selector)
Intersection: this = this INTERSECT ( expression )
friend std::ostream & operator<<(std::ostream &out, const Selector &selector)
Pretty print the set-expression with part names.
Definition: Selector.cpp:207
Selector & complement()
Complement: this = !(this) Postcondition: this is a compound expression.
Definition: Selector.cpp:42
This is a class for selecting buckets based on a set of meshparts and set logic.
Definition: Selector.hpp:112
Selector()
A default Selector selects nothing.
Definition: Selector.cpp:23
An application-defined subset of a problem domain.
Definition: Part.hpp:49
Selector operator!() const
Complement: return !(this)
Definition: Selector.hpp:138
std::ostream & operator<<(std::ostream &s, const Bucket &k)
Print the part names for which this bucket is a subset.
Definition: Bucket.cpp:239
Selector & operator|=(const Selector &selector)
Union: this = this UNION ( expression )
Definition: Selector.cpp:87
A fundamental unit within the discretization of a problem domain, including but not limited to nodes...
Definition: Entity.hpp:120
Sierra Toolkit.
bool apply(const std::pair< PartIterator, PartIterator > &part_range, Compare comp) const
Is the intersection of the &#39;part_ords&#39; parts a member of the set defined by the selector expression...
Definition: Selector.hpp:165
void compoundAll()
Turn the entire expression into a compound.
Definition: Selector.cpp:36
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31
A container for the field data of a homogeneous collection of entities.
Definition: Bucket.hpp:94
bool operator()(const Part &part) const
Is this part a member of the set defined by the selector expression.
Definition: Selector.cpp:56
eastl::iterator_traits< InputIterator >::difference_type count(InputIterator first, InputIterator last, const T &value)