BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
path.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/model/path.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "mvvm/model/path.h"
16 #include <algorithm>
17 #include <iterator>
18 #include <numeric>
19 #include <sstream>
20 
21 using namespace ModelView;
22 
23 //! Constructs Path object from string containing sequence of integers ("0,0,1,3").
24 
25 Path Path::fromString(const std::string& str)
26 {
27  Path result;
28 
29  std::string str_spaces(str);
30  std::replace(str_spaces.begin(), str_spaces.end(), ',', ' ');
31 
32  std::istringstream iss(str_spaces);
33  std::for_each(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
34  [&result](auto x) { result.append(std::stoi(x)); });
35  return result;
36 }
37 
38 //! Constructs Path object from vector of integers..
39 
40 Path Path::fromVector(const std::vector<int>& data)
41 {
42  Path result;
43  std::for_each(data.begin(), data.end(), [&result](auto x) { result.append(x); });
44  return result;
45 }
46 
47 //! Returns string representing path ("0,0,1,3").
48 
49 std::string Path::str() const
50 {
51  auto comma_fold = [](std::string a, int b) { return std::move(a) + ',' + std::to_string(b); };
52  return m_data.empty() ? std::string()
53  : std::accumulate(std::next(m_data.begin()), m_data.end(),
54  std::to_string(m_data[0]), comma_fold);
55 }
56 
58 {
59  m_data.push_back(element);
60 }
61 
63 {
64  m_data.insert(m_data.begin(), element);
65 }
66 
68 {
69  return m_data.begin();
70 }
71 
73 {
74  return m_data.begin();
75 }
76 
78 {
79  return m_data.end();
80 }
81 
83 {
84  return m_data.end();
85 }
Supports navigation through SessionModel.
Definition: path.h:35
void prepend(PathElement element)
Definition: path.cpp:62
static Path fromVector(const std::vector< int > &data)
Constructs Path object from vector of integers..
Definition: path.cpp:40
static Path fromString(const std::string &str)
Constructs Path object from string containing sequence of integers ("0,0,1,3").
Definition: path.cpp:25
std::string str() const
Returns string representing path ("0,0,1,3").
Definition: path.cpp:49
container_t::iterator iterator
Definition: path.h:39
container_t::const_iterator const_iterator
Definition: path.h:40
int PathElement
Definition: path.h:37
iterator end()
Definition: path.cpp:77
iterator begin()
Definition: path.cpp:67
void append(PathElement element)
Definition: path.cpp:57
container_t m_data
Definition: path.h:60
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?