|
Boost : |
From: Victor A. Wagner, Jr. (vawjr_at_[hidden])
Date: 2002-05-17 02:27:01
At Wednesday 2002/05/15 19:02, you wrote:
>On Wed, 15 May 2002, Dave Harris wrote:
>
> > In-Reply-To: <Pine.GSO.4.05.10205160011080.5723-100000_at_galois>
> > On Thu, 16 May 2002 00:18:41 +1000 (EST) Damien Fisher
> > (damien_at_[hidden]) wrote:
> > > I would like to see something like this in Boost:
> > >
> > > template<typename Container>
> > > bool is_single_element(const Container &cont);
> >
> > Should it be defined for iterator pairs as well as, or instead of,
> > containers? Most algorithms work on iterators.
>
>I like this. Someone pointed out to me it in a private e-mail that a
>length-checked distance function might fit in better with the current
>STL naming, eg:
>
>template<typename FwdIt>
>std::iterator_traits<FwdIt>::distance_type n
>distance_n(
> FwdIt first, FwdIt last,
> std::iterator_traits<FwdIt>::distance_type n
>)
>{
> std::iterator_traits<FwdIt>::distance_type i = 0;
> while(i != n && first != last)
> {
> ++first;
> ++i;
> }
>
> return i;
>}
>
>With an appropriate specialization for random-access iterators.
>
>Do people prefer this? If so, I can do it properly (add the appropriate
>specialization), add test cases and docs, etc. I have wanted this many
>times, and it seems to have gotten a reasonable response (mostly
>pointing out the mistakes in my example implementation, which I hadn't
>really meant as a real implementation, and which was written late at
>night, but, hey, mea culpa for writing e-mails half-asleep :) ).
That's very nice
... unfortunately, it doesn't "drop in" to any of the algorithms which only
will use ++iter and a != test for "end".
Can you think of any way to "wrap" it so that we could call copy, or any of
the other algorithms with an "iterator" which somehow does your distance_n
when ++ is "applied" ??
This would allow (well, at least me) to write things like:
copy(stride_iterator(cont.begin(), cont.end(), 5), cont.end(),
someoutputiterator); // copy every 5th element
I suppose the "ending condition" wouldn't actually be necessary if the
operator !=() were designed to return false iff it had become == the
"cont.end()" provided at construction time.
>_______________________________________________
>Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Victor A. Wagner Jr. http://rudbek.com
PGP RSA fingerprint = 4D20 EBF6 0101 B069 3817 8DBF C846 E47A
PGP D-H fingerprint = 98BC 65E3 1A19 43EC 3908 65B9 F755 E6F4 63BB 9D93
The five most dangerous words in the English language:
"There oughta be a law"
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk