map_utils.h 2.01 KB
Newer Older
1
/*
2
 *  Copyright (C) 2017-2019 Savoir-faire Linux Inc.
3
 *
4
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

21
#pragma once
22 23

#include <vector>
24 25 26
#include <iterator>
#include <algorithm>
#include <tuple>
27

Adrien Béraud's avatar
Adrien Béraud committed
28
namespace jami { namespace map_utils {
29

30 31 32
///< Return the N-th type of a tuple type used as the Container compliant value type
template <typename C, std::size_t N>
using type_element = typename std::remove_cv<typename std::tuple_element<N, typename C::value_type>::type>::type;
33

34 35 36 37
///< Extract in a std::vector object each N-th values of tuples contained in a Container compliant object \a container.
template <std::size_t N, typename C>
inline std::vector<type_element<C, N>>
extractElements(const C& container)
38
{
39 40 41 42 43 44 45
    std::vector<type_element<C, N>> result;
    if (container.size() > 0) {
        result.resize(container.size());
        auto iter = std::begin(container);
        std::generate(std::begin(result), std::end(result), [&]{ return std::get<N>(*iter++); });
    }
    return result;
46 47
}

48 49 50 51
template <typename M>
inline auto
extractKeys(const M& map) -> decltype(extractElements<0>(map))
{ return extractElements<0>(map); }
52

53 54 55 56
template <typename M>
inline auto
extractValues(const M& map) -> decltype(extractElements<1>(map))
{ return extractElements<1>(map); }
57

Adrien Béraud's avatar
Adrien Béraud committed
58
}} // namespace jami::map_utils