TBTK
IndexDescriptor.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_DESCRIPTOR
24 #define COM_DAFER45_TBTK_INDEX_DESCRIPTOR
25 
26 #include "TBTK/IndexTree.h"
27 #include "TBTK/Serializable.h"
28 #include "TBTK/TBTKMacros.h"
29 
30 namespace TBTK{
31 
34 public:
36  enum class Format {None, Ranges, Custom};
37 
39  IndexDescriptor(Format format);
40 
42  IndexDescriptor(const IndexDescriptor &indexDescriptor);
43 
45  IndexDescriptor(IndexDescriptor &&indexDescriptor);
46 
49  IndexDescriptor(const std::string &serialization, Mode mode);
50 
53 
56 
59 
61  Format getFormat() const;
62 
64  void setDimensions(unsigned int dimensions);
65 
67  unsigned int getDimensions() const;
68 
70  void setRanges(const int *ranges, unsigned int dimensions);
71 
73 // int* getRanges();
74  std::vector<int> getRanges() const;
75 
77 // const int* getRanges() const;
78 
80  void setIndexTree(const IndexTree &indexTree);
81 
83  const IndexTree& getIndexTree() const;
84 
86  int getLinearIndex(
87  const Index &index,
88  bool returnNegativeForMissingIndex = false
89  ) const;
90 
92  unsigned int getSize() const;
93 
95  bool contains(const Index &index) const;
96 
98  virtual std::string serialize(Mode mode) const;
99 private:
101  Format format;
102 
103  class NoneFormat{
104  public:
105  };
106 
107  class RangeFormat{
108  public:
110  unsigned int dimensions;
111 
113  int *ranges;
114  };
115 
116  class CustomFormat{
117  public:
119  IndexTree *indexTree;
120  };
121 
123  union Descriptor{
124  NoneFormat noneFormat;
125  RangeFormat rangeFormat;
126  CustomFormat customFormat;
127  };
128 
130  Descriptor descriptor;
131 };
132 
134  return format;
135 }
136 
137 inline void IndexDescriptor::setDimensions(unsigned int dimensions){
138  TBTKAssert(
139  format == Format::Ranges,
140  "IndexDescriptor::setDimensions()",
141  "The IndexDescriptor is not of the format Format::Ranges.",
142  ""
143  );
144  descriptor.rangeFormat.dimensions = dimensions;
145  if(descriptor.rangeFormat.ranges != NULL)
146  delete [] descriptor.rangeFormat.ranges;
147  descriptor.rangeFormat.ranges = new int[dimensions];
148 }
149 
150 inline unsigned int IndexDescriptor::getDimensions() const{
151  TBTKAssert(
152  format == Format::Ranges,
153  "IndexDescriptor::getDimensions()",
154  "The IndexDescriptor is not of the format Format::Ranges.",
155  ""
156  );
157  return descriptor.rangeFormat.dimensions;
158 }
159 
160 inline void IndexDescriptor::setRanges(const int *ranges, unsigned int dimensions){
161  TBTKAssert(
162  format == Format::Ranges,
163  "IndexDescriptor::setRanges()",
164  "The IndexDescriptor is not of the format Format::Ranges.",
165  ""
166  );
167  descriptor.rangeFormat.dimensions = dimensions;
168  if(descriptor.rangeFormat.ranges != NULL)
169  delete [] descriptor.rangeFormat.ranges;
170  descriptor.rangeFormat.ranges = new int[dimensions];
171  for(unsigned int n = 0; n < dimensions; n++)
172  descriptor.rangeFormat.ranges[n] = ranges[n];
173 }
174 
175 //inline int* IndexDescriptor::getRanges(){
176 inline std::vector<int> IndexDescriptor::getRanges() const{
177  TBTKAssert(
178  format == Format::Ranges,
179  "IndexDescriptor::setDimensions()",
180  "The IndexDescriptor is not of the format Format::Ranges.",
181  ""
182  );
183 // return descriptor.rangeFormat.ranges;
184 
185  std::vector<int> ranges;
186  for(unsigned int n = 0; n < descriptor.rangeFormat.dimensions; n++)
187  ranges.push_back(descriptor.rangeFormat.ranges[n]);
188 
189  return ranges;
190 }
191 
192 /*inline const int* IndexDescriptor::getRanges() const{
193  TBTKAssert(
194  format == Format::Ranges,
195  "IndexDescriptor::setDimensions()",
196  "The IndexDescriptor is not of the format Format::Ranges.",
197  ""
198  );
199  return descriptor.rangeFormat.ranges;
200 }*/
201 
203  TBTKAssert(
204  format == Format::Custom,
205  "IndexDescriptor::getIndexTree()",
206  "The IndexDescriptor is not of the format Format::Custom.",
207  ""
208  );
209 
210  return *descriptor.customFormat.indexTree;
211 }
212 
214  const Index &index,
215  bool returnNegativeForMissingIndex
216 ) const{
217  TBTKAssert(
218  format == Format::Custom,
219  "IndexDescriptor::getOffset()",
220  "The IndexDescriptor is not of the format Format::Custom.",
221  ""
222  );
223 
224  return descriptor.customFormat.indexTree->getLinearIndex(
225  index,
226  IndexTree::SearchMode::MatchWildcards,
227  returnNegativeForMissingIndex
228  );
229 }
230 
231 inline bool IndexDescriptor::contains(const Index &index) const{
232  TBTKAssert(
233  format == Format::Custom,
234  "IndexDescriptor::contains()",
235  "The IndexDescriptor is not of the format Format::Custom.",
236  ""
237  );
238 
239  if(
240  descriptor.customFormat.indexTree->getLinearIndex(
241  index,
242  IndexTree::SearchMode::StrictMatch,
243  true
244  ) == -1
245  ){
246  return false;
247  }
248  else{
249  return true;
250  }
251 }
252 
253 }; //End namespace TBTK
254 
255 #endif
unsigned int getSize() const
IndexDescriptor(Format format)
IndexDescriptor & operator=(const IndexDescriptor &rhs)
Describes the index structure of data stored for several indices.
Definition: IndexDescriptor.h:33
const IndexTree & getIndexTree() const
Definition: IndexDescriptor.h:202
Precompiler macros.
Definition: Serializable.h:40
Data structure for mapping physical indices to a linear index.
std::vector< int > getRanges() const
Definition: IndexDescriptor.h:176
void setRanges(const int *ranges, unsigned int dimensions)
Definition: IndexDescriptor.h:160
virtual std::string serialize(Mode mode) const
Format
Definition: IndexDescriptor.h:36
Data structure for mapping physical indices to a linear index.
Definition: IndexTree.h:34
Flexible physical index.
Definition: Index.h:70
Definition: ModelFactory.h:35
unsigned int getDimensions() const
Definition: IndexDescriptor.h:150
Format getFormat() const
Definition: IndexDescriptor.h:133
void setDimensions(unsigned int dimensions)
Definition: IndexDescriptor.h:137
int getLinearIndex(const Index &index, bool returnNegativeForMissingIndex=false) const
Definition: IndexDescriptor.h:213
Mode
Definition: Serializable.h:44
Abstract base class for serializable objects.
void setIndexTree(const IndexTree &indexTree)
bool contains(const Index &index) const
Definition: IndexDescriptor.h:231