Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
package edu.uwm.cs.util | ||
|
||
case object UniversalCharSet extends Set[Char] { | ||
class ComplementarySet[A] private (private val without: Set[A]) extends collection.immutable.Set[A] { | ||
val size = Math.MAX_INT // should be infinite | ||
|
||
def contains(e: Char) = true | ||
def this() = this(Set()) | ||
|
||
override def exists(f: Char=>Boolean) = true | ||
def contains(e: A) = !without.contains(e) | ||
|
||
override def forall(f: Char=>Boolean) = false | ||
override def exists(f: A=>Boolean) = !without.exists(f) | ||
|
||
def **(that: Set[Char]) = that | ||
override def forall(f: A=>Boolean) = false | ||
|
||
def +(e: Char) = UniversalCharSet | ||
def **(that: Set[A]) = new ComplementarySet(that -- without) | ||
|
||
def -(e: Char) = throw new AssertionError("Cannot remove anything from the universal set") | ||
def +(e: A) = { | ||
if (without contains e) | ||
new ComplementarySet(without - e) | ||
else | ||
this | ||
} | ||
|
||
def -(e: A) = new ComplementarySet(without + e) | ||
|
||
override def ++(other: Iterable[A]) = other match { | ||
case that: ComplementarySet[A] => new ComplementarySet(this.without ** that.without) | ||
|
||
case _ => without -- other | ||
} | ||
|
||
override def ++(that: Iterable[Char]) = UniversalCharSet | ||
override def --(other: Iterable[A]) = other match { | ||
case that: ComplementarySet[A] => new ComplementarySet(this.without ++ that.without) | ||
|
||
case _ => without ++ other | ||
} | ||
|
||
def subsetOf(that: Set[Char]) = false | ||
def subsetOf(other: Set[A]) = other match { | ||
case that: ComplementarySet[A] => that.without subsetOf this.without | ||
case _ => false | ||
} | ||
|
||
def empty[A] = Set[A]() | ||
|
||
def elements = throw new AssertionError("Cannot enumerate the universal set") | ||
def elements = throw new AssertionError("Cannot enumerate the complementary set") | ||
|
||
override def toString = "UniversalCharSet" | ||
override def toString = "ComplementarySet(%s)".format(without) | ||
|
||
override def equals(other: Any) = other match { | ||
case that: AnyRef => this eq that | ||
case that: ComplementarySet[A] => this.without == that.without | ||
case _ => false | ||
} | ||
|
||
override def hashCode = 0 // why not? | ||
override def hashCode = ~(without.hashCode) | ||
} | ||
|
||
case object UniversalCharSet extends ComplementarySet[Char] |