DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_BasicEntitySet.cpp
1 //---------------------------------------------------------------------------//
2 /*
3  Copyright (c) 2012, Stuart R. Slattery
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  *: Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  *: Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  *: Neither the name of the University of Wisconsin - Madison nor the
18  names of its contributors may be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 //---------------------------------------------------------------------------//
39 //---------------------------------------------------------------------------//
40 
41 #include "DTK_BasicEntitySet.hpp"
42 #include "DTK_DBC.hpp"
43 
44 #include <Teuchos_CommHelpers.hpp>
45 #include <Teuchos_OrdinalTraits.hpp>
46 #include <Teuchos_Ptr.hpp>
47 
48 namespace DataTransferKit
49 {
50 //---------------------------------------------------------------------------//
51 // BasicEntitySetIterator implementation.
52 //---------------------------------------------------------------------------//
53 // Default constructor.
54 BasicEntitySetIterator::BasicEntitySetIterator()
55  : d_entity( NULL )
56 { /* ... */
57 }
58 
59 //---------------------------------------------------------------------------//
60 // Constructor.
61 BasicEntitySetIterator::BasicEntitySetIterator(
62  Teuchos::RCP<std::unordered_map<EntityId, Entity>> map,
63  const PredicateFunction &predicate )
64  : d_map( map )
65  , d_map_it( d_map->begin() )
66 {
67  if ( ( d_map->size() > 0 ) && ( d_map_it != d_map->end() ) )
68  {
69  d_entity = &( d_map_it->second );
70  }
71  this->b_predicate = predicate;
72 }
73 
74 //---------------------------------------------------------------------------//
75 // Copy constructor.
76 BasicEntitySetIterator::BasicEntitySetIterator(
77  const BasicEntitySetIterator &rhs )
78  : d_map( rhs.d_map )
79  , d_map_it( rhs.d_map_it )
80 {
81  if ( ( d_map->size() > 0 ) && ( d_map_it != d_map->end() ) )
82  {
83  d_entity = &( d_map_it->second );
84  }
85  this->b_predicate = rhs.b_predicate;
86 }
87 
88 //---------------------------------------------------------------------------//
89 // Assignment operator.
92 {
93  this->b_predicate = rhs.b_predicate;
94  if ( &rhs == this )
95  {
96  return *this;
97  }
98  d_map = rhs.d_map;
99  d_map_it = rhs.d_map_it;
100  if ( ( d_map->size() > 0 ) && ( d_map_it != d_map->end() ) )
101  {
102  d_entity = &( d_map_it->second );
103  }
104  return *this;
105 }
106 
107 //---------------------------------------------------------------------------//
108 // Pre-increment operator.
109 EntityIterator &BasicEntitySetIterator::operator++()
110 {
111  ++d_map_it;
112  return *this;
113 }
114 
115 //---------------------------------------------------------------------------//
116 // Dereference operator.
117 Entity &BasicEntitySetIterator::operator*( void )
118 {
119  this->operator->();
120  return *d_entity;
121 }
122 
123 //---------------------------------------------------------------------------//
124 // Dereference operator.
125 Entity *BasicEntitySetIterator::operator->( void )
126 {
127  DTK_REQUIRE( d_map_it != d_map->end() );
128  d_entity = &( d_map_it->second );
129  return d_entity;
130 }
131 
132 //---------------------------------------------------------------------------//
133 // Equal comparison operator.
134 bool BasicEntitySetIterator::operator==( const EntityIterator &rhs ) const
135 {
136  const BasicEntitySetIterator *rhs_vec =
137  static_cast<const BasicEntitySetIterator *>( &rhs );
138  const BasicEntitySetIterator *rhs_vec_impl =
139  static_cast<const BasicEntitySetIterator *>(
140  rhs_vec->b_iterator_impl.get() );
141  return ( rhs_vec_impl->d_map_it == d_map_it );
142 }
143 
144 //---------------------------------------------------------------------------//
145 // Not equal comparison operator.
146 bool BasicEntitySetIterator::operator!=( const EntityIterator &rhs ) const
147 {
148  const BasicEntitySetIterator *rhs_vec =
149  static_cast<const BasicEntitySetIterator *>( &rhs );
150  const BasicEntitySetIterator *rhs_vec_impl =
151  static_cast<const BasicEntitySetIterator *>(
152  rhs_vec->b_iterator_impl.get() );
153  return ( rhs_vec_impl->d_map_it != d_map_it );
154 }
155 
156 //---------------------------------------------------------------------------//
157 // An iterator assigned to the beginning.
158 EntityIterator BasicEntitySetIterator::begin() const
159 {
160  return BasicEntitySetIterator( d_map, this->b_predicate );
161 }
162 
163 //---------------------------------------------------------------------------//
164 // An iterator assigned to the end.
165 EntityIterator BasicEntitySetIterator::end() const
166 {
167  BasicEntitySetIterator end_it( d_map, this->b_predicate );
168  end_it.d_map_it = d_map->end();
169  return end_it;
170 }
171 
172 //---------------------------------------------------------------------------//
173 // Create a clone of the iterator. We need this for the copy constructor
174 // and assignment operator to pass along the underlying implementation.
175 std::unique_ptr<EntityIterator> BasicEntitySetIterator::clone() const
176 {
177  return std::unique_ptr<EntityIterator>(
178  new BasicEntitySetIterator( *this ) );
179 }
180 
181 //---------------------------------------------------------------------------//
182 // BasicEntitySet implementation.
183 //---------------------------------------------------------------------------//
184 // Constructor.
186  const Teuchos::RCP<const Teuchos::Comm<int>> comm,
187  const int physical_dimension )
188  : d_comm( comm )
189  , d_physical_dim( physical_dimension )
190  , d_entities( 4 )
191 { /* ... */
192 }
193 
194 //---------------------------------------------------------------------------//
195 // Add an entity to the set.
196 void BasicEntitySet::addEntity( const Entity &entity )
197 {
198  d_entities[entity.topologicalDimension()].emplace( entity.id(), entity );
199 }
200 
201 //---------------------------------------------------------------------------//
202 // Get the parallel communicator for the entity set.
203 Teuchos::RCP<const Teuchos::Comm<int>> BasicEntitySet::communicator() const
204 {
205  return d_comm;
206 }
207 
208 //---------------------------------------------------------------------------//
209 // Return the physical dimension of the entities in the set.
210 int BasicEntitySet::physicalDimension() const { return d_physical_dim; }
211 
212 //---------------------------------------------------------------------------//
213 // Given an EntityId, get the entity.
214 void BasicEntitySet::getEntity( const EntityId entity_id,
215  const int topological_dimension,
216  Entity &entity ) const
217 {
218  DTK_CHECK( d_entities[topological_dimension].count( entity_id ) );
219  entity = d_entities[topological_dimension].find( entity_id )->second;
220 }
221 
222 //---------------------------------------------------------------------------//
223 // Get an iterator over a subset of the entity set that satisfies the given
224 // predicate.
226 BasicEntitySet::entityIterator( const int topological_dimension,
227  const PredicateFunction &predicate ) const
228 {
229  Teuchos::RCP<std::unordered_map<EntityId, Entity>> map_ptr =
230  Teuchos::rcpFromRef( d_entities[topological_dimension] );
231  return BasicEntitySetIterator( map_ptr, predicate );
232 }
233 
234 //---------------------------------------------------------------------------//
235 // Given an entity, get the entities of the given type that are adjacent to
236 // it.
238  const Entity &entity, const int adjacent_dimension,
239  Teuchos::Array<Entity> &adjacent_entities ) const
240 {
241  bool not_implemented = true;
242  DTK_INSIST( !not_implemented );
243 }
244 
245 //---------------------------------------------------------------------------//
246 
247 } // end namespace DataTransferKit
248 
249 //---------------------------------------------------------------------------//
250 // end DTK_BasicEntitySet.cpp
251 //---------------------------------------------------------------------------//
Geometric entity interface definition.
Definition: DTK_Entity.hpp:61
EntityIterator entityIterator(const int topological_dimension, const PredicateFunction &predicate=EntitySet::selectAll) const override
Get an iterator over a subset of the entity set that satisfies the given predicate.
void getEntity(const EntityId entity_id, const int topological_dimension, Entity &entity) const override
Entity access functions.
virtual void getAdjacentEntities(const Entity &entity, const int adjacent_dimension, Teuchos::Array< Entity > &adjacent_entities) const override
Given an entity, get the entities of the given type that are adjacent to it.
Entity iterator interface.
void addEntity(const Entity &entity)
Add an entity to the set.
Assertions and Design-by-Contract for error handling.
BasicEntitySet(const Teuchos::RCP< const Teuchos::Comm< int >> comm, const int physical_dimension)
Constructor.
ementation of iterator over entities in a basic set.
Teuchos::RCP< const Teuchos::Comm< int > > communicator() const override
Parallel functions.
std::function< bool(Entity)> PredicateFunction
Predicate function typedef.
Definition: DTK_Types.hpp:64
int physicalDimension() const override
Geometric data functions.
unsigned long int EntityId
Entity id type.
Definition: DTK_Types.hpp:50
int topologicalDimension() const
Return the topological dimension of the entity.
Definition: DTK_Entity.cpp:98
BasicEntitySetIterator & operator=(const BasicEntitySetIterator &rhs)
Assignment operator.
DTK_BasicEntitySet.cpp.
EntityId id() const
Client interface.
Definition: DTK_Entity.cpp:82