From patchwork Fri Apr 23 02:09:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12086 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com 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 016D1BDB1A for ; Fri, 23 Apr 2021 02:09:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 72B8268875; Fri, 23 Apr 2021 04:09:44 +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="eefE2yGf"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8E00C68861 for ; Fri, 23 Apr 2021 04:09:41 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1587A564 for ; Fri, 23 Apr 2021 04:09:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1619143781; bh=gwxpHarXEzslxbmx64EyvNGa+6qWoCnFGckQuWyZRqM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=eefE2yGfGrRUIaKSWZDIW+4JigCZnrHkbCpmx4ht5evwcKVnMa7EmrtvJQGaoUFPW JlD0JWeU2iKlWMtu24zdAXufi/rIdb6TC8O7h4/2Fmn8oPVeM/bb15WgFnNFD6SD3z tM+96L/AcDlGWjyaj86bWEL/lrWoSWQpP24bsVI4= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 23 Apr 2021 05:09:30 +0300 Message-Id: <20210423020932.2760-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 In-Reply-To: <20210423020932.2760-1-laurent.pinchart@ideasonboard.com> References: <20210423020932.2760-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH/RFC 1/3] libcamera: utils: Add enumerate view for range-based for loops 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" Range-based for loops are handy and widely preferred in C++, but are limited in their ability to replace for loops that require access to a loop counter. The enumerate() function solves this problem by wrapping the \a iterable in an adapter that, when used as a range-expression, will provide iterators whose value_type is a pair of index and value reference. The iterable must support std::begin() and std::end(). This includes all containers provided by the standard C++ library, as well as C-style arrays. A typical usage pattern would use structured binding to store the index and value in two separate variables: std::vector values = ...; for (auto [index, value] : utils::enumerate(values)) { ... } Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- include/libcamera/internal/utils.h | 86 ++++++++++++++++++++++++++++++ src/libcamera/utils.cpp | 29 ++++++++++ test/utils.cpp | 59 ++++++++++++++++++++ 3 files changed, 174 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index d0146b71727d..9372a75889ac 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -9,12 +9,14 @@ #include #include +#include #include #include #include #include #include #include +#include #include #ifndef __DOXYGEN__ @@ -230,6 +232,90 @@ details::reverse_adapter reverse(T &&iterable) return { iterable }; } +namespace details { + +template +class enumerate_iterator +{ +private: + using base_reference = typename std::iterator_traits::reference; + +public: + using difference_type = typename std::iterator_traits::difference_type; + using value_type = std::pair; + using pointer = value_type *; + using reference = value_type &; + using iterator_category = typename std::iterator_traits::iterator_category; + + explicit enumerate_iterator(Base iter) + : current_(iter), pos_(0) + { + } + + enumerate_iterator &operator++() + { + ++current_; + ++pos_; + return *this; + } + + bool operator!=(const enumerate_iterator &other) const + { + return current_ != other.current_; + } + + value_type operator*() const + { + return { pos_, *current_ }; + } + +private: + Base current_; + difference_type pos_; +}; + +template +class enumerate_adapter +{ +public: + using iterator = enumerate_iterator; + + enumerate_adapter(Base begin, Base end) + : begin_(begin), end_(end) + { + } + + iterator begin() + { + return iterator{ begin_ }; + } + + iterator end() + { + return iterator{ end_ }; + } + +private: + Base begin_; + Base end_; +}; + +} /* namespace details */ + +template +auto enumerate(T &iterable) -> details::enumerate_adapter +{ + return { std::begin(iterable), std::end(iterable) }; +} + +#ifndef __DOXYGEN__ +template +auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter +{ + return { std::begin(iterable), std::end(iterable) }; +} +#endif + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index c4098a74e0ab..ff9a5832b10e 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -472,6 +472,35 @@ std::string libcameraSourcePath() * loop, will cause the loop to iterate over the \a iterable in reverse order */ +/** + * \fn enumerate(T &iterable) + * \brief Wrap an iterable to enumerate index and value in a range-based loop + * \param[in] iterable The iterable + * + * Range-based for loops are handy and widely preferred in C++, but are limited + * in their ability to replace for loops that require access to a loop counter. + * The enumerate() function solves this problem by wrapping the \a iterable in + * an adapter that, when used as a range-expression, will provide iterators + * whose value_type is a pair of index and value reference. + * + * The iterable must support std::begin() and std::end(). This includes all + * containers provided by the standard C++ library, as well as C-style arrays. + * + * A typical usage pattern would use structured binding to store the index and + * value in two separate variables: + * + * \code{.cpp} + * std::vector values = ...; + * + * for (auto [index, value] : utils::enumerate(values)) { + * ... + * } + * \endcode + * + * \return A value of unspecified type that, when used in a range-based for + * loop, iterates over an indexed view of the \a iterable + */ + } /* namespace utils */ } /* namespace libcamera */ diff --git a/test/utils.cpp b/test/utils.cpp index 08f293898fd9..7e24c71e4775 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -12,6 +12,7 @@ #include #include +#include #include "libcamera/internal/utils.h" @@ -73,6 +74,60 @@ protected: return TestPass; } + int testEnumerate() + { + std::vector integers{ 1, 2, 3, 4, 5 }; + int i = 0; + + for (auto [index, value] : utils::enumerate(integers)) { + if (index != i || value != i + 1) { + cerr << "utils::enumerate() test failed: i=" << i + << ", index=" << index << ", value=" << value + << std::endl; + return TestFail; + } + + /* Verify that we can modify the value. */ + --value; + ++i; + } + + if (integers != std::vector{ 0, 1, 2, 3, 4 }) { + cerr << "Failed to modify container in enumerated range loop" << endl; + return TestFail; + } + + Span span{ integers }; + i = 0; + + for (auto [index, value] : utils::enumerate(span)) { + if (index != i || value != i) { + cerr << "utils::enumerate() test failed: i=" << i + << ", index=" << index << ", value=" << value + << std::endl; + return TestFail; + } + + ++i; + } + + const int array[] = { 0, 2, 4, 6, 8 }; + i = 0; + + for (auto [index, value] : utils::enumerate(array)) { + if (index != i || value != i * 2) { + cerr << "utils::enumerate() test failed: i=" << i + << ", index=" << index << ", value=" << value + << std::endl; + return TestFail; + } + + ++i; + } + + return TestPass; + } + int run() { /* utils::hex() test. */ @@ -177,6 +232,10 @@ protected: return TestFail; } + /* utils::enumerate() test. */ + if (testEnumerate() != TestPass) + return TestFail; + return TestPass; } };