TBTK
Need a break? Support the development by playing Polarity Puzzles
TBTK::IndexTree Class Reference

Data structure for mapping physical indices to linear indices. More...

#include <IndexTree.h>

Inheritance diagram for TBTK::IndexTree:
TBTK::Serializable TBTK::Streamable

Classes

class  ConstIterator
 

Public Types

enum  SearchMode { StrictMatch, MatchWildcards }
 
- Public Types inherited from TBTK::Serializable
enum  Mode { Debug, Binary, XML, JSON }
 

Public Member Functions

 IndexTree ()
 
 IndexTree (const std::string &serialization, Mode mode)
 
virtual ~IndexTree ()
 
void add (const Index &index)
 
void generateLinearMap ()
 
bool getLinearMapIsGenerated () const
 
int getLinearIndex (const Index &index, SearchMode searchMode=SearchMode::StrictMatch, bool returnNegativeForMissingIndex=false) const
 
Index getPhysicalIndex (int linearIndex) const
 
bool contains (const Index &index)
 
int getSize () const
 
std::vector< unsigned int > getSubindicesMatching (int subindexValue, const Index &index, SearchMode searchMode) const
 
std::vector< IndexgetIndexList (const Index &pattern) const
 
bool equals (const IndexTree &indexTree) const
 
ConstIterator begin () const
 
ConstIterator cbegin () const
 
ConstIterator end () const
 
ConstIterator cend () const
 
virtual std::string toString () const
 
std::string serialize (Mode mode) const
 
- Public Member Functions inherited from TBTK::Serializable
template<>
std::string serialize (const bool &data, Mode mode)
 
template<>
std::string serialize (const double &data, Mode mode)
 
template<>
std::string serialize (const std::complex< double > &data, Mode mode)
 
template<>
std::string serialize (const int &data, Mode mode)
 
template<>
std::string serialize (const unsigned int &data, Mode mode)
 
template<>
std::string serialize (const SpinMatrix &data, Mode mode)
 
template<>
std::string serialize (const Statistics &data, Mode mode)
 
template<>
std::string serialize (const std::vector< std::complex< double >> &data, Mode mode)
 
template<>
int deserialize (const std::string &serialization, Mode mode)
 
template<>
unsigned int deserialize (const std::string &serialization, Mode mode)
 
template<>
double deserialize (const std::string &serialization, Mode mode)
 
template<>
std::complex< double > deserialize (const std::string &serialization, Mode mode)
 
template<>
SpinMatrix deserialize (const std::string &serialization, Mode mode)
 
template<>
Statistics deserialize (const std::string &serialization, Mode mode)
 
template<>
std::vector< std::complex< double > > deserialize (const std::string &serialization, Mode mode)
 

Friends

bool operator== (const IndexTree &lhs, const IndexTree &rhs)
 
bool operator!= (const IndexTree &lhs, const IndexTree &rhs)
 

Additional Inherited Members

- Static Public Member Functions inherited from TBTK::Serializable
static bool hasID (const std::string &serialization, Mode mode)
 
static std::string getID (const std::string &serialization, Mode mode)
 
static std::string extractComponent (const std::string &serialization, const std::string &containerID, const std::string &componentID, const std::string &componentName, Mode mode)
 
- Static Protected Member Functions inherited from TBTK::Serializable
static bool validate (const std::string &serialization, const std::string &id, Mode mode)
 
static std::string getContent (const std::string &serialization, Mode mode)
 
static std::vector< std::string > split (const std::string &content, Mode mode)
 
template<typename DataType >
static std::enable_if<!std::is_pointer< DataType >::value, std::string >::type serialize (const DataType &data, Mode mode)
 
template<typename DataType >
static std::enable_if< std::is_pointer< DataType >::value, std::string >::type serialize (const DataType &data, Mode mode)
 
template<typename DataType >
static DataType deserialize (const std::string &serialization, Mode mode)
 
static std::string extract (const std::string &serialization, Mode mode, std::string component)
 

Detailed Description

Data structure for mapping physical indices to linear indices.

Member Enumeration Documentation

◆ SearchMode

Enum class for selecting mode used to search indices with getLinearIndex().

