TBTK
Need a break? Support the development by playing Polarity Puzzles
PatternValidator.h
1 /* Copyright 2019 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_PROPERTY_EXTRACTOR_PATTERN_VALIDATOR
24 #define COM_DAFER45_TBTK_PROPERTY_EXTRACTOR_PATTERN_VALIDATOR
25 
26 #include "TBTK/Index.h"
27 
28 #include <string>
29 
30 namespace TBTK{
31 namespace PropertyExtractor{
32 
75 public:
78 
79  //TBTKFeature PropertyExtractor.PatternValidator.checkNumRequiredComponentIndices.1 2019-11-01
80  //TBTKFeature PropertyExtractor.PatternValidator.checkNumRequiredComponentIndices.2 2019-11-01
81  //TBTKFeature PropertyExtractor.PatternValidator.checkNumRequiredComponentIndices.3 2019-11-01
89  void setNumRequiredComponentIndices(int numRequiredComponentIndices);
90 
91  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.1 2019-11-01
92  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.2 2019-11-01
93  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.3 2019-11-01
94  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.4 2019-11-01
95  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.5 2019-11-01
100  const std::vector<Subindex> &allowedSubindexFlags
101  );
102 
107  void setCallingFunctionName(const std::string &callingFunctionsName);
108 
109  //TBTKFeature PropertyExtractor.PatternValidator.checkNumRequiredComponentIndices.1 2019-11-01
110  //TBTKFeature PropertyExtractor.PatternValidator.checkNumRequiredComponentIndices.2 2019-11-01
111  //TBTKFeature PropertyExtractor.PatternValidator.checkNumRequiredComponentIndices.3 2019-11-01
112  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.1 2019-11-01
113  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.2 2019-11-01
114  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.3 2019-11-01
115  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.4 2019-11-01
116  //TBTKFeature PropertyExtractor.PatternValidator.checkAllowedSubindexFlags.5 2019-11-01
118  void validate(const std::vector<Index> &patterns) const;
119 private:
121  int numRequiredComponentIndices;
122 
124  std::vector<Subindex> allowedSubindexFlags;
125 
127  std::string callingFunctionName;
128 
131  void validateNumRequiredComponentIndices(
132  const std::vector<Index> &patterns
133  ) const;
134 
138  void validateAllowedSubindexFlags(
139  const std::vector<Index> &patterns
140  ) const;
141 };
142 
144  numRequiredComponentIndices = -1;
145 }
146 
148  int numRequiredComponentIndices
149 ){
150  this->numRequiredComponentIndices = numRequiredComponentIndices;
151 }
152 
154  const std::vector<Subindex> &allowedSubindexFlags
155 ){
156  this->allowedSubindexFlags = allowedSubindexFlags;
157 }
158 
160  const std::string &callingFunctionName
161 ){
162  this->callingFunctionName = callingFunctionName;
163 }
164 
166  const std::vector<Index> &patterns
167 ) const{
168  if(numRequiredComponentIndices != -1)
169  validateNumRequiredComponentIndices(patterns);
170  validateAllowedSubindexFlags(patterns);
171 }
172 
173 inline void PatternValidator::validateNumRequiredComponentIndices(
174  const std::vector<Index> &patterns
175 ) const{
176  for(unsigned int n = 0; n < patterns.size(); n++){
177  TBTKAssert(
178  patterns[n].split().size()
179  == (unsigned int)numRequiredComponentIndices,
180  callingFunctionName,
181  "Unexpected number of pattern component Indices. The"
182  << " pattern was expected to have '"
183  << numRequiredComponentIndices << "', but the pattern"
184  << " '" << patterns[n].toString() << "' has '"
185  << patterns[n].split().size() << "' components.",
186  ""
187  );
188  }
189 }
190 
191 inline void PatternValidator::validateAllowedSubindexFlags(
192  const std::vector<Index> &patterns
193 ) const{
194  for(unsigned int n = 0; n < patterns.size(); n++){
195  std::vector<Index> components = patterns[n].split();
196  for(unsigned int m = 0; m < components.size(); m++){
197  for(unsigned int c = 0; c < components[m].getSize(); c++){
198  int subindex = components[m][c];
199  if(subindex < 0){
200  bool isValid = false;
201  for(
202  unsigned int k = 0;
203  k < allowedSubindexFlags.size();
204  k++
205  ){
206  if(
207  subindex
208  == allowedSubindexFlags[k]
209  ){
210  isValid = true;
211  break;
212  }
213  }
214 
215  if(!isValid){
216  TBTKExit(
217  callingFunctionName,
218  "Invalid subindex at"
219  << " position '" << c
220  << "' in component"
221  << " Index '" << m
222  << "' of the pattern '"
223  << patterns[n].toString()
224  << "'.",
225  ""
226  );
227  }
228  }
229  }
230  }
231  }
232 }
233 
234 }; //End of namespace PropertyExtractor
235 }; //End of namespace TBTK
236 
237 #endif
Physical index.
void validate(const std::vector< Index > &patterns) const
Definition: PatternValidator.h:165
void setCallingFunctionName(const std::string &callingFunctionsName)
Definition: PatternValidator.h:159
void setNumRequiredComponentIndices(int numRequiredComponentIndices)
Definition: PatternValidator.h:147
Definition: Boolean.h:32
Validates patterns that are passed to PropertyExtractor calls.
Definition: PatternValidator.h:74
PatternValidator()
Definition: PatternValidator.h:143
void setAllowedSubindexFlags(const std::vector< Subindex > &allowedSubindexFlags)
Definition: PatternValidator.h:153