blob: a4568f3f84897feb2c68043586bb9ffa582371b8 [file] [log] [blame]
Alex Vakulenko677fd362014-08-20 16:39:18 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenkofed60b02015-10-27 09:53:05 -07005#ifndef LIBBRILLO_BRILLO_MAP_UTILS_H_
6#define LIBBRILLO_BRILLO_MAP_UTILS_H_
Alex Vakulenko677fd362014-08-20 16:39:18 -07007
8#include <map>
Alex Vakulenko1d734c22014-08-25 09:30:19 -07009#include <set>
Alex Vakulenko677fd362014-08-20 16:39:18 -070010#include <utility>
11#include <vector>
12
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070013namespace brillo {
Alex Vakulenko677fd362014-08-20 16:39:18 -070014
Alex Vakulenko1d734c22014-08-25 09:30:19 -070015// Given an STL map, returns a set containing all keys from the map.
Alex Vakulenko677fd362014-08-20 16:39:18 -070016template<typename T>
Alex Vakulenko847b8712014-08-29 10:43:06 -070017inline std::set<typename T::key_type> GetMapKeys(const T& map) {
Alex Vakulenko1d734c22014-08-25 09:30:19 -070018 std::set<typename T::key_type> keys;
Alex Vakulenko677fd362014-08-20 16:39:18 -070019 for (const auto& pair : map)
Alex Vakulenko1d734c22014-08-25 09:30:19 -070020 keys.insert(keys.end(), pair.first); // Map keys are already sorted.
Alex Vakulenko677fd362014-08-20 16:39:18 -070021 return keys;
22}
23
Alex Vakulenkof7fd94d2014-12-15 11:02:56 -080024// Given an STL map, returns a vector containing all keys from the map.
25// The keys in the vector are sorted.
26template<typename T>
27inline std::vector<typename T::key_type> GetMapKeysAsVector(const T& map) {
28 std::vector<typename T::key_type> keys;
29 keys.reserve(map.size());
30 for (const auto& pair : map)
31 keys.push_back(pair.first);
32 return keys;
33}
34
Alex Vakulenko1d734c22014-08-25 09:30:19 -070035// Given an STL map, returns a vector containing all values from the map.
Alex Vakulenko677fd362014-08-20 16:39:18 -070036template<typename T>
Alex Vakulenko847b8712014-08-29 10:43:06 -070037inline std::vector<typename T::mapped_type> GetMapValues(const T& map) {
Alex Vakulenko677fd362014-08-20 16:39:18 -070038 std::vector<typename T::mapped_type> values;
39 values.reserve(map.size());
40 for (const auto& pair : map)
41 values.push_back(pair.second);
42 return values;
43}
44
Alex Vakulenko1d734c22014-08-25 09:30:19 -070045// Given an STL map, returns a vector of key-value pairs from the map.
Alex Vakulenko677fd362014-08-20 16:39:18 -070046template<typename T>
Alex Vakulenko847b8712014-08-29 10:43:06 -070047inline std::vector<std::pair<typename T::key_type, typename T::mapped_type>>
Alex Vakulenko05d29042015-01-13 09:39:25 -080048MapToVector(const T& map) {
Alex Vakulenko677fd362014-08-20 16:39:18 -070049 std::vector<std::pair<typename T::key_type, typename T::mapped_type>> vector;
50 vector.reserve(map.size());
51 for (const auto& pair : map)
52 vector.push_back(pair);
53 return vector;
54}
55
Bertrand SIMONNET993233e2014-12-12 15:28:00 -080056// Given an STL map, returns the value associated with a given key or a default
57// value if the key is not present in the map.
58template<typename T>
Alex Vakulenko05d29042015-01-13 09:39:25 -080059inline typename T::mapped_type GetOrDefault(
60 const T& map,
61 typename T::key_type key,
62 const typename T::mapped_type& def) {
Bertrand SIMONNET993233e2014-12-12 15:28:00 -080063 typename T::const_iterator it = map.find(key);
64 if (it == map.end())
65 return def;
66 return it->second;
67}
68
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070069} // namespace brillo
Alex Vakulenko677fd362014-08-20 16:39:18 -070070
Alex Vakulenkofed60b02015-10-27 09:53:05 -070071#endif // LIBBRILLO_BRILLO_MAP_UTILS_H_