1) In the - method, test if the list are allready sorted (which is a O(n+m)
operation), them adapt the algo (ie O(n+m) or O(n*m)).
But as soon as you have used '+' to generate the union of two sets, it will
almost always be unordered again anyway.
IMO it would be better to have a different class for that behaviour (i.e.
your option 3). If there's nothing like it under Library/Datastructure in
RAA, it could be yours
Or you could use one of the 'tree' implementations, which maintains an
ordered list (not quite the same as a Set, since it allows duplicate keys)
I'm not sure how organised this is though - it would be nice to have a
common 'Tree' base class with subclasses for different implementations.
2) In the - method do a sort first (which is a O(n*ln n+m*ln m) operation)
and them do the - with the O(n+m) algo. Because the - operator is a set
operator, the order doesn't really make sense...
To me, an Array is by definition an ordered list, so I wouldn't want it
randomly reordered. I'd also have said that A-B could be a useful array
(rather than set) operation, to remove a few elements from an array:
[5,0,1,3,7]-[0]
=> [5, 1, 3, 7]
Except that in Ruby it also eliminates duplicates from A, so it can't really
be used in this way:
['h','e','l','l','o']-['e']
=> ["h", "l", "o"]
So, A- is not a null operation
I'm sure there must be a reason for this. It seems that an Array is also
intended to be a poor man's Set, but as you've found, it doesn't do that job
very well.
Regarding your Set implementation as ordered Arrays: there's the potential
for even faster operation. For example, the union (+) operator can take
advantage of the fact that its two operands are already sorted. A 'merge'
operation, at the same time removing duplicates, can be done in O(n+m).
Regards,
Brian.