[libcamera-devel,v3,1/2] libcamera: utils: Add map_keys() function

Message ID 20200702231107.1800602-2-niklas.soderlund@ragnatech.se
State Accepted
Commit c58bec935c3adffac9d99b1675a563d7f2e9ae04
Headers show
Series
  • libcamera: utils: Add map_keys() function
Related show

Commit Message

Niklas Söderlund July 2, 2020, 11:11 p.m. UTC
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Add a map_keys() function to the utils namespace to extract keys from a
map.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
[Niklas: change return type to std::vector instead of std::set]
Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 include/libcamera/internal/utils.h | 10 ++++++++++
 src/libcamera/utils.cpp            |  7 +++++++
 test/utils.cpp                     | 22 ++++++++++++++++++++++
 3 files changed, 39 insertions(+)

Patch

diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h
index 0953423ee8d04466..8d026cc6c0fe777a 100644
--- a/include/libcamera/internal/utils.h
+++ b/include/libcamera/internal/utils.h
@@ -15,6 +15,7 @@ 
 #include <string>
 #include <string.h>
 #include <sys/time.h>
+#include <vector>
 
 #define ARRAY_SIZE(a)	(sizeof(a) / sizeof(a[0]))
 
@@ -36,6 +37,15 @@  const char *basename(const char *path);
 char *secure_getenv(const char *name);
 std::string dirname(const std::string &path);
 
+template<typename T>
+std::vector<typename T::key_type> map_keys(const T &map)
+{
+	std::vector<typename T::key_type> keys;
+	std::transform(map.begin(), map.end(), std::back_inserter(keys),
+		       [](const auto &value) { return value.first; });
+	return keys;
+}
+
 template<class InputIt1, class InputIt2>
 unsigned int set_overlap(InputIt1 first1, InputIt1 last1,
 			 InputIt2 first2, InputIt2 last2)
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index d55338fe681a8516..0567328fe31ba6f9 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -127,6 +127,13 @@  std::string dirname(const std::string &path)
 	return path.substr(0, pos + 1);
 }
 
+/**
+ * \fn std::vector<typename T::key_type> map_keys(const T &map)
+ * \brief Retrieve the keys of a std::map<>
+ * \param[in] map The map whose keys to retrieve
+ * \return A std::vector<> containing the keys of \a map
+ */
+
 /**
  * \fn libcamera::utils::set_overlap(InputIt1 first1, InputIt1 last1,
  *				     InputIt2 first2, InputIt2 last2)
diff --git a/test/utils.cpp b/test/utils.cpp
index 66b91f1203e1f4e8..f482e6a1d829edbd 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -6,6 +6,7 @@ 
  */
 
 #include <iostream>
+#include <map>
 #include <sstream>
 #include <string>
 #include <vector>
@@ -144,6 +145,27 @@  protected:
 		if (TestPass != testDirname())
 			return TestFail;
 
+
+		/* utils::map_keys() test. */
+		const std::map<std::string, unsigned int> map{
+			{ "zero", 0 },
+			{ "one", 1 },
+			{ "two", 2 },
+		};
+		std::vector<std::string> expectedKeys{
+			"zero",
+			"one",
+			"two",
+		};
+
+		std::sort(expectedKeys.begin(), expectedKeys.end());
+
+		const std::vector<std::string> keys = utils::map_keys(map);
+		if (keys != expectedKeys) {
+			cerr << "utils::map_keys() test failed" << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}
 };