Constructor & Destructor Documentation

◆ IndexTree() [1/2]

TBTK::IndexTree::IndexTree ( )

Constructors an IndexTree.

◆ IndexTree() [2/2]

TBTK::IndexTree::IndexTree ( const std::string &  serialization,
Mode  mode 
)

Constructor. Constructs the IndexTree from a serialization string.

Parameters
serializationSerialization string from which to construct the IndexTree.
modeMode with which the string has been serialized.

◆ ~IndexTree()

virtual TBTK::IndexTree::~IndexTree ( )
virtual

Destructor.

Member Function Documentation

◆ add()

void TBTK::IndexTree::add ( const Index index)

Add index to the IndexTree. The IndexTree allows for Indices with both non-negative as well as negative subindices to be added to the tree. For Index Indices with non-negative subindices the IndexTree simply adds the suplied Indices . Negative subindices with the value IDX_SEPARATOR are similarly used to add Indices consisting of multiple Indices . All other negative subindices are interpreted as wildcards. What this means is that the corresponding subindex will be marked as a wildcard and the wildcard type (IDX_ALL, IDX_SPIN, etc) is saved in the Index structure, which allows for Indices with arbitrary values in the corresponding subindex to be searched for using SearchMode::MatchWildrcards.

Any wildcard subindex will be replaced by zero and the full Index then stored in the Index structure. This means that for example {1, IDX_ALL, 3} will be stored as {1, 0, 3} in the IndexTree and will count as one towards the size of the Index tree. {1, 0, 3} is therefore also the Index that is returned when matching against {1, IDX_ALL, 3}.

Also note that it is a fatal error to add two Indices with similar Index structure but different wildcard type to the IndexTree. For example, it is invalid to simultaneously add {1, IDX_ALL, 3} and {1, IDX_SPIN, 4} to the IndexTree. However, it is OK to add {1, IDX_ALL, 3} and {2, IDX_SPIN, 4} since the index structure of the later pair is differentiated already at the first subindex.

Parameters
indexIndex to add.

◆ begin()

IndexTree::ConstIterator TBTK::IndexTree::begin ( ) const
inline

Get ConstIterator.

Returns
ConstIterator initialized to point at the first Index.

◆ cbegin()

IndexTree::ConstIterator TBTK::IndexTree::cbegin ( ) const
inline

Get ConstIterator.

Returns
ConstIterator initialized to point at the first Index.

◆ cend()

IndexTree::ConstIterator TBTK::IndexTree::cend ( ) const
inline

Create ConstIterator pointing to the end.

Returns
ConstIterator pointing to the end of the IndexTree.

◆ contains()

bool TBTK::IndexTree::contains ( const Index index)

Check whether a given Index is contained in the IndexTree.

Parameters
index
Returns
True if the IndexTree contains the given Index, otherwise false.

◆ end()

IndexTree::ConstIterator TBTK::IndexTree::end ( ) const
inline

Create ConstIterator pointing to the end.

Returns
ConstIterator pointing to the end of the IndexTree.

◆ equals()

bool TBTK::IndexTree::equals ( const IndexTree indexTree) const

Returns true if the two IndexTrees contain the same Indices .

Parameters
indexTreeIndexTree to compare to.
Returns
True if the two IndexTrees contain the same Indices .

◆ generateLinearMap()

void TBTK::IndexTree::generateLinearMap ( )

Generate a linear map from the Indices that has been added to the non-negative numbers.

Note that Indices that have been added with a wildcard subindex have the wildcards replaced by zero and the resulting Index is treated as a normal Index.

◆ getIndexList()

std::vector<Index> TBTK::IndexTree::getIndexList ( const Index pattern) const

Generate a list containing all the Indices in the IndexTree that satisfies the specified pattern.

Parameters
patternPattern to match against.
Returns
An std::vector<Index> containing all the Indices in the IndexTree that matches the specified pattern.

◆ getLinearIndex()

int TBTK::IndexTree::getLinearIndex ( const Index index,
SearchMode  searchMode = SearchMode::StrictMatch,
bool  returnNegativeForMissingIndex = false 
) const

Get the linear index corresponding to the given physical Index. The function can be executed in one of several different ways. First of all the function can be executed in one of the following modes.

