« Return to Thread: operator overloading

Re: operator overloading

by Tom Nichols :: Rate this Message:

Reply to Author | View in Thread

Ah, yes I see your issue.  The > and < operators (comparison) assume that
A < B  OR B <= A will always be true.  When you're talking about sets,
A may not be a subset of B AND B might not be a subset of A.  So I
suppose the less than/ greater than does not really equate to subset
notation.  You could use leftShift and rightShift...

Try this?

class MySet {
  def items = []
  public boolean leftShift(other) {
    if( ! (other instanceof MySet) ) return false
    (this.items - other.items).size == 0
  }

  public boolean rightShift(other) {
    if( ! (other instanceof MySet) ) return -1
    (other.items - this.items).size == 0
  }

  public boolean equals( other ) {
    if( ! (other instanceof MySet) ) return -1
    (other.items - this.items).size == 0 &&
      (this.items - other.items).size == 0
  }
}

def set1 = new MySet( items:[1,2,3] )
def set2 = new MySet( items:[1,2,3] )
assert set1 == set2
assert set1 << set2 && set2 << set1
assert set1 >> set2 && set2 >> set1

set2.items -= 3   //set2 == [1,2]
assert set1 != set2
assert set2 << set1 && set1 >> set2
assert !(set1 << set2) && !(set2 >> set1)

set3 = new MySet( items:[1,8] )
assert !( set1 >> set3 ) && !( set3 >> set1 )
assert !( set1 << set3 ) && !( set3 << set1 )

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: operator overloading