220 33197 <68bbd6d3-1578-43e1-b15f-563c9d6fb27c@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Arthur O'Dwyer <arthur.j.odwyer@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: [idea for proposal] Adding std::shift to <algorithm>
Date: Wed, 12 Jul 2017 16:47:04 -0700 (PDT)
Lines: 198
Approved: news@gmane.org
Message-ID: <68bbd6d3-1578-43e1-b15f-563c9d6fb27c@isocpp.org>
References: <bd1a5d3b-ccc1-44ab-91cc-a4d7195c9dc3@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_138_1339289051.1499903224955"
X-Trace: blaine.gmane.org 1499903229 18877 195.159.176.226 (12 Jul 2017 23:47:09 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Wed, 12 Jul 2017 23:47:09 +0000 (UTC)
Cc: dan@soundradix.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDLZJYWNDQIPT2M2ZMCRUBDZJKXY2@isocpp.org Thu Jul 13 01:47:03 2017
Return-path: <std-proposals+bncBDLZJYWNDQIPT2M2ZMCRUBDZJKXY2@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-vk0-f72.google.com ([209.85.213.72])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDLZJYWNDQIPT2M2ZMCRUBDZJKXY2@isocpp.org>)
	id 1dVRLS-0004VJ-3t
	for gclcip-std-proposals@m.gmane.org; Thu, 13 Jul 2017 01:47:02 +0200
Original-Received: by mail-vk0-f72.google.com with SMTP id 130sf13740630vka.2
        for <gclcip-std-proposals@m.gmane.org>; Wed, 12 Jul 2017 16:47:07 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=cUzU8TY8y30JMeL+yZTyKDd3vNyhyG0WWR/lp4H9//I=;
        b=cg9GK/hAMgYGfsjeRe0Y1e59cPGh2OQmw1xDntXiQLJmB9+uj9vs8V26bOYuNFePD1
         7Nowowchyq1bE3mr7mFpdSDRVMTD7e/+ReyFIL5u2kHFTxqefO8vDq6RBNMlAYgpdYrK
         fiRXYujTEb5XuC4Nxvq3ioOhK6dTg3lgo6ca1Is5l5O12/QTQyeUNqF1l0c8PhaxIdJK
         Z0DsquthihkakqbLQHaBv1XeDpyitqLRobVUL5049cI0BNk0s714SsJUoBBgbXdZX073
         VOa/qCVa+H7b6UBTH//Bc5s2IRL7f0SVvP+gbf53Bq/YJ7931F4weUZSwJfjQxSb3PJv
         4qsQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=cUzU8TY8y30JMeL+yZTyKDd3vNyhyG0WWR/lp4H9//I=;
        b=n3qkH1uaf3fbBlRVI92eC+egiUu6a8qvXm1W4tASlNWU0gvDIOPtp+VD1sskQ9goIG
         +fXlwJ5tz2ZLHK0XnSkIhjJLDgHUqVJNV8WChS+pFUGZ2WSK4XL4RaA52KxvsVEpnguz
         eiFViGQIPXAi4bOmYL0ylYuXsG8RdPz+JyozdBh29gew2vF1HrRvEgPbwSkgXFEq/Fj1
         IRmAq+BEKCe/pazLrE8ssG+DqJ4pYqrKPhAjfB9rpAHxEiPXn5Pukee1XRqqP3I6/zFM
         px1D8DH77yGFUKRRXiTTopHT88BBFCKSd5QEkHq69vCAcRDyq3R/nQ2aeGm1XOfLlBrW
         bpJA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to
         :references:subject:mime-version:x-original-sender:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=cUzU8TY8y30JMeL+yZTyKDd3vNyhyG0WWR/lp4H9//I=;
        b=JE6+MPOjYMG1CrlmJXdChQ/Cm25zRD5Lc6dnPoUtemQFH9HSuTaZzFOTTfHWpsddEx
         o0wpRsaznJKoKRqhW28B3mZqUmI0fMs0GE6kSjczAdtlSHXJJBgmsLkjV8C4JUPr2YTe
         3o9jbLjIzVIl0yHfSHtPM9znnwdv2iuOpY2ze5sDGgKTPteWbQZ8SimO1SZSr8VILWda
         hkW9ZhA+ysuQYg1sjCpeKh6L2cGEP1lNZWoSThDkcImDPzOviHeHJuevpDTeQf/u8s6I
         M/keAaUbZTbaFEUwTh+mWQKef+a//1+G4MWvv2UVCrEcIxIHKuuh9PhsZp2o9LVd3EMt
         D/Tg==
X-Gm-Message-State: AIVw111+adIlcCCoksSRLstfnUPJw2Mfx7Nm+UThkJnoK205HA6/WzKq
	UWyy/cJ2IEeBudcI
