220 7218 <a25301a7-e4c6-4245-b053-00d46ca5c7d4@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Benjamin Lindley <benjameslindley@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: "count_while" and "count_until" algorithms
Date: Tue, 8 Oct 2013 22:13:02 -0700 (PDT)
Lines: 307
Approved: news@gmane.org
Message-ID: <a25301a7-e4c6-4245-b053-00d46ca5c7d4@isocpp.org>
References: <CAOpASk64dV+3ZUW+Z8wcEqug_oSaztvJp1zsDB_QdAxi+fwcsA@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_1690_26279800.1381295582429"
X-Trace: ger.gmane.org 1381295580 13285 80.91.229.3 (9 Oct 2013 05:13:00 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 9 Oct 2013 05:13:00 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDI7THWOWMDRBX6L2OJAKGQEU6E4JDI@isocpp.org Wed Oct 09 07:13:04 2013
Return-path: <std-proposals+bncBDI7THWOWMDRBX6L2OJAKGQEU6E4JDI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-vc0-f197.google.com ([209.85.220.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDI7THWOWMDRBX6L2OJAKGQEU6E4JDI@isocpp.org>)
	id 1VTm52-0000bs-CO
	for gclcip-std-proposals@m.gmane.org; Wed, 09 Oct 2013 07:13:04 +0200
Original-Received: by mail-vc0-f197.google.com with SMTP id gd11sf638261vcb.4
        for <gclcip-std-proposals@m.gmane.org>; Tue, 08 Oct 2013 22:13:03 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to: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
         :content-type;
        bh=fvSFpFPriRscvk6YYRPFzc0aYk7dataB5FQUbtuzLS8=;
        b=QUbXz5NnIOr4AxwOXEupCDPkCqeoVAH8/u0gNy8sW+1ZVSNDtj402/kS+g5NRL6kzy
         1Rh5buvxNAbmsTHoBf8jr2+ApkH0OYnC/9hMr36wqVjgSguIo8j7NVvvRmKnYH3hLxsS
         2TVlivqIUhEbPUF4ZO+7h1z7H15/+8CvD1Jsv0K3YrKfS48OdBImPb8v1paiFaLHCtly
         lO07uj7BzKdjWx+AVSfarsx9F/D+DG7Fs/n3nRinRSUilAz03jMcuDfe575sdSxH9S23
         EhqzcL4J0B4W8wBP5WhHXHrC30dgAkJMxtO5O+mBx5inXfJ4D0yNu9IJNCWsGDsYKJTZ
         yqJw==
X-Received: by 10.236.135.211 with SMTP id u59mr4956609yhi.43.1381295583463;
        Tue, 08 Oct 2013 22:13:03 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.70.228 with SMTP id p4ls503511qeu.43.gmail; Tue, 08 Oct
 2013 22:13:02 -0700 (PDT)
X-Received: by 10.49.71.34 with SMTP id r2mr8493qeu.14.1381295582876;
        Tue, 08 Oct 2013 22:13:02 -0700 (PDT)
In-Reply-To: <CAOpASk64dV+3ZUW+Z8wcEqug_oSaztvJp1zsDB_QdAxi+fwcsA@mail.gmail.com>
X-Original-Sender: benjameslindley@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: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:7218
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/7218>

------=_Part_1690_26279800.1381295582429
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

What about creating an iterator adapter instead?  One which keeps track of=
=20
how many times it has been incremented?

typedef std::istream_iterator<int> base_t;
base_t ib(std::cin), ie;
std::counting_iterator<base_t> b(ib), e(ie);
auto p =3D std::find_if(b, e, pred);
std::cout << p.count();

Then you could apply it to other algorithms besides find/find_if.

On Tuesday, October 8, 2013 3:04:23 PM UTC-5, R=E9my Lefevre wrote:
>
> Hi,
>
> I thought about two new algorithms, that could be named "count_while" and=
=20
> "count_until". Here are their behavior:
>
> template <class InputIterator, class UnaryPredicate>
>
>
>   typename iterator_traits<InputIterator>::difference_type
>
>
>     count_while (InputIterator first, InputIterator last, UnaryPredicate =
pred)
>
> {
>   typename iterator_traits<InputIterator>::difference_type ret =3D 0;
>
>
>   while (first!=3Dlast && pred(*first)) {
>
>
>     ++ret;
>     ++first;
>
>
>   }
>   return ret;
>
> }
>
>
> template <class InputIterator, class UnaryPredicate>
>
>
>   typename iterator_traits<InputIterator>::difference_type
>
>
>     count_until (InputIterator first, InputIterator last, UnaryPredicate =
pred)
>
> {
>
>   typename iterator_traits<InputIterator>::difference_type ret =3D 0;
>
>
>   while (first!=3Dlast && !pred(*first)) {
>
>
>     ++ret;
>     ++first;
>
>
>   }
>   return ret;
>
>
> }
>
>
> What do you think about adding these two components to the algorithms=20
> library ?
>

--=20

---=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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

------=_Part_1690_26279800.1381295582429
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">What about creating an iterator adapter instead?&nbsp; One=
 which keeps track of how many times it has been incremented?<br><br><div c=
lass=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-=
color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wra=
p: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><=
span style=3D"color: #008;" class=3D"styled-by-prettify">typedef</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">istream_iterator</span><span style=
=3D"color: #080;" class=3D"styled-by-prettify">&lt;int&gt;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> base_t</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br>base_t ib</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">std</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">cin</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">),</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> ie</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>std</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">counting_iterator</span><=
span style=3D"color: #080;" class=3D"styled-by-prettify">&lt;base_t&gt;</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> b</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">ib</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">),</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> e</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify">ie</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> p </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> std</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify">find_if</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">b</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 e</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> pred</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br>std</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify">cout </span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> p</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify">count</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">();</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br></span></div></code></div><br>Then you could apply it to other algorith=
ms besides find/find_if.<br><br>On Tuesday, October 8, 2013 3:04:23 PM UTC-=
5, R=E9my Lefevre wrote:<blockquote class=3D"gmail_quote" style=3D"margin: =
0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div d=
ir=3D"ltr">Hi,<br><br>I thought about two new algorithms, that could be nam=
ed "count_while" and "count_until". Here are their behavior:<br><br><div st=
yle=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bord=
er-style:solid;border-width:1px;word-wrap:break-word">

<code><div><pre><code><var><span style=3D"color:rgb(0,0,136)">template</spa=
n></var><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(1=
02,102,0)">&lt;</span><var><span style=3D"color:rgb(0,0,136)">class</span><=
/var><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,=
0,102)">InputIterator</span><span style=3D"color:rgb(102,102,0)">,</span><s=
pan style=3D"color:rgb(0,0,0)"> </span><var><span style=3D"color:rgb(0,0,13=
6)">class</span></var><span style=3D"color:rgb(0,0,0)"> </span><span style=
=3D"color:rgb(102,0,102)">UnaryPredicate</span><span style=3D"color:rgb(102=
,102,0)">&gt;</span><span style=3D"color:rgb(0,0,0)"><br>

