12 #ifndef HIPIPE_CORE_INDEX_MAPPER_HPP
13 #define HIPIPE_CORE_INDEX_MAPPER_HPP
15 #include <range/v3/range/conversion.hpp>
16 #include <range/v3/view/transform.hpp>
18 #include <unordered_map>
23 namespace rgv = ranges::views;
35 template<
typename Rng>
54 throw std::out_of_range{
"The index_mapper does not contain the given value."};
56 return val2idx_.at(val);
60 std::size_t
index_for(
const T& val, std::size_t defval)
const
62 auto pos = val2idx_.find(val);
63 if (pos == val2idx_.end())
return defval;
69 std::vector<std::size_t>
index_for(
const std::vector<T>& vals)
const
72 vals, [
this](
const T& val) {
78 std::vector<std::size_t>
index_for(
const std::vector<T>& vals, std::size_t defval)
const
81 vals, [
this, defval](
const T& val) {
90 const T&
at(
const std::size_t& idx)
const
93 throw std::out_of_range{
"Index " +
std::to_string(idx) +
" cannot be found in "
102 std::vector<T>
at(
const std::vector<std::size_t>& idxs)
const
105 idxs, [
this](std::size_t idx) {
106 return this->
at(idx);
119 throw std::invalid_argument{
"The element is already present in the index mapper."};
121 val2idx_[val] = idx2val_.size();
122 idx2val_.push_back(std::move(val));
123 return idx2val_.size() - 1;
132 CPP_template(
class Rng)(requires ranges::container<Rng>)
135 for (
auto& val : rng)
insert(std::move(val));
142 for (
auto&& val : rng)
insert(std::forward<decltype(val)>(val));
162 CPP_template(
class Rng)(requires ranges::container<Rng>)
165 for (
auto& val : rng)
try_insert(std::move(val));
172 for (
auto&& val : rng)
try_insert(std::forward<decltype(val)>(val));
180 return val2idx_.count(val);
184 std::size_t
size()
const
186 return val2idx_.size();
192 const std::vector<T>&
values()
const
198 std::unordered_map<T, std::size_t> val2idx_;
199 std::vector<T> idx2val_;
214 template<
typename Rng,
typename T = ranges::range_value_t<Rng>>