TBTK
Subindex.h
Go to the documentation of this file.
1 /* Copyright 2019 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_SUBINDEX
24 #define COM_DAFER45_TBTK_SUBINDEX
25 
26 #include "TBTK/Integer.h"
27 #include "TBTK/Natural.h"
29 
30 #ifndef TBTK_DISABLE_NLOHMANN_JSON
31 # include "TBTK/json.hpp"
32 #endif
33 
34 namespace TBTK{
35 
36 #if TBTK_WRAP_PRIMITIVE_TYPES
37 
92 public:
93  //TBTKFeature Core.Subindex.Construction.1 2019-09-22
95  Subindex(){};
96 
97  //TBTKFeature Core.Subindex.Construction.2.C++ 2019-09-22
101  constexpr Subindex(Integer value) : value(value) {}
102 
103  //TBTKFeature Core.Subindex.Construction.2.C++ 2019-09-22
107  constexpr Subindex(int value) : value(value) {}
108 
109  //TBTKFeature Core.Subindex.Construction.2.C++ 2019-09-22
113  constexpr Subindex(unsigned int value) : value(value) {}
114 
115  //TBTKFeature Core.Subindex.Serialization.1 2019-09-22
122  Subindex(const std::string &serialization, Serializable::Mode mode);
123 
124  //TBTKFeature Core.Subindex.isWildcard.1 2019-09-22
125  //TBTKFeature Core.Subindex.isWildcard.2 2019-09-22
129  bool isWildcard() const;
130 
131  //TBTKFeature Core.Subindex.isLabeledWildcard.1 2019-09-22
132  //TBTKFeature Core.Subindex.isLabeledWildcard.2 2019-09-22
137  bool isLabeledWildcard() const;
138 
143  Subindex getWildcardLabel() const;
144 
145  //TBTKFeature Core.Subindex.isSummationIndex.1 2019-09-22
146  //TBTKFeature Core.Subindex.isSummationIndex.2 2019-09-22
151  bool isSummationIndex() const;
152 
156  bool isRangeIndex() const;
157 
158  //TBTKFeature Core.Subindex.isSpinIndex.1 2019-09-22
159  //TBTKFeature Core.Subindex.isSpinIndex.2 2019-09-22
163  bool isSpinIndex() const;
164 
165  //TBTKFeature Core.Subindex.isIndexSeparator.1 2019-09-22
166  //TBTKFeature Core.Subindex.isIndexSeparator.2 2019-09-22
171  bool isIndexSeparator() const;
172 
173  //TBTKFeature Core.Subindex.operatorInt.1.C++ 2019-09-22
175  constexpr operator int() const{ return value; };
176 
177  //TBTKFeature Core.Subindex.operatorAssignment.1.C++ 2019-09-22
184  value = rhs;
185 
186  return *this;
187  }
188 
189  //TBTKFeature Core.Subindex.operatorAssignment.1.C++ 2019-09-22
195  Subindex& operator=(int rhs){
196  value = rhs;
197 
198  return *this;
199  }
200 
201  //TBTKFeature Core.Subindex.operatorAdditionAssignment.1.C++ 2019-09-22
208  value += rhs.value;
209 
210  return *this;
211  }
212 
213  //TBTKFeature Core.Subindex.operatorSubtractionAssignment.1.C++ 2019-09-22
220  value -= rhs.value;
221 
222  return *this;
223  }
224 
225  //TBTKFeature Core.Subindex.operatorMultiplicationAssignment.1.C++ 2019-09-22
232  value *= rhs.value;
233 
234  return *this;
235  }
236 
237  //TBTKFeature Core.Subindex.operatorDivisionAssignment.1.C++ 2019-09-22
244  value /= rhs.value;
245 
246  return *this;
247  }
248 
249  //TBTKFeature Core.Subindex.operatorPreIncrement.1.C++ 2019-09-22
254  value++;
255 
256  return *this;
257  }
258 
259  //TBTKFeature Core.Subindex.operatorPostIncrement.1.C++ 2019-09-22
264  Subindex previous(*this);
265  operator++();
266 
267  return previous;
268  }
269 
270  //TBTKFeature Core.Subindex.operatorPreDecrement.1.C++ 2019-09-22
275  value--;
276 
277  return *this;
278  }
279 
280  //TBTKFeature Core.Subindex.operatorPostIncrement.1.C++ 2019-09-22
285  Subindex previous(*this);
286  operator--();
287 
288  return previous;
289  }
290 
291  //TBTKFeature Core.Subindex.operatorFunction.1.C++ 2019-09-22
292  //TBTKFeature Core.Subindex.operatorFunction.2.C++ 2019-09-22
303  Subindex operator()(unsigned int label) const;
304 
311  friend std::ostream& operator<<(std::ostream &os, const Subindex subindex){
312  os << subindex.value;
313 
314  return os;
315  }
316 
323  friend std::istream& operator>>(std::istream &is, Subindex &subindex){
324  int i;
325  is >> i;
326  subindex.value = Integer(i);
327 
328  return is;
329  }
330 
338  std::string serialize(Serializable::Mode mode) const;
339 
340 #ifndef TBTK_DISABLE_NLOHMANN_JSON
341 
345  friend void to_json(nlohmann::json &j, const Subindex &subindex){
346  to_json(j, subindex.value);
347  }
348 
353  friend void from_json(const nlohmann::json &j, Subindex &subindex){
354  from_json(j, subindex.value);
355  }
356 #endif
357 private:
359  Integer value;
360 };
361 
392 //_a_ and _aX_ are shorthand notation for IDX_ALL and IDX_ALL_. Never use
393 //shorthands in library code.
394 constexpr Subindex IDX_FLAG_MASK = (int)0x00FFFFFF;
395 constexpr Subindex IDX_ALL = (int)(0xBFFFFFFF & ~0x20000000);
396 constexpr Subindex _a_ = IDX_ALL;
397 constexpr Subindex IDX_SUM_ALL = (int)(0xBFFFFFFF & ~0x20000001);
398 constexpr Subindex IDX_SPIN = (int)(0xBFFFFFFF & ~0x20000002);
399 constexpr Subindex IDX_SEPARATOR = (int)(0xBFFFFFFF & ~0x20000003);
400 constexpr Subindex IDX_ALL_ = (int)(0xBFFFFFFF & ~0x10000000);
401 constexpr Subindex _aX_ = IDX_ALL_;
402 constexpr Subindex IDX_RANGE = (int)(0xBFFFFFFF & ~0x08000000);
403 constexpr Subindex IDX_X = (int)(IDX_RANGE & ~0x00000000);
404 constexpr Subindex IDX_Y = (int)(IDX_RANGE & ~0x00000001);
405 constexpr Subindex IDX_Z = (int)(IDX_RANGE & ~0x00000002);
406 
408  const std::string &serialization,
409  Serializable::Mode mode
410 ) :
411  value(serialization, mode)
412 {
413 }
414 
415 inline bool Subindex::isWildcard() const{
416  return value == IDX_ALL;
417 }
418 
419 inline bool Subindex::isLabeledWildcard() const{
420  return (value | IDX_FLAG_MASK) == IDX_ALL_;
421 }
422 
424  TBTKAssert(
426  "Subindex::getWildcardLabel()",
427  "The Subindex is not a labeled wildcard.",
428  ""
429  );
430 
431  if((value & IDX_FLAG_MASK) == 0)
432  return 0;
433  else
434  return Subindex(-(int)(value | ~IDX_FLAG_MASK));
435 }
436 
437 inline bool Subindex::isSummationIndex() const{
438  return value == IDX_SUM_ALL;
439 }
440 
441 inline bool Subindex::isRangeIndex() const{
442  return (value | IDX_FLAG_MASK) == IDX_RANGE;
443 }
444 
445 inline bool Subindex::isSpinIndex() const{
446  return value == IDX_SPIN;
447 }
448 
449 inline bool Subindex::isIndexSeparator() const{
450  return value == IDX_SEPARATOR;
451 }
452 
453 inline Subindex Subindex::operator()(unsigned int label) const{
454  TBTKAssert(
455  value == IDX_ALL_.value,
456  "Subindex::operator()",
457  "Unsupported subindex type. This function is only supported"
458  << " for the IDX_ALL_ Subindex.",
459  ""
460  );
461 
462  return Subindex(value & (-(int)label | ~IDX_FLAG_MASK));
463 }
464 
465 inline std::string Subindex::serialize(Serializable::Mode mode) const{
466  return value.serialize(mode);
467 }
468 
469 #else
470  typedef int Subindex;
471 #endif
472 
473 }; //End of namespace TBTK
474 
475 #endif
Subindex getWildcardLabel() const
Definition: Subindex.h:423
constexpr Subindex(Integer value)
Definition: Subindex.h:101
bool isRangeIndex() const
Definition: Subindex.h:441
Subindex()
Definition: Subindex.h:95
friend std::istream & operator>>(std::istream &is, Subindex &subindex)
Definition: Subindex.h:323
Subindex operator++(int)
Definition: Subindex.h:263
Subindex & operator*=(Subindex rhs)
Definition: Subindex.h:231
Subindex & operator=(int rhs)
Definition: Subindex.h:195
Integer number.
Definition: Integer.h:37
std::string serialize(Serializable::Mode mode) const
Definition: Subindex.h:465
An entry in an Index.
Definition: Subindex.h:91
constexpr Subindex(int value)
Definition: Subindex.h:107
constexpr Subindex(unsigned int value)
Definition: Subindex.h:113
Subindex & operator/=(Subindex rhs)
Definition: Subindex.h:243
Subindex & operator+=(Subindex rhs)
Definition: Subindex.h:207
Subindex operator()(unsigned int label) const
Definition: Subindex.h:453
Base class for psudo-serializable objects.
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
Definition: PseudoSerializable.h:31
friend void from_json(const nlohmann::json &j, Subindex &subindex)
Definition: Subindex.h:353
Integer number.
Subindex & operator++()
Definition: Subindex.h:253
constexpr Subindex IDX_FLAG_MASK
Special subindex values.
Definition: Subindex.h:394
Definition: Boolean.h:32
Natural number.
Mode
Definition: Serializable.h:47
std::string serialize(Serializable::Mode mode) const
Definition: Integer.h:199
bool isWildcard() const
Definition: Subindex.h:415
Subindex & operator=(Integer rhs)
Definition: Subindex.h:183
friend void to_json(nlohmann::json &j, const Subindex &subindex)
Definition: Subindex.h:345
Subindex & operator-=(Subindex rhs)
Definition: Subindex.h:219
Subindex & operator--()
Definition: Subindex.h:274
Subindex operator--(int)
Definition: Subindex.h:284
friend std::ostream & operator<<(std::ostream &os, const Subindex subindex)
Definition: Subindex.h:311