TBTK
Index.h
Go to the documentation of this file.
1 /* Copyright 2016 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_INDEX
24 #define COM_DAFER45_TBTK_INDEX
25 
26 #include "TBTK/Serializable.h"
27 #include "TBTK/Streams.h"
28 
29 #include <vector>
30 
31 namespace TBTK{
32 
52 enum {
53  _ = -1, //Shorthand notation for IDX_ALL. Always use IDX_ALL in
54  ___ = -1, //library code.
55  IDX_ALL = -1,
56  IDX_SUM_ALL = -2,
57  IDX_X = -3,
58  IDX_Y = -4,
59  IDX_Z = -5,
60  IDX_SPIN = -6,
61  IDX_SEPARATOR = -7
62 };
63 
70 class Index{
71 public:
73  Index(){};
74 
78  Index(std::initializer_list<int> i) : indices(i){};
79 
83  Index(std::vector<int> i) : indices(i){};
84 
88  Index(const Index &index) : indices(index.indices){};
89 
95  Index(const Index &head, const Index &tail);
96 
101  Index(std::initializer_list<std::initializer_list<int>> indexList);
102 
107  Index(const std::vector<std::vector<int>> &indexList);
108 
113  Index(std::initializer_list<Index> indexList);
114 
119  Index(const std::string &indexString);
120 
127  Index(const std::string &serialization, Serializable::Mode mode);
128 
136  bool equals(const Index &index, bool allowWildcard = false) const;
137 
143  int& at(unsigned int n);
144 
150  const int& at(unsigned int n) const;
151 
158  unsigned int getSize() const;
159 
163  void reserve(unsigned int size);
164 
168  void push_back(int subindex);
169 
173  int popFront();
174 
178  int popBack();
179 
185 
194  Index getSubIndex(int first, int last);
195 
200  bool isPatternIndex() const;
201 
203  void print() const;
204 
208  std::string toString() const;
209 
215  friend bool operator<(const Index &i1, const Index &i2);
216 
222  friend bool operator>(const Index &i1, const Index &i2);
223 
229  int& operator[](unsigned int subindex);
230 
236  const int& operator[](unsigned int subindex) const;
237 
244  std::string serialize(Serializable::Mode mode) const;
245 
249  unsigned int getSizeInBytes() const;
250 private:
252  std::vector<int> indices;
253 };
254 
255 inline void Index::print() const{
256  Streams::out << "{";
257  for(unsigned int n = 0; n < indices.size(); n++){
258  if(n != 0)
259  Streams::out << ", ";
260  Streams::out << indices.at(n);
261  }
262  Streams::out << "}\n";
263 }
264 
265 inline std::string Index::toString() const{
266  std::string str = "{";
267  bool isFirstIndex = true;
268  for(unsigned int n = 0; n < indices.size(); n++){
269 /* if(n != 0)
270  str += ", ";*/
271  int subindex = indices.at(n);
272  if(!isFirstIndex && subindex != IDX_SEPARATOR)
273  str += ", ";
274  else
275  isFirstIndex = false;
276  switch(subindex){
277  case IDX_ALL:
278  str += "IDX_ALL";
279  break;
280  case IDX_SUM_ALL:
281  str += "IDX_SUM_ALL";
282  break;
283  case IDX_X:
284  str += "IDX_X";
285  break;
286  case IDX_Y:
287  str += "IDX_Y";
288  break;
289  case IDX_Z:
290  str += "IDX_Z";
291  break;
292  case IDX_SPIN:
293  str += "IDX_SPIN";
294  break;
295  case IDX_SEPARATOR:
296  str += "}, {";
297  isFirstIndex = true;
298  break;
299  default:
300  str += std::to_string(subindex);
301  break;
302  }
303  }
304  str += "}";
305 
306  return str;
307 }
308 
309 inline bool Index::equals(const Index &index, bool allowWildcard) const{
310  if(indices.size() == index.indices.size()){
311  for(unsigned int n = 0; n < indices.size(); n++){
312  if(indices.at(n) != index.indices.at(n)){
313  if(!allowWildcard)
314  return false;
315  else{
316  if(
317  indices.at(n) == IDX_ALL ||
318  index.indices.at(n) == IDX_ALL
319  )
320  continue;
321  else
322  return false;
323  }
324  }
325  }
326  }
327  else{
328  return false;
329  }
330 
331  return true;
332 }
333 
334 inline int& Index::at(unsigned int n){
335  return indices.at(n);
336 }
337 
338 inline const int& Index::at(unsigned int n) const{
339  return indices.at(n);
340 }
341 
342 inline unsigned int Index::getSize() const{
343  return indices.size();
344 }
345 
346 inline void Index::reserve(unsigned int size){
347  indices.reserve(size);
348 }
349 
350 inline void Index::push_back(int subindex){
351  indices.push_back(subindex);
352 }
353 
354 inline int Index::popFront(){
355  int first = indices.at(0);
356  indices.erase(indices.begin());
357 
358  return first;
359 }
360 
361 inline int Index::popBack(){
362  int last = indices.back();
363  indices.pop_back();
364 
365  return last;
366 }
367 
368 inline bool Index::isPatternIndex() const{
369  for(unsigned int n = 0; n < indices.size(); n++)
370  if(indices.at(n) < 0)
371  return true;
372 
373  return false;
374 }
375 
376 inline int& Index::operator[](unsigned int subindex){
377  return indices[subindex];
378 }
379 
380 inline const int& Index::operator[](unsigned int subindex) const{
381  return indices[subindex];
382 }
383 
384 inline unsigned int Index::getSizeInBytes() const{
385  return sizeof(*this) + sizeof(int)*indices.capacity();
386 }
387 
388 }; //End of namespace TBTK
389 
390 #endif
void reserve(unsigned int size)
Definition: Index.h:346
void print() const
Definition: Index.h:255
bool equals(const Index &index, bool allowWildcard=false) const
Definition: Index.h:309
Index getUnitRange()
unsigned int getSizeInBytes() const
Definition: Index.h:384
friend bool operator<(const Index &i1, const Index &i2)
Index()
Definition: Index.h:73
Index(std::vector< int > i)
Definition: Index.h:83
int & operator[](unsigned int subindex)
Definition: Index.h:376
Index(const Index &index)
Definition: Index.h:88
int popFront()
Definition: Index.h:354
std::string serialize(Serializable::Mode mode) const
static std::ostream out
Definition: Streams.h:36
friend bool operator>(const Index &i1, const Index &i2)
void push_back(int subindex)
Definition: Index.h:350
Flexible physical index.
Definition: Index.h:70
Definition: ModelFactory.h:35
unsigned int getSize() const
Definition: Index.h:342
Mode
Definition: Serializable.h:44
Index getSubIndex(int first, int last)
Index(std::initializer_list< int > i)
Definition: Index.h:78
Abstract base class for serializable objects.
int & at(unsigned int n)
Definition: Index.h:334
Streams for TBTK output.
std::string toString() const
Definition: Index.h:265
bool isPatternIndex() const
Definition: Index.h:368
int popBack()
Definition: Index.h:361