Dune Core Modules (2.9.0)

partitionlist.hh
1#ifndef DUNE_SPGRID_PARTITIONLIST_HH
2#define DUNE_SPGRID_PARTITIONLIST_HH
3
4#include <iterator>
5
6#include <dune/grid/spgrid/partition.hh>
7
8namespace Dune
9{
10
11 // SPPartitionList
12 // ---------------
13
14 template< int dim >
15 class SPPartitionList
16 {
17 typedef SPPartitionList< dim > This;
18
19 protected:
20 struct Node;
21
22 public:
23 typedef SPPartition< dim > Partition;
24
25 typedef typename Partition::MultiIndex MultiIndex;
26 typedef typename Partition::Mesh Mesh;
27
28 struct Iterator;
29
30 SPPartitionList () : head_( nullptr ) {}
31
32 SPPartitionList ( const This &other ) : head_( other.head_ ? new Node( *other.head_ ) : nullptr ) {}
33
34 SPPartitionList ( This &&other ) : head_( other.head_ ) { other.head_ = nullptr; }
35
36 ~SPPartitionList () { delete head_; }
37
38 This &operator= ( const This &other )
39 {
40 delete head_;
41 head_ = (other.head_ ? new Node( *other.head_ ) : nullptr);
42 return *this;
43 }
44
45 This &operator= ( This &&other )
46 {
47 delete head_;
48 head_ = other.head_;
49 other.head_ = nullptr;
50 return *this;
51 }
52
53 This &operator+= ( const Partition &partition );
54
55 Iterator begin () const { return Iterator( head_ ); }
56 Iterator end () const { return Iterator( nullptr ); }
57
58 bool contains ( const MultiIndex &id, unsigned int number ) const;
59 const Partition *findPartition ( const MultiIndex &id ) const;
60 int volume () const;
61
62 bool empty () const { return !head_; }
63 unsigned int size () const;
64
65 protected:
66 Node *head_;
67 };
68
69
70
71 // SPPartitionList::Node
72 // ---------------------
73
74 template< int dim >
75 struct SPPartitionList< dim >::Node
76 {
77 explicit Node ( const Partition &partition )
78 : partition_( partition ),
79 next_( nullptr )
80 {}
81
82 Node ( const Node &other )
83 : partition_( other.partition_ ),
84 next_( other.next_ ? new Node( *other.next_ ) : nullptr )
85 {}
86
87 Node ( Node &&other )
88 : partition_( other.partition_ ),
89 next_( other.next_ )
90 {
91 other.next_ = nullptr;
92 }
93
94 ~Node () { delete next_; }
95
96 Node &operator= ( const Node & ) = delete;
97 Node &operator= ( Node && ) = delete;
98
99 void append ( Node *other )
100 {
101 if( next_ )
102 next_->append( other );
103 else
104 next_ = other;
105 }
106
107 const Partition &partition () const { return partition_; }
108
109 const Node *next () const { return next_; }
110
111 private:
112 Partition partition_;
113 Node *next_;
114 };
115
116
117
118 // SPPartitionList::Iterator
119 // -------------------------
120
121 template< int dim >
122 struct SPPartitionList< dim >::Iterator
123 : public std::iterator< std::forward_iterator_tag, const Partition >
124 {
125 explicit Iterator ( const Node *node = nullptr )
126 : node_( node )
127 {}
128
129 Iterator &operator++ ()
130 {
131 assert( *this );
132 node_ = node_->next();
133 return *this;
134 }
135
136 Iterator operator++ ( int ) { Iterator copy( *this ); ++(*this); return copy; }
137
138 operator bool () const { return bool( node_ ); }
139
140 bool operator== ( const Iterator &other ) const { return (node_ == other.node_); }
141 bool operator!= ( const Iterator &other ) const { return (node_ != other.node_); }
142
143 const Partition &operator* () const { assert( *this ); return node_->partition(); }
144 const Partition *operator-> () const { assert( *this ); return &(node_->partition()); }
145
146 private:
147 const Node *node_;
148 };
149
150
151
152 // Implementation of SPPartitionList
153 // ---------------------------------
154
155 template< int dim >
156 inline typename SPPartitionList< dim >::This &
157 SPPartitionList< dim >::operator+= ( const Partition &partition )
158 {
159 if( head_ )
160 head_->append( new Node( partition ) );
161 else
162 head_ = new Node( partition );
163 return *this;
164 }
165
166
167 template< int dim >
168 inline bool
169 SPPartitionList< dim >
170 ::contains ( const MultiIndex &id, unsigned int number ) const
171 {
172 const Partition *partition = findPartition( id );
173 assert( !partition || (partition->number() == number) );
174 return bool( partition );
175 }
176
177
178 template< int dim >
179 inline const typename SPPartitionList< dim >::Partition *
180 SPPartitionList< dim >::findPartition ( const MultiIndex &id ) const
181 {
182 for( const Node *it = head_; it; it = it->next() )
183 {
184 if( it->partition().contains( id ) )
185 return &(it->partition());
186 }
187 return nullptr;
188 }
189
190
191 template< int dim >
192 inline int SPPartitionList< dim >::volume () const
193 {
194 int volume = 0;
195 for( const Node *it = head_; it; it = it->next() )
196 volume += it->partition().volume();
197 return volume;
198 }
199
200
201 template< int dim >
202 inline unsigned int SPPartitionList< dim >::size () const
203 {
204 unsigned int size = 0;
205 for( const Node *it = head_; it; it = it->next() )
206 ++size;
207 return size;
208 }
209
210
211
212 // Auxilliary Functions for SPPartitionList
213 // ----------------------------------------
214
215 template< class char_type, class traits, int dim >
216 inline std::basic_ostream< char_type, traits > &
217 operator<< ( std::basic_ostream< char_type, traits > &out, const SPPartitionList< dim > &partition )
218 {
219 typedef typename SPPartitionList< dim >::Iterator Iterator;
220 std::string separator = "";
221 for( Iterator it = partition.begin(); it; ++it )
222 {
223 out << separator << *it;
224 separator = "; ";
225 }
226 return out;
227 }
228
229} // namespace Dune
230
231#endif // #ifndef DUNE_SPGRID_PARTITIONLIST_HH
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:237
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:259
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)