TBTK
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/SourceAmplitude.h"
27 #include "TBTK/IndexedDataTree.h"
28 #include "TBTK/Serializable.h"
29 #include "TBTK/TBTKMacros.h"
30 
31 #include <vector>
32 
33 namespace TBTK{
34 
40 public:
43 
51  SourceAmplitudeSet(const std::string &serializeation, Mode mode);
52 
54  virtual ~SourceAmplitudeSet();
55 
59  void add(const SourceAmplitude &sourceAmplitude);
60 
69  std::vector<SourceAmplitude>& get(
70  const Index &index
71  );
72 
81  const std::vector<SourceAmplitude>& get(
82  const Index &index
83  ) const;
84 
85  class Iterator;
86  class ConstIterator;
87 private:
90  template<bool isConstIterator>
91  class _Iterator{
92  public:
95  typedef typename std::conditional<
96  isConstIterator,
97  const SourceAmplitude&,
98  SourceAmplitude&
99  >::type SourceAmplitudeReferenceType;
100 
102  void operator++();
103 
105  SourceAmplitudeReferenceType operator*();
106 
108  bool operator==(const _Iterator &rhs);
109 
111  bool operator!=(const _Iterator &rhs);
112  private:
115  typedef typename std::conditional<
116  isConstIterator,
118  std::vector<SourceAmplitude>
121  std::vector<SourceAmplitude>
122  >::Iterator
123  >::type IteratorType;
124 
126  unsigned int currentSourceAmplitude;
127 
129  IteratorType iterator;
130 
132  IteratorType iteratorEnd;
133 
136  friend class Iterator;
137  friend class ConstIterator;
138 
141  typedef typename std::conditional<
142  isConstIterator,
143  const IndexedDataTree<
144  std::vector<SourceAmplitude>
145  >,
147  std::vector<SourceAmplitude>
148  >
149  >::type SourceAmplitudeTreeType;
150 
153  _Iterator(
154  SourceAmplitudeTreeType &sourceAmplitudeTree,
155  bool end = false
156  );
157  };
158 public:
161  class Iterator : public _Iterator<false>{
162  private:
163  Iterator(
165  std::vector<SourceAmplitude>
166  > &sourceAmplitudeTree,
167  bool end = false
168  ) : _Iterator(sourceAmplitudeTree, end){};
169 
172  friend class SourceAmplitudeSet;
173  };
174 
177  class ConstIterator : public _Iterator<true>{
178  private:
180  const IndexedDataTree<
181  std::vector<SourceAmplitude>
182  > &sourceAmplitudeTree,
183  bool end = false
184  ) : _Iterator(sourceAmplitudeTree, end){};
185 
188  friend class SourceAmplitudeSet;
189  };
190 
196 
202 
208 
214 
220 
226 
228  virtual std::string serialize(Mode mode) const;
229 
231  unsigned int getSizeInBytes() const;
232 private:
235 };
236 
238  const SourceAmplitude &sourceAmplitude
239 ){
240  try{
241  std::vector<SourceAmplitude> &sourceAmplitudes
242  = sourceAmplitudeTree.get(sourceAmplitude.getIndex());
243  sourceAmplitudes.push_back(sourceAmplitude);
244  }
245  catch(ElementNotFoundException &e){
246  sourceAmplitudeTree.add(
247  std::vector<SourceAmplitude>(),
248  sourceAmplitude.getIndex()
249  );
250  std::vector<SourceAmplitude> &sourceAmplitudes
251  = sourceAmplitudeTree.get(sourceAmplitude.getIndex());
252  sourceAmplitudes.push_back(sourceAmplitude);
253  }
254 }
255 
256 inline std::vector<SourceAmplitude>& SourceAmplitudeSet::get(
257  const Index &index
258 ){
259  return sourceAmplitudeTree.get(index);
260 }
261 
262 inline const std::vector<SourceAmplitude>& SourceAmplitudeSet::get(
263  const Index &index
264 ) const{
265  return sourceAmplitudeTree.get(index);
266 }
267 
268 inline unsigned int SourceAmplitudeSet::getSizeInBytes() const{
269  return sizeof(this) - sizeof(sourceAmplitudeTree)
270  + sourceAmplitudeTree.getSizeInBytes();
271 }
272 
273 template<bool isConstIterator>
274 SourceAmplitudeSet::_Iterator<isConstIterator>::_Iterator(
275  SourceAmplitudeTreeType &sourceAmplitudeTree,
276  bool end
277 ) :
278  currentSourceAmplitude(0),
279  iterator(
280  end ? sourceAmplitudeTree.end() : sourceAmplitudeTree.begin()
281  ),
282  iteratorEnd(sourceAmplitudeTree.end())
283 {
284 }
285 
286 template<bool isConstIterator>
287 void SourceAmplitudeSet::_Iterator<isConstIterator>::operator++(){
288  if(iterator != iteratorEnd){
289  const std::vector<SourceAmplitude> &sourceAmplitudes = *iterator;
290  if(currentSourceAmplitude+1 == sourceAmplitudes.size()){
291  currentSourceAmplitude = 0;
292  ++iterator;
293  }
294  else{
295  currentSourceAmplitude++;
296  }
297  }
298 }
299 
300 template<bool isConstIterator>
301 typename SourceAmplitudeSet::_Iterator<isConstIterator>::SourceAmplitudeReferenceType
302 SourceAmplitudeSet::_Iterator<isConstIterator>::operator*(){
303  return (*iterator)[currentSourceAmplitude];
304 }
305 
306 template<bool isConstIterator>
307 bool SourceAmplitudeSet::_Iterator<isConstIterator>::operator==(
308  const _Iterator<isConstIterator> &rhs
309 ){
310  if(
311  iterator == rhs.iterator
312  && currentSourceAmplitude == rhs.currentSourceAmplitude
313  ){
314  return true;
315  }
316  else{
317  return false;
318  }
319 }
320 
321 template<bool isConstIterator>
322 bool SourceAmplitudeSet::_Iterator<isConstIterator>::operator!=(
323  const _Iterator<isConstIterator> &rhs
324 ){
325  if(
326  iterator != rhs.iterator
327  || currentSourceAmplitude != rhs.currentSourceAmplitude
328  ){
329  return true;
330  }
331  else{
332  return false;
333  }
334 }
335 
336 }; //End of namespace TBTK
337 
338 #endif
unsigned int getSizeInBytes() const
Definition: IndexedDataTree.h:2149
unsigned int getSizeInBytes() const
Definition: SourceAmplitudeSet.h:268
FockStateRuleSet operator*(const LadderOperator< BitRegister > &ladderOperator, const FockStateRuleSet &fockStateRuleSet)
Definition: FockStateRuleSet.h:103
Definition: SourceAmplitudeSet.h:177
Precompiler macros.
std::vector< SourceAmplitude > & get(const Index &index)
Definition: SourceAmplitudeSet.h:256
Definition: Serializable.h:40
virtual std::string serialize(Mode mode) const
Source amplitude for equations with a source term.
Definition: SourceAmplitude.h:40
SourceAmplitudeSet::Iterator end()
Data structure for storing data associated with an index.
Definition: ElementNotFoundException.h:10
SourceAmplitudeSet::Iterator begin()
Definition: IndexedDataTree.h:43
Definition: SourceAmplitudeSet.h:161
void add(const SourceAmplitude &sourceAmplitude)
Definition: SourceAmplitudeSet.h:237
Source amplitude for equations with a source term.
SourceAmplitude container.
Definition: SourceAmplitudeSet.h:39
const Index & getIndex() const
Definition: SourceAmplitude.h:129
SourceAmplitudeSet::ConstIterator cbegin() const
Flexible physical index.
Definition: Index.h:69
Definition: ModelFactory.h:35
void add(const Data &data, const Index &index)
Definition: IndexedDataTree.h:1227
Mode
Definition: Serializable.h:44
bool get(Data &data, const Index &index) const
Definition: IndexedDataTree.h:1587
Abstract base class for serializable objects.
SourceAmplitudeSet::ConstIterator cend() const