Skip to content
Permalink
Browse files

IndexType now a class instead of an enum; makes IndexType's user exte…

…nsible

IndexType is now a small class holding an immutable fixed-length string (a std::array of chars). Its size is exactly that of a long integer. This design allows users to define their own IndexTypes by writing 

    auto MyType = IndexType("MyType");

then defining an Index of this type as

    auto I = Index("I",10,MyType);
  • Loading branch information
emstoudenmire committed Aug 13, 2015
1 parent 9492bac commit a92c17a5678df85ecc98774f16bd80806949eebf
Showing with 140 additions and 49 deletions.
  1. +2 −2 itensor/Makefile
  2. +6 −45 itensor/index.cc
  3. +1 −2 itensor/index.h
  4. +131 −0 itensor/indextype.h
@@ -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
@@ -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<string,3>
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_));

@@ -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;

@@ -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 <array>
#include <cstring>
#include <cctype>
#ifdef DEBUG
#include <iostream>
#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<char,ITStoreSize()>;
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

0 comments on commit a92c17a

Please sign in to comment.
You can’t perform that action at this time.