std.algorithm set operations on SortedRanges only?

bearophile bearophileHUGS at lycos.com
Mon Jul 22 14:22:41 PDT 2013


In Phobos we have std.range.assumeSorted, std.algorithm.isSorted, 
and various sort() functions that return a SortedRange. If you 
call a release() on a SortedRange you get the range that's under 
it (often an array).

(A SortedRange was so far only a RandomAccessRange, but I've 
shown Andrei that a ForwardRange is OK too, so it will be 
expanded for all input ranges.)

Given all that, isn't it good to have the "Set operations" (that 
actually work on _sorted bags_, so their name is misleading and 
probably even wrong) of std.algorithm, like 
std.algorithm.setDifference accept only SortedRanges?

SetDifference!(less, SortedRange1, SortedRange2) 
setDifference(alias less = "a < b", R1, R2)(1 r1, SortedRange2 
r2);

The disadvantage is that such functions become a bit more fussy, 
so you can't give them normal arrays. The advantage is that if 
you have to keep around sorted data, you can perform on it all 
kinds of elaborations available only with sorted data and the 
type system will catch you mistakes like using unsorted data 
where sorted is required.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list