Set operation like cartesian product

Andrea Fontana nospam at example.com
Thu Feb 28 08:17:07 PST 2013


On Thursday, 28 February 2013 at 15:32:00 UTC, Andrea Fontana 
wrote:
> I see cartesianProduct in std.algorithm. I read:
>
> auto N = sequence!"n"(0); // the range of natural numbers
> auto N2 = cartesianProduct(N, N); // the range of all pairs of 
> natural numbers
>
> So it gives (0,0) (0,1) (1,0) ... and so on.
>
> Is there a way to generate only tuple:
>
> a[0] > a[1] (0,1) (0,2) ... (1,2) (1,3) .. (2,3) (2,4)
>
> or
>
> a[0] >= a[1] (0,0) (0,1) (0,2) ... (1,1) (1,2) (1,3) .. (2,2) 
> (2,3) (2,4)
>
> Or the only way is use filter!(...) after cartesianProduct?

PS: In my case I can't use filter!(...): I'm trying to compare an 
array of struct with itself to find similar ones, so I can't 
filter Tuple(struct, struct) in any way...

I have to write:

for (i; 0..arr.length) for(j; i+1..arr.length) // check arr[i] 
and arr[j] for similarities

Any ideas?




More information about the Digitalmars-d-learn mailing list