Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 1 | // 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 Vakulenko | fed60b0 | 2015-10-27 09:53:05 -0700 | [diff] [blame] | 5 | #ifndef LIBBRILLO_BRILLO_MAP_UTILS_H_ |
| 6 | #define LIBBRILLO_BRILLO_MAP_UTILS_H_ |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
Alex Vakulenko | 1d734c2 | 2014-08-25 09:30:19 -0700 | [diff] [blame] | 9 | #include <set> |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 13 | namespace brillo { |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | 1d734c2 | 2014-08-25 09:30:19 -0700 | [diff] [blame] | 15 | // Given an STL map, returns a set containing all keys from the map. |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 16 | template<typename T> |
Alex Vakulenko | 847b871 | 2014-08-29 10:43:06 -0700 | [diff] [blame] | 17 | inline std::set<typename T::key_type> GetMapKeys(const T& map) { |
Alex Vakulenko | 1d734c2 | 2014-08-25 09:30:19 -0700 | [diff] [blame] | 18 | std::set<typename T::key_type> keys; |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 19 | for (const auto& pair : map) |
Alex Vakulenko | 1d734c2 | 2014-08-25 09:30:19 -0700 | [diff] [blame] | 20 | keys.insert(keys.end(), pair.first); // Map keys are already sorted. |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 21 | return keys; |
| 22 | } |
| 23 | |
Alex Vakulenko | f7fd94d | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 24 | // Given an STL map, returns a vector containing all keys from the map. |
| 25 | // The keys in the vector are sorted. |
| 26 | template<typename T> |
| 27 | inline 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 Vakulenko | 1d734c2 | 2014-08-25 09:30:19 -0700 | [diff] [blame] | 35 | // Given an STL map, returns a vector containing all values from the map. |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 36 | template<typename T> |
Alex Vakulenko | 847b871 | 2014-08-29 10:43:06 -0700 | [diff] [blame] | 37 | inline std::vector<typename T::mapped_type> GetMapValues(const T& map) { |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 38 | 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 Vakulenko | 1d734c2 | 2014-08-25 09:30:19 -0700 | [diff] [blame] | 45 | // Given an STL map, returns a vector of key-value pairs from the map. |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 46 | template<typename T> |
Alex Vakulenko | 847b871 | 2014-08-29 10:43:06 -0700 | [diff] [blame] | 47 | inline std::vector<std::pair<typename T::key_type, typename T::mapped_type>> |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame] | 48 | MapToVector(const T& map) { |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 49 | 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 SIMONNET | 993233e | 2014-12-12 15:28:00 -0800 | [diff] [blame] | 56 | // 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. |
| 58 | template<typename T> |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame] | 59 | inline typename T::mapped_type GetOrDefault( |
| 60 | const T& map, |
| 61 | typename T::key_type key, |
| 62 | const typename T::mapped_type& def) { |
Bertrand SIMONNET | 993233e | 2014-12-12 15:28:00 -0800 | [diff] [blame] | 63 | typename T::const_iterator it = map.find(key); |
| 64 | if (it == map.end()) |
| 65 | return def; |
| 66 | return it->second; |
| 67 | } |
| 68 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 69 | } // namespace brillo |
Alex Vakulenko | 677fd36 | 2014-08-20 16:39:18 -0700 | [diff] [blame] | 70 | |
Alex Vakulenko | fed60b0 | 2015-10-27 09:53:05 -0700 | [diff] [blame] | 71 | #endif // LIBBRILLO_BRILLO_MAP_UTILS_H_ |