BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
containerutils.h
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/utils/containerutils.h
6 //! @brief Defines 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 #ifndef BORNAGAIN_MVVM_MODEL_MVVM_UTILS_CONTAINERUTILS_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_UTILS_CONTAINERUTILS_H
17 
18 #include "mvvm/model_export.h"
19 #include <algorithm>
20 #include <complex>
21 #include <iterator>
22 #include <memory>
23 #include <string>
24 #include <type_traits>
25 #include <unordered_set>
26 #include <vector>
27 
28 namespace ModelView {
29 
30 namespace Utils {
31 
32 template <class T> struct is_unique_ptr : std::false_type {
33 };
34 
35 template <class T, class D> struct is_unique_ptr<std::unique_ptr<T, D>> : std::true_type {
36 };
37 
38 //! Returns index corresponding to the first occurance of the item in the container.
39 //! If item doesn't exist, will return -1. Works with containers containing unique_ptr.
40 
41 template <typename It, typename T> int IndexOfItem(It begin, It end, const T& item)
42 {
43  It pos;
44  if constexpr (is_unique_ptr<typename std::iterator_traits<It>::value_type>::value)
45  pos = find_if(begin, end, [&item](const auto& x) { return x.get() == item; });
46  else
47  pos = find_if(begin, end, [&item](const auto& x) { return x == item; });
48 
49  return pos == end ? -1 : static_cast<int>(std::distance(begin, pos));
50 }
51 
52 //! Returns index corresponding to the first occurance of the item in the container.
53 //! If item doesn't exist, will return -1. Works with containers containing unique_ptr.
54 
55 template <typename C, typename T> int IndexOfItem(const C& container, const T& item)
56 {
57  return IndexOfItem(container.begin(), container.end(), item);
58 }
59 
60 //! Returns vector containing results of elemntwise unary function application.
61 
62 template <typename It, typename UnaryFunction>
63 std::vector<double> Apply(It begin, It end, UnaryFunction func)
64 {
65  std::vector<double> result;
66  std::transform(begin, end, std::back_inserter(result), func);
67  return result;
68 }
69 
70 //! Returns vector with real part of complex numbers.
71 
72 template <typename C> std::vector<double> Real(const C& container)
73 {
74  return Apply(std::begin(container), std::end(container),
75  [](const auto& x) { return std::real(x); });
76 }
77 
78 //! Returns vector with imag part of complex numbers.
79 
80 template <typename C> std::vector<double> Imag(const C& container)
81 {
82  return Apply(std::begin(container), std::end(container),
83  [](const auto& x) { return std::imag(x); });
84 }
85 
86 //! Returns copy of container with all duplicated elements filtered our. The order is preserved.
87 
88 template <typename C> C UniqueWithOrder(const C& container)
89 {
90  C result;
91 
92  using valueType = typename C::value_type;
93  std::unordered_set<valueType> unique;
94 
95  std::copy_if(container.begin(), container.end(), std::back_inserter(result),
96  [&unique](auto x) { return unique.insert(x).second; });
97 
98  return result;
99 }
100 
101 //! Returns true if container contains a given element.
102 
103 template <typename A, typename B> bool Contains(const A& container, const B& element)
104 {
105  return std::find(container.begin(), container.end(), element) != container.end();
106 }
107 
108 } // namespace Utils
109 
110 } // namespace ModelView
111 
112 #endif // BORNAGAIN_MVVM_MODEL_MVVM_UTILS_CONTAINERUTILS_H
bool Contains(const A &container, const B &element)
Returns true if container contains a given element.
std::vector< double > Real(const C &container)
Returns vector with real part of complex numbers.
C UniqueWithOrder(const C &container)
Returns copy of container with all duplicated elements filtered our. The order is preserved.
std::vector< double > Apply(It begin, It end, UnaryFunction func)
Returns vector containing results of elemntwise unary function application.
int IndexOfItem(It begin, It end, const T &item)
Returns index corresponding to the first occurance of the item in the container.
std::vector< double > Imag(const C &container)
Returns vector with imag part of complex numbers.
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81