X-Received: by 10.31.5.76 with SMTP id 73mr696906vkf.34.1499903227174;
        Wed, 12 Jul 2017 16:47:07 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.7.228 with SMTP id g97ls4058283ioi.26.gmail; Wed, 12 Jul
 2017 16:47:05 -0700 (PDT)
X-Received: by 10.31.69.77 with SMTP id s74mr3419vka.1.1499903225343;
        Wed, 12 Jul 2017 16:47:05 -0700 (PDT)
In-Reply-To: <bd1a5d3b-ccc1-44ab-91cc-a4d7195c9dc3@isocpp.org>
X-Original-Sender: arthur.j.odwyer@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:33197
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/33197>

------=_Part_138_1339289051.1499903224955
Content-Type: multipart/alternative; 
	boundary="----=_Part_139_914765861.1499903224956"

------=_Part_139_914765861.1499903224956
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Wednesday, July 12, 2017 at 2:53:19 AM UTC-7, d...@soundradix.com wrote:
>
> Hi,
>
> Would anyone be interested in adding std::shift to <algorithm>?=20
>
> It would be similar to both:
> - std::rotate, but without moving the head elements back to the tail. Thi=
s=20
> would allow a more efficient implementation and clearer semantics in case=
=20
> rotation is not needed as well as correctness in case rotation is undesir=
ed.
> - <algorithm>'s std::move/std::move_backward (depending on the shift=20
> direction).
>
> std::shift should probably accommodate both left and right shifts by one=
=20
> of:
> - giving it either begin() and end(), or rbegin() and rend(), similar to=
=20
> how std::rotate works for both left and right rotations. The advantage is=
=20
> compactness of the implementation.
> - allowing the shift count parameter to be either positive or negative.=
=20
> The advantage is compactness, though it might not be clear which directio=
n=20
> is which - to be consistent with rotate, positive integers should shift t=
o=20
> the left.
> - having std::shift_right and std::shift_left functions. The advantage is=
=20
> clarity when calling the methods, although the same argument could be mad=
e=20
> for having separate std::rotate_left and std::rotate_right instead of=20
> std::rotate, which we don't have.
>

std::rotate is actually just a rotation; it doesn't need "left" or "right"=
=20
qualification because they're 100% equivalent. Consider a classroom globe=
=20
with London in front, facing you. Now "rotate" the globe until Beijing is=
=20
in front. It doesn't matter if you rotate left or rotate right; the outcome=
=20
is exactly the same either way.
=20

> Here's a sample implementation of a shift to the right direction:
>
> template<class BidirIt>=20
> void shift_right(BidirIt first, BidirIt last, unsigned int n =3D 1)=20
> {=20
>     std::move_backward(first, last - n, last);=20
> }
>
> This demonstrates that while std::shift is implementable with=20
> std::move/std::move_backward,
> 1) It isn't immediately clear from the code (at least to my eyes) that=20
> this is a shift right, unless you are intimately familiar with=20
> std::move_backward.
> 2) Different calls, either to std::move or to std::move_backward, are=20
> required, depending on the shift direction.
>

If you're shifting the whole container's contents, you could use either of=
=20
these:
    std::move(ctr.rbegin() + n, ctr.rend(), ctr.rbegin());
    std::move_backward(ctr.begin(), ctr.end() - n, ctr.end())?
I don't currently see the use-case for this "shift just a piece of a=20
container" algorithm, I mean as distinct from std::move and=20
std::move_backward which already exist. Do you have a use-case?

Re naming, notice in passing that std::valarray::shift() exists, and=20
std::left_shift<T> and std::right_shift<T> do *not* exist but obviously=20
should. The name std::shift itself is indeed still available. I question=20
whether it should be used for this purpose, though.

=E2=80=93Arthur

--=20
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 e=
mail 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/68bbd6d3-1578-43e1-b15f-563c9d6fb27c%40isocpp.or=
g.

------=_Part_139_914765861.1499903224956
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Wednesday, July 12, 2017 at 2:53:19 AM UTC-7, d...@soun=
dradix.com wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margi=
n-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"l=
tr">Hi,<div><br></div><div>Would anyone be interested in adding std::shift =
to &lt;algorithm&gt;?=C2=A0</div><div><br></div><div>It would be similar to=
 both:</div><div>- std::rotate, but without moving the head elements back t=
o the tail. This would allow a more efficient implementation and clearer se=
mantics in case rotation is not needed as well as correctness in case rotat=
ion is undesired.</div><div>- &lt;algorithm&gt;&#39;s std::move/std::move_b=
ackward (depending on the shift direction).</div><div><br></div><div>std::s=
hift should probably accommodate both left and right shifts by one of:</div=
><div>- giving it either begin() and end(), or rbegin() and rend(), similar=
 to how std::rotate works for both left and right rotations. The advantage =
