diff options
author | michelou <michelou@epfl.ch> | 2008-02-22 15:23:15 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2008-02-22 15:23:15 +0000 |
commit | bb37eb067be49ed9f7b070a7cab354801c38f9ad (patch) | |
tree | e3081526b2ec16da0399d1c37226f8a3da0835e2 | |
parent | ae85da1eacee620e4d33e048715f52a9aa34b426 (diff) | |
download | scala-bb37eb067be49ed9f7b070a7cab354801c38f9ad.tar.gz scala-bb37eb067be49ed9f7b070a7cab354801c38f9ad.tar.bz2 scala-bb37eb067be49ed9f7b070a7cab354801c38f9ad.zip |
fixed #468
-rw-r--r-- | src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala | 22 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/backend/opt/DeadCodeElimination.scala | 4 | ||||
-rw-r--r-- | src/library/scala/List.scala | 55 | ||||
-rw-r--r-- | test/files/run/lists.check | 12 | ||||
-rw-r--r-- | test/files/run/lists.scala | 138 |
5 files changed, 128 insertions, 103 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala b/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala index ed7e1cefc8..23cc7b94e1 100644 --- a/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala +++ b/src/compiler/scala/tools/nsc/backend/msil/GenMSIL.scala @@ -1,5 +1,5 @@ /* NSC -- new scala compiler - * Copyright 2005-2007 LAMP/EPFL + * Copyright 2005-2008 LAMP/EPFL * @author Nikolay Mihaylov */ @@ -581,7 +581,7 @@ abstract class GenMSIL extends SubComponent { } if (mcode != null) { - for (local <- m.locals.diff(m.params)) { + for (local <- m.locals -- m.params) { if (settings.debug.value) log("add local var: " + local + ", of kind " + local.kind) val t: MsilType = msilType(local.kind) @@ -861,7 +861,7 @@ abstract class GenMSIL extends SubComponent { // problem: block may already be added, and and needs to be moved. // if nextblock NOT in b: check if nextblock in blocksToPut, if NOT, check if movable, else don't put if (nextBlock != null && b.contains(nextBlock)) { - val blocksToAdd = nextBlock :: b.diff(List(nextBlock)) + val blocksToAdd = nextBlock :: (b - nextBlock) nextBlock = null addBlocks(blocksToAdd) } @@ -874,7 +874,7 @@ abstract class GenMSIL extends SubComponent { { // the block is not part of some catch or finally code currentBlock.addBasicBlock(x) - blocksToPut = blocksToPut.diff(List(x)) + blocksToPut = blocksToPut - x if (settings.debug.value) log(" -> addBlocks(" + xs + ")") addBlocks(xs) } else { @@ -885,7 +885,7 @@ abstract class GenMSIL extends SubComponent { // is optimized by compiler (no try left) if(untreatedHandlers.forall(h => (!h.blocks.contains(x) || h.covered.isEmpty))) { - blocksToPut = blocksToPut.diff(List(x)) + blocksToPut = blocksToPut - x addBlocks(xs) } else addBlocks(xs ::: List(x)) @@ -981,7 +981,7 @@ abstract class GenMSIL extends SubComponent { //else () //assert(firstBlockAfter(exh) == outside(0), "try/catch leaving to multiple targets: " + firstBlockAfter(exh) + ", new: " + outside(0)) val last = leaving(0)._1 - (blocks.diff(List(last)) ::: List(last), None) + ((blocks - last) ::: List(last), None) } else { val outside = leaving.flatMap(p => p._2) //assert(outside.forall(b => b == outside(0)), "exception-block leaving to multiple targets") @@ -1003,7 +1003,7 @@ abstract class GenMSIL extends SubComponent { // shorter try-catch-finally last (the ones contained in another) affectedHandlers = affectedHandlers.sort({(h1, h2) => h1.covered.length > h2.covered.length}) affectedHandlers = affectedHandlers.filter(h => {h.covered.length == affectedHandlers(0).covered.length}) - untreatedHandlers = untreatedHandlers.diff(affectedHandlers) + untreatedHandlers = untreatedHandlers -- affectedHandlers // more than one catch produces more than one exh, but we only need one var singleAffectedHandler: ExceptionHandler = affectedHandlers(0) // List[ExceptionHandler] = Nil @@ -1017,7 +1017,7 @@ abstract class GenMSIL extends SubComponent { h1.addBlock(block) case None => () } - val orderedCatchBlocks = h1.startBlock :: adaptedBlocks.diff(List(h1.startBlock)) + val orderedCatchBlocks = h1.startBlock :: (adaptedBlocks - h1.startBlock) exceptionBlock match { case Some(excBlock) => @@ -1048,7 +1048,7 @@ abstract class GenMSIL extends SubComponent { singleAffectedHandler.finalizer.addBlock(block) case None => () } - val blocks = singleAffectedHandler.finalizer.startBlock :: blocks0.diff(List(singleAffectedHandler.finalizer.startBlock)) + val blocks = singleAffectedHandler.finalizer.startBlock :: (blocks0 - singleAffectedHandler.finalizer.startBlock) currentBlock = excBlock.finallyBlock addBlocks(blocks) } @@ -1148,7 +1148,7 @@ abstract class GenMSIL extends SubComponent { "untreated exception handlers left: " + untreatedHandlers) // remove catch blocks from empty handlers (finally-blocks remain) untreatedHandlers.foreach((h) => { - orderedBlocks = orderedBlocks.diff(h.blocks) + orderedBlocks = orderedBlocks -- h.blocks }) // take care of order in which exHInstructions are executed (BeginExceptionBlock as last) @@ -1810,7 +1810,7 @@ abstract class GenMSIL extends SubComponent { idx += 1 // sizeOf(l.kind) } - val locvars = m.locals.diff(params) + val locvars = m.locals -- params idx = 0 for (l <- locvars) { diff --git a/src/compiler/scala/tools/nsc/backend/opt/DeadCodeElimination.scala b/src/compiler/scala/tools/nsc/backend/opt/DeadCodeElimination.scala index b7cc9998cd..04b23ed493 100644 --- a/src/compiler/scala/tools/nsc/backend/opt/DeadCodeElimination.scala +++ b/src/compiler/scala/tools/nsc/backend/opt/DeadCodeElimination.scala @@ -83,8 +83,8 @@ abstract class DeadCodeElimination extends SubComponent { mark sweep(m) accessedLocals = accessedLocals.removeDuplicates - if (m.locals.diff(accessedLocals).length > 0) { - log("Removed dead locals: " + m.locals.diff(accessedLocals)) + if ((m.locals -- accessedLocals).length > 0) { + log("Removed dead locals: " + (m.locals -- accessedLocals)) m.locals = accessedLocals.reverse } } diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala index 7d65d6d35c..b6086e2460 100644 --- a/src/library/scala/List.scala +++ b/src/library/scala/List.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2008, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -436,14 +436,30 @@ sealed abstract class List[+A] extends Seq[A] { * Add an element <code>x</code> at the beginning of this list. * </p> * - * @param x the element to append. - * @return the list with <code>x</code> appended at the beginning. + * @param x the element to prepend. + * @return the list with <code>x</code> added at the beginning. * @ex <code>1 :: List(2, 3) = List(2, 3).::(1) = List(1, 2, 3)</code> */ def ::[B >: A] (x: B): List[B] = new scala.::(x, this) /** <p> + * Add an element <code>x</code> at the end of this list. + * </p> + * + * @param x the element to append. + * @return the list with <code>x</code> added at the end. + */ + def +[B >: A](x: B): List[B] = + if (isEmpty) List(x) + else { + val buf = new ListBuffer[B] + this copyToBuffer buf + buf += x + buf.toList + } + + /** <p> * Returns a list resulting from the concatenation of the given * list <code>prefix</code> and this list. * </p> @@ -464,6 +480,15 @@ sealed abstract class List[+A] extends Seq[A] { b.prependToList(this) } + /** Appends two list objects. + */ + override def ++[B >: A](that: Iterable[B]): List[B] = { + val buf = new ListBuffer[B] + this copyToBuffer buf + that copyToBuffer buf + buf.toList + } + /** Reverse the given prefix and append the current list to that. * This function is equivalent to an application of <code>reverse</code> * on the prefix followed by a call to <code>:::</code>, but more @@ -1168,8 +1193,19 @@ sealed abstract class List[+A] extends Seq[A] { * @param that the list of elements to remove from this list. * @return this list without the elements of the given list * <code>that</code>. + * @deprecated use <code>--</code> instead */ - def diff[B >: A](that: List[B]): List[B] = { + @deprecated + def diff[B >: A](that: List[B]): List[B] = this -- that + + /** Computes the difference between this list and the given list + * <code>that</code>. + * + * @param that the list of elements to remove from this list. + * @return this list without the elements of the given list + * <code>that</code>. + */ + def -- [B >: A](that: List[B]): List[B] = { val b = new ListBuffer[B] var these = this while (!these.isEmpty) { @@ -1179,13 +1215,22 @@ sealed abstract class List[+A] extends Seq[A] { b.toList } + /** Computes the difference between this list and the given object + * <code>x</code>. + * + * @param x the object to remove from this list. + * @return this list without the elements of the given object + * <code>x</code>. + */ + def - [B >: A](x: B): List[B] = + this -- List(x) + def flatten[B](implicit f : A => Iterable[B]) : List[B] = { val buf = new ListBuffer[B] foreach(f(_).foreach(buf += _)) buf.toList } - /** Computes the intersection between this list and the given list * <code>that</code>. * diff --git a/test/files/run/lists.check b/test/files/run/lists.check deleted file mode 100644 index 1da0075273..0000000000 --- a/test/files/run/lists.check +++ /dev/null @@ -1,12 +0,0 @@ -test check_count was successful -test check_diff was successful -test check_exists was successful -test check_filter was successful -test check_foldLeft was successful -test check_forall was successful -test check_intersect was successful -test check_remove was successful -test check_union was successful -test check_zip was successful -test check_zipAll was successful - diff --git a/test/files/run/lists.scala b/test/files/run/lists.scala index 947433d6a6..fc0fec466e 100644 --- a/test/files/run/lists.scala +++ b/test/files/run/lists.scala @@ -5,111 +5,103 @@ //############################################################################ -object Test { +import testing.SUnit._ + +/** Test the Scala implementation of class <code>scala.List</code>. + * + * @author Stephane Micheloud + */ +object Test extends TestConsoleMain { + def suite = new TestSuite( + Test1, //count, exists, filter, .. + Test2 //#468 + ) +} - val xs1 = List(1, 2, 3) - val xs2 = List('a', 'b') - val xs3 = List(List(1, 2), List(4, 5)) - val xs4 = List(2, 4, 6, 8) - val xs5 = List(List(3, 4), List(3), List(4, 5)) +object Test1 extends TestCase("ctor") with Assert { + override def enableStackTrace = false + override def runTest { + val xs1 = List(1, 2, 3) + val xs2 = List('a', 'b') + val xs3 = List(List(1, 2), List(4, 5)) + val xs4 = List(2, 4, 6, 8) + val xs5 = List(List(3, 4), List(3), List(4, 5)) - def check_count: Int = { + { val n1 = xs1 count { e => e % 2 != 0 } val n2 = xs4 count { e => e < 5 } - n1 + n2 + assertEquals("check_count", 4, n1 + n2) } - - def check_diff: Int = { + /* deprecated + { val ys1 = xs1 diff xs4 val ys2 = xs3 diff xs5 - ys1.length + ys2.length - } - - def check_exists: Boolean = { + assertEquals("check_diff", 3, ys1.length + ys2.length) + }*/ + { val b1 = xs1 exists { e => e % 2 == 0 } val b2 = xs4 exists { e => e == 5 } - b1 & b2 + assertEquals("check_exists", false , b1 & b2) } - - def check_filter: Int = { + { val ys1 = xs1 filter { e => e % 2 == 0 } val ys2 = xs4 filter { e => e < 5 } - ys1.length + ys2.length + assertEquals("check_filter", 3, ys1.length + ys2.length) } - - def check_foldLeft: Int = { + { val n1 = xs1.foldLeft(0)((e1, e2) => e1 + e2) val ys1 = xs4.foldLeft(List[Int]())((e1, e2) => e2 :: e1) - n1 + ys1.length + assertEquals("check_foldLeft", 10, n1 + ys1.length) } - - def check_forall: Boolean = { + { val b1 = xs1 forall { e => e < 10} val b2 = xs4 forall { e => e % 2 == 0 } - b1 & b2 + assertEquals("check_forall", true, b1 & b2) } - - def check_intersect: Int = { + { val ys1 = xs1 intersect xs4 val ys2 = xs3 intersect xs5 - ys1.length + ys2.length + assertEquals("check_intersect", 2, ys1.length + ys2.length) } - - def check_remove: Int = { + { val ys1 = xs1 remove { e => e % 2 != 0 } val ys2 = xs4 remove { e => e < 5 } - ys1.length + ys2.length + assertEquals("check_remove", 3, ys1.length + ys2.length) } - - def check_union: Int = { - val ys1 = xs1 union xs4; - val ys2 = xs3 union xs5; - ys1.length + ys2.length + { + val ys1 = xs1 union xs4 + val ys2 = xs3 union xs5 + assertEquals("check_union", 10, ys1.length + ys2.length) } - - def check_zip: Int = { - val ys1 = xs1 zip xs2; - val ys2 = xs1 zip xs3; - ys1.length + ys2.length + { + val ys1 = xs1 zip xs2 + val ys2 = xs1 zip xs3 + assertEquals("check_zip", 4, ys1.length + ys2.length) } - - def check_zipAll: Int = { + { val ys1 = xs1.zipAll(xs2, 0, '_') val ys2 = xs2.zipAll(xs1, '_', 0) val ys3 = xs1.zipAll(xs3, 0, List(-1)) - ys1.length + ys2.length + ys3.length + assertEquals("check_zipAll", 9, ys1.length + ys2.length + ys3.length) } - - def check_success[A](name: String, closure: => A, expected: A) { - print("test " + name) - try { - val actual: A = closure - if (actual == expected) - print(" was successful") - else - print(" failed: expected "+ expected +", found "+ actual) - } - catch { - case exception: Throwable => - print(" raised exception " + exception) - } - println } +} + +object Test2 extends TestCase("t0468") with Assert { + override def enableStackTrace = false + override def runTest { + val xs1 = List(1, 2, 3) + val xs2 = List(0) - def main(args: Array[String]) { - check_success("check_count", check_count, 4) - check_success("check_diff", check_diff, 3) - check_success("check_exists", check_exists, false) - check_success("check_filter", check_filter, 3) - check_success("check_foldLeft", check_foldLeft, 10) - check_success("check_forall", check_forall, true) - check_success("check_intersect", check_intersect, 2) - check_success("check_remove", check_remove, 3) - check_success("check_union", check_union, 10) - check_success("check_zip", check_zip, 4) - check_success("check_zipAll", check_zipAll, 9) - println + val ys1 = xs1 + 4 + assertEquals("check_+", List(1, 2, 3, 4), ys1) + + val ys2 = ys1 - 4 + assertEquals("check_-", xs1, ys2) + + val n2 = (xs1 ++ ys1).length + val n3 = (xs1 ++ Nil).length + val n4 = (xs1 ++ ((new collection.mutable.ArrayBuffer[Int]) + 0)).length + assertEquals("check_++", 14, n2 + n3 + n4) } } - -//############################################################################ |