www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.algorithm set operations on SortedRanges only?

reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 22 2013
parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Monday, 22 July 2013 at 21:22:42 UTC, bearophile wrote:
 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);
I would expect functions that require a sorted range to take a SortedRange.
Jul 22 2013