&nbsp; </span><var><span style=3D"color:rgb(0,0,136)">typename</span></var>=
<span style=3D"color:rgb(0,0,0)"> iterator_traits</span><span style=3D"colo=
r:rgb(102,102,0)">&lt;</span><span style=3D"color:rgb(102,0,102)">InputIter=
ator</span><span style=3D"color:rgb(102,102,0)">&gt;<wbr>::</span><span sty=
le=3D"color:rgb(0,0,0)">difference_type<br>

&nbsp; &nbsp; count_while </span><span style=3D"color:rgb(102,102,0)">(</sp=
an><span style=3D"color:rgb(102,0,102)">InputIterator</span><span style=3D"=
color:rgb(0,0,0)"> first</span><span style=3D"color:rgb(102,102,0)">,</span=
><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,0,10=
2)">InputIterator</span><span style=3D"color:rgb(0,0,0)"> </span><span styl=
e=3D"color:rgb(0,0,136)">last</span><span style=3D"color:rgb(102,102,0)">,<=
/span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102=
,0,102)">UnaryPredicate</span><span style=3D"color:rgb(0,0,0)"> pred</span>=
<span style=3D"color:rgb(102,102,0)">)</span><span style=3D"color:rgb(0,0,0=
)"><br>

</span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rg=
b(0,0,0)"><br>&nbsp; </span><var><span style=3D"color:rgb(0,0,136)">typenam=
e</span></var><span style=3D"color:rgb(0,0,0)"> iterator_traits</span><span=
 style=3D"color:rgb(102,102,0)">&lt;</span><span style=3D"color:rgb(102,0,1=
