From 889b70d62d1b2fa974951866cb8bbebe66fce3a7 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Wed, 17 Apr 2019 00:36:04 +0300 Subject: libcamera: utils: Add set_overlap() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new set_overlap() function counts the number of overlapping elements in the intersection of two sorted ranges defined by their beginning and ending iterators. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- src/libcamera/include/utils.h | 19 +++++++++++++++++++ src/libcamera/utils.cpp | 12 ++++++++++++ 2 files changed, 31 insertions(+) (limited to 'src') diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 79038a96..97bd470a 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -26,6 +26,25 @@ std::unique_ptr make_unique(Args&&... args) char *secure_getenv(const char *name); +template +unsigned int set_overlap(InputIt1 first1, InputIt1 last1, + InputIt2 first2, InputIt2 last2) +{ + unsigned int count = 0; + + while (first1 != last1 && first2 != last2) { + if (*first1 < *first2) { + ++first1; + } else { + if (!(*first2 < *first1)) + count++; + ++first2; + } + } + + return count; +} + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index cd0fd761..e38f3268 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -68,6 +68,18 @@ char *secure_getenv(const char *name) * \brief Constructs an object of type T and wraps it in a std::unique_ptr. */ +/** + * \fn libcamera::utils::set_overlap(InputIt1 first1, InputIt1 last1, + * InputIt2 first2, InputIt2 last2) + * \brief Count the number of elements in the intersection of two ranges + * + * Count the number of elements in the intersection of the sorted ranges [\a + * first1, \a last1) and [\a first1, \a last2). Elements are compared using + * operator< and the ranges must be sorted with respect to the same. + * + * \return The number of elements in the intersection of the two ranges + */ + } /* namespace utils */ } /* namespace libcamera */ -- cgit v1.2.1