Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
lhaData.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef LHADATA_H_INCLUDED
24 #define LHADATA_H_INCLUDED
25 
26 #include "lhaBlocks.h"
28 #include <vector>
29 #include <optional>
30 #include <string>
31 #include <string_view>
32 
33 namespace mty::lha {
34 
43 BlockType blockType(std::string_view name);
52 std::ostream &operator<<(
53  std::ostream &out,
54  BlockType type
55  );
56 
60 using FloatType = long double;
61 
70 struct LHAElement {
71 
76  size_t id;
80  size_t id_sup;
85 };
86 
90 struct Comparator {
91 
109  static bool compare(
110  std::vector<size_t> const &pos1,
111  std::vector<size_t> const &pos2
112  )
113  {
114  if (pos1.size() != pos2.size())
115  return pos1.size() < pos2.size();
116  auto iter1 = pos1.begin();
117  auto iter2 = pos2.begin();
118  while (iter1 != pos1.end()) {
119  if (*iter1 < *iter2)
120  return true;
121  else if (*iter2 < *iter1)
122  return false;
123  ++iter1;
124  ++iter2;
125  }
126  return false;
127  }
128 
141  static bool compare(
142  LHAElement const &A,
143  LHAElement const &B
144  )
145  {
146  if (A.id != B.id)
147  return A.id < B.id;
148  return A.id_sup < B.id_sup;
149  }
150 };
151 
160 class LHABlock {
161 
162 public:
163 
164  IMPLEMENTS_STD_VECTOR(LHAElement, elements)
165 
166 
169  LHABlock() = default;
173  ~LHABlock() = default;
174 
180  LHABlock(std::string const &);
181 
185  std::string const &getName() const {
186  return name;
187  }
193  void setName(std::string const &t_name) {
194  name = t_name;
195  }
196 
197  bool isName(std::string_view t_name) const;
198 
208  std::optional<LHAElement> getElement(size_t id) const;
209 
221  std::optional<LHAElement> getElement(
222  size_t id,
223  size_t id_sup = -1
224  ) const;
225 
239  std::vector<LHAElement> getMultipleElements(size_t id) const;
240 
244  void sortElements();
245 
253  void addElement(
254  size_t id,
255  size_t id_sup,
257  );
264  void addElement(
265  size_t id,
266  FloatType value
267  );
268 
274  void addElement(LHAElement const &t_element);
275 
281  void addElement(LHAElement &&t_element);
282 
283 private:
284 
285 
289  std::string name;
293  std::vector<LHAElement> elements;
294 };
295 
302 class LHAFileData {
303 
304 public:
305 
306  static constexpr size_t npos = -1;
307 
308  IMPLEMENTS_STD_VECTOR(LHABlock, blocks)
309 
310 
313  LHAFileData() = default;
317  ~LHAFileData() = default;
318 
329  size_t findBlock(std::string_view nameBlock) const;
335  void addBlock(std::string_view nameBlock);
336 
348  template<class ...Args>
350  std::string_view nameBlock,
351  Args &&...args
352  )
353  {
354  for (auto &b : blocks)
355  if (b.getName() == nameBlock) {
356  b.addElement(std::forward<Args>(args)...);
357  return;
358  }
359  }
360 
369  std::vector<FloatType> getValues(
370  std::string_view nameBlock
371  ) const;
372 
382  std::optional<FloatType> getValue(
383  std::string_view nameBlock,
384  size_t id
385  ) const;
386 
397  std::optional<FloatType> getValue(
398  std::string_view nameBlock,
399  size_t i,
400  size_t j
401  ) const;
402 
403 private:
404 
408  std::vector<LHABlock> blocks;
409 };
410 
419 std::ostream &operator<<(
420  std::ostream &out,
421  LHAElement const &element
422  );
423 
432 std::ostream &operator<<(
433  std::ostream &out,
434  LHABlock const &block
435  );
436 
445 std::ostream &operator<<(
446  std::ostream &out,
447  LHAFileData const &data
448  );
449 
450 
451 std::string tolower(std::string const &str);
452 
453 } // End of namespace mty::lha
454 
455 #endif
LHABlock, containing a vector of LHAElement.
Definition: lhaData.h:160
static bool compare(std::vector< size_t > const &pos1, std::vector< size_t > const &pos2)
Compares two vectors of positions.
Definition: lhaData.h:109
size_t id
Id of the element in the block, or row position for a matrix element.
Definition: lhaData.h:76
FloatType value
Value of the element.
Definition: lhaData.h:84
std::ostream & operator<<(std::ostream &out, BlockType type)
Output stream overload for BlockType.
Definition: lhaData.cpp:58
size_t id_sup
Column position for a matrix element.
Definition: lhaData.h:80
Corresponds to a line of a .lha file.
Definition: lhaData.h:70
void setName(std::string const &t_name)
Sets the name of the block.
Definition: lhaData.h:193
static bool compare(LHAElement const &A, LHAElement const &B)
Compares two LHAElement.
Definition: lhaData.h:141
std::string const & getName() const
Definition: lhaData.h:185
std::vector< LHABlock > blocks
Vector of blocks representing the .lha file.
Definition: lhaData.h:408
BlockType blockType(std::string_view name)
Returns the BlockType corresponding to its name. The name must correspond to the enumeration name (se...
Definition: lhaData.cpp:30
long double FloatType
Floating point type for LHA support. Long double is 64 bits usually.
Definition: lhaData.h:60
Namespace containing all lha utilities.
Definition: lha.h:37
Final class containing all the data coming from a LHA file.
Definition: lhaData.h:302
std::string name
Name of the block.
Definition: lhaData.h:289
std::vector< LHAElement > elements
Vector of LHAElement in the block.
Definition: lhaData.h:293
void addElement(std::string_view nameBlock, Args &&...args)
Adds an element (LHAElement) to a block (LHABlock).
Definition: lhaData.h:349
Helper class to define a comparison function for positions.
Definition: lhaData.h:90
Contains an enumeration of LHA blocks.
BlockType
Enumeration of possible LHA blocks in MARTY.
Definition: lhaBlocks.h:31