02)">InputIterator</span><span style=3D"color:rgb(102,102,0)">&gt;<wbr>::</=
span><span style=3D"color:rgb(0,0,0)">difference_type ret </span><span styl=
e=3D"color:rgb(102,102,0)">=3D</span><span style=3D"color:rgb(0,0,0)"> </sp=
an><span style=3D"color:rgb(0,102,102)">0</span><span style=3D"color:rgb(10=
2,102,0)">;</span><span style=3D"color:rgb(0,0,0)"><br>

&nbsp; </span><var><span style=3D"color:rgb(0,0,136)">while</span></var><sp=
an style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">=
(</span><span style=3D"color:rgb(0,0,0)">first</span><span style=3D"color:r=
gb(102,102,0)">!=3D</span><span style=3D"color:rgb(0,0,136)">last</span><va=
r><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102=
,0)">&amp;&amp;</span><span style=3D"color:rgb(0,0,0)"> </span></var><var><=
/var><span style=3D"color:rgb(0,0,0)">pred</span><span style=3D"color:rgb(1=
02,102,0)">(*</span><span style=3D"color:rgb(0,0,0)">first</span><span styl=
e=3D"color:rgb(102,102,0)">))</span><span style=3D"color:rgb(0,0,0)"> </spa=
n><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0=
,0)"><br>

&nbsp; &nbsp; </span><span style=3D"color:rgb(102,102,0)">++</span><span st=
yle=3D"color:rgb(0,0,0)">ret</span><span style=3D"color:rgb(102,102,0)">;</=
span><span style=3D"color:rgb(0,0,0)"><br>&nbsp; &nbsp; </span><span style=
=3D"color:rgb(102,102,0)">++</span><span style=3D"color:rgb(0,0,0)">first</=
span><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(=
0,0,0)"><br>

&nbsp; </span><span style=3D"color:rgb(102,102,0)">}</span><span style=3D"c=
olor:rgb(0,0,0)"><br>&nbsp; </span><var><span style=3D"color:rgb(0,0,136)">=
return</span></var><span style=3D"color:rgb(0,0,0)"> ret</span><span style=
=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0,0)"><br>

</span><span style=3D"color:rgb(102,102,0)">}</span></code></pre></div></co=
de></div><br><div style=3D"background-color:rgb(250,250,250);border-color:r=
gb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word">

<code><div><pre><code><var><span style=3D"color:rgb(0,0,136)">template</spa=
n></var><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(1=
02,102,0)">&lt;</span><var><span style=3D"color:rgb(0,0,136)">class</span><=
/var><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,=
0,102)">InputIterator</span><span style=3D"color:rgb(102,102,0)">,</span><s=
pan style=3D"color:rgb(0,0,0)"> </span><var><span style=3D"color:rgb(0,0,13=
6)">class</span></var><span style=3D"color:rgb(0,0,0)"> </span><span style=
=3D"color:rgb(102,0,102)">UnaryPredicate</span><span style=3D"color:rgb(102=
,102,0)">&gt;</span><span style=3D"color:rgb(0,0,0)"><br>