StrictMatch:
A strict match between the suplied Index and the Index structure ecoded in the IndexTree is required. An Index added with a wildcard subindex must be retrieved with the same wildcard Index specified in the same subindex position. This is the standard mode of execution.
MatchWildcards:
Subindices marked as wildcard indices in the IndexTree are accepted independently of the subindex value in the Index passed to this function. For example, if {1, IDX_ALL, 3} has been added to the IndexTree, {1, 2, 3} will be accepted by the IndexTree as a valid Index. If a wildcard type is specified in the Index passed to this function, its type has to agree with that encoded in the IndexTree.

Second, the flag returnNegativeForMissingIndex can be set to true to make the function return -1 if an Index does not exists, or set to false to make the function throw an IndexException.

Parameters
indexThe Index to return the linear index for.
searchModeThe mode to use when searching for Indices.
returnNegativeForMissingIndexIf set to false, the request for a missing Index will result in an IndexException being thrown, but if set to true, a missing Index results in -1 being returned.
Returns
The linear Index for the given physical Index. Returns -1 if the Index is not found and returnNegativeForMissingIndex is set to true.
Exceptions
IndexExceptionIf the requested Index does not exist and returnNegativeForMissingIndex is set to false.

◆ getLinearMapIsGenerated()

bool TBTK::IndexTree::getLinearMapIsGenerated ( ) const
inline

Get whether the linear map has been generated.

Returns
True if the linear map has been generated.

◆ getPhysicalIndex()

Index TBTK::IndexTree::getPhysicalIndex ( int  linearIndex) const

Get physical index corresponding to given linear index.

Parameters
linearIndexLinear index.
Returns
The physical Index that corresponds to the given linear Index.

◆ getSize()

int TBTK::IndexTree::getSize ( ) const
inline

Get size.

Returns
The number of Indices stored in the IndexTree. Returns -1 if IndexTree::generateLinearMap() has not yet been called.

◆ getSubindicesMatching()

std::vector<unsigned int> TBTK::IndexTree::getSubindicesMatching ( int  subindexValue,
const Index index,
SearchMode  searchMode 
) const

First searches the IndexTree for a matching Index and then returns a list of subindiex positions that matches the given subindex value. If executed in SearchMode::StrictMatch, the function will simply return the matching subindices in the input Index itself. However, if executed in SearchMode::MatchWildcards, the IndexTree will be searched for an Index that is matching up to a wildcard, according to the specification of IndexTree::getLinearIndex(), and then perform the subindex matching on the resulting Index.

Example: If {1, IDX_ALL, 3, IDX_SPIN, 5, IDX_ALL} is stored in the IndexTree and the method is called as getSubindicesMatching( IDX_ALL, {1, 2, 3, 4, 5, 6}, IndexTree::SearchMode::MatchWildcards) then {1, IDX_ALL, 3, IDX_SPIN, 5, IDX_ALL} will first be found and then matched against the subindex value IDX_ALL, which results in a vector containing 1 and 5 being returned.

Parameters
subindexValueThe subindex value to match against.
indexThe Index to match against.
searchModeMode to use when matching 'index' against the Indices in the IndexTree.
Returns
An std::vector<int> containing the subindex positions for which subindexValue has the same value as the matched Index.

◆ serialize()

std::string TBTK::IndexTree::serialize ( Mode  mode) const
virtual

◆ toString()

virtual std::string TBTK::IndexTree::toString ( ) const
virtual

Implements Streamable::toString().

Implements TBTK::Streamable.

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const IndexTree lhs,
const IndexTree rhs 
)
friend

Inequality operator.

Parameters
lhsThe left hand side of the comparison operator.
rhsThe right hand side of the comparison operator.
Falseif the two IndexTrees describe identical Index structures, otherwise true.

◆ operator==

bool operator== ( const IndexTree lhs,
const IndexTree rhs 
)
friend

Comparison operator.

Parameters
lhsThe left hand side of the comparison operator.
rhsThe right hand side of the comparison operator.
Trueif the two IndexTrees describe identical Index structures, otherwise false.

The documentation for this class was generated from the following file: