220 33178 <bd1a5d3b-ccc1-44ab-91cc-a4d7195c9dc3@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: dan@soundradix.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: [idea for proposal] Adding std::shift to <algorithm>
Date: Wed, 12 Jul 2017 02:53:19 -0700 (PDT)
Lines: 156
Approved: news@gmane.org
Message-ID: <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_8235_1977344816.1499853199870"
X-Trace: blaine.gmane.org 1499853204 31888 195.159.176.226 (12 Jul 2017 09:53:24 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Wed, 12 Jul 2017 09:53:24 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCPJTS7U6MJBBEHDS7FQKGQE6HKMMYI@isocpp.org Wed Jul 12 11:53:19 2017
Return-path: <std-proposals+bncBCPJTS7U6MJBBEHDS7FQKGQE6HKMMYI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yw0-f199.google.com ([209.85.161.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCPJTS7U6MJBBEHDS7FQKGQE6HKMMYI@isocpp.org>)
	id 1dVEKa-0007sP-Af
	for gclcip-std-proposals@m.gmane.org; Wed, 12 Jul 2017 11:53:16 +0200
Original-Received: by mail-yw0-f199.google.com with SMTP id b82sf13821060ywc.11
        for <gclcip-std-proposals@m.gmane.org>; Wed, 12 Jul 2017 02:53:22 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:subject:mime-version:x-original-sender
         :reply-to:precedence:mailing-list:list-id:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=n7TsC/SoSgWk6LwBBUo5EbcXosSLh/hU/j9JocLbALY=;
        b=eIfFZnj9+viNaasf6bfYkbwp6HtAnJyQDCovP/CzWwpGBHd+gwuFj4M6c/Pq5xcJ1W
         r2km7aGXR1HMzCu0KnA/cPZLaND6Tb469D5YD3tTLBknaC8FbSvx+InspTrXITzWKceT
         tLG5azy0HLUQmfkSKf1UTpWbJRlOBDxyB6S21Yf1PE096CK5sufsvmXx0Bz9I6HJowQp
         y7IcvappDKBB/j7kWSv1KxCeEhBA0oCHsTdn4gYTEfO9DijSoeQfrW5iF4r5uZU4fwAu
         N23MGpR2iNHjOsGxr+9Ao7uM0nlurd6wEpilKDUrRjN38Dd+fxBCOzHX5o63J8aE7Jwt
         QOOw==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=soundradix.com; s=google;
        h=date:from:to:message-id:subject:mime-version:x-original-sender
         :reply-to:precedence:mailing-list:list-id:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=n7TsC/SoSgWk6LwBBUo5EbcXosSLh/hU/j9JocLbALY=;
        b=alhKLTd56mHP8l0g5aUJpb43+h+AplxzIsoLw8EuKVTkUjBtkyKTdmref3+IIH0kDQ
         D4E+lEZgUvuZJCEkxLdaOtcvmhHaBVdgY70WwsXtT4uqbRMsu5qmujiYITWHjkDmL7NZ
         PBmO493GYIQX9723NKpmcJaQ8FtmRVS+Lg9PVmaWP+OUWXtd1N82MoPvXi/XgursDl6R
         UunWVoj78nQeXxoHBHY06zgrQ11qKQNMpcZ1rEZD4Um+UpTZeKKJfEIP7aEISbfd/neU
         pgBfAVFxnkkMZGd/fIBbD/aclS6Ajwmhcon43NkzYnw1Mm77GPLNsvO3yi3glxttnLHh
         9KjQ==
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:message-id: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=n7TsC/SoSgWk6LwBBUo5EbcXosSLh/hU/j9JocLbALY=;
        b=X0bbIJqtFo2aMzDPTK20brxDG5uy41xpgDd2jEqXGyt5WrBjNp0YEDvPJUSuwr76Ev
         U4u2ZtRzDSwxbeEHOQ2GYmoRJa0aztUv/g6txbDRhGcJpJyqk97JlsJwpfP+ZOtrMDpg
         mVayc2OKpM0U09jkNOhW6yG9d7MdUctul8lANnwS1n6qPVMILIJOrrKwgWjypIrJhCJz
         y4dMUnRU8RDE7h8VmynA9yHxJE7HoNDxnj0jTfmZTVQb40BX6rHfxxXWMEXQLKySCNNU
         Y1qF5EDJynvVxelY3w242e8dzBLOXBEQx10OP5uwnh/XHefbBSXYGzk4H/LPucH0oC/H
         40Cg==
X-Gm-Message-State: AIVw111V7C8dl/1c/hnHW6JnXx6LWvv5bqjy8VBN1gRgyGf/+3ymAo28
	LmH5q5gxbB5eJqYu
X-Received: by 10.13.221.84 with SMTP id g81mr2555378ywe.126.1499853201634;
        Wed, 12 Jul 2017 02:53:21 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.36.101.129 with SMTP id u123ls1103420itb.2.gmail; Wed, 12 Jul
 2017 02:53:20 -0700 (PDT)
X-Received: by 10.36.34.10 with SMTP id o10mr304546ito.10.1499853200366;
        Wed, 12 Jul 2017 02:53:20 -0700 (PDT)
X-Original-Sender: dan@soundradix.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:33178
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/33178>

------=_Part_8235_1977344816.1499853199870
Content-Type: multipart/alternative; 
	boundary="----=_Part_8236_1520123693.1499853199870"

------=_Part_8236_1520123693.1499853199870
Content-Type: text/plain; charset="UTF-8"

Hi,

Would anyone be interested in adding std::shift to <algorithm>? 

It would be similar to both:
- std::rotate, but without moving the head elements back to the tail. This 
would allow a more efficient implementation and clearer semantics in case 
rotation is not needed as well as correctness in case rotation is undesired.
- <algorithm>'s std::move/std::move_backward (depending on the shift 
direction).

std::shift should probably accommodate both left and right shifts by one of:
- 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.
- 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 with rotate, positive integers should shift to the 
left.
- having std::shift_right and std::shift_left functions. The advantage is 
clarity when calling the methods, although the same argument could be made 
for having separate std::rotate_left and std::rotate_right instead of 
std::rotate, which we don't have.

Here's a sample implementation of a shift to the right direction:

template<class BidirIt> 
void shift_right(BidirIt first, BidirIt last, unsigned int n = 1) 
{ 
    std::move_backward(first, last - n, last); 
}

This demonstrates that while std::shift is implementable with 
std::move/std::move_backward,
1) It isn't immediately clear from the code (at least to my eyes) that this 
is a shift right, unless you are intimately familiar with 
std::move_backward.
2) Different calls, either to std::move or to std::move_backward, are 
required, depending on the shift direction.