&nbsp; </span><var><span style=3D"color:rgb(0,0,136)">typename</span></var>=
<span style=3D"color:rgb(0,0,0)"> iterator_traits</span><span style=3D"colo=
r:rgb(102,102,0)">&lt;</span><span style=3D"color:rgb(102,0,102)">InputIter=
ator</span><span style=3D"color:rgb(102,102,0)">&gt;<wbr>::</span><span sty=
le=3D"color:rgb(0,0,0)">difference_type<br>

&nbsp; &nbsp; count_until </span><span style=3D"color:rgb(102,102,0)">(</sp=
an><span style=3D"color:rgb(102,0,102)">InputIterator</span><span style=3D"=
color:rgb(0,0,0)"> first</span><span style=3D"color:rgb(102,102,0)">,</span=
><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,0,10=
2)">InputIterator</span><span style=3D"color:rgb(0,0,0)"> </span><span styl=
e=3D"color:rgb(0,0,136)">last</span><span style=3D"color:rgb(102,102,0)">,<=
/span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102=
,0,102)">UnaryPredicate</span><span style=3D"color:rgb(0,0,0)"> pred</span>=
<span style=3D"color:rgb(102,102,0)">)</span><span style=3D"color:rgb(0,0,0=
)"><br>

</span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rg=
b(0,0,0)"><br><code><pre><code><span style=3D"color:rgb(0,0,0)">&nbsp; </sp=
an><var><span style=3D"color:rgb(0,0,136)">typename</span></var><span style=
=3D"color:rgb(0,0,0)"> iterator_traits</span><span style=3D"color:rgb(102,1=
02,0)">&lt;</span><span style=3D"color:rgb(102,0,102)">InputIterator</span>=
<span style=3D"color:rgb(102,102,0)">&gt;<wbr>::</span><span style=3D"color=
:rgb(0,0,0)">difference_type ret </span><span style=3D"color:rgb(102,102,0)=
">=3D</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:r=
gb(0,102,102)">0</span><span style=3D"color:rgb(102,102,0)">;</span><span s=
tyle=3D"color:rgb(0,0,0)"><br>

&nbsp; </span><var><span style=3D"color:rgb(0,0,136)">while</span></var><sp=
an style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">=
(</span><span style=3D"color:rgb(0,0,0)">first</span><span style=3D"color:r=
gb(102,102,0)">!=3D</span><span style=3D"color:rgb(0,0,136)">last</span><va=
r><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102=
,0)">&amp;&amp;</span><span style=3D"color:rgb(0,0,0)"> </span></var><var><=
/var><span style=3D"color:rgb(0,0,0)">!pred</span><span style=3D"color:rgb(=
102,102,0)">(*</span><span style=3D"color:rgb(0,0,0)">first</span><span sty=
le=3D"color:rgb(102,102,0)">))</span><span style=3D"color:rgb(0,0,0)"> </sp=
an><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,=
0,0)"><br>

&nbsp; &nbsp; </span><span style=3D"color:rgb(102,102,0)">++</span><span st=
yle=3D"color:rgb(0,0,0)">ret</span><span style=3D"color:rgb(102,102,0)">;</=
span><span style=3D"color:rgb(0,0,0)"><br>&nbsp; &nbsp; </span><span style=
=3D"color:rgb(102,102,0)">++</span><span style=3D"color:rgb(0,0,0)">first</=
span><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(=
0,0,0)"><br>

&nbsp; </span><span style=3D"color:rgb(102,102,0)">}</span><span style=3D"c=
olor:rgb(0,0,0)"><br>&nbsp; </span><var><span style=3D"color:rgb(0,0,136)">=
return</span></var><span style=3D"color:rgb(0,0,0)"> ret;<br>

}</span><span style=3D"color:rgb(102,102,0)"></span></code></pre></code></s=
pan></code><code><span style=3D"color:rgb(0,0,0)"></span><span style=3D"col=
or:rgb(102,102,0)"></span></code></pre></div></code></div>

<br>What do you think about adding these two components to the algorithms l=
ibrary ?</div>
</blockquote></div>

<p></p>

-- <br />
&nbsp;<br />
--- <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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_1690_26279800.1381295582429--

.
