Topic: std::reverse_lookup for arrays, does it at least make


Author: "Ivan G." <nekotekina@gmail.com>
Date: Thu, 13 Oct 2016 05:42:40 -0700 (PDT)
Raw View
------=_Part_322_1492261473.1476362560337
Content-Type: multipart/alternative;
 boundary="----=_Part_323_2049650385.1476362560338"

------=_Part_323_2049650385.1476362560338
Content-Type: text/plain; charset=UTF-8

Hi. Would it make sense to add an array search builtin accessible via (for
example) std::reverse_lookup?

template <typename ArrayType, size_t ArraySize, typename Compare>
constexpr ArrayType* reverse_lookup(ArrayType(&array)[ArraySize], Compare&&
compare)
{
        return SOME_BUILTIN_REVERSE_LOOKUP(array, compare);
}

(returns pointer to the first found element, nullptr otherwise)

The purpose is (hopefully) optimization because manually implemented linear
search seems ineffective, and binary search requires sorting which is not
guaranteed. Another possibility would be similar constexpr
lower_bound/upper_bound-alike binary search functions expected to fail
loudly if the array is not appropriately sorted.

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/8c3194d9-dd08-4bf9-9612-82b6aff3d55f%40isocpp.org.

------=_Part_323_2049650385.1476362560338
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Hi. Would it make sense to add an array search builti=
n accessible via (for example) std::reverse_lookup?<span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br></span></div><div><br></div><div><div=
 style=3D"background-color: #FAFAFA; border-color: #BBB; border-style: soli=
d; border-width: 1px; word-wrap: break-word;" class=3D"prettyprint"><code c=
lass=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">template</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">typename</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pre=
ttify">ArrayType</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> si=
ze_t </span><span style=3D"color: #606;" class=3D"styled-by-prettify">Array=
Size</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #606;" class=3D"styled-by-prettify">Compare</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">constexpr</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"style=
d-by-prettify">ArrayType</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">*</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> reverse_lookup</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #606;" class=3D"styled-by-prettify"=
>ArrayType</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
(&amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">arra=
y</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)[</span>=
<span style=3D"color: #606;" class=3D"styled-by-prettify">ArraySize</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">],</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #606;" class=3D"styled-by-prettify">Compare</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">&amp;&amp;</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> compare</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color: #00=
8;" class=3D"styled-by-prettify">return</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> SOME_BUILTIN_REVERSE_LOOKUP</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">array</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> compare</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">}</span></div></code></div><br>(returns pointer to the first foun=
d element, nullptr otherwise)</div><div><br></div><div>The purpose is (hope=
fully) optimization because manually implemented linear search seems ineffe=
ctive, and binary search requires sorting which is not guaranteed. Another =
possibility would be similar constexpr lower_bound/upper_bound-alike binary=
 search functions expected to fail loudly if the array is not appropriately=
 sorted.</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/8c3194d9-dd08-4bf9-9612-82b6aff3d55f%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8c3194d9-dd08-4bf9-9612-82b6aff3d55f=
%40isocpp.org</a>.<br />

------=_Part_323_2049650385.1476362560338--

------=_Part_322_1492261473.1476362560337--

.