Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com> 5 : * 6 : * This program is free software; you can redistribute it and/or modify 7 : * it under the terms of the GNU General Public License as published by 8 : * the Free Software Foundation; either version 3 of the License, or 9 : * (at your option) any later version. 10 : * 11 : * This program is distributed in the hope that it will be useful, 12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : * GNU General Public License for more details. 15 : * 16 : * You should have received a copy of the GNU General Public License 17 : * along with this program; if not, write to the Free Software 18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 : */ 20 : 21 : #pragma once 22 : 23 : #include <vector> 24 : #include <iterator> 25 : #include <algorithm> 26 : #include <tuple> 27 : 28 : namespace jami { 29 : namespace map_utils { 30 : 31 : ///< Return the N-th type of a tuple type used as the Container compliant value type 32 : template<typename C, std::size_t N> 33 : using type_element = 34 : typename std::remove_cv<typename std::tuple_element<N, typename C::value_type>::type>::type; 35 : 36 : ///< Extract in a std::vector object each N-th values of tuples contained in a Container compliant 37 : ///< object \a container. 38 : template<std::size_t N, typename C> 39 : inline std::vector<type_element<C, N>> 40 2 : extractElements(const C& container) 41 : { 42 2 : std::vector<type_element<C, N>> result; 43 2 : if (container.size() > 0) { 44 2 : result.resize(container.size()); 45 2 : auto iter = std::begin(container); 46 17 : std::generate(std::begin(result), std::end(result), [&] { return std::get<N>(*iter++); }); 47 : } 48 2 : return result; 49 0 : } 50 : 51 : template<typename M> 52 : inline auto 53 1 : extractKeys(const M& map) -> decltype(extractElements<0>(map)) 54 : { 55 1 : return extractElements<0>(map); 56 : } 57 : 58 : template<typename M> 59 : inline auto 60 1 : extractValues(const M& map) -> decltype(extractElements<1>(map)) 61 : { 62 1 : return extractElements<1>(map); 63 : } 64 : 65 : } // namespace map_utils 66 : } // namespace jami