diff options
author | Martin Odersky <odersky@gmail.com> | 2003-02-14 14:39:24 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2003-02-14 14:39:24 +0000 |
commit | 21b147f7caf8b558b00044f493cf9da392c7e30e (patch) | |
tree | a964142a10a16e7c9a64b03e44fdde1d9f1b63be /test/files/pos/IntSet.scala | |
parent | fd3f10df3cd88fe3b970f8312a479ae63f0803b8 (diff) | |
download | scala-21b147f7caf8b558b00044f493cf9da392c7e30e.tar.gz scala-21b147f7caf8b558b00044f493cf9da392c7e30e.tar.bz2 scala-21b147f7caf8b558b00044f493cf9da392c7e30e.zip |
Initial version.
Diffstat (limited to 'test/files/pos/IntSet.scala')
-rw-r--r-- | test/files/pos/IntSet.scala | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/test/files/pos/IntSet.scala b/test/files/pos/IntSet.scala new file mode 100644 index 0000000000..850a5cb923 --- /dev/null +++ b/test/files/pos/IntSet.scala @@ -0,0 +1,36 @@ +trait IntSet { + def incl(x: Int): IntSet; + def contains(x: Int): Boolean; + def foreach(f: Int => Unit): Unit; + def union(that: IntSet): IntSet; +} +module Empty extends IntSet { + def contains(x: Int): Boolean = Boolean.False; + def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty); + def foreach(f: Int => Unit): Unit = (); + def union(that: IntSet): IntSet = that; +} +class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet { + def contains(x: Int): Boolean = + if (x < elem) left contains x + else if (x > elem) right contains x + else True; + def incl(x: Int): IntSet = + if (x < elem) new NonEmpty(elem, left incl x, right) + else if (x > elem) new NonEmpty(elem, left, right incl x) + else this; + def foreach(f: Int => Unit): Unit = { + left foreach f; + f(elem); + right foreach f; + } + def union(that: IntSet): IntSet = (left union (right union that)) incl elem; +} +module test { + def main = { + val x = Empty incl 1 incl 2; + val y = Empty incl 2 incl 3; + x foreach java.lang.System.out.println; + y foreach java.lang.System.out.println; + } +}
\ No newline at end of file |