std.string and ranges

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Feb 11 20:46:31 PST 2009


Andrei Alexandrescu wrote:
> Denis Koroskin wrote:
> [about shuffling ranges]
>> I'm with bearophile here. Not because his version is shorter, but because
>> 1) some containers/ranges might not have a dup method
>> 2) it is potentially faster because it may avoid unnecessary data 
>> copying (relevant for large arrays).
>>
>> That said, having both versions is preferred by me with names like 
>> shuffle and shuffledCopy.
>> The same could be applied to sort - sortedCopy might be useful, too.
> 
> Oh, I understand now. Incidentally topNCopy exists already, and it 
> becomes a sortedCopy for n = input.length. But perhaps an explicit 
> function would drive the point home better.
> 
> About shuffleCopy, you and Leonardo are both right that it could be 
> significantly faster than copying and then shuffling the copy. The 
> solution that I see most in the spirit of the new range design is to 
> have a ShuffledRange that, given another range, iterates it in a random 
> manner. An ordinary copy() call closes the deal if a copy is needed.

Wait, I take that back. I don't know of solid ways to sort into a copy 
or shuffle into a copy. For shuffling I'd need auxiliary memory (in 
addition to the copy itself), and for sorting I'd be better off copying 
and then sorting in place. Could anyone illuminate me about better ways?

Andrei



More information about the Digitalmars-d mailing list