TBTK
ArrayState.h
Go to the documentation of this file.
1 /* Copyright 2017 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_ARRAY_STATE
24 #define COM_DAFER45_TBTK_ARRAY_STATE
25 
26 #include "TBTK/AbstractState.h"
27 #include "TBTK/DefaultOperator.h"
28 #include "TBTK/TBTKMacros.h"
29 
30 #include <complex>
31 #include <sstream>
32 
33 namespace TBTK{
34 
35 class ArrayState : public AbstractState{
36 public:
38  ArrayState(
39  std::initializer_list<unsigned int> resolution
40  );
41 
43  virtual ~ArrayState();
44 
46  virtual ArrayState* clone() const;
47 
49  virtual std::complex<double> getOverlap(const AbstractState &bra) const;
50 
52  virtual std::complex<double> getMatrixElement(
53  const AbstractState &bra,
55  ) const;
56 
58  void setAmplitude(
59  std::complex<double> amplitude,
60  std::initializer_list<unsigned int> element
61  );
62 
64  void setAmplitude(
65  std::complex<double> amplitude,
66  const std::vector<unsigned int> &element
67  );
68 
70  void setAmplitude(
71  std::complex<double> amplitude,
72  const Index &element
73  );
74 
76  const std::complex<double>& getAmplitude(
77  std::initializer_list<unsigned int> element
78  ) const;
79 
81  const std::complex<double>& getAmplitude(
82  const std::vector<unsigned int> &element
83  ) const;
84 
86  const std::complex<double>& getAmplitude(
87  const Index &element
88  ) const;
89 protected:
91  const std::vector<unsigned int>& getResolution() const;
92 private:
93  class Storage{
94  public:
96  Storage(std::initializer_list<unsigned int> resolution);
97 
99  ~Storage();
100 
102  void grab();
103 
106  bool release();
107 
109  void setElement(
110  std::complex<double> value,
111  std::initializer_list<unsigned int> element
112  );
113 
115  void setElement(
116  std::complex<double> value,
117  const std::vector<unsigned int> &element
118  );
119 
121  void setElement(
122  std::complex<double> value,
123  const Index &element
124  );
125 
127  const std::complex<double>& getElement(
128  std::initializer_list<unsigned int> element
129  ) const;
130 
132  const std::complex<double>& getElement(
133  const std::vector<unsigned int> &element
134  ) const;
135 
137  const std::complex<double>& getElement(
138  const Index &element
139  ) const;
140 
142  const std::vector<unsigned int>& getResolution() const;
143  private:
145  unsigned int referenceCounter;
146 
148  std::complex<double> *data;
149 
151  std::vector<unsigned int> resolution;
152  };
153 
154  Storage *storage;
155 };
156 
157 inline void ArrayState::Storage::grab(){
158  referenceCounter++;
159 }
160 
161 inline bool ArrayState::Storage::release(){
162  referenceCounter--;
163  if(referenceCounter == 0)
164  return true;
165  else
166  return false;
167 }
168 
170  std::complex<double> amplitude,
171  std::initializer_list<unsigned int> element
172 ){
173  storage->setElement(amplitude, element);
174 }
175 
177  std::complex<double> amplitude,
178  const std::vector<unsigned int> &element
179 ){
180  storage->setElement(amplitude, element);
181 }
182 
184  std::complex<double> amplitude,
185  const Index &element
186 ){
187  storage->setElement(amplitude, element);
188 }
189 
190 inline const std::complex<double>& ArrayState::getAmplitude(
191  std::initializer_list<unsigned int> element
192 ) const{
193  return storage->getElement(element);
194 }
195 
196 inline const std::complex<double>& ArrayState::getAmplitude(
197  const std::vector<unsigned int> &element
198 ) const{
199  return storage->getElement(element);
200 }
201 
202 inline const std::complex<double>& ArrayState::getAmplitude(
203  const Index &element
204 ) const{
205  return storage->getElement(element);
206 }
207 
208 inline const std::vector<unsigned int>& ArrayState::getResolution() const{
209  return storage->getResolution();
210 }
211 
212 inline void ArrayState::Storage::setElement(
213  std::complex<double> value,
214  std::initializer_list<unsigned int> element
215 ){
216  unsigned int x = *(element.begin() + 0);
217  unsigned int y = *(element.begin() + 1);
218  unsigned int z = *(element.begin() + 2);
219  data[resolution[2]*(resolution[1]*x + y) + z] = value;
220 }
221 
222 inline void ArrayState::Storage::setElement(
223  std::complex<double> value,
224  const std::vector<unsigned int> &element
225 ){
226  unsigned int x = element.at(0);
227  unsigned int y = element.at(1);
228  unsigned int z = element.at(2);
229  data[resolution[2]*(resolution[1]*x + y) + z] = value;
230 }
231 
232 inline void ArrayState::Storage::setElement(
233  std::complex<double> value,
234  const Index &element
235 ){
236  unsigned int x = element.at(0);
237  unsigned int y = element.at(1);
238  unsigned int z = element.at(2);
239  data[resolution[2]*(resolution[1]*x + y) + z] = value;
240 }
241 
242 inline const std::complex<double>& ArrayState::Storage::getElement(
243  std::initializer_list<unsigned int> element
244 ) const{
245  unsigned int x = *(element.begin() + 0);
246  unsigned int y = *(element.begin() + 1);
247  unsigned int z = *(element.begin() + 2);
248  return data[resolution[2]*(resolution[1]*x + y) + z];
249 }
250 
251 inline const std::complex<double>& ArrayState::Storage::getElement(
252  const std::vector<unsigned int> &element
253 ) const{
254  unsigned int x = element.at(0);
255  unsigned int y = element.at(1);
256  unsigned int z = element.at(2);
257  return data[resolution[2]*(resolution[1]*x + y) + z];
258 }
259 
260 inline const std::complex<double>& ArrayState::Storage::getElement(
261  const Index &element
262 ) const{
263  unsigned int x = element.at(0);
264  unsigned int y = element.at(1);
265  unsigned int z = element.at(2);
266  return data[resolution[2]*(resolution[1]*x + y) + z];
267 }
268 
269 inline const std::vector<unsigned int>& ArrayState::Storage::getResolution() const{
270  return resolution;
271 }
272 
273 }; //End of namespace TBTK
274 
275 #endif
void setAmplitude(std::complex< double > amplitude, std::initializer_list< unsigned int > element)
Definition: ArrayState.h:169
virtual std::complex< double > getMatrixElement(const AbstractState &bra, const AbstractOperator &o=DefaultOperator()) const
Abstract state class from which other states inherit.
Precompiler macros.
Definition: AbstractOperator.h:30
virtual ~ArrayState()
Definition: AbstractState.h:37
Flexible physical index.
Definition: Index.h:70
Definition: ModelFactory.h:35
Definition: ArrayState.h:35
ArrayState(std::initializer_list< unsigned int > resolution)
const std::complex< double > & getAmplitude(std::initializer_list< unsigned int > element) const
Definition: ArrayState.h:190
Default (dummy) operator class for indicating default behavior.
const std::vector< unsigned int > & getResolution() const
Definition: ArrayState.h:208
Definition: DefaultOperator.h:30
int & at(unsigned int n)
Definition: Index.h:334
virtual ArrayState * clone() const
virtual std::complex< double > getOverlap(const AbstractState &bra) const