mercredi 30 novembre 2016

How to apply permutation to std::map values

I have a std::vector<T> and a std::map<U, std::size_t>, where the map acts as a different way to index the vector, i.e. the value in a key-value pair of the map is the index of a vector element. I can guarantee that not only the keys, but also the values of the map are unique.

Now when I apply a std::sort to the vector, that obviously changes the indices under which elements can be found, and I'd like to update my map accordingly.

I have found this answer for applying a permutation to a second vector. What I want to do is apply this permutation to the values of the map.

The following code does work, but is of course terribly inefficient

template <typename T, typename U>
std::vector<T> apply_permutation(const std::vector<T>& vec,
                                 std::map<U, std::size_t>& m,
                                 const std::vector<std::size_t>& p)
{
    std::vector<T> sorted_vec(vec.size());
    std::transform(p.begin(), p.end(), sorted_vec.begin(),
                   [&](std::size_t i){ return vec[i]; });
    for (std::size_t i(0); i != p.size(); ++i)
        for (auto it = m.begin(); it != m.end(); ++it)
            if (it->second == p[i])
                it->second = i;

    return sorted_vec;
}

I'm looking for a more efficient way to keep the vector and map in sync.

Aucun commentaire:

Enregistrer un commentaire