import std.stdio; class StupidSet(T) { private bool[T] data; void insert(in T x[] ...) { foreach (val; x) data[val] = true; } // check if two sets are equal. bool opEquals(in typeof(this) y) const { if (y.data.length != data.length) return false; foreach (idx, dummy; y.data) { if (!(idx in data)) return false; } return true; } // compare two sets float opCmp(in typeof(this) y) const { auto y_data_len = y.data.length, x_data_len = data.length; // if I have less elements than you, test if I am a subset of you. if (x_data_len < y_data_len) { foreach (idx, dummy; data) { if (!(idx in y.data)) // the sets are not contained within each other; we are not comparable. return float.nan; } // I am a strict subset of you. return -1.f; // reuse opEquals. } else if (x_data_len == y_data_len) return opEquals(y) ? 0 : float.nan; // reverse the roles of you and me. else return -y.opCmp(this); } } void main () { auto s = new StupidSet!(int); auto t = new StupidSet!(int); s.insert(2, 4); t.insert(2, 3); writefln(s > t); // strict superset writefln(s >= t); // superset writefln(s == t); // equal writefln(s <= t); // subset writefln(s < t); // strict subset writefln(s != t); // not equal writefln(s !>= t); // not superset, etc. writefln(); // should print 5 false and 2 true in order. t.insert(4); // now t is a strict superset of s. writefln(s > t); // strict superset writefln(s >= t); // superset writefln(s == t); // equal writefln(s <= t); // subset writefln(s < t); // strict subset writefln(s != t); // not equal writefln(s !>= t); // not superset, etc. // should print 3 false and 4 true in order. }