dune-spgrid 2.7
partitionlist.hh
Go to the documentation of this file.
1#ifndef DUNE_SPGRID_PARTITIONLIST_HH
2#define DUNE_SPGRID_PARTITIONLIST_HH
3
4#include <iterator>
5
7
8namespace Dune
9{
10
11 // SPPartitionList
12 // ---------------
13
14 template< int dim >
16 {
18
19 protected:
20 struct Node;
21
22 public:
24
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:
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 >
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 &
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
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 *
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 >
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
Definition: iostream.hh:7
bool operator==(const FieldVector< ct, dim > &a, const SPNormalVector< ct, dim > &b)
Definition: normal.hh:121
std::basic_ostream< char_type, traits > & operator<<(std::basic_ostream< char_type, traits > &out, const SPCube< ct, dim > &cube)
Definition: cube.hh:148
SPMultiIndex< dim > operator*(const SPMultiIndex< dim > &a, const int &b)
Definition: multiindex.hh:258
bool operator!=(const FieldVector< ct, dim > &a, const SPNormalVector< ct, dim > &b)
Definition: normal.hh:128
Definition: partition.hh:79
unsigned int number() const
Definition: partition.hh:242
Definition: partitionlist.hh:16
Iterator end() const
Definition: partitionlist.hh:56
SPPartition< dim > Partition
Definition: partitionlist.hh:20
const Partition * findPartition(const MultiIndex &id) const
Definition: partitionlist.hh:180
int volume() const
Definition: partitionlist.hh:192
SPPartitionList(This &&other)
Definition: partitionlist.hh:34
Partition::Mesh Mesh
Definition: partitionlist.hh:26
SPPartitionList(const This &other)
Definition: partitionlist.hh:32
~SPPartitionList()
Definition: partitionlist.hh:36
Partition::MultiIndex MultiIndex
Definition: partitionlist.hh:25
This & operator+=(const Partition &partition)
Definition: partitionlist.hh:157
bool empty() const
Definition: partitionlist.hh:62
unsigned int size() const
Definition: partitionlist.hh:202
SPPartitionList()
Definition: partitionlist.hh:30
Node * head_
Definition: partitionlist.hh:66
This & operator=(const This &other)
Definition: partitionlist.hh:38
Iterator begin() const
Definition: partitionlist.hh:55
bool contains(const MultiIndex &id, unsigned int number) const
Definition: partitionlist.hh:170
Definition: partitionlist.hh:76
Node(const Node &other)
Definition: partitionlist.hh:82
Node(const Partition &partition)
Definition: partitionlist.hh:77
void append(Node *other)
Definition: partitionlist.hh:99
Node(Node &&other)
Definition: partitionlist.hh:87
const Partition & partition() const
Definition: partitionlist.hh:107
const Node * next() const
Definition: partitionlist.hh:109
~Node()
Definition: partitionlist.hh:94
Definition: partitionlist.hh:124
Iterator(const Node *node=nullptr)
Definition: partitionlist.hh:125