From patchwork Thu Jun 25 01:23:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 4201 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 57FD2C0100 for ; Thu, 25 Jun 2020 01:23:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2427D609C6; Thu, 25 Jun 2020 03:23:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="pPTIeehc"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F1868603BE for ; Thu, 25 Jun 2020 03:23:40 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 235AF2A8 for ; Thu, 25 Jun 2020 03:23:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1593048220; bh=ToMwbWqqMERI3NElole+0jhlwow0O7H4xs+ONQChtqM=; h=From:To:Subject:Date:From; b=pPTIeehcwuGz+KUAFDSbmqXz5iaW0ZRgxoi2aSbmrEHraI00cCgablG8nwfK2TB/V LMcgqOpUDCqtuc94JtjhKYXOkMzWVCNtmcgRnF/PTI3EGvStyFdbeO6JSOUOuiSypM U3kDQM1RKjrE2+2O19g8TSPowpZFkPCIG14v4CXY= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 25 Jun 2020 04:23:35 +0300 Message-Id: <20200625012335.7717-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: utils: Add map_keys() function X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a map_keys() function to the utils namespace to extract keys from a map. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- include/libcamera/internal/utils.h | 10 ++++++++++ src/libcamera/utils.cpp | 7 +++++++ test/utils.cpp | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index 0953423ee8d0..4fe4843922a9 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,15 @@ const char *basename(const char *path); char *secure_getenv(const char *name); std::string dirname(const std::string &path); +template +std::set map_keys(const T &map) +{ + std::set keys; + std::transform(map.begin(), map.end(), std::inserter(keys, keys.end()), + [](const auto &value) { return value.first; }); + return keys; +} + template 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 d55338fe681a..1eb8970a5bbc 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::set 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::set<> 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 66b91f1203e1..a9d072030f1c 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -6,6 +6,8 @@ */ #include +#include +#include #include #include #include @@ -144,6 +146,25 @@ protected: if (TestPass != testDirname()) return TestFail; + + /* utils::map_keys() test. */ + const std::map map{ + { "zero", 0 }, + { "one", 1 }, + { "two", 2 }, + }; + const std::set expectedKeys{ + "zero", + "one", + "two", + }; + + const std::set keys = utils::map_keys(map); + if (keys != expectedKeys) { + cerr << "utils::map_keys() test failed" << endl; + return TestFail; + } + return TestPass; } };