sparcians / map

Modeling Architectural Platform
Apache License 2.0
164 stars 58 forks source link

Cool map flipper #25

Closed ghost closed 4 years ago

ghost commented 4 years ago

This would be nice to add to the Utils.hpp:


    // Function to invert a maps or an unordered_map, or any type of
    // class that has key/value semantics.  The variadic template
    // argument is necessary 'cause map/unordered_map don't have just
    // 2 template parameters.  ;)
    template <typename K, typename V, typename... TArgs, template<typename...> class MapT>
    MapT<V, K> flip_map(const MapT<K, V, TArgs...> & map)
    {
        MapT<V, K> inv;
        for(auto [key, value] : map) {
            inv.insert(std::make_pair(value, key));
        }
        return inv;
    }
avinabadasgupta commented 4 years ago

This could potentially flip maps into multimaps. What is the use case for this? Can we do away with boost bimap?

klingaard commented 4 years ago

Use case: I have an std::map<std::string, MyEnumType>. I want to get a flipped map for printing.

You know my stance on Boost. ;)

avinabadasgupta commented 4 years ago

Okay, your values are guaranteed to be unique. Then I don't think there will be a problem of returning multimaps.