Re: Andrei's "iterators must go" presentation
On May 30, 6:32 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
If I rewrite my version to use ranges, I end up with:
template< typename C, typename T > static
std::vector<C> split_disp( const C& what, const T& that)
{
typedef typename C::const_range Range;
std::vector<C> ret;
Range range = what.all();
while( !range.empty() )
{
Range range2 = std::find( range, that );
ret.push_back( C(range-range2) );
if (!range2.empty()) range2.popFront(); // skip 'that'
range = range2;
}
if (ret.empty())
ret.push_back( what );
return ret;
}
The only 'problem' that I have with this code is that the range-
arithmetic (range - range2) could be confusing. For example, what would
be the result if the two ranges do not have a common start or end point?
I was thinking of making up a function like intersection, but the rng-
rng was mentioned by someone earlier, though I can't requote because I
find the post...if it exists.
But, you're right, and even if there was an intersection function
that returns what the two ranges have in common, there's no guarantee
the ranges are at all related. And if they are, how does the function
know it unless it tries to iterate one range until it runs into the
other, and tries with the other range if the first iteration failed,
or just do simple position comparisons if they're random access
ranges. (Was that sentence even coherent?)
In short: I don't think a function like that could efficiently exist.
The alternative was a hideously long line constructing a new range
based off the front iterators of rng and rng2, which having access to
seemed discouraged since Andrei says they shouldn't exist,
constructing a C with that range, then push_backing that into the
vector. (And if WE shouldn't be look at the internal iterators, why
should the intersection function?)
ret.push_back( C( C::const_range( rng.front(), rng2.front() ) ) );
Oh, I could always break that into several lines, but doing something
so simple, it doesn't seem I should have to.
So, I guess the problem I'm proposing exists: If you have three
iterators, you can have three ranges with ease. If you have two
ranges, it seems difficult to calculate the third. Does anyone know of
an easy-efficient way?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]