[{"id":24320,"web_url":"https://patchwork.libcamera.org/comment/24320/","msgid":"<20220803065208.jcfcls7gobrygi7s@uno.localdomain>","date":"2022-08-03T06:52:08","subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_pixelformat:\n\tImplement std::hash specialization","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi LAurent,\n\nOn Tue, Aug 02, 2022 at 10:47:05PM +0300, Laurent Pinchart wrote:\n> Inject a specialization of std::hash<> for the V4L2PixelFormat class in\n> the std namespace to make it possible to store instances of the class in\n> the std::unordered_map and std::unordered_set containers.\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nI started with the same, then I thought polluting the std namespace\nwas not a good idea ? Do you think it's not an issue ?\n\n> ---\n>  include/libcamera/internal/v4l2_pixelformat.h | 13 +++++++++++++\n>  1 file changed, 13 insertions(+)\n>\n> diff --git a/include/libcamera/internal/v4l2_pixelformat.h b/include/libcamera/internal/v4l2_pixelformat.h\n> index d5400f90a67e..fcc1dadd4de0 100644\n> --- a/include/libcamera/internal/v4l2_pixelformat.h\n> +++ b/include/libcamera/internal/v4l2_pixelformat.h\n> @@ -8,6 +8,7 @@\n>\n>  #pragma once\n>\n> +#include <functional>\n>  #include <ostream>\n>  #include <stdint.h>\n>  #include <string>\n> @@ -55,3 +56,15 @@ private:\n>  std::ostream &operator<<(std::ostream &out, const V4L2PixelFormat &f);\n>\n>  } /* namespace libcamera */\n> +\n> +namespace std {\n> +\n> +template<>\n> +struct hash<libcamera::V4L2PixelFormat> {\n> +\tsize_t operator()(libcamera::V4L2PixelFormat const &format) const noexcept\n> +\t{\n> +\t\treturn hash<uint32_t>{}(format.fourcc());\n\niirc just \"return format.fourcc();\" was enough\n\n> +\t}\n> +};\n> +\n> +} /* namespace std */\n> --\n> Regards,\n>\n> Laurent Pinchart\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 90D33C3272\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  3 Aug 2022 06:52:13 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D37CC63312;\n\tWed,  3 Aug 2022 08:52:12 +0200 (CEST)","from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net\n\t[217.70.183.195])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E36196330E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  3 Aug 2022 08:52:10 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby mail.gandi.net (Postfix) with ESMTPSA id 1668E60002;\n\tWed,  3 Aug 2022 06:52:09 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1659509532;\n\tbh=qAyHXFCMWS6ufw7Chu5Hs7ZBR8Yy6++NtxgvsLT154s=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=KSxbp0CRsHcGqfPJRZLBj6Vf+o/YWdSITl0qlFqvzZnB7nWH90uX1cL0+hwb/9Civ\n\tX2HU677qd9D+AlfxTaWk4mA923yauSSQBzoKLJjwKwcgIt8EwySqhxhqyfKixKWGUt\n\tUqYyPjVjZeKZ0mwU0yZdaOngIjAfIGc5oCQ2cRg8qd7dPUGemZcvbl2dc8qkPTUlcj\n\tx/YNpJoBuWZcaA0n58cz1sWHob9GoUOazCNfiF5KWK9vlAOHE0QyzYonssMDY3lagA\n\tnaRHnI1Vf62Vi+Q0gPRwU1IlNly2H1Dz7wS/nook9dhfC8Se0MXXOLWjPbBRnt1dAF\n\t/1rd2LS78r7tQ==","Date":"Wed, 3 Aug 2022 08:52:08 +0200","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Message-ID":"<20220803065208.jcfcls7gobrygi7s@uno.localdomain>","References":"<20220802194705.7539-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220802194705.7539-1-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_pixelformat:\n\tImplement std::hash specialization","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":24322,"web_url":"https://patchwork.libcamera.org/comment/24322/","msgid":"<Yuo5zJRcoNpL9od8@pendragon.ideasonboard.com>","date":"2022-08-03T09:03:08","subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_pixelformat:\n\tImplement std::hash specialization","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nOn Wed, Aug 03, 2022 at 08:52:08AM +0200, Jacopo Mondi wrote:\n> On Tue, Aug 02, 2022 at 10:47:05PM +0300, Laurent Pinchart wrote:\n> > Inject a specialization of std::hash<> for the V4L2PixelFormat class in\n> > the std namespace to make it possible to store instances of the class in\n> > the std::unordered_map and std::unordered_set containers.\n> >\n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> \n> I started with the same, then I thought polluting the std namespace\n> was not a good idea ? Do you think it's not an issue ?\n\nIn general it's a bad idea to add custom code to the std namespace, but\nfor hash specializations, that's specifically allowed and recommended.\n\n> > ---\n> >  include/libcamera/internal/v4l2_pixelformat.h | 13 +++++++++++++\n> >  1 file changed, 13 insertions(+)\n> >\n> > diff --git a/include/libcamera/internal/v4l2_pixelformat.h b/include/libcamera/internal/v4l2_pixelformat.h\n> > index d5400f90a67e..fcc1dadd4de0 100644\n> > --- a/include/libcamera/internal/v4l2_pixelformat.h\n> > +++ b/include/libcamera/internal/v4l2_pixelformat.h\n> > @@ -8,6 +8,7 @@\n> >\n> >  #pragma once\n> >\n> > +#include <functional>\n> >  #include <ostream>\n> >  #include <stdint.h>\n> >  #include <string>\n> > @@ -55,3 +56,15 @@ private:\n> >  std::ostream &operator<<(std::ostream &out, const V4L2PixelFormat &f);\n> >\n> >  } /* namespace libcamera */\n> > +\n> > +namespace std {\n> > +\n> > +template<>\n> > +struct hash<libcamera::V4L2PixelFormat> {\n> > +\tsize_t operator()(libcamera::V4L2PixelFormat const &format) const noexcept\n> > +\t{\n> > +\t\treturn hash<uint32_t>{}(format.fourcc());\n> \n> iirc just \"return format.fourcc();\" was enough\n\nThat would match the Hash requirement\n(https://en.cppreference.com/w/cpp/named_req/Hash) indeed:\n\n    h(k) -> std::size_t\n\n    The returned value depends only on the value of k for the duration of\n    the program.\n\n    All evaluations of h(k) executed within a given execution of a program\n    yield the same result for the same value of k.\n\n    The probability of h(a)==h(b) for a!=b should approach\n    1.0/std::numeric_limits<std::size_t>::max().\n\nand that's actually how std::hash<unsigned int> is implemented in libc++\nand libstdc++. I went for hash<uint32_t>{}(format.fourcc()) to use the\nsame hash mechanism as in the C++ library without having to check how\nit's implemented. All this will be inlined by the compiler anyway.\n\n> > +\t}\n> > +};\n> > +\n> > +} /* namespace std */","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 295D1C3272\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  3 Aug 2022 09:03:17 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A04DF63313;\n\tWed,  3 Aug 2022 11:03:16 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 83D2A603EF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  3 Aug 2022 11:03:14 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EACED8B;\n\tWed,  3 Aug 2022 11:03:13 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1659517396;\n\tbh=hzeAVXV2Dy5NFn5DAZ9vzNWA2abKqp24FXN+iNRrdLM=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=mMVhKfQPZIN0z3nHPtPnRiAXlDiC1IPeqaSuHobyHHVfBp4ENEjNEMgnjC6ZL6niW\n\ty8hDEDuzpw4HamB2eBT8Q9CcPKsTe67+Q8upNPFET9334xKgYQ4XrzdDvIEN7osag5\n\tG06+FJDGB7b/aZjxj/KkWFHwOKsgvBa9hR5jDHbNZiUaPL3NQedd89nbyhURTN5v55\n\tks2MwlobQbgjCC+gVFQB6uSMUk9EuPCZECIK3I+TzEsc5Ntwl4rPp7yn54cDWJOyBQ\n\tTszwYXkOC26gUM23Kq6kYO2oxslVTdlxWakXjsqcH27lMBuWVbLtblY9q76kStkXRV\n\tNvcpYaqBvGxbg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1659517394;\n\tbh=hzeAVXV2Dy5NFn5DAZ9vzNWA2abKqp24FXN+iNRrdLM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Ea8qYFmlyTFTrv1yJRXLyE8cR2ab9ohfXqzqvTIoNlU26huys9EqKZrYjRJzw896w\n\tSug4ExaKPm/lXVO5Cx+hdMfVvurDj0J9nLQyPdnmPEeTHDvXJXqcs4RiGgBiYlalJd\n\tGa+k8KDGZcMBVDCsdLUrKxaCc3WPuOhjsQ74j51U="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Ea8qYFml\"; dkim-atps=neutral","Date":"Wed, 3 Aug 2022 12:03:08 +0300","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<Yuo5zJRcoNpL9od8@pendragon.ideasonboard.com>","References":"<20220802194705.7539-1-laurent.pinchart@ideasonboard.com>\n\t<20220803065208.jcfcls7gobrygi7s@uno.localdomain>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220803065208.jcfcls7gobrygi7s@uno.localdomain>","Subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_pixelformat:\n\tImplement std::hash specialization","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]