TBTK
Need a break? Support the development by playing Polarity Puzzles
BasisStateSet.h
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 
17 
24 #ifndef COM_DAFER45_TBTK_BASIS_STATE_SET
25 #define COM_DAFER45_TBTK_BASIS_STATE_SET
26 
27 #include "TBTK/AbstractState.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 
39 class BasisStateSet : public Serializable{
40 public:
42  BasisStateSet();
43 
51  BasisStateSet(const std::string &serializeation, Mode mode);
52 
54  virtual ~BasisStateSet();
55 
59  void add(const AbstractState &state);
60 
66  AbstractState& get(const Index &index);
67 
73  const AbstractState& get(const Index &index) const;
74 
75  class Iterator;
76  class ConstIterator;
77 private:
80  template<bool isConstIterator>
81  class _Iterator{
82  public:
85  typedef typename std::conditional<
86  isConstIterator,
87  const AbstractState&,
88  AbstractState&
89  >::type BasisStateReferenceType;
90 
92  void operator++();
93 
95  BasisStateReferenceType operator*();
96 
98  bool operator==(const _Iterator &rhs);
99 
101  bool operator!=(const _Iterator &rhs);
102  private:
105  typedef typename std::conditional<
106  isConstIterator,
107  IndexedDataTree<AbstractState*>::ConstIterator,
108  IndexedDataTree<AbstractState*>::Iterator
109  >::type IteratorType;
110 
112  IteratorType iterator;
113 
115  IteratorType iteratorEnd;
116 
119  friend class Iterator;
120  friend class ConstIterator;
121 
124  typedef typename std::conditional<
125  isConstIterator,
126  const IndexedDataTree<AbstractState*>,
127  IndexedDataTree<AbstractState*>
128  >::type BasisStateTreeType;
129 
132  _Iterator(
133  BasisStateTreeType &basisStateTree,
134  bool end = false
135  );
136  };
137 public:
139  class Iterator : public _Iterator<false>{
140  private:
141  Iterator(
142  IndexedDataTree<AbstractState*> &basisStateTree,
143  bool end = false
144  ) : _Iterator(basisStateTree, end){};
145 
147  friend class BasisStateSet;
148  };
149 
151  class ConstIterator : public _Iterator<true>{
152  private:
153  ConstIterator(
154  const IndexedDataTree<
155  AbstractState*
156  > &basisStateTree,
157  bool end = false
158  ) : _Iterator(basisStateTree, end){};
159 
161  friend class BasisStateSet;
162  };
163 
168  BasisStateSet::Iterator begin();
169 
174  BasisStateSet::ConstIterator begin() const;
175 
180  BasisStateSet::ConstIterator cbegin() const;
181 
185  BasisStateSet::Iterator end();
186 
190  BasisStateSet::ConstIterator end() const;
191 
195  BasisStateSet::ConstIterator cend() const;
196 
198  virtual std::string serialize(Mode mode) const;
199 
201  unsigned int getSizeInBytes() const;
202 private:
204  IndexedDataTree<AbstractState*> basisStateTree;
205 };
206 
207 inline void BasisStateSet::add(const AbstractState &state){
208  basisStateTree.add(
209  state.clone(),
210  state.getIndex()
211  );
212 }
213 
214 inline AbstractState& BasisStateSet::get(const Index &index){
215  return *basisStateTree.get(index);
216 }
217 
218 inline const AbstractState& BasisStateSet::get(const Index &index) const{
219  return *basisStateTree.get(index);
220 }
221 
222 inline unsigned int BasisStateSet::getSizeInBytes() const{
223  TBTKNotYetImplemented("BasisStateSet::getSizeInBytes()");
224 }
225 
226 template<bool isConstIterator>
227 BasisStateSet::_Iterator<isConstIterator>::_Iterator(
228  BasisStateTreeType &basisStateTree,
229  bool end
230 ) :
231  iterator(
232  end ? basisStateTree.end() : basisStateTree.begin()
233  ),
234  iteratorEnd(basisStateTree.end())
235 {
236 }
237 
238 template<bool isConstIterator>
239 void BasisStateSet::_Iterator<isConstIterator>::operator++(){
240  if(iterator != iteratorEnd)
241  ++iterator;
242 }
243 
244 template<bool isConstIterator>
245 typename BasisStateSet::_Iterator<isConstIterator>::BasisStateReferenceType
246 BasisStateSet::_Iterator<isConstIterator>::operator*(){
247  return *(*iterator);
248 }
249 
250 template<bool isConstIterator>
251 bool BasisStateSet::_Iterator<isConstIterator>::operator==(
252  const _Iterator<isConstIterator> &rhs
253 ){
254  if(iterator == rhs.iterator)
255  return true;
256  else
257  return false;
258 }
259 
260 template<bool isConstIterator>
261 bool BasisStateSet::_Iterator<isConstIterator>::operator!=(
262  const _Iterator<isConstIterator> &rhs
263 ){
264  if(iterator != rhs.iterator)
265  return true;
266  else
267  return false;
268 }
269 
270 }; //End of namespace TBTK
271 
272 #endif
273 
Precompiler macros.
Data structure for storing data associated with an index.
Definition: Boolean.h:32
const Vector2d operator*(double lhs, const Vector2d &rhs)
Definition: Vector2d.h:129
Abstract base class for serializable objects.
bool operator!=(const IndexTree &lhs, const IndexTree &rhs)
Definition: IndexTree.h:391