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 "Streams.h"
27 
28 #include <vector>
29 
30 namespace TBTK{
31 
32 enum {
33  IDX_SUM_ALL = -1, IDX_ALL = -1,
34  IDX_X = -2,
35  IDX_Y = -3,
36  IDX_Z = -4,
37  IDX_SPIN = -5
38 };
39 
44 class Index{
45 public:
47  Index(std::initializer_list<int> i) : indices(i){};
48 
50  Index(std::vector<int> i) : indices(i){};
51 
53  Index(const Index &index) : indices(index.indices){};
54 
57  Index(const Index &head, const Index &tail);
58 
63  bool equals(const Index &index, bool allowWildcard = false) const;
64 
66  int& at(unsigned int n);
67 
69  const int& at(unsigned int n) const;
70 
72  unsigned int size() const;
73 
75  void push_back(int subindex);
76 
78  int popFront();
79 
81  int popBack();
82 
86 
89  Index getSubIndex(int first, int last);
90 
92  void print() const;
93 
95  std::string toString() const;
96 
99  friend bool operator<(const Index &i1, const Index &i2);
100 
103  friend bool operator>(const Index &i1, const Index &i2);
104 private:
106  std::vector<int> indices;
107 };
108 
109 inline void Index::print() const{
110  Util::Streams::out << "{";
111  for(unsigned int n = 0; n < indices.size(); n++){
112  if(n != 0)
113  Util::Streams::out << ", ";
114  Util::Streams::out << indices.at(n);
115  }
116  Util::Streams::out << "}\n";
117 }
118 
119 inline std::string Index::toString() const{
120  std::string str = "{";
121  for(unsigned int n = 0; n < indices.size(); n++){
122  if(n != 0)
123  str += ", ";
124  str += std::to_string(indices.at(n));
125  }
126  str += "}";
127 
128  return str;
129 }
130 
131 inline bool Index::equals(const Index &index, bool allowWildcard) const{
132  if(indices.size() == index.indices.size()){
133  for(unsigned int n = 0; n < indices.size(); n++){
134  if(indices.at(n) != index.indices.at(n)){
135  if(!allowWildcard)
136  return false;
137  else{
138  if(
139  indices.at(n) == IDX_ALL ||
140  index.indices.at(n) == IDX_ALL
141  )
142  continue;
143  else
144  return false;
145  }
146  }
147  }
148  }
149  else{
150  return false;
151  }
152 
153  return true;
154 }
155 
156 inline int& Index::at(unsigned int n){
157  return indices.at(n);
158 }
159 
160 inline const int& Index::at(unsigned int n) const{
161  return indices.at(n);
162 }
163 
164 inline unsigned int Index::size() const{
165  return indices.size();
166 }
167 
168 inline void Index::push_back(int subindex){
169  indices.push_back(subindex);
170 }
171 
172 inline int Index::popFront(){
173  int first = indices.at(0);
174  indices.erase(indices.begin());
175 
176  return first;
177 }
178 
179 inline int Index::popBack(){
180  int last = indices.back();
181  indices.pop_back();
182 
183  return last;
184 }
185 
186 }; //End of namespace TBTK
187 
188 #endif
void print() const
Definition: Index.h:109
bool equals(const Index &index, bool allowWildcard=false) const
Definition: Index.h:131
Index getUnitRange()
Definition: Index.cpp:83
unsigned int size() const
Definition: Index.h:164
static std::ostream out
Definition: Streams.h:37
friend bool operator<(const Index &i1, const Index &i2)
Definition: Index.cpp:35
Index(std::vector< int > i)
Definition: Index.h:50
Index(const Index &index)
Definition: Index.h:53
int popFront()
Definition: Index.h:172
friend bool operator>(const Index &i1, const Index &i2)
Definition: Index.cpp:59
void push_back(int subindex)
Definition: Index.h:168
Definition: Index.h:44
Definition: AbstractOperator.h:26
Index getSubIndex(int first, int last)
Definition: Index.cpp:92
Index(std::initializer_list< int > i)
Definition: Index.h:47
int & at(unsigned int n)
Definition: Index.h:156
Streams for TBTK output.
std::string toString() const
Definition: Index.h:119
int popBack()
Definition: Index.h:179