TBTK
IndexTree.h
Go to the documentation of this file.
1 /* Copyright 2017 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_TREE
24 #define COM_DAFER45_TBTK_INDEX_TREE
25 
26 #include "TBTK/Index.h"
27 #include "TBTK/Serializable.h"
28 
29 #include <vector>
30 
31 namespace TBTK{
32 
34 class IndexTree : public Serializable{
35 public:
37  IndexTree();
38 
45  IndexTree(const std::string &serialization, Mode mode);
46 
48  virtual ~IndexTree();
49 
80  void add(const Index &index);
81 
88  void generateLinearMap();
89 
93  bool getLinearMapIsGenerated() const;
94 
97  enum class SearchMode{StrictMatch, /*IgnoreWildcards,*/ MatchWildcards};
98 
135  int getLinearIndex(
136  const Index &index,
137  SearchMode searchMode = SearchMode::StrictMatch,
138  bool returnNegativeForMissingIndex = false
139  ) const;
140 
147  Index getPhysicalIndex(int linearIndex) const;
148 
154  int getSize() const;
155 
179  std::vector<unsigned int> getSubindicesMatching(
180  int subindexValue,
181  const Index &index,
182  SearchMode searchMode
183  ) const;
184 
193  std::vector<Index> getIndexList(const Index &pattern) const;
194 
202  bool equals(const IndexTree &indexTree) const;
203 
204 // class Iterator;
205  class ConstIterator;
206 private:
209  template<bool isConstIterator>
210  class _Iterator{
211  public:
214  typedef typename std::conditional<
215  isConstIterator,
216  const Index,
217  Index
218  >::type IndexType;
219 
221  void operator++();
222 
224  IndexType operator*();
225 
227  bool operator==(const _Iterator &rhs) const;
228 
230  bool operator!=(const _Iterator &rhs) const;
231 
233 // void reset();
234 
236 // void searchNext();
237 
239 // const Index* getIndex() const;
240 // const Index getIndex() const;
241 
243 // bool getHasReachedEnd() const;
244  private:
247  typedef typename std::conditional<
248  isConstIterator,
249  const IndexTree*,
250  IndexTree*
251  >::type IndexTreePointerType;
252 
254  IndexTreePointerType indexTree;
255 
257  std::vector<int> currentIndex;
258 
264  bool skipNextIndex;
265 
267 // bool hasReachedEnd;
268 
270  _Iterator(IndexTreePointerType indexTree, bool end = false);
271 
275  bool searchNext(
276  const IndexTree *indexTree,
277  unsigned int subindex
278  );
279 
281  friend class Iterator;
282  friend class ConstIterator;
283  };
284 public:
287  class ConstIterator : public _Iterator<true>{
288  private:
290  const IndexTree *indexTree,
291  bool end = false
292  ) : _Iterator<true>(indexTree, end){}
293 
295  friend class IndexTree;
296  };
297 
301  ConstIterator begin() const;
302 
306  ConstIterator cbegin() const;
307 
311  ConstIterator end() const;
312 
316  ConstIterator cend() const;
317 
319  std::string serialize(Mode mode) const;
320 private:
322  std::vector<IndexTree> children;
323 
326  bool indexIncluded;
327 
330  bool wildcardIndex;
331 
334  int wildcardType;
335 
339  bool indexSeparator;
340 
342  int linearIndex;
343 
345  int size;
346 
349  void add(const Index& index, unsigned int subindex);
350 
353  int generateLinearMap(int i);
354 
357  int getLinearIndex(
358  const Index &index,
359  unsigned int subindex,
360  SearchMode searchMode,
361  bool returnNegativeForMissingIndex
362  ) const;
363 
366  void getPhysicalIndex(
367  int linearIndex,
368  std::vector<int> *indices
369  ) const;
370 
372  int getMinIndex() const;
373 
375  int getMaxIndex() const;
376 };
377 
379  if(size == -1)
380  return false;
381  else
382  return true;
383 }
384 
386  return ConstIterator(this);
387 }
388 
390  return ConstIterator(this);
391 }
392 
394  return ConstIterator(this, true);
395 }
396 
398  return ConstIterator(this, true);
399 }
400 
401 inline int IndexTree::getSize() const{
402  return size;
403 }
404 
405 /*inline bool IndexTree::Iterator::getHasReachedEnd() const{
406  return hasReachedEnd;
407 }*/
408 
409 template<bool isConstIterator>
410 IndexTree::_Iterator<isConstIterator>::_Iterator(IndexTreePointerType indexTree, bool end){
411  if(end){
412  this->indexTree = indexTree;
413  currentIndex.push_back(indexTree->children.size());
414  }
415  else{
416  this->indexTree = indexTree;
417  currentIndex.push_back(0);
418  skipNextIndex = false;
419 // hasReachedEnd = false;
420 // if(!searchNext(this->indexTree, 0))
421 // hasReachedEnd = true;
422  searchNext(this->indexTree, 0);
423  }
424 }
425 
426 /*void IndexTree::Iterator::reset(){
427  currentIndex.clear();
428  currentIndex.push_back(0);
429  skipNextIndex = false;
430  hasReachedEnd = false;
431  if(!searchNext(indexTree, 0))
432  hasReachedEnd = true;
433 }*/
434 
435 /*void IndexTree::Iterator::searchNext(){
436  skipNextIndex = true;
437  if(!searchNext(indexTree, 0))
438  hasReachedEnd = true;
439 }*/
440 
441 template<bool isConstIterator>
442 void IndexTree::_Iterator<isConstIterator>::operator++(){
443  skipNextIndex = true;
444 // if(!searchNext(indexTree, 0))
445 // hasReachedEnd = true;
446  searchNext(indexTree, 0);
447 }
448 
449 template<bool isConstIterator>
450 bool IndexTree::_Iterator<isConstIterator>::searchNext(
451  const IndexTree *indexTree,
452  unsigned int subindex
453 ){
454  if(indexTree->children.size() == 0){
455  if(indexTree->indexIncluded){
456  if(skipNextIndex)
457  skipNextIndex = false;
458  else
459  return true;
460  }
461  }
462 
463  unsigned int n = currentIndex.at(subindex);
464  while(n < indexTree->children.size()){
465  if(subindex+1 == currentIndex.size())
466  currentIndex.push_back(0);
467  if(searchNext(&indexTree->children.at(n), subindex+1))
468  return true;
469 
470  currentIndex.pop_back();
471  n = ++currentIndex.back();
472  }
473 
474  return false;
475 }
476 
477 //const Index* IndexTree::Iterator::getIndex() const{
478 /*const Index IndexTree::Iterator::getIndex() const{
479 // if(currentIndex.at(0) == (int)indexTree->children.size())
480 // return NULL;
481  if(currentIndex.at(0) == (int)indexTree->children.size())
482  return Index();
483 
484 // Index *index = new Index({});
485 // Index *index = new Index();
486  Index index;
487 
488  const IndexTree *indexTreeBranch = this->indexTree;
489  for(unsigned int n = 0; n < currentIndex.size()-1; n++){
490 // if(indexTreeBranch->indexSeparator)
491 // index->push_back(IDX_SEPARATOR);
492  if(indexTreeBranch->indexSeparator)
493  index.push_back(IDX_SEPARATOR);
494 
495 // if(indexTreeBranch->wildcardIndex)
496 // index->push_back(indexTreeBranch->wildcardType);
497 // else
498 // index->push_back(currentIndex.at(n));
499  if(indexTreeBranch->wildcardIndex)
500  index.push_back(indexTreeBranch->wildcardType);
501  else
502  index.push_back(currentIndex.at(n));
503 
504  if(n < currentIndex.size()-1)
505  indexTreeBranch = &indexTreeBranch->children.at(currentIndex.at(n));
506  }
507 
508  return index;
509 }*/
510 
511 template<bool isConstIterator>
512 typename IndexTree::_Iterator<isConstIterator>::IndexType IndexTree::_Iterator<isConstIterator>::operator*(){
513  if(currentIndex.at(0) == (int)indexTree->children.size()){
514  TBTKExit(
515  "IndexTree::_Iterator<isConstIterator>::operator*()",
516  "Out of range access. Tried to access an element using"
517  << " an Iterator that points beyond the last element.",
518  ""
519  );
520  }
521 
522  Index index;
523 
524  const IndexTree *indexTreeBranch = this->indexTree;
525  for(unsigned int n = 0; n < currentIndex.size()-1; n++){
526  if(indexTreeBranch->indexSeparator)
527  index.push_back(IDX_SEPARATOR);
528 
529  if(indexTreeBranch->wildcardIndex)
530  index.push_back(indexTreeBranch->wildcardType);
531  else
532  index.push_back(currentIndex.at(n));
533 
534  if(n < currentIndex.size()-1)
535  indexTreeBranch = &indexTreeBranch->children.at(currentIndex.at(n));
536  }
537 
538  return index;
539 }
540 
541 template<bool isConstIterator>
542 bool IndexTree::_Iterator<isConstIterator>::operator==(const _Iterator &rhs) const{
543  if(indexTree != rhs.indexTree)
544  return false;
545 
546  if(currentIndex.size() != rhs.currentIndex.size())
547  return false;
548 
549  for(unsigned int n = 0; n < currentIndex.size(); n++)
550  if(currentIndex[n] != rhs.currentIndex[n])
551  return false;
552 
553  return true;
554 };
555 
556 template<bool isConstIterator>
557 bool IndexTree::_Iterator<isConstIterator>::operator!=(const _Iterator &rhs) const{
558  return !operator==(rhs);
559 }
560 
561 }; //End of namesapce TBTK
562 
563 #endif
564 
int getSize() const
Definition: IndexTree.h:401
FockStateRuleSet operator*(const LadderOperator< BitRegister > &ladderOperator, const FockStateRuleSet &fockStateRuleSet)
Definition: FockStateRuleSet.h:103
Flexible physical index.
virtual ~IndexTree()
ConstIterator cbegin() const
Definition: IndexTree.h:389
ConstIterator begin() const
Definition: IndexTree.h:385
Definition: Serializable.h:40
bool equals(const IndexTree &indexTree) const
friend class Index
Definition: Serializable.h:172
std::string serialize(Mode mode) const
std::vector< unsigned int > getSubindicesMatching(int subindexValue, const Index &index, SearchMode searchMode) const
SearchMode
Definition: IndexTree.h:97
void add(const Index &index)
Index getPhysicalIndex(int linearIndex) const
void push_back(int subindex)
Definition: Index.h:367
ConstIterator end() const
Definition: IndexTree.h:393
int getLinearIndex(const Index &index, SearchMode searchMode=SearchMode::StrictMatch, bool returnNegativeForMissingIndex=false) const
ConstIterator cend() const
Definition: IndexTree.h:397
bool getLinearMapIsGenerated() const
Definition: IndexTree.h:378
Data structure for mapping physical indices to linear indices.
Definition: IndexTree.h:34
Flexible physical index.
Definition: Index.h:69
Definition: IndexTree.h:287
void generateLinearMap()
Definition: ModelFactory.h:35
Mode
Definition: Serializable.h:44
Abstract base class for serializable objects.
std::vector< Index > getIndexList(const Index &pattern) const