Sierra Toolkit  Version of the Day
Part.cpp
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 #include <stk_mesh/base/Types.hpp>
10 #include <stk_mesh/base/Part.hpp>
11 #include <stk_util/util/string_case_compare.hpp>
12 
13 #include <algorithm>
14 #include <ostream>
15 #include <sstream>
16 
17 namespace stk_classic {
18 namespace mesh {
19 
20 //----------------------------------------------------------------------
21 
22 Part * find( const PartVector & parts , const std::string & name )
23 {
24  PartVector::const_iterator i = parts.begin();
25 
26  while ( i != parts.end() && not_equal_case((*i)->name(),name) ) { ++i ; }
27 
28  return i != parts.end() ? *i : NULL ;
29 }
30 
31 //----------------------------------------------------------------------
32 
33 std::ostream &
34 print( std::ostream & os , const char * const lead , const Part & p )
35 {
36  const PartVector & supersets = p.supersets();
37  const PartVector & subsets = p.subsets();
38  const PartVector & intersection = p.intersection_of();
39 
40  std::vector<Part*>::const_iterator i ;
41 
42  if ( lead != NULL ) { os << lead ; }
43  os << "Part[ " ;
44  os << p.name() ;
45  os << " , " ;
46  os << p.mesh_meta_data_ordinal() ;
47  os << " ] {" ;
48  os << std::endl ;
49 
50  if ( lead != NULL ) { os << lead ; }
51  os << " Supersets {" ;
52  for ( i = supersets.begin() ; i != supersets.end() ; ++i ) {
53  const std::string & n = (*i)->name() ; os << " " << n ;
54  }
55  os << " }" << std::endl ;
56 
57  if ( lead != NULL ) { os << lead ; }
58  os << " Intersection_Of {" ;
59  for ( i = intersection.begin() ; i != intersection.end() ; ++i ) {
60  const std::string & n = (*i)->name() ; os << " " << n ;
61  }
62  os << " } }" << std::endl ;
63 
64  if ( lead != NULL ) { os << lead ; }
65  os << " Subsets {" ;
66  for ( i = subsets.begin() ; i != subsets.end() ; ++i ) {
67  const std::string & n = (*i)->name() ; os << " " << n ;
68  }
69  os << " }" << std::endl ;
70 
71  return os ;
72 }
73 
74 //----------------------------------------------------------------------
75 
76 void order( PartVector & v )
77 {
78  PartVector::iterator ev = v.end();
79  PartVector::iterator iv = v.begin();
80  std::sort( iv , ev , PartLess() );
81  iv = std::unique( iv , ev );
82  v.erase( iv , ev );
83 }
84 
85 bool insert( PartVector & v , Part & part )
86 {
87  const PartVector::iterator e = v.end();
88  PartVector::iterator i = v.begin();
89 
90  i = std::lower_bound( i , e , part , PartLess() );
91 
92  const bool new_member = i == e || *i != & part ;
93 
94  if ( new_member ) { v.insert( i , &part ); }
95  return new_member ;
96 }
97 
98 void remove( PartVector & v , Part & part )
99 {
100  const PartVector::iterator e = v.end();
101  PartVector::iterator i = v.begin();
102 
103  i = std::lower_bound( i , e , part , PartLess() );
104 
105  if ( i != e && *i == & part ) { v.erase( i ); }
106 }
107 
108 bool contain( const PartVector & v , const Part & part )
109 {
110  const PartVector::const_iterator e = v.end();
111  PartVector::const_iterator i = v.begin();
112 
113  i = std::lower_bound( i , e , part , PartLess() );
114 
115  return i != e && *i == & part ;
116 }
117 
118 bool contain( const PartVector & super , const PartVector & sub )
119 {
120  bool result = ( ! sub.empty() ) && ( sub.size() <= super.size() );
121 
122  if ( result ) {
123  PartLess comp ;
124 
125  const PartVector::const_iterator ev = super.end();
126  PartVector::const_iterator iv = super.begin();
127 
128  const PartVector::const_iterator ep = sub.end();
129  PartVector::const_iterator ip = sub.begin();
130 
131  while ( result && ip != ep ) {
132  Part * const q = *ip ; ++ip ;
133  iv = std::lower_bound( iv , ev , q , comp );
134  result = iv != ev && *iv == q ;
135  }
136  }
137 
138  return result ;
139 }
140 
141 size_t intersect( const PartVector & v , const PartVector & p )
142 {
143  // Both lists must be sorted, assume v.size() > p.size()
144 
145  const PartVector::const_iterator ev = v.end();
146  PartVector::const_iterator iv = v.begin();
147 
148  const PartVector::const_iterator ep = p.end();
149  PartVector::const_iterator ip = p.begin();
150 
151  size_t count = 0 ;
152 
153  for ( ; ip != ep && iv != ev ; ++ip ) {
154  Part * const q = *ip ;
155  iv = std::lower_bound( iv , ev , q , PartLess() );
156  if ( iv != ev && *iv == q ) { ++count ; }
157  }
158 
159  return count ;
160 }
161 
162 size_t intersect( const PartVector & v , const PartVector & p , PartVector & r )
163 {
164  // Both lists must be sorted, assume v.size() > p.size()
165 
166  const PartVector::const_iterator ev = v.end();
167  PartVector::const_iterator iv = v.begin();
168 
169  const PartVector::const_iterator ep = p.end();
170  PartVector::const_iterator ip = p.begin();
171 
172  for ( ; ip != ep && iv != ev ; ++ip ) {
173  Part * const q = *ip ;
174  iv = std::lower_bound( iv , ev , q , PartLess() );
175  if ( iv != ev && *iv == q ) { r.push_back( q ); }
176  }
177 
178  return r.size() ;
179 }
180 
181 bool intersect( const Part & a , const Part & b )
182 {
183  const PartVector & a_sub = a.subsets();
184  const PartVector & b_sub = b.subsets();
185  return contain( a_sub , b ) ||
186  contain( b_sub , a ) ||
187  intersect( b_sub , a_sub );
188 }
189 
190 std::string convert_to_internal_name(const std::string& part_name)
191 {
192  std::ostringstream out;
193  out << INTERNAL_PART_PREFIX << part_name << INTERNAL_PART_POSTFIX;
194  std::string out_str = out.str();
195  return out_str;
196 }
197 
198 
199 //----------------------------------------------------------------------
200 //----------------------------------------------------------------------
201 
202 
203 } // namespace mesh
204 } // namespace stk_classic
205 
std::ostream & print(std::ostream &os, const char *const lead, const Part &p)
Print a part&#39;s information including supersets, subsets, and intersection. Each line starts with the ...
Definition: Part.cpp:34
void order(PartVector &v)
Order a collection of parts: invoke sort and then unique.
Definition: Part.cpp:76
bool not_equal_case(const char *lhs, const char *rhs)
Case-insensitive inequality compare.
bool contain(const PartVector &super, const PartVector &sub)
Query containment for two properly ordered PartVector.
Definition: Part.cpp:118
An application-defined subset of a problem domain.
Definition: Part.hpp:49
const PartVector & intersection_of() const
Parts for which this part is defined as the intersection.
Definition: Part.hpp:81
bool intersect(const Part &a, const Part &b)
Query if two parts intersect; i.e., if one is a subset of the other or they share a common subset...
Definition: Part.cpp:181
const std::string & name() const
Application-defined text name of this part.
Definition: Part.hpp:67
unsigned mesh_meta_data_ordinal() const
Internally generated ordinal of this part that is unique within the owning meta data manager...
Definition: Part.hpp:72
const PartVector & supersets() const
Parts that are supersets of this part.
Definition: Part.hpp:75
Ordering operator for parts.
Definition: Part.hpp:150
const PartVector & subsets() const
Parts that are subsets of this part.
Definition: Part.hpp:78
Sierra Toolkit.
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31
Part * find(const PartVector &parts, const std::string &name)
Find a part by name in a collection of parts.
Definition: Part.cpp:22
bool insert(PartVector &v, Part &part)
Insert a part into a properly ordered collection of parts. Returns true if this is a new insertion...
Definition: Part.cpp:85