TBTK
Need a break? Support the development by playing Polarity Puzzles
ArrayState.h
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 
17 
24 #ifndef COM_DAFER45_TBTK_ARRAY_STATE
25 #define COM_DAFER45_TBTK_ARRAY_STATE
26 
27 #include "TBTK/AbstractState.h"
28 #include "TBTK/DefaultOperator.h"
29 #include "TBTK/TBTKMacros.h"
30 
31 #include <complex>
32 #include <sstream>
33 
34 namespace TBTK{
35 
36 class ArrayState : public AbstractState{
37 public:
39  ArrayState(
40  std::initializer_list<unsigned int> resolution
41  );
42 
44  virtual ~ArrayState();
45 
47  virtual ArrayState* clone() const;
48 
50  virtual std::complex<double> getOverlap(const AbstractState &bra) const;
51 
53  virtual std::complex<double> getMatrixElement(
54  const AbstractState &bra,
55  const AbstractOperator &o = DefaultOperator()
56  ) const;
57 
59  void setAmplitude(
60  std::complex<double> amplitude,
61  std::initializer_list<unsigned int> element
62  );
63 
65  void setAmplitude(
66  std::complex<double> amplitude,
67  const std::vector<unsigned int> &element
68  );
69 
71  void setAmplitude(
72  std::complex<double> amplitude,
73  const Index &element
74  );
75 
77  const std::complex<double>& getAmplitude(
78  std::initializer_list<unsigned int> element
79  ) const;
80 
82  const std::complex<double>& getAmplitude(
83  const std::vector<unsigned int> &element
84  ) const;
85 
87  const std::complex<double>& getAmplitude(
88  const Index &element
89  ) const;
90 protected:
92  const std::vector<unsigned int>& getResolution() const;
93 private:
94  class Storage{
95  public:
97  Storage(std::initializer_list<unsigned int> resolution);
98 
100  ~Storage();
101 
103  void grab();
104 
107  bool release();
108 
110  void setElement(
111  std::complex<double> value,
112  std::initializer_list<unsigned int> element
113  );
114 
116  void setElement(
117  std::complex<double> value,
118  const std::vector<unsigned int> &element
119  );
120 
122  void setElement(
123  std::complex<double> value,
124  const Index &element
125  );
126 
128  const std::complex<double>& getElement(
129  std::initializer_list<unsigned int> element
130  ) const;
131 
133  const std::complex<double>& getElement(
134  const std::vector<unsigned int> &element
135  ) const;
136 
138  const std::complex<double>& getElement(
139  const Index &element
140  ) const;
141 
143  const std::vector<unsigned int>& getResolution() const;
144  private:
146  unsigned int referenceCounter;
147 
149  std::complex<double> *data;
150 
152  std::vector<unsigned int> resolution;
153  };
154 
155  Storage *storage;
156 };
157 
158 inline void ArrayState::Storage::grab(){
159  referenceCounter++;
160 }
161 
162 inline bool ArrayState::Storage::release(){
163  referenceCounter--;
164  if(referenceCounter == 0)
165  return true;
166  else
167  return false;
168 }
169 
170 inline void ArrayState::setAmplitude(
171  std::complex<double> amplitude,
172  std::initializer_list<unsigned int> element
173 ){
174  storage->setElement(amplitude, element);
175 }
176 
177 inline void ArrayState::setAmplitude(
178  std::complex<double> amplitude,
179  const std::vector<unsigned int> &element
180 ){
181  storage->setElement(amplitude, element);
182 }
183 
184 inline void ArrayState::setAmplitude(
185  std::complex<double> amplitude,
186  const Index &element
187 ){
188  storage->setElement(amplitude, element);
189 }
190 
191 inline const std::complex<double>& ArrayState::getAmplitude(
192  std::initializer_list<unsigned int> element
193 ) const{
194  return storage->getElement(element);
195 }
196 
197 inline const std::complex<double>& ArrayState::getAmplitude(
198  const std::vector<unsigned int> &element
199 ) const{
200  return storage->getElement(element);
201 }
202 
203 inline const std::complex<double>& ArrayState::getAmplitude(
204  const Index &element
205 ) const{
206  return storage->getElement(element);
207 }
208 
209 inline const std::vector<unsigned int>& ArrayState::getResolution() const{
210  return storage->getResolution();
211 }
212 
213 inline void ArrayState::Storage::setElement(
214  std::complex<double> value,
215  std::initializer_list<unsigned int> element
216 ){
217  unsigned int x = *(element.begin() + 0);
218  unsigned int y = *(element.begin() + 1);
219  unsigned int z = *(element.begin() + 2);
220  data[resolution[2]*(resolution[1]*x + y) + z] = value;
221 }
222 
223 inline void ArrayState::Storage::setElement(
224  std::complex<double> value,
225  const std::vector<unsigned int> &element
226 ){
227  unsigned int x = element.at(0);
228  unsigned int y = element.at(1);
229  unsigned int z = element.at(2);
230  data[resolution[2]*(resolution[1]*x + y) + z] = value;
231 }
232 
233 inline void ArrayState::Storage::setElement(
234  std::complex<double> value,
235  const Index &element
236 ){
237  unsigned int x = element.at(0);
238  unsigned int y = element.at(1);
239  unsigned int z = element.at(2);
240  data[resolution[2]*(resolution[1]*x + y) + z] = value;
241 }
242 
243 inline const std::complex<double>& ArrayState::Storage::getElement(
244  std::initializer_list<unsigned int> element
245 ) const{
246  unsigned int x = *(element.begin() + 0);
247  unsigned int y = *(element.begin() + 1);
248  unsigned int z = *(element.begin() + 2);
249  return data[resolution[2]*(resolution[1]*x + y) + z];
250 }
251 
252 inline const std::complex<double>& ArrayState::Storage::getElement(
253  const std::vector<unsigned int> &element
254 ) const{
255  unsigned int x = element.at(0);
256  unsigned int y = element.at(1);
257  unsigned int z = element.at(2);
258  return data[resolution[2]*(resolution[1]*x + y) + z];
259 }
260 
261 inline const std::complex<double>& ArrayState::Storage::getElement(
262  const Index &element
263 ) const{
264  unsigned int x = element.at(0);
265  unsigned int y = element.at(1);
266  unsigned int z = element.at(2);
267  return data[resolution[2]*(resolution[1]*x + y) + z];
268 }
269 
270 inline const std::vector<unsigned int>& ArrayState::Storage::getResolution() const{
271  return resolution;
272 }
273 
274 }; //End of namespace TBTK
275 
276 #endif
277 
Precompiler macros.
Definition: Boolean.h:32