Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Base/Flip_map.hpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.,
7 : : 2022-2025 J. Bakosi
8 : : All rights reserved. See the LICENSE file for details.
9 : : \brief Flip a std::map yielding a multimap sorted by std::map::value_type
10 : : \details Flip a std::map yielding a multimap sorted by std::map::value_type.
11 : : Credit goes to Oli Charlesworth: http://stackoverflow.com/a/5056797
12 : : */
13 : : // *****************************************************************************
14 : : #ifndef Flip_map_h
15 : : #define Flip_map_h
16 : :
17 : : namespace tk {
18 : :
19 : : //! Flip a std::pair of arbitrary types
20 : : //! \param[in] p std::pair of arbitrary types, A and B
21 : : //! \return std::pair of arbitrary types, B and A
22 : : template< typename A, typename B >
23 : 3 : std::pair< B, A > flip_pair( const std::pair< A ,B >& p )
24 : 3 : { return std::pair< B, A >( p.second, p.first ); }
25 : :
26 : : //! Flip a std::map of arbitrary types, yielding a std::multimap sorted by
27 : : //! std::map::value_type.
28 : : //! \param[in] src std::map of arbitrary key and value pairs of types A and B
29 : : //! \return std::multimap of arbitrary key and value pairs of types B and A
30 : : template< typename A, typename B >
31 [ + - ]: 1 : std::multimap< B, A > flip_map( const std::map< A, B >& src ) {
32 : : std::multimap< B, A > dst;
33 [ + - ]: 1 : std::transform( src.begin(), src.end(), std::inserter(dst, dst.begin()),
34 : : flip_pair< A ,B > );
35 : 1 : return dst;
36 : : }
37 : :
38 : : } // tk::
39 : :
40 : : #endif // Flip_map_h
|