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  _a_ = -1, //Shorthand notation for IDX_ALL. Always use IDX_ALL in
54  IDX_ALL = -1, //library code.
55  IDX_SUM_ALL = -2,
56  IDX_X = -3,
57  IDX_Y = -4,
58  IDX_Z = -5,
59  IDX_SPIN = -6,
60  IDX_SEPARATOR = -7
61 };
62 
69 class Index{
70 public:
72  Index(){};
73 
77  Index(std::initializer_list<int> i) : indices(i){};
78 
83  std::initializer_list<unsigned int> i
84  ) : indices(i.begin(), i.end()){};
85 
89  Index(std::vector<int> i) : indices(i){};
90 
94  Index(std::vector<unsigned int> i) : indices(i.begin(), i.end()){};
95 
99  Index(const Index &index) : indices(index.indices){};
100 
106  Index(const Index &head, const Index &tail);
107 
112  Index(std::initializer_list<std::initializer_list<int>> indexList);
113 
118  Index(const std::vector<std::vector<int>> &indexList);
119 
124  Index(std::initializer_list<Index> indexList);
125 
130  Index(const std::string &indexString);
131 
138  Index(const std::string &serialization, Serializable::Mode mode);
139 
147  bool equals(const Index &index, bool allowWildcard = false) const;
148 
154  int& at(unsigned int n);
155 
161  const int& at(unsigned int n) const;
162 
169  unsigned int getSize() const;
170 
174  void reserve(unsigned int size);
175 
179  void push_back(int subindex);
180 
184  int popFront();
185 
189  int popBack();
190 
196 
205  Index getSubIndex(int first, int last) const;
206 
211  std::vector<Index> split() const;
212 
217  bool isPatternIndex() const;
218 
220  void print() const;
221 
225  std::string toString() const;
226 
232  friend bool operator<(const Index &i1, const Index &i2);
233 
239  friend bool operator>(const Index &i1, const Index &i2);
240 
246  int& operator[](unsigned int subindex);
247 
253  const int& operator[](unsigned int subindex) const;
254 
261  std::string serialize(Serializable::Mode mode) const;
262 
266  unsigned int getSizeInBytes() const;
267 private:
269  std::vector<int> indices;
270 };
271 
272 inline void Index::print() const{
273  Streams::out << "{";
274  for(unsigned int n = 0; n < indices.size(); n++){
275  if(n != 0)
276  Streams::out << ", ";
277  Streams::out << indices.at(n);
278  }
279  Streams::out << "}\n";
280 }
281 
282 inline std::string Index::toString() const{
283  std::string str = "{";
284  bool isFirstIndex = true;
285  for(unsigned int n = 0; n < indices.size(); n++){
286 /* if(n != 0)
287  str += ", ";*/
288  int subindex = indices.at(n);
289  if(!isFirstIndex && subindex != IDX_SEPARATOR)
290  str += ", ";
291  else
292  isFirstIndex = false;
293  switch(subindex){
294  case IDX_ALL:
295  str += "IDX_ALL";
296  break;
297  case IDX_SUM_ALL:
298  str += "IDX_SUM_ALL";
299  break;
300  case IDX_X:
301  str += "IDX_X";
302  break;
303  case IDX_Y:
304  str += "IDX_Y";
305  break;
306  case IDX_Z:
307  str += "IDX_Z";
308  break;
309  case IDX_SPIN:
310  str += "IDX_SPIN";
311  break;
312  case IDX_SEPARATOR:
313  str += "}, {";
314  isFirstIndex = true;
315  break;
316  default:
317  str += std::to_string(subindex);
318  break;
319  }
320  }
321  str += "}";
322 
323  return str;
324 }
325 
326 inline bool Index::equals(const Index &index, bool allowWildcard) const{
327  if(indices.size() == index.indices.size()){
328  for(unsigned int n = 0; n < indices.size(); n++){
329  if(indices.at(n) != index.indices.at(n)){
330  if(!allowWildcard)
331  return false;
332  else{
333  if(
334  indices.at(n) == IDX_ALL ||
335  index.indices.at(n) == IDX_ALL
336  )
337  continue;
338  else
339  return false;
340  }
341  }
342  }
343  }
344  else{
345  return false;
346  }
347 
348  return true;
349 }
350 
351 inline int& Index::at(unsigned int n){
352  return indices.at(n);
353 }
354 
355 inline const int& Index::at(unsigned int n) const{
356  return indices.at(n);
357 }
358 
359 inline unsigned int Index::getSize() const{
360  return indices.size();
361 }
362 
363 inline void Index::reserve(unsigned int size){
364  indices.reserve(size);
365 }
366 
367 inline void Index::push_back(int subindex){
368  indices.push_back(subindex);
369 }
370 
371 inline int Index::popFront(){
372  int first = indices.at(0);
373  indices.erase(indices.begin());
374 
375  return first;
376 }
377 
378 inline int Index::popBack(){
379  int last = indices.back();
380  indices.pop_back();
381 
382  return last;
383 }
384 
385 inline std::vector<Index> Index::split() const{
386  std::vector<Index> components;
387  components.push_back(Index());
388  for(unsigned int n = 0; n < indices.size(); n++){
389  if(indices[n] == IDX_SEPARATOR)
390  components.push_back(Index());
391  else
392  components.back().push_back(indices[n]);
393  }
394 
395  return components;
396 }
397 
398 inline bool Index::isPatternIndex() const{
399  for(unsigned int n = 0; n < indices.size(); n++)
400  if(indices.at(n) < 0)
401  return true;
402 
403  return false;
404 }
405 
406 inline int& Index::operator[](unsigned int subindex){
407  return indices[subindex];
408 }
409 
410 inline const int& Index::operator[](unsigned int subindex) const{
411  return indices[subindex];
412 }
413 
414 inline unsigned int Index::getSizeInBytes() const{
415  return sizeof(*this) + sizeof(int)*indices.capacity();
416 }
417 
418 }; //End of namespace TBTK
419 
420 #endif
void reserve(unsigned int size)
Definition: Index.h:363
Index getSubIndex(int first, int last) const
Index(std::vector< unsigned int > i)
Definition: Index.h:94
void print() const
Definition: Index.h:272
bool equals(const Index &index, bool allowWildcard=false) const
Definition: Index.h:326
Index getUnitRange()
unsigned int getSizeInBytes() const
Definition: Index.h:414
friend bool operator<(const Index &i1, const Index &i2)
Index()
Definition: Index.h:72
Index(std::vector< int > i)
Definition: Index.h:89
int & operator[](unsigned int subindex)
Definition: Index.h:406
Index(const Index &index)
Definition: Index.h:99
int popFront()
Definition: Index.h:371
std::string serialize(Serializable::Mode mode) const
Index(std::initializer_list< unsigned int > i)
Definition: Index.h:82
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:367
std::vector< Index > split() const
Definition: Index.h:385
Flexible physical index.
Definition: Index.h:69
Definition: ModelFactory.h:35
unsigned int getSize() const
Definition: Index.h:359
Mode
Definition: Serializable.h:44
Index(std::initializer_list< int > i)
Definition: Index.h:77
Abstract base class for serializable objects.
int & at(unsigned int n)
Definition: Index.h:351
Streams for TBTK output.
std::string toString() const
Definition: Index.h:282
bool isPatternIndex() const
Definition: Index.h:398
int popBack()
Definition: Index.h:378