From -4995726660776742560
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8d7ae4b2a3e77f8b
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2003-08-10 10:35:15 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: bop2@telia.com ("Bo Persson")
Newsgroups: comp.std.c++
Subject: Re: Why is there no range type in the standard library?
Date: Sun, 10 Aug 2003 17:35:14 +0000 (UTC)
Organization: Telia Internet
Lines: 96
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <c0pZa.19360$mU6.23558@newsb.telia.net>
References: <95e0e5ef.0308060330.28aefaae@posting.google.com> <bh4192$fan$1@hercules.btinternet.com>
Mime-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Trace: mail2news.demon.co.uk 1060536914 135 10.0.0.1 (10 Aug 2003 17:35:14 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Sun, 10 Aug 2003 17:35:14 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.12)
	id 19lu64-000022-00
	for mail2news@news.news.demon.net; Sun, 10 Aug 2003 17:35:13 +0000
X-Received: from localhost (localhost [[UNIX: localhost]]) by mulga.cs.mu.OZ.AU
	id DAA20720; Mon, 11 Aug 2003 03:35:10 +1000 (EST)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Path: comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Delivered-To: std-c++@ucar.edu
X-Original-Recipient: <comp-std-c++@moderators.isc.org>
X-Newsgroups: comp.std.c++
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-NNTP-Posting-Date: Sun, 10 Aug 2003 12:20:56 CEST
X-Spam-Status: No, hits=-5.4 required=5.0
	tests=BAYES_01,PRIORITY_NO_NAME,QUOTED_EMAIL_TEXT,REFERENCES
	version=2.55
X-Spam-Checker-Version: SpamAssassin 2.55 (1.174.2.19-2003-05-19-exp)
Xref: archiver1.google.com comp.std.c++:20553


"Chris Newton" <chrisnewton@no.junk.please.btinternet.com> skrev i
meddelandet news:bh4192$fan$1@hercules.btinternet.com...
> Stefan Heinzmann wrote [abridged]...
> > Given that the notion of an iterator range is rather fundamental
in
> > the standard library, I wonder why this wasn't turned into a class
> > template in the STL.

Possibly becase it would rule out some kinds of iterators, making the
entire concept less powerful.

>> Something like this:
> >
> > template<typename Iter>
> > struct iterator_range {
> >     Iter begin, end;

This requires the range to be constant. Usually, nothing reqires
container.end() to remain constant in a  loop.

> >
> >     size_t size() const { return std::distance(begin,end); }

This rules out the use of input and output iterators. How would you
compute the distance() for some keyboard input?

> >     bool empty() const { return begin == end; }

This requires end() to be a constant.

> >     bool operator==(const Iter& rhs) const
> >         { return (begin == rhs.begin) && (end == rhs.end); }

This rules out not only input and output iterators, but also filtering
iterators. Just because begin() and end() compares equal, the
intermediate values does not have to be the same, or the same number.

> >     // ... potentially more methods here
> > };
>
> I've often wondered about this myself. A lot of algorithms
inherently
> run over a range, and a single range object more naturally and
flexibly
> represents the required concept than a pair of iterators. Consider,
for
> example, the typical loop implied by many standard algorithms:
>
>    for (iterator_type it = begin_it; it != end_it; ++it) { ... }

I would say that this would be typical for a for_each() call, where
the iterator values *are* captured at the point of call. An ordinary
for loop is much more powerful.

>
> How could we provide iterators over a circular data structure that
> satisfied the usual equality operations, but also allowed us to run
such
> an algorithm over an entire circular container? What would
> container_list::begin() and circular_list::end() mean in a context
like
> that anyway?

The end() function should return a value that is somehow recognized as
the end of the list. Works fine for std::list for example.

The container requirements are quite simple: begin() returns an
iterator that refers to the first element, if any. Otherwise it
returns end(). end() returns an iterator that is different from an
iterator to any of the elements in the container, using one-of-the-end
is just an implementation detail not a requirement.

>
> In general, I think that supporting the concept of a range would
allow
> more natural extension of the standard library to support newer data
> structures and algorithms, as well as offering a cleaner
presentation of
> what is already available.

But you would also need to keep at least some of the current library,
as it presents a more general interface. Then you will have some
algorithms for range types and some for iterators. I don't immediately
see how this makes it cleaner.


Bo Persson
bop2@telia.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