So, implementing std::shift would allow writing more readable code.

Thanks,
Dan

-- 
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/bd1a5d3b-ccc1-44ab-91cc-a4d7195c9dc3%40isocpp.org.

------=_Part_8236_1520123693.1499853199870
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div><br></div><div>Would anyone be interested in addin=
g 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 el=
ements back to the tail. This would allow a more efficient implementation a=
nd clearer semantics in case rotation is not needed as well as correctness =
in case rotation is undesired.</div><div>- &lt;algorithm&gt;&#39;s std::mov=
e/std::move_backward (depending on the shift direction).</div><div><br></di=
v><div>std::shift should probably accommodate both left and right shifts by=
 one of:</div><div>- giving it either begin() and end(), or rbegin() and re=
nd(), similar to how std::rotate works for both left and right rotations. T=
he 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 with rotate, positive integers should shift to the left.<br></di=
v><div><div>- having std::shift_right and std::shift_left functions. The ad=
vantage is clarity when calling the methods, although the same argument cou=
ld be made for having separate std::rotate_left and std::rotate_right inste=
ad of std::rotate, which we don&#39;t have.</div></div><div><br></div><div>=
Here&#39;s a sample implementation of a shift to the right direction:</div>=
<br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250=
); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code clas=
s=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;=
" class=3D"styled-by-prettify">template</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">class</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-=
prettify">BidirIt</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> <br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">void=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> shift_rig=
ht</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span>=
<span style=3D"color: #606;" class=3D"styled-by-prettify">BidirIt</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> first</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #60=
6;" class=3D"styled-by-prettify">BidirIt</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">last</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">uns=
igned</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">int</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> n </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066;=
" class=3D"styled-by-prettify">1</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"style=
d-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 std</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">move_backward</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">f=
irst</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">last</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">-</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> n</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">last</span><span style=3D"color: #660;" class=3D"styled-by-prettify">);<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br></span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br></span></div></code><=
/div><div><br></div><div>This demonstrates that while std::shift is impleme=
ntable with std::move/std::move_backward,</div><div>1) It isn&#39;t immedia=
tely clear from the code (at least to my eyes) that this is a shift right, =
unless you are intimately familiar with std::move_backward.</div><div>2) Di=
fferent calls, either to std::move or to std::move_backward, are required, =
depending on the shift direction.</div><div><br></div><div>So, implementing=
 std::shift would allow writing more readable code.</div><div><br></div><di=
v>Thanks,</div><div>Dan</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/bd1a5d3b-ccc1-44ab-91cc-a4d7195c9dc3%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/bd1a5d3b-ccc1-44ab-91cc-a4d7195c9dc3=
%40isocpp.org</a>.<br />

------=_Part_8236_1520123693.1499853199870--

------=_Part_8235_1977344816.1499853199870--

.
