TBTK
Need a break? Support the development by playing Polarity Puzzles
SourceAmplitudeSet.h
Go to the documentation of this file.
1 /* Copyright 2018 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_SOURCE_AMPLITUDE_SET
24 #define COM_DAFER45_TBTK_SOURCE_AMPLITUDE_SET
25 
26 #include "TBTK/SerializableVector.h"
27 #include "TBTK/SourceAmplitude.h"
28 #include "TBTK/IndexedDataTree.h"
29 #include "TBTK/Serializable.h"
30 #include "TBTK/TBTKMacros.h"
31 
32 #include <vector>
33 
34 namespace TBTK{
35 
41 public:
44 
52  SourceAmplitudeSet(const std::string &serializeation, Mode mode);
53 
55  virtual ~SourceAmplitudeSet();
56 
60  void add(const SourceAmplitude &sourceAmplitude);
61 
70  std::vector<SourceAmplitude>& get(
71  const Index &index
72  );
73 
82  const std::vector<SourceAmplitude>& get(
83  const Index &index
84  ) const;
85 
86  class Iterator;
87  class ConstIterator;
88 private:
91  template<bool isConstIterator>
92  class _Iterator{
93  public:
96  typedef typename std::conditional<
97  isConstIterator,
98  const SourceAmplitude&,
100  >::type SourceAmplitudeReferenceType;
101 
103  void operator++();
104 
106  SourceAmplitudeReferenceType operator*();
107 
109  bool operator==(const _Iterator &rhs);
110 
112  bool operator!=(const _Iterator &rhs);
113  private:
116  typedef typename std::conditional<
117  isConstIterator,
119  SerializableVector<SourceAmplitude>
122  SerializableVector<SourceAmplitude>
123  >::Iterator
124  >::type IteratorType;
125 
127  unsigned int currentSourceAmplitude;
128 
130  IteratorType iterator;
131 
133  IteratorType iteratorEnd;
134 
137  friend class Iterator;
138  friend class ConstIterator;
139 
142  typedef typename std::conditional<
143  isConstIterator,
144  const IndexedDataTree<
145  SerializableVector<SourceAmplitude>
146  >,
148  SerializableVector<SourceAmplitude>
149  >
150  >::type SourceAmplitudeTreeType;
151 
154  _Iterator(
155  SourceAmplitudeTreeType &sourceAmplitudeTree,
156  bool end = false
157  );
158  };
159 public:
162  class Iterator : public _Iterator<false>{
163  private:
164  Iterator(
166  SerializableVector<SourceAmplitude>
167  > &sourceAmplitudeTree,
168  bool end = false
169  ) : _Iterator(sourceAmplitudeTree, end){};
170 
173  friend class SourceAmplitudeSet;
174  };
175 
178  class ConstIterator : public _Iterator<true>{
179  private:
181  const IndexedDataTree<
182  SerializableVector<SourceAmplitude>
183  > &sourceAmplitudeTree,
184  bool end = false
185  ) : _Iterator(sourceAmplitudeTree, end){};
186 
189  friend class SourceAmplitudeSet;
190  };
191 
197 
203 
209 
215 
221 
227 
229  virtual std::string serialize(Mode mode) const;
230 
232  unsigned int getSizeInBytes() const;
233 private:
236 };
237 
239  const SourceAmplitude &sourceAmplitude
240 ){
241  try{
242  std::vector<SourceAmplitude> &sourceAmplitudes
243  = sourceAmplitudeTree.get(sourceAmplitude.getIndex());
244  sourceAmplitudes.push_back(sourceAmplitude);
245  }
246  catch(ElementNotFoundException &e){
247  sourceAmplitudeTree.add(
248  std::vector<SourceAmplitude>(),
249  sourceAmplitude.getIndex()
250  );
251  std::vector<SourceAmplitude> &sourceAmplitudes
252  = sourceAmplitudeTree.get(sourceAmplitude.getIndex());
253  sourceAmplitudes.push_back(sourceAmplitude);
254  }
255 }
256 
257 inline std::vector<SourceAmplitude>& SourceAmplitudeSet::get(
258  const Index &index
259 ){
260  return sourceAmplitudeTree.get(index);
261 }
262 
263 inline const std::vector<SourceAmplitude>& SourceAmplitudeSet::get(
264  const Index &index
265 ) const{
266  return sourceAmplitudeTree.get(index);
267 }
268 
269 inline unsigned int SourceAmplitudeSet::getSizeInBytes() const{
270  return sizeof(this) - sizeof(sourceAmplitudeTree)
271  + sourceAmplitudeTree.getSizeInBytes();
272 }
273 
274 template<bool isConstIterator>
275 SourceAmplitudeSet::_Iterator<isConstIterator>::_Iterator(
276  SourceAmplitudeTreeType &sourceAmplitudeTree,
277  bool end
278 ) :
279  currentSourceAmplitude(0),
280  iterator(
281  end ? sourceAmplitudeTree.end() : sourceAmplitudeTree.begin()
282  ),
283  iteratorEnd(sourceAmplitudeTree.end())
284 {
285 }
286 
287 template<bool isConstIterator>
288 void SourceAmplitudeSet::_Iterator<isConstIterator>::operator++(){
289  if(iterator != iteratorEnd){
290  const std::vector<SourceAmplitude> &sourceAmplitudes = *iterator;
291  if(currentSourceAmplitude+1 == sourceAmplitudes.size()){
292  currentSourceAmplitude = 0;
293  ++iterator;
294  }
295  else{
296  currentSourceAmplitude++;
297  }
298  }
299 }
300 
301 template<bool isConstIterator>
302 typename SourceAmplitudeSet::_Iterator<isConstIterator>::SourceAmplitudeReferenceType
303 SourceAmplitudeSet::_Iterator<isConstIterator>::operator*(){
304  return (*iterator)[currentSourceAmplitude];
305 }
306 
307 template<bool isConstIterator>
308 bool SourceAmplitudeSet::_Iterator<isConstIterator>::operator==(
309  const _Iterator<isConstIterator> &rhs
310 ){
311  if(
312  iterator == rhs.iterator
313  && currentSourceAmplitude == rhs.currentSourceAmplitude
314  ){
315  return true;
316  }
317  else{
318  return false;
319  }
320 }
321 
322 template<bool isConstIterator>
323 bool SourceAmplitudeSet::_Iterator<isConstIterator>::operator!=(
324  const _Iterator<isConstIterator> &rhs
325 ){
326  if(
327  iterator != rhs.iterator
328  || currentSourceAmplitude != rhs.currentSourceAmplitude
329  ){
330  return true;
331  }
332  else{
333  return false;
334  }
335 }
336 
337 }; //End of namespace TBTK
338 
339 #endif
TBTK::SourceAmplitudeSet::Iterator
Definition: SourceAmplitudeSet.h:162
TBTK::IndexedDataTree
Definition: IndexedDataTree.h:45
TBTK::IndexedDataTree::add
void add(const Data &data, const Index &index)
Definition: IndexedDataTree.h:355
TBTK::IndexedDataTree::getSizeInBytes
unsigned int getSizeInBytes() const
Definition: IndexedDataTree.h:750
TBTK::SourceAmplitudeSet
SourceAmplitude container.
Definition: SourceAmplitudeSet.h:40
TBTK::SourceAmplitudeSet::get
std::vector< SourceAmplitude > & get(const Index &index)
Definition: SourceAmplitudeSet.h:257
TBTK::SourceAmplitudeSet::serialize
virtual std::string serialize(Mode mode) const
TBTK::SourceAmplitudeSet::getSizeInBytes
unsigned int getSizeInBytes() const
Definition: SourceAmplitudeSet.h:269
TBTK::SourceAmplitudeSet::end
SourceAmplitudeSet::Iterator end()
TBTK::SourceAmplitudeSet::SourceAmplitudeSet
SourceAmplitudeSet()
TBTK::SourceAmplitudeSet::add
void add(const SourceAmplitude &sourceAmplitude)
Definition: SourceAmplitudeSet.h:238
IndexedDataTree.h
Data structure for storing data associated with an index.
TBTK::SourceAmplitudeSet::begin
SourceAmplitudeSet::Iterator begin()
TBTK::IndexedDataTree::get
bool get(Data &data, const Index &index) const
Definition: IndexedDataTree.h:467
TBTK::Serializable
Definition: Serializable.h:43
SourceAmplitude.h
Source amplitude for equations with a source term.
TBTK::ElementNotFoundException
Definition: ElementNotFoundException.h:10
TBTK::SourceAmplitude
Source amplitude for equations with a source term.
Definition: SourceAmplitude.h:40
TBTK::SourceAmplitudeSet::ConstIterator
Definition: SourceAmplitudeSet.h:178
TBTK::SourceAmplitudeSet::~SourceAmplitudeSet
virtual ~SourceAmplitudeSet()
TBTK::operator!=
bool operator!=(const IndexTree &lhs, const IndexTree &rhs)
Definition: IndexTree.h:391
Serializable.h
Abstract base class for serializable objects.
TBTK::SourceAmplitudeSet::cbegin
SourceAmplitudeSet::ConstIterator cbegin() const
TBTK::SourceAmplitudeSet::cend
SourceAmplitudeSet::ConstIterator cend() const
TBTKMacros.h
Precompiler macros.
TBTK::operator*
const Vector2d operator*(double lhs, const Vector2d &rhs)
Definition: Vector2d.h:129
TBTK::Serializable::Mode
Mode
Definition: Serializable.h:47
TBTK::SourceAmplitude::getIndex
const Index & getIndex() const
Definition: SourceAmplitude.h:146
TBTK::Index
Physical index.
Definition: Index.h:44