TBTK
Need a break? Support the development by playing Polarity Puzzles
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/Subindex.h"
27 #include "TBTK/Serializable.h"
28 #include "TBTK/Streams.h"
29 
30 #include <vector>
31 
32 namespace TBTK{
33 
44 class Index{
45 public:
46  //TBTKFeature Core.Index.Construction.1 2019-09-19
48  Index(){};
49 
50  //TBTKFeature Core.Index.Construction.2 2019-09-19
54  Index(std::initializer_list<Subindex> i) : indices(i){};
55 
59 /* Index(
60  std::initializer_list<unsigned int> i
61  ) : indices(i.begin(), i.end()){};*/
62 
63  //TBTKFeature Core.Index.Construction.3.C++ 2019-09-19
67  Index(std::vector<Subindex> i) : indices(i){};
68 
72 // Index(std::vector<unsigned int> i) : indices(i.begin(), i.end()){};
73 
74  //TBTKFeature Core.Index.Copy.1 2019-09-19
75  //TBTKFeature Core.Index.Copy.2 2019-09-23
79  Index(const Index &index) : indices(index.indices){};
80 
81  //TBTKFeature Core.Index.Construction.4 2019-09-19
82  //TBTKFeature Core.Index.Construction.5.C++ 2019-09-19
88  Index(const Index &head, const Index &tail);
89 
94 // Index(std::initializer_list<std::initializer_list<Subindex>> indexList);
95 
96  //TBTKFeature Core.Index.Construction.8.C++ 2019-09-19
101  Index(const std::vector<std::vector<Subindex>> &indexList);
102 
103  //TBTKFeature Core.Index.Construction.6 2019-09-19
104  //TBTKFeature Core.Index.Construction.7.C++ 2019-09-19
109  Index(std::initializer_list<Index> indexList);
110 
115  Index(std::vector<Index> indexList);
116 
117  //TBTKFeature Core.Index.Construction.9 2019-09-19
122  Index(const std::string &indexString);
123 
124  //TBTKFeature Core.Index.Serialization.1 2019-09-19
131  Index(const std::string &serialization, Serializable::Mode mode);
132 
133  //TBTKFeature Core.Index.Equals.1 2019-09-19
134  //TBTKFeature Core.Index.Equals.2 2019-09-19
135  //TBTKFeature Core.Index.Equals.3 2019-09-19
136  //TBTKFeature Core.Index.Equals.4 2019-09-21
137  //TBTKFeature Core.Index.Equals.5 2019-09-19
138  //TBTKFeature Core.Index.Equals.6 2019-09-19
139  //TBTKFeature Core.Index.Equals.7 2019-09-19
140  //TBTKFeature Core.Index.Equals.8 2019-09-19
141  //TBTKFeature Core.Index.Equals.9 2019-09-19
142  //TBTKFeature Core.Index.Equals.10 2019-09-19
150  bool equals(const Index &index, bool allowWildcard = false) const;
151 
152  //TBTKFeature Core.Index.at.1 2019-09-19
158  Subindex& at(unsigned int n);
159 
160  //TBTKFeature Core.Index.at.2.C++ 2019-09-19
166  const Subindex& at(unsigned int n) const;
167 
168  //TBTKFeature Core.Index.getSize.1 2019-09-19
169  //TBTKFeature Core.Index.getSize.2 2019-09-19
170  //TBTKFeature Core.Index.getSize.3 2019-09-19
171  //TBTKFeature Core.Index.getSize.4 2019-09-19
172  //TBTKFeature Core.Index.getSize.5 2019-09-19
179  unsigned int getSize() const;
180 
184  void reserve(unsigned int size);
185 
186  //TBTKFeature Core.Index.pushBack.1 2019-09-19
190  void pushBack(Subindex subindex);
191 
192  //TBTKFeature Core.Index.popFront.1 2019-09-19
196  Subindex popFront();
197 
198  //TBTKFeature Core.Index.popBack.1 2019-09-19
202  Subindex popBack();
203 
204  //TBTKFeature Core.Index.insert.1 2019-10-28
209  void insert(unsigned int n, Subindex subindex);
210 
211  //TBTKFeature Core.Index.erase.1 2019-10-28
217  Subindex erase(unsigned int n);
218 
219  //TBTKFeature Core.Index.getUnitRange.1 2019-09-19
225 
226  //TBTKFeature Core.Index.getSubIndex.1 2019-09-21
235  Index getSubIndex(int first, int last) const;
236 
237  //TBTKFeature Core.Index.split.1 2019-09-19
242  std::vector<Index> split() const;
243 
244  //TBTKFeature Core.Index.isPatternIndex.1 2019-09-19
245  //TBTKFeature Core.Index.isPatternIndex.2 2019-09-19
246  //TBTKFeature Core.Index.isPatternIndex.3 2019-09-19
247  //TBTKFeature Core.Index.isPatternIndex.4 2019-09-19
248  //TBTKFeature Core.Index.isPatternIndex.5 2019-09-19
249  //TBTKFeature Core.Index.isPatternIndex.6 2019-09-19
250  //TBTKFeature Core.Index.isPatternIndex.7 2019-09-19
251  //TBTKFeature Core.Index.isPatternIndex.8 2019-09-19
256  bool isPatternIndex() const;
257 
259  void print() const;
260 
261  //TBTKFeature Core.Index.toString.1 2019-09-19
262  //TBTKFeature Core.Index.toString.2 2019-09-19
266  std::string toString() const;
267 
274  friend std::ostream& operator<<(
275  std::ostream &stream,
276  const Index &index
277  );
278 
279  //TBTKFeature Core.Index.operator<.1.C++ 2019-09-19
280  //TBTKFeature Core.Index.operator<.2.C++ 2019-09-19
281  //TBTKFeature Core.Index.operator<.3.C++ 2019-09-19
282  //TBTKFeature Core.Index.operator<.4.C++ 2019-09-19
283  //TBTKFeature Core.Index.operator<.5.C++ 2019-09-19
289  friend bool operator<(const Index &i1, const Index &i2);
290 
291  //TBTKFeature Core.Index.operator>.1.C++ 2019-09-19
292  //TBTKFeature Core.Index.operator>.2.C++ 2019-09-19
293  //TBTKFeature Core.Index.operator>.3.C++ 2019-09-19
294  //TBTKFeature Core.Index.operator>.4.C++ 2019-09-19
295  //TBTKFeature Core.Index.operator>.5.C++ 2019-09-19
301  friend bool operator>(const Index &i1, const Index &i2);
302 
303  //TBTKFeature Core.Index.operator[].1.C++ 2019-09-19
304  //TBTKFeature Core.Index.operator[].2.C++ 2019-09-19
310  Subindex& operator[](unsigned int subindex);
311 
312  //TBTKFeature Core.Index.operator[].1.C++ 2019-09-19
313  //TBTKFeature Core.Index.operator[].2.C++ 2019-09-19
319  const Subindex& operator[](unsigned int subindex) const;
320 
321  //TBTKFeature Core.Index.Serialization.1 2019-09-19
328  std::string serialize(Serializable::Mode mode) const;
329 
333  unsigned int getSizeInBytes() const;
334 private:
336  std::vector<Subindex> indices;
337 };
338 
339 inline void Index::print() const{
340  Streams::out << "{";
341  for(unsigned int n = 0; n < indices.size(); n++){
342  if(n != 0)
343  Streams::out << ", ";
344  Streams::out << indices.at(n);
345  }
346  Streams::out << "}\n";
347 }
348 
349 inline std::string Index::toString() const{
350  std::string str = "{";
351  bool isFirstIndex = true;
352  for(unsigned int n = 0; n < indices.size(); n++){
353  Subindex subindex = indices.at(n);
354  if(!isFirstIndex && !subindex.isIndexSeparator())
355  str += ", ";
356  else
357  isFirstIndex = false;
358  if(subindex.isWildcard()){
359  str += "IDX_ALL";
360  }
361  else if(subindex.isSummationIndex()){
362  str += "IDX_SUM_ALL";
363  }
364  else if(subindex.isRangeIndex()){
365  switch(subindex){
366  case IDX_X:
367  str += "IDX_X";
368  break;
369  case IDX_Y:
370  str += "IDX_Y";
371  break;
372  case IDX_Z:
373  str += "IDX_Z";
374  break;
375  default:
376  TBTKExit(
377  "Index::toString()",
378  "This should never happen, contact the"
379  << " developer.",
380  ""
381  );
382  }
383  }
384  else if(subindex.isSpinIndex()){
385  str += "IDX_SPIN";
386  }
387  else if(subindex.isIndexSeparator()){
388  str += "}, {";
389  isFirstIndex = true;
390  }
391  else if(subindex.isLabeledWildcard()){
392  str += "IDX_ALL_(";
393  str += std::to_string(subindex.getWildcardLabel());
394  str += ")";
395  }
396  else{
397  str += std::to_string(subindex);
398  }
399  }
400  str += "}";
401 
402  return str;
403 }
404 
405 inline std::ostream& operator<<(std::ostream &stream, const Index &index){
406  stream << index.toString();
407 
408  return stream;
409 }
410 
411 inline bool Index::equals(const Index &index, bool allowWildcard) const{
412  if(indices.size() == index.indices.size()){
413  for(unsigned int n = 0; n < indices.size(); n++){
414  if(indices.at(n) != index.indices.at(n)){
415  if(!allowWildcard)
416  return false;
417  else{
418  if(
419  indices.at(n).isWildcard() ||
420  index.indices.at(n).isWildcard()
421  ){
422  continue;
423  }
424  else if(
425  indices.at(n).isLabeledWildcard()
426  ){
427  for(
428  unsigned int c = 0;
429  c < indices.size();
430  c++
431  ){
432  if(
433  indices.at(c)
434  == indices.at(n)
435  && index.indices.at(c)
436  != index.indices.at(n)
437  ){
438  return false;
439  }
440  }
441  }
442  else if(
443  index.indices.at(n).isLabeledWildcard()
444  ){
445  for(
446  unsigned int c = 0;
447  c < indices.size();
448  c++
449  ){
450  if(
451  index.indices.at(c)
452  == index.indices.at(n)
453  && indices.at(c)
454  != indices.at(n)
455  ){
456  return false;
457  }
458  }
459  }
460  else{
461  return false;
462  }
463  }
464  }
465  }
466  }
467  else{
468  return false;
469  }
470 
471  return true;
472 }
473 
474 inline Subindex& Index::at(unsigned int n){
475  return indices.at(n);
476 }
477 
478 inline const Subindex& Index::at(unsigned int n) const{
479  return indices.at(n);
480 }
481 
482 inline unsigned int Index::getSize() const{
483  return indices.size();
484 }
485 
486 inline void Index::reserve(unsigned int size){
487  indices.reserve(size);
488 }
489 
490 inline void Index::pushBack(Subindex subindex){
491  indices.push_back(subindex);
492 }
493 
495  Subindex first = indices.at(0);
496  indices.erase(indices.begin());
497 
498  return first;
499 }
500 
502  Subindex last = indices.back();
503  indices.pop_back();
504 
505  return last;
506 }
507 
508 inline void Index::insert(unsigned int n, Subindex subindex){
509  indices.insert(indices.begin() + n, subindex);
510 }
511 
512 inline Subindex Index::erase(unsigned int n){
513  Subindex subindex = indices[n];
514  indices.erase(indices.begin() + n);
515 
516  return subindex;
517 }
518 
519 inline std::vector<Index> Index::split() const{
520  std::vector<Index> components;
521  components.push_back(Index());
522  for(unsigned int n = 0; n < indices.size(); n++){
523  if(indices[n].isIndexSeparator())
524  components.push_back(Index());
525  else
526  components.back().pushBack(indices[n]);
527  }
528 
529  return components;
530 }
531 
532 inline bool Index::isPatternIndex() const{
533  for(unsigned int n = 0; n < indices.size(); n++)
534  if(indices.at(n) < 0)
535  return true;
536 
537  return false;
538 }
539 
540 inline Subindex& Index::operator[](unsigned int subindex){
541  return indices[subindex];
542 }
543 
544 inline const Subindex& Index::operator[](unsigned int subindex) const{
545  return indices[subindex];
546 }
547 
548 inline unsigned int Index::getSizeInBytes() const{
549  return sizeof(*this) + sizeof(int)*indices.capacity();
550 }
551 
552 }; //End of namespace TBTK
553 
554 #endif
Subindex getWildcardLabel() const
Definition: Subindex.h:423
Subindex popBack()
Definition: Index.h:501
void reserve(unsigned int size)
Definition: Index.h:486
void pushBack(Subindex subindex)
Definition: Index.h:490
An entry in an Index.
bool isRangeIndex() const
Definition: Subindex.h:441
Index(std::initializer_list< Subindex > i)
Definition: Index.h:54
Index getUnitRange()
unsigned int getSizeInBytes() const
Definition: Index.h:548
An entry in an Index.
Definition: Subindex.h:91
Subindex & at(unsigned int n)
Definition: Index.h:474
std::string toString() const
Definition: Index.h:349
friend std::ostream & operator<<(std::ostream &stream, const Index &index)
Definition: Index.h:405
Subindex & operator[](unsigned int subindex)
Definition: Index.h:540
std::string serialize(Serializable::Mode mode) const
Index getSubIndex(int first, int last) const
friend bool operator<(const Index &i1, const Index &i2)
Index()
Definition: Index.h:48
void insert(unsigned int n, Subindex subindex)
Definition: Index.h:508
bool isIndexSeparator() const
Definition: Subindex.h:449
bool isSpinIndex() const
Definition: Subindex.h:445
bool isLabeledWildcard() const
Definition: Subindex.h:419
bool isSummationIndex() const
Definition: Subindex.h:437
Index(const Index &index)
Definition: Index.h:79
void print() const
Definition: Index.h:339
static std::ostream out
Definition: Streams.h:70
friend bool operator>(const Index &i1, const Index &i2)
unsigned int getSize() const
Definition: Index.h:482
Physical index.
Definition: Index.h:44
Definition: Boolean.h:32
bool equals(const Index &index, bool allowWildcard=false) const
Definition: Index.h:411
std::vector< Index > split() const
Definition: Index.h:519
Subindex popFront()
Definition: Index.h:494
Mode
Definition: Serializable.h:47
bool isPatternIndex() const
Definition: Index.h:532
bool isWildcard() const
Definition: Subindex.h:415
Abstract base class for serializable objects.
Streams for TBTK output.
Subindex erase(unsigned int n)
Definition: Index.h:512
Index(std::vector< Subindex > i)
Definition: Index.h:67