Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -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 | |
| 5 | #ifndef BUFFET_MAP_UTILS_H_ |
| 6 | #define BUFFET_MAP_UTILS_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <vector> |
| 10 | |
| 11 | namespace chromeos { |
| 12 | |
| 13 | // Given an STL map returns a vector containing all keys from the map |
| 14 | template<typename T> |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 15 | std::vector<typename T::key_type> GetMapKeys(const T& map) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 16 | std::vector<typename T::key_type> keys; |
| 17 | keys.reserve(map.size()); |
| 18 | for (auto&& pair : map) |
| 19 | keys.push_back(pair.first); |
| 20 | return keys; |
| 21 | } |
| 22 | |
| 23 | // Given an STL map returns a vector containing all values from the map |
| 24 | template<typename T> |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 25 | std::vector<typename T::mapped_type> GetMapValues(const T& map) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 26 | std::vector<typename T::mapped_type> values; |
| 27 | values.reserve(map.size()); |
| 28 | for (auto&& pair : map) |
| 29 | values.push_back(pair.second); |
| 30 | return values; |
| 31 | } |
| 32 | |
| 33 | // Given an STL map returns a vector of key-value pairs from the map |
| 34 | template<typename T> |
| 35 | std::vector<std::pair<typename T::key_type, |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 36 | typename T::mapped_type>> MapToVector(const T& map) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 37 | std::vector<std::pair<typename T::key_type, typename T::mapped_type>> vector; |
| 38 | vector.reserve(map.size()); |
| 39 | for (auto&& pair : map) |
| 40 | vector.push_back(pair); |
| 41 | return vector; |
| 42 | } |
| 43 | |
| 44 | } // namespace chromeos |
| 45 | |
| 46 | #endif // BUFFET_MAP_UTILS_H_ |