KennyTM~ wrote:
bearophile wrote:
Sergey Gromov:
I'd use dot "⋅" and cross "×" products for 3D, union "∪" and
intersection "∩", subset "⊂" and superset "⊃" and their
negative forms.
I don't think I'd use anything else.
I just want to note that the whole thread is almost unreadable on the
digitalmars.com/webnews/, because it doesn't digest unicode chars at
all. So adding unicode to D will give problems to show code.
Unrelated to the unicode, but related on those opSubset, opSuperset, etc:
while implementing a set() class with the same API of the Python sets,
I have seen there are the following operators/methods too:
issubset(other) set <= other Test whether every element in the set is
in other.
set < other Test whether the set is a true subset of other, that is,
set <= other and set != other.
issuperset(other) set >= other Test whether every element in other is
in the set.
set > other Test whether the set is a true superset of other, that is,
set >= other and set != other.
A full opCmp can't be defined on sets, so I think in D1 we can't
overload <= >= among sets... I think this is a problem has to be
solved in D2, because sets are important enough.
Bye,
bearophile
If the two sets are incomparable, just return NaN... We need an opCmp
that returns a float :)
Actually I've made a working solution. Even the exotic operators like
!<= (not a subset of, ⊈) works too. It's designed for demonstration, not
performance, though.
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.
}