|
Boost : |
Subject: Re: [boost] [Containers] Review
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2011-08-11 10:10:08
Nathan Ridge wrote:
>> Regarding the batched insert, we have previously discussed the
>> complexity of inserting a range. Fundamentally, however, I don't think
>> it is acceptable to have efficient insertion ONLY in the case of the
>> insert(begin,end) method: it is too common to want to insert several
>> values in a loop without creating a temporary container. I'm not aware
>> of any established (i.e. std:: or boost::) pattern for doing this:
>
> There is no need to create a temporary container to use a method that
> takes a pair of iterators. With the help of the Boost.Range library
> and some lambdas (Boost.Phoenix or C++0x, depending on your taste
> and constraints), you should be able to create a lazy range view
> of wherever you're getting your data from in your loop.
Well, yes and no. If I show this code to a Java or C programmer, they
can probably work out what it does very easily:
int i = 0;
while (i != 10) {
container.insert(i);
i = f(i);
}
How easily would they understand your version?
There are also cases where it is fundamentally hard to write a range
generator without using e.g. co-routines:
void towers_of_hanoi(container& c, string from, string to, string tmp,
int n)
{
if (n==1) {
c.append(make_pair(from,to)); // "Move a disk from " from " to " to."
} else {
towers_of_hanoi(c,from,tmp,to,n-1);
towers_of_hanoi(c,from,to,tmp,1);
towers_of_hanoi(c,tmp,to,from,n-1);
}
}
I really don't think it's acceptable to require users to use tricky and
limited features like lazy range generators just to get efficient
insertion into a container.
Regards, Phil.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk