15 #ifndef BORNAGAIN_MVVM_MODEL_MVVM_UTILS_CONTAINERUTILS_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_UTILS_CONTAINERUTILS_H
18 #include "mvvm/model_export.h"
24 #include <type_traits>
25 #include <unordered_set>
35 template <
class T,
class D>
struct is_unique_ptr<
std::unique_ptr<T, D>> : std::true_type {
41 template <
typename It,
typename T>
int IndexOfItem(It begin, It end,
const T& item)
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; });
47 pos = find_if(begin, end, [&item](
const auto& x) {
return x == item; });
49 return pos == end ? -1 :
static_cast<int>(std::distance(begin, pos));
55 template <
typename C,
typename T>
int IndexOfItem(
const C& container,
const T& item)
57 return IndexOfItem(container.begin(), container.end(), item);
62 template <
typename It,
typename UnaryFunction>
63 std::vector<double>
Apply(It begin, It end, UnaryFunction func)
65 std::vector<double> result;
66 std::transform(begin, end, std::back_inserter(result), func);
72 template <
typename C> std::vector<double>
Real(
const C& container)
74 return Apply(std::begin(container), std::end(container),
75 [](
const auto& x) {
return std::real(x); });
80 template <
typename C> std::vector<double>
Imag(
const C& container)
82 return Apply(std::begin(container), std::end(container),
83 [](
const auto& x) {
return std::imag(x); });
92 using valueType =
typename C::value_type;
93 std::unordered_set<valueType> unique;
95 std::copy_if(container.begin(), container.end(), std::back_inserter(result),
96 [&unique](
auto x) { return unique.insert(x).second; });
103 template <
typename A,
typename B>
bool Contains(
const A& container,
const B& element)
105 return std::find(container.begin(), container.end(), element) != container.end();
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.