diff --git a/itensor/Makefile b/itensor/Makefile index 9d8dce89..312224b0 100644 --- a/itensor/Makefile +++ b/itensor/Makefile @@ -21,7 +21,7 @@ SOURCES+= mpo.cc SOURCES+= tevol.cc SOURCES+= autompo.cc -HEADERS=global.h real.h permutation.h index.h \ +HEADERS=global.h real.h permutation.h indextype.h index.h \ indexset.h counter.h simplematrix.h itensor.h \ qn.h iqindex.h iqtdat.h \ detail/skip_iterator.h iqtensor.h \ @@ -101,7 +101,7 @@ mkdebugdir: clean: rm -fr *.o .debug_objs libitensor.a libitensor-g.a -DEPHEADERS=real.h global.h index.h permutation.h +DEPHEADERS=real.h global.h indextype.h index.h permutation.h index.o: $(DEPHEADERS) .debug_objs/index.o: $(DEPHEADERS) DEPHEADERS+= indexset.h diff --git a/itensor/index.cc b/itensor/index.cc index bbb3b78e..9726f387 100644 --- a/itensor/index.cc +++ b/itensor/index.cc @@ -9,36 +9,6 @@ namespace itensor { using std::string; using std::stringstream; -std::ostream& -operator<<(std::ostream& s, const IndexType& it) - { - if(it == Link) s << "Link"; - else if(it == Site) s << "Site"; - else if(it == All) s << "All"; - return s; - } - -int -IndexTypeToInt(IndexType it) - { - if(it == Link) return 1; - if(it == Site) return 2; - if(it == All) return 3; - Error("No integer value defined for IndexType."); - return -1; - } - -IndexType -IntToIndexType(int i) - { - if(i == 1) return Link; - if(i == 2) return Site; - if(i == 3) return All; - printfln("No IndexType value defined for i=%d\n",i); - Error("Undefined IntToIndexType value"); - return Link; - } - string putprimes(string s, int plev) { @@ -59,16 +29,10 @@ putprimes(string s, int plev) return str.str(); } -string -nameindex(IndexType it, int plev) +string static +nameindex(IndexType const& it, int plev) { - static const array - indextypename = {{ "Link","Site", "All" }}; -#ifdef DEBUG - return putprimes(indextypename.at(int(it)),plev); -#else - return putprimes(indextypename[int(it)],plev); -#endif + return putprimes(it.c_str(),plev); } string @@ -110,7 +74,7 @@ Index() id_(0), primelevel_(0), m_(1), - type_(Site), + type_(NullInd), sname_("Null") { } @@ -200,8 +164,7 @@ write(std::ostream& s) const s.write((char*) &primelevel_,sizeof(primelevel_)); - const int t = IndexTypeToInt(type_); - s.write((char*) &t,sizeof(t)); + type_.write(s); s.write((char*) &(id_),sizeof(id_)); @@ -224,9 +187,7 @@ read(std::istream& s) } #endif - int t; - s.read((char*) &t,sizeof(t)); - type_ = IntToIndexType(t); + type_.read(s); s.read((char*) &id_, sizeof(id_)); diff --git a/itensor/index.h b/itensor/index.h index 1195d193..a72a565c 100644 --- a/itensor/index.h +++ b/itensor/index.h @@ -5,11 +5,10 @@ #ifndef __ITENSOR_INDEX_H #define __ITENSOR_INDEX_H #include "global.h" +#include "indextype.h" namespace itensor { -enum IndexType { Link, Site, All }; - //Forward declarations class IndexVal; diff --git a/itensor/indextype.h b/itensor/indextype.h new file mode 100644 index 00000000..0ea56e09 --- /dev/null +++ b/itensor/indextype.h @@ -0,0 +1,131 @@ +// +// Distributed under the ITensor Library License, Version 1.1 +// (See accompanying LICENSE file.) +// +#ifndef __ITENSOR_INDEXTYPE_H_ +#define __ITENSOR_INDEXTYPE_H_ + +#include +#include +#include +#ifdef DEBUG +#include +#endif + +#ifdef DEBUG +#define CHECK_IND(X) check_ind(X); +#else +#define CHECK_IND(X) +#endif + +namespace itensor { + +size_t inline constexpr +ITSize() { return 7ul; } + +size_t inline constexpr +ITStoreSize() { return 1+ITSize(); } + +class IndexType + { + public: + using storage_type = std::array; + private: + storage_type name_; + public: + + explicit + IndexType(const char* name) + { + name_.fill('\0'); + auto len = std::min(std::strlen(name),size()); +#ifdef DEBUG + if(std::strlen(name) > size()) + { + std::cout << "Warning: IndexType name will be truncated to " << size() << " chars" << std::endl; + } +#endif + for(size_t j = 0; j < len; ++j) + { + name_[j] = name[j]; + } + assert(name_[size()]=='\0'); + } + + const char* + c_str() const { assert(name_[size()]=='\0'); return &(name_[0]); } + + operator const char*() const { return c_str(); } + + size_t static constexpr + size() { return ITSize(); } + + const char& + operator[](size_t i) const { CHECK_IND(i) return name_[i]; } + + char& + operator[](size_t i) { CHECK_IND(i) return name_[i]; } + + void + write(std::ostream& s) const; + + void + read(std::istream& s); + + private: + void + check_ind(size_t j) const + { + if(j >= size()) Error("IndexType: index out of range"); + } + }; + +void inline IndexType:: +write(std::ostream& s) const + { + for(size_t n = 0; n < IndexType::size(); ++n) + s.write((char*) &(name_[n]),sizeof(char)); + } + +void inline IndexType:: +read(std::istream& s) + { + for(size_t n = 0; n < IndexType::size(); ++n) + s.read((char*) &(name_[n]),sizeof(char)); + } + + +bool inline +operator==(const IndexType& t1, const IndexType& t2) + { + for(size_t j = 0; j < IndexType::size(); ++j) + if(t1[j] != t2[j]) return false; + return true; + } + +bool inline +operator!=(const IndexType& t1, const IndexType& t2) + { + return !operator==(t1,t2); + } + +const auto All = IndexType("All"); +const auto NullInd = IndexType("NullInd"); +const auto Link = IndexType("Link"); +const auto Site = IndexType("Site"); +const auto Atype = IndexType("Atype"); +const auto Btype = IndexType("Btype"); +const auto Ctype = IndexType("Ctype"); +const auto Dtype = IndexType("Dtype"); +const auto Xtype = IndexType("Xtype"); +const auto Ytype = IndexType("Ytype"); +const auto Ztype = IndexType("Ztype"); +const auto Wtype = IndexType("Wtype"); +const auto Vtype = IndexType("Vtype"); + + +} // namespace itensor + +#undef CHECK_IND + +#endif