TBTK
Need a break? Support the development by playing Polarity Puzzles
AnnotatedArray.h
Go to the documentation of this file.
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_ANNOTATED_ARRAY
24 #define COM_DAFER45_TBTK_ANNOTATED_ARRAY
25 
26 #include "TBTK/Array.h"
27 
28 namespace TBTK{
29 
40 template<typename DataType, typename AxesType>
41 class AnnotatedArray : public Array<DataType>{
42 public:
45 
51  const Array<DataType> &array,
52  const std::vector<std::vector<AxesType>> &axes
53  );
54 
62  const std::string &serialization,
64  );
65 
69  const std::vector<std::vector<AxesType>>& getAxes() const;
70 
72  std::string serialize(Serializable::Mode mode) const;
73 private:
75  std::vector<std::vector<AxesType>> axes;
76 };
77 
78 template<typename DataType, typename AxesType>
80 }
81 
82 template<typename DataType, typename AxesType>
84  const Array<DataType> &array,
85  const std::vector<std::vector<AxesType>> &axes
86 ) :
87  Array<DataType>(array),
88  axes(axes)
89 {
90  const std::vector<unsigned int> &ranges = array.getRanges();
91  TBTKAssert(
92  ranges.size() == axes.size(),
93  "AnnotatedArray::AnnotatedArray()",
94  "Incompatible dimensions. 'array' has '" << ranges.size()
95  << "' dimensions, while 'axes' have '" << axes.size() << "'"
96  << " dimensions.",
97  ""
98  );
99  for(unsigned int n = 0; n < ranges.size(); n++){
100  TBTKAssert(
101  ranges[n] == axes[n].size(),
102  "AnnotatedArray::AnnotatedArray()",
103  "Incompatible sizes. Dimension '" << n << " of 'array'"
104  << " has size '" << ranges[n] << "', but axis '" << n
105  << "' has size '" << axes[n].size() << "'.",
106  ""
107  );
108  }
109 }
110 
111 template<typename DataType, typename AxesType>
113  const std::string &serialization,
114  Serializable::Mode mode
115 ) :
116  Array<DataType>(
117  Serializable::extractComponent(
118  serialization,
119  "AnnotatedArray",
120  "Array",
121  "array",
122  mode
123  ),
124  mode
125  )
126 {
127  TBTKAssert(
128  Serializable::validate(serialization, "AnnotatedArray", mode),
129  "AnnotatedArray::AnnotatedArray()",
130  "Unable to parse string as AnnotatedArray '" << serialization
131  << "'.",
132  ""
133  );
134 
135  switch(mode){
136  case Serializable::Mode::JSON:
137  {
138  try{
139  nlohmann::json j
140  = nlohmann::json::parse(serialization);
141  nlohmann::json jsonAxes = j.at("axes");
142  for(
143  nlohmann::json::iterator axisIterator
144  = jsonAxes.begin();
145  axisIterator != jsonAxes.end();
146  ++axisIterator
147  ){
148  unsigned int axisId
149  = atoi(axisIterator.key().c_str());
150  for(unsigned int n = axes.size(); n <= axisId; n++)
151  axes.push_back(std::vector<AxesType>());
152  for(
153  nlohmann::json::iterator iterator
154  = axisIterator->begin();
155  iterator != axisIterator->end();
156  ++iterator
157  ){
158  axes.back().push_back(
160  AxesType
161  >(*iterator, mode)
162  );
163  }
164  }
165  }
166  catch(nlohmann::json::exception &e){
167  TBTKExit(
168  "AnnotatedArray::AnnotatedArray()",
169  "Unable to parse string as AnnotatedArray '"
170  << serialization << "'.",
171  ""
172  );
173  }
174 
175  break;
176  }
177  default:
178  TBTKExit(
179  "AnnotatedArray::AnnotatedArray()",
180  "Unable to parse string as AnnotatedArray '"
181  << serialization << "'.",
182  ""
183  );
184  }
185 }
186 
187 template<typename DataType, typename AxesType>
188 const std::vector<std::vector<AxesType>>& AnnotatedArray<
189  DataType,
190  AxesType
191 >::getAxes() const{
192  return axes;
193 }
194 
195 template<typename DataType, typename AxesType>
197  Serializable::Mode mode
198 ) const{
199  switch(mode){
200  case Serializable::Mode::JSON:
201  {
202  nlohmann::json j;
203  j["id"] = "AnnotatedArray";
204  j["array"] = nlohmann::json::parse(
206  );
207  j["axes"] = nlohmann::json();
208  for(unsigned int n = 0; n < axes.size(); n++){
209  std::string index = std::to_string(n);
210  j["axes"][index] = nlohmann::json();
211  for(unsigned int c = 0; c < axes[n].size(); c++){
212  j["axes"][index].push_back(
214  axes[n][c],
215  mode
216  )
217  );
218  }
219  }
220 
221  return j.dump();
222  }
223  default:
224  TBTKExit(
225  "AnnotatedArray::serialize()",
226  "Only Serializable::Mode::JSON is supported yet.",
227  ""
228  );
229  }
230 }
231 
232 }; //End of namesapce TBTK
233 
234 #endif
TBTK::Array
Multi-dimensional array.
Definition: Array.h:98
TBTK::AnnotatedArray
Array with additional information about its axes.
Definition: AnnotatedArray.h:41
TBTK::Serializable::validate
static bool validate(const std::string &serialization, const std::string &id, Mode mode)
Array.h
Multi-dimensional array.
TBTK::AnnotatedArray::getAxes
const std::vector< std::vector< AxesType > > & getAxes() const
Definition: AnnotatedArray.h:191
TBTK::Serializable::serialize
virtual std::string serialize(Mode mode) const =0
TBTK::Serializable
Definition: Serializable.h:43
TBTK::AnnotatedArray::AnnotatedArray
AnnotatedArray()
Definition: AnnotatedArray.h:79
TBTK::Array::getRanges
const std::vector< unsigned int > & getRanges() const
Definition: Array.h:1228
TBTK::AnnotatedArray::serialize
std::string serialize(Serializable::Mode mode) const
Definition: AnnotatedArray.h:196
TBTK::Serializable::Mode
Mode
Definition: Serializable.h:47
TBTK::Serializable::deserialize
static DataType deserialize(const std::string &serialization, Mode mode)
Definition: Serializable.h:272