TBTK
Need a break? Support the development by playing Polarity Puzzles
OverlapAmplitudeSet.h
Go to the documentation of this file.
1 /* Copyright 2019 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_OVERLAP_AMPLITUDE_SET
24 #define COM_DAFER45_TBTK_OVERLAP_AMPLITUDE_SET
25 
26 #include "TBTK/OverlapAmplitude.h"
27 #include "TBTK/IndexedDataTree.h"
28 #include "TBTK/Serializable.h"
29 #include "TBTK/TBTKMacros.h"
30 
31 namespace TBTK{
32 
38 public:
41 
49  OverlapAmplitudeSet(const std::string &serializeation, Mode mode);
50 
52  virtual ~OverlapAmplitudeSet();
53 
57  void add(const OverlapAmplitude &overlapAmplitude);
58 
65  OverlapAmplitude& get(
66  const Index &braIndex,
67  const Index &ketIndex
68  );
69 
76  const OverlapAmplitude& get(
77  const Index &braIndex,
78  const Index &ketIndex
79  ) const;
80 
86  bool getAssumeOrthonormalBasis() const;
87 
88  class Iterator;
89  class ConstIterator;
90 private:
93  template<bool isConstIterator>
94  class _Iterator{
95  public:
98  typedef typename std::conditional<
99  isConstIterator,
100  const OverlapAmplitude&,
101  OverlapAmplitude&
102  >::type OverlapAmplitudeReferenceType;
103 
105  void operator++();
106 
108  OverlapAmplitudeReferenceType operator*();
109 
111  bool operator==(const _Iterator &rhs);
112 
114  bool operator!=(const _Iterator &rhs);
115  private:
118  typedef typename std::conditional<
119  isConstIterator,
122  >::type IteratorType;
123 
125  IteratorType iterator;
126 
128  IteratorType iteratorEnd;
129 
132  friend class Iterator;
133  friend class ConstIterator;
134 
137  typedef typename std::conditional<
138  isConstIterator,
140  IndexedDataTree<OverlapAmplitude>
141  >::type OverlapAmplitudeTreeType;
142 
145  _Iterator(
146  OverlapAmplitudeTreeType &overlapAmplitudeTree,
147  bool end = false
148  );
149  };
150 public:
153  class Iterator : public _Iterator<false>{
154  private:
155  Iterator(
158  > &overlapAmplitudeTree,
159  bool end = false
160  ) : _Iterator(overlapAmplitudeTree, end){};
161 
164  friend class OverlapAmplitudeSet;
165  };
166 
169  class ConstIterator : public _Iterator<true>{
170  private:
172  const IndexedDataTree<
174  > &overlapAmplitudeTree,
175  bool end = false
176  ) : _Iterator(overlapAmplitudeTree, end){};
177 
180  friend class OverlapAmplitudeSet;
181  };
182 
188 
194 
200 
206 
212 
218 
220  virtual std::string serialize(Mode mode) const;
221 
223  unsigned int getSizeInBytes() const;
224 private:
226  IndexedDataTree<OverlapAmplitude> overlapAmplitudeTree;
227 
231  bool assumeOrthonormalBasis;
232 };
233 
235  const OverlapAmplitude &overlapAmplitude
236 ){
237  overlapAmplitudeTree.add(
238  overlapAmplitude,
239  {
240  overlapAmplitude.getBraIndex(),
241  overlapAmplitude.getKetIndex()
242  }
243  );
244 
245  assumeOrthonormalBasis = false;
246 }
247 
249  const Index &braIndex,
250  const Index &ketIndex
251 ){
252  return overlapAmplitudeTree.get({braIndex, ketIndex});
253 }
254 
256  const Index &braIndex,
257  const Index &ketIndex
258 ) const{
259  return overlapAmplitudeTree.get({braIndex, ketIndex});
260 }
261 
263  return assumeOrthonormalBasis;
264 }
265 
266 inline unsigned int OverlapAmplitudeSet::getSizeInBytes() const{
267  return sizeof(this) - sizeof(overlapAmplitudeTree)
268  + overlapAmplitudeTree.getSizeInBytes();
269 }
270 
271 template<bool isConstIterator>
272 OverlapAmplitudeSet::_Iterator<isConstIterator>::_Iterator(
273  OverlapAmplitudeTreeType &overlapAmplitudeTree,
274  bool end
275 ) :
276  iterator(
277  end ? overlapAmplitudeTree.end() : overlapAmplitudeTree.begin()
278  ),
279  iteratorEnd(overlapAmplitudeTree.end())
280 {
281 }
282 
283 template<bool isConstIterator>
284 void OverlapAmplitudeSet::_Iterator<isConstIterator>::operator++(){
285  if(iterator != iteratorEnd)
286  ++iterator;
287 }
288 
289 template<bool isConstIterator>
290 typename OverlapAmplitudeSet::_Iterator<
291  isConstIterator
292 >::OverlapAmplitudeReferenceType
293 OverlapAmplitudeSet::_Iterator<isConstIterator>::operator*(){
294  return *iterator;
295 }
296 
297 template<bool isConstIterator>
298 bool OverlapAmplitudeSet::_Iterator<isConstIterator>::operator==(
299  const _Iterator<isConstIterator> &rhs
300 ){
301  if(iterator == rhs.iterator)
302  return true;
303  else
304  return false;
305 }
306 
307 template<bool isConstIterator>
308 bool OverlapAmplitudeSet::_Iterator<isConstIterator>::operator!=(
309  const _Iterator<isConstIterator> &rhs
310 ){
311  if(iterator != rhs.iterator)
312  return true;
313  else
314  return false;
315 }
316 
317 }; //End of namespace TBTK
318 
319 #endif
unsigned int getSizeInBytes() const
Definition: IndexedDataTree.h:750
Iterator end()
Definition: IndexedDataTree.h:827
Definition: OverlapAmplitudeSet.h:169
OverlapAmplitude container.
Definition: OverlapAmplitudeSet.h:37
OverlapAmplitude & get(const Index &braIndex, const Index &ketIndex)
Definition: OverlapAmplitudeSet.h:248
Precompiler macros.
void add(const Data &data, const Index &index)
Definition: IndexedDataTree.h:355
Definition: Serializable.h:43
Definition: IndexedDataTree.h:183
Data structure for storing data associated with an index.
bool get(Data &data, const Index &index) const
Definition: IndexedDataTree.h:467
const Index & getKetIndex() const
Definition: OverlapAmplitude.h:190
OverlapAmplitudeSet::ConstIterator cbegin() const
const Index & getBraIndex() const
Definition: OverlapAmplitude.h:186
bool getAssumeOrthonormalBasis() const
Definition: OverlapAmplitudeSet.h:262
unsigned int getSizeInBytes() const
Definition: OverlapAmplitudeSet.h:266
void add(const OverlapAmplitude &overlapAmplitude)
Definition: OverlapAmplitudeSet.h:234
OverlapAmplitudeSet::ConstIterator cend() const
Definition: OverlapAmplitudeSet.h:153
Definition: IndexedDataTree.h:45
Overlap amplitude between state &#39;bra&#39; and &#39;ket&#39;.
Definition: OverlapAmplitude.h:37
Overlap amplitude between state &#39;bra&#39; and &#39;ket&#39;.
Physical index.
Definition: Index.h:44
Definition: IndexedDataTree.h:170
Definition: Boolean.h:32
virtual std::string serialize(Mode mode) const
const Vector2d operator*(double lhs, const Vector2d &rhs)
Definition: Vector2d.h:129
Mode
Definition: Serializable.h:47
OverlapAmplitudeSet::Iterator end()
Abstract base class for serializable objects.
OverlapAmplitudeSet::Iterator begin()
bool operator!=(const IndexTree &lhs, const IndexTree &rhs)
Definition: IndexTree.h:391
Iterator begin()
Definition: IndexedDataTree.h:808