Skip to content
Snippets Groups Projects
Commit 23ea8f7f authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add simox::apply

parent c3a8f767
No related branches found
No related tags found
No related merge requests found
......@@ -82,6 +82,7 @@ SET(INCLUDES
EigenStdVector.h
algorithm/for_each_if.h
algorithm/apply.hpp
color/cmaps/colormaps.h
color/cmaps/Named.h
......
#pragma once
#include <functional>
#include <map>
#include <vector>
namespace simox
{
template <typename ValueIn, typename ValueOut>
ValueOut apply(const ValueIn& value, const std::function<ValueIn(ValueOut)>& op)
{
return op(value);
}
template <typename ValueIn, typename ValueOut>
std::vector<ValueOut> apply(const std::vector<ValueIn>& vector, const std::function<ValueIn(ValueOut)>& op)
{
std::vector<ValueOut> result;
std::transform(vector.begin(), vector.end(), std::back_inserter(result), [&op](const auto& v)
{
return apply(v, op);
});
return result;
}
template <typename ValueIn, typename ValueOut, typename Key>
std::map<Key, ValueOut> apply(const std::map<Key, ValueIn>& map, const std::function<ValueIn(ValueOut)>& op)
{
std::map<Key, ValueOut> result;
for (const auto& [name, value] : map)
{
result[name] = apply(value, op);
}
return result;
}
}
......@@ -4,6 +4,8 @@
#include <map>
#include <string>
#include <SimoxUtility/algorithm/apply.hpp>
#include "Color.h"
#include "interpolation.h"
......@@ -56,10 +58,10 @@ namespace simox::color
/// Apply this colormap to a vector.
template <typename V>
std::vector<Color> operator()(const std::vector<V>& vector) const;
std::vector<Color> operator()(const std::vector<V>& vector) const { return simox::apply(vector, *this); }
/// Apply this colormap to a map.
template <typename K, typename V>
std::map<K, Color> operator()(const std::map<K, V>& map) const;
std::map<K, Color> operator()(const std::map<K, V>& map) const { return simox::apply(map, *this); }
std::string name() const { return _name; }
void setName(const std::string& name) { this->_name = name; }
......@@ -99,26 +101,6 @@ namespace simox::color
};
template<typename V>
std::vector<Color> ColorMap::operator()(const std::vector<V>& vector) const
{
std::vector<Color> result;
std::transform(vector.begin(), vector.end(), std::back_inserter(result), *this);
return result;
}
template<typename K, typename V>
std::map<K, Color> ColorMap::operator()(const std::map<K, V>& map) const
{
std::map<K, Color> result;
for (const auto& [name, value] : map)
{
result[name] = (*this)(value);
}
return result;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment