Set Intersection and Set Difference on Compile-Time lists
David Sanders via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 25 10:08:36 PDT 2017
I have two compile-time lists of types. How do I find their set
intersection (to determine if one is a subset of the other) and
their set difference? See the block comments below:
import std.variant;
alias Zero = void;
struct One{}
struct Difference(T, U) {
static if (is(U == Zero)) alias type = T;
else static if(is(T == U)) alias type = Zero;
else static if (is(T _ == VariantN!V, V...)) {
static if(is(U __ == VariantN!W, W...)) {
/* if W[1..$] is a subset of V[1..$] alias type =
Algebraic!(setDifference(V[1..$], W[1..$])) */
} else {
/* if U is subset of V[1..$] alias type =
Algebraic!(setDifference(V[1..$], U)) */
}
} else static if(is(U _ == VariantN!V, V...)) {
/* if V[1..$] is a single element set containing T alias
type = Zero */
}
unittest
{
static assert (is(Zero == Difference!(Zero, Zero).type));
static assert (is(One == Difference!(One, Zero).type));
static assert (!is(Difference!(Zero, One).type));
static assert (is(Algebraic!(One) ==
Difference!(Algebraic!(One, One), Algebraic!(One)).type));
}
}
void main() {
alias type = Difference!(One, Zero).type;
}
More information about the Digitalmars-d-learn
mailing list