blob: f5e3f303a1ff9ee1f7fb184e895614433461e851 [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -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
5#ifndef BUFFET_MAP_UTILS_H_
6#define BUFFET_MAP_UTILS_H_
7
8#include <map>
Alex Vakulenkoaf23b322014-05-08 16:25:45 -07009#include <utility>
Chris Sosa45d9f102014-03-24 11:18:54 -070010#include <vector>
11
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070012namespace buffet {
Chris Sosa45d9f102014-03-24 11:18:54 -070013
14// Given an STL map returns a vector containing all keys from the map
15template<typename T>
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070016std::vector<typename T::key_type> GetMapKeys(const T& map) {
Chris Sosa45d9f102014-03-24 11:18:54 -070017 std::vector<typename T::key_type> keys;
18 keys.reserve(map.size());
Alex Vakulenkoa0424dd2014-06-13 16:10:17 -070019 for (const auto& pair : map)
Chris Sosa45d9f102014-03-24 11:18:54 -070020 keys.push_back(pair.first);
21 return keys;
22}
23
24// Given an STL map returns a vector containing all values from the map
25template<typename T>
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070026std::vector<typename T::mapped_type> GetMapValues(const T& map) {
Chris Sosa45d9f102014-03-24 11:18:54 -070027 std::vector<typename T::mapped_type> values;
28 values.reserve(map.size());
Alex Vakulenkoa0424dd2014-06-13 16:10:17 -070029 for (const auto& pair : map)
Chris Sosa45d9f102014-03-24 11:18:54 -070030 values.push_back(pair.second);
31 return values;
32}
33
34// Given an STL map returns a vector of key-value pairs from the map
35template<typename T>
36std::vector<std::pair<typename T::key_type,
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070037 typename T::mapped_type>> MapToVector(const T& map) {
Chris Sosa45d9f102014-03-24 11:18:54 -070038 std::vector<std::pair<typename T::key_type, typename T::mapped_type>> vector;
39 vector.reserve(map.size());
Alex Vakulenkoa0424dd2014-06-13 16:10:17 -070040 for (const auto& pair : map)
Chris Sosa45d9f102014-03-24 11:18:54 -070041 vector.push_back(pair);
42 return vector;
43}
44
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070045} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -070046
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070047#endif // BUFFET_MAP_UTILS_H_