TBTK
Need a break? Support the development by playing Polarity Puzzles
LookupTableMap.h
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 
17 
24 #ifndef COM_DAFER45_TBTK_LOOKUP_TABLE_MAP
25 #define COM_DAFER45_TBTK_LOOKUP_TABLE_MAP
26 
27 #include "TBTK/FockStateMap/FockStateMap.h"
28 #include "TBTK/BitRegister.h"
30 
31 namespace TBTK{
32 namespace FockStateMap{
33 
34 template<typename BIT_REGISTER>
35 class LookupTableMap : public FockStateMap<BIT_REGISTER>{
36 public:
38  LookupTableMap(unsigned int exponentialDimension);
39 
41  virtual ~LookupTableMap();
42 
44  virtual unsigned int getBasisSize() const;
45 
47  virtual unsigned int getBasisIndex(
48  const FockState<BIT_REGISTER> &fockState
49  ) const;
50 
52  virtual FockState<BIT_REGISTER> getFockState(unsigned int index) const;
53 
55  void addState(const FockState<BIT_REGISTER> &fockState);
56 private:
58  std::vector<FockState<BIT_REGISTER>> states;
59 };
60 
61 template<typename BIT_REGISTER>
62 LookupTableMap<BIT_REGISTER>::LookupTableMap(
63  unsigned int exponentialDimension
64 ) :
65  FockStateMap<BIT_REGISTER>(exponentialDimension)
66 {
67 }
68 
69 template<typename BIT_REGISTER>
70 LookupTableMap<BIT_REGISTER>::~LookupTableMap(){
71 }
72 
73 template<typename BIT_REGISTER>
74 unsigned int LookupTableMap<BIT_REGISTER>::getBasisSize() const{
75  return states.size();
76 }
77 
78 template<typename BIT_REGISTER>
79 unsigned int LookupTableMap<BIT_REGISTER>::getBasisIndex(
80  const FockState<BIT_REGISTER> &fockState
81 ) const{
82  unsigned int min = 0;
83  unsigned int max = states.size()-1;
84  while(min <= max){
85  unsigned int currentState = (min+max)/2;
86  if(fockState.getBitRegister() > states.at(currentState).getBitRegister())
87  min = currentState + 1;
88  else if(fockState.getBitRegister() < states.at(currentState).getBitRegister())
89  max = currentState - 1;
90  else if(fockState.getBitRegister() == states.at(currentState).getBitRegister())
91  return currentState;
92  }
93  TBTKExit(
94  "LookupTableFockStateMap<BIT_REGISTER>::getBasisIndex()",
95  "FockState not found.",
96  ""
97  );
98 }
99 
100 template<typename BIT_REGISTER>
101 FockState<BIT_REGISTER> LookupTableMap<BIT_REGISTER>::getFockState(
102  unsigned int index
103 ) const{
104  return states.at(index);
105 }
106 
107 template<typename BIT_REGISTER>
108 void LookupTableMap<BIT_REGISTER>::addState(
109  const FockState<BIT_REGISTER> &fockState
110 ){
111  if(
112  states.size() == 0
113  || states.back().getBitRegister() < fockState.getBitRegister()
114  ){
115  states.push_back(fockState);
116  }
117  else{
118  unsigned int min = 0;
119  unsigned int max = states.size()-1;
120  while(min <= max){
121  unsigned int currentState = (min+max)/2;
122  if(
123  fockState.getBitRegister()
124  > states.at(currentState).getBitRegister()
125  ){
126  min = currentState + 1;
127  }
128  else if(
129  fockState.getBitRegister()
130  < states.at(currentState).getBitRegister()
131  ){
132  max = currentState - 1;
133  }
134 
135  if(min >= max){
136  if(
137  fockState.getBitRegister()
138  < states.at(
139  currentState
140  ).getBitRegister()
141  ){
142  states.insert(
143  states.begin() + currentState,
144  fockState
145  );
146  }
147  else{
148  states.insert(
149  states.begin() + currentState
150  + 1,
151  fockState
152  );
153  }
154  break;
155  }
156  }
157  }
158 }
159 
160 }; //End of namespace FockStateMap
161 }; //End of namespace TBTK
162 
163 #endif
164 
Register of bits.
DataType max(const Array< DataType > &array)
Definition: ArrayAlgorithms.h:841
Register of bits.
DataType min(const Array< DataType > &array)
Definition: ArrayAlgorithms.h:851
Definition: Boolean.h:32