is compactness of the implementation.</div><div>- allowing the shift count =
parameter to be either positive or negative. The advantage is compactness, =
though it might not be clear which direction is which - to be consistent wi=
th rotate, positive integers should shift to the left.<br></div><div><div>-=
 having std::shift_right and std::shift_left functions. The advantage is cl=
arity when calling the methods, although the same argument could be made fo=
r having separate std::rotate_left and std::rotate_right instead of std::ro=
tate, which we don&#39;t have.</div></div></div></blockquote><div><br></div=
><div>std::rotate is actually just a rotation; it doesn&#39;t need &quot;le=
ft&quot; or &quot;right&quot; qualification because they&#39;re 100% equiva=
lent. Consider a classroom globe with London in front, facing you. Now &quo=
t;rotate&quot; the globe until Beijing is in front. It doesn&#39;t matter i=
f you rotate left or rotate right; the outcome is exactly the same either w=
ay.</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin=
: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div=
 dir=3D"ltr"><div>Here&#39;s a sample implementation of a shift to the righ=
t direction:<br></div><br><div style=3D"background-color:rgb(250,250,250);b=
order:1px solid rgb(187,187,187);word-wrap:break-word"><code><div><span sty=
le=3D"color:#008">template</span><span style=3D"color:#660">&lt;</span><spa=
n style=3D"color:#008">class</span><span style=3D"color:#000"> </span><span=
 style=3D"color:#606">BidirIt</span><span style=3D"color:#660">&gt;</span><=
span style=3D"color:#000"> <br></span><span style=3D"color:#008">void</span=
><span style=3D"color:#000"> shift_right</span><span style=3D"color:#660">(=
</span><span style=3D"color:#606">BidirIt</span><span style=3D"color:#000">=
 first</span><span style=3D"color:#660">,</span><span style=3D"color:#000">=
 </span><span style=3D"color:#606">BidirIt</span><span style=3D"color:#000"=
> </span><span style=3D"color:#008">last</span><span style=3D"color:#660">,=
</span><span style=3D"color:#000"> </span><span style=3D"color:#008">unsign=
ed</span><span style=3D"color:#000"> </span><span style=3D"color:#008">int<=
/span><span style=3D"color:#000"> n </span><span style=3D"color:#660">=3D</=
span><span style=3D"color:#000"> </span><span style=3D"color:#066">1</span>=
<span style=3D"color:#660">)</span><span style=3D"color:#000"> <br></span><=
span style=3D"color:#660">{</span><span style=3D"color:#000"> <br>=C2=A0 =
=C2=A0 std</span><span style=3D"color:#660">::</span><span style=3D"color:#=
000">move_backward</span><span style=3D"color:#660">(</span><span style=3D"=
color:#000">first</span><span style=3D"color:#660">,</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#008">last</span><span style=3D"col=
or:#000"> </span><span style=3D"color:#660">-</span><span style=3D"color:#0=
00"> n</span><span style=3D"color:#660">,</span><span style=3D"color:#000">=
 </span><span style=3D"color:#008">last</span><span style=3D"color:#660">);=
</span><span style=3D"color:#000"> <br></span><span style=3D"color:#660">}<=
/span><span style=3D"color:#000"><br></span></div></code></div><div><br></d=
iv><div>This demonstrates that while std::shift is implementable with std::=
move/std::move_backward,</div><div>1) It isn&#39;t immediately clear from t=
he code (at least to my eyes) that this is a shift right, unless you are in=
timately familiar with std::move_backward.</div><div>2) Different calls, ei=
ther to std::move or to std::move_backward, are required, depending on the =
shift direction.</div></div></blockquote><div><br></div><div>If you&#39;re =
shifting the whole container&#39;s contents, you could use either of these:=
</div><div>=C2=A0 =C2=A0 std::move(ctr.rbegin() + n, ctr.rend(), ctr.rbegin=
());</div><div>=C2=A0 =C2=A0 std::move_backward(ctr.begin(), ctr.end() - n,=
 ctr.end())?</div><div>I don&#39;t currently see the use-case for this &quo=
t;shift just a piece of a container&quot; algorithm, I mean as distinct fro=
m std::move and std::move_backward which already exist. Do you have a use-c=
ase?</div><div><br></div><div>Re naming, notice in passing that std::valarr=
ay::shift() exists, and std::left_shift&lt;T&gt; and std::right_shift&lt;T&=
gt; do <i><b>not</b></i> exist but obviously should. The name std::shift it=
self is indeed still available. I question whether it should be used for th=
is purpose, though.</div><div><br></div><div>=E2=80=93Arthur</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/68bbd6d3-1578-43e1-b15f-563c9d6fb27c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/68bbd6d3-1578-43e1-b15f-563c9d6fb27c=
%40isocpp.org</a>.<br />

------=_Part_139_914765861.1499903224956--

------=_Part_138_1339289051.1499903224955--

.
