From 0f87b7b5780267d05d467a2f0075ee746f2cef2f Mon Sep 17 00:00:00 2001 From: Stefan Zeiger Date: Fri, 2 Dec 2011 15:26:03 +0100 Subject: Enumeration and Enumeration.ValueSet improvements - Make Enumeration.ValueSet a SortedSet and back it by a BitSet - Add toBitMask and fromBitMask methods for value sets - Add an Ordering for the values - Deprecate names seq in the Enumeration constructor - Add + method to Value for easy ValueSet creation --- test/files/run/enums.check | 10 ++++++++++ test/files/run/enums.scala | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'test/files/run') diff --git a/test/files/run/enums.check b/test/files/run/enums.check index f53aba8794..93eadae6e3 100644 --- a/test/files/run/enums.check +++ b/test/files/run/enums.check @@ -3,3 +3,13 @@ test Test2 was successful test Test3 was successful test Test4 was successful +D1.ValueSet(North, East) +D2.ValueSet(North, East) +D1.ValueSet(North, East, West) +D2.ValueSet(North, East, West) +List(101) +List(101) +D1.ValueSet(North, East) +D2.ValueSet(North, East) +WeekDays.ValueSet(Tue, Wed, Thu, Fri) + diff --git a/test/files/run/enums.scala b/test/files/run/enums.scala index 6dda8cbc6e..8c6c88ea07 100644 --- a/test/files/run/enums.scala +++ b/test/files/run/enums.scala @@ -65,6 +65,35 @@ object Test4 { } } +object Test5 { + + object D1 extends Enumeration(0) { + val North, South, East, West = Value; + } + + object D2 extends Enumeration(-2) { + val North, South, East, West = Value; + } + + object WeekDays extends Enumeration { + val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value + } + + def run { + val s1 = D1.ValueSet(D1.North, D1.East) + val s2 = D2.North + D2.East + println(s1) + println(s2) + println(s1 + D1.West) + println(s2 + D2.West) + println(s1.toBitMask.map(_.toBinaryString).toList) + println(s2.toBitMask.map(_.toBinaryString).toList) + println(D1.ValueSet.fromBitMask(s1.toBitMask)) + println(D2.ValueSet.fromBitMask(s2.toBitMask)) + println(WeekDays.values.range(WeekDays.Tue, WeekDays.Sat)) + } +} + //############################################################################ // Test code @@ -94,6 +123,8 @@ object Test { check_success("Test3", Test3.run, 1); check_success("Test4", Test4.run, 0); Console.println; + Test5.run; + Console.println; } } -- cgit v1.2.3 From db7bd31896231cbb8f018ffd8a82bc956d066e40 Mon Sep 17 00:00:00 2001 From: Simon Ochsenreither Date: Fri, 2 Dec 2011 15:29:51 +0100 Subject: Makes Range#sum an O(1) operation instead of an O(n) one. Partially fixes SI-4658. NumericRange stays slow, thanks to the brilliant idea that Numeric doesn't need a division operation. --- src/library/scala/collection/immutable/Range.scala | 7 ++ test/files/run/t4658.check | 80 ++++++++++++++++++++++ test/files/run/t4658.scala | 41 +++++++++++ 3 files changed, 128 insertions(+) create mode 100644 test/files/run/t4658.check create mode 100644 test/files/run/t4658.scala (limited to 'test/files/run') diff --git a/src/library/scala/collection/immutable/Range.scala b/src/library/scala/collection/immutable/Range.scala index 47ce2f0341..3736096f36 100644 --- a/src/library/scala/collection/immutable/Range.scala +++ b/src/library/scala/collection/immutable/Range.scala @@ -211,6 +211,13 @@ extends collection.AbstractSeq[Int] final def contains(x: Int) = isWithinBoundaries(x) && ((x - start) % step == 0) + final override def sum[B >: Int](implicit num: Numeric[B]): Int = { + val len = length + if (len == 0) 0 + else if (len == 1) head + else (len.toLong * (head + last) / 2).toInt + } + override def toIterable = this override def toSeq = this diff --git a/test/files/run/t4658.check b/test/files/run/t4658.check new file mode 100644 index 0000000000..743b0faee3 --- /dev/null +++ b/test/files/run/t4658.check @@ -0,0 +1,80 @@ +Ranges: +1073741824 +1073741824 +0 +0 +55 +25 +1 +-45 +-55 +0 +-24 +-30 +0 +-40 +-55 +-10 +-24 +-30 +-10 +IntRanges: +Disabled #1 +Disabled #2 +0 +0 +55 +25 +1 +-45 +-55 +0 +-24 +-30 +0 +-40 +-55 +-10 +-24 +-30 +-10 +LongRanges: +Disabled #1 +Disabled #2 +0 +0 +55 +25 +1 +-45 +-55 +0 +-24 +-30 +0 +-40 +-55 +-10 +-24 +-30 +-10 +BigIntRanges: +Disabled #1 +Disabled #2 +0 +0 +55 +25 +1 +-45 +-55 +0 +-24 +-30 +0 +-40 +-55 +-10 +-24 +-30 +-10 diff --git a/test/files/run/t4658.scala b/test/files/run/t4658.scala new file mode 100644 index 0000000000..e1799fae9b --- /dev/null +++ b/test/files/run/t4658.scala @@ -0,0 +1,41 @@ +import scala.collection.immutable.NumericRange +//#4658 +object Test { + + case class R(start: Int, end: Int, step: Int = 1, inclusive: Boolean = true) + + val rangeData = Array( + R(1, Int.MaxValue), R(-Int.MaxValue, -1), R(0, 0), R(0,0, inclusive = false), R(1,10), + R(1,10,2), R(1,10,11), R(-10, -5), R(-10, 0), R(-10, 10), R(-10, -5, 2), R(-10, 0, 2), R(-10, 10, 2), + R(-10, -5, inclusive = false), R(-10, 0, inclusive = false), R(-10, 10, inclusive = false), + R(-10, -5, 2, inclusive = false), R(-10, 0, 2, inclusive = false), R(-10, 10, 2, inclusive = false) + ) + + def ranges = rangeData.map(r => if (r.inclusive) r.start to r.end by r.step else r.start until r.end by r.step) + + def numericIntRanges = rangeData.map(r => if (r.inclusive) NumericRange.inclusive(r.start, r.end, r.step) else NumericRange(r.start, r.end, r.step)) + + def numericLongRanges = rangeData.map(r => if (r.inclusive) NumericRange.inclusive(r.start.toLong, r.end, r.step) else NumericRange(r.start.toLong, r.end, r.step)) + + def numericBigIntRanges = rangeData.map(r => if (r.inclusive) NumericRange.inclusive(BigInt(r.start), BigInt(r.end), BigInt(r.step)) else NumericRange(BigInt(r.start), BigInt(r.end), BigInt(r.step))) + + def main(args: Array[String]) { + // We drop the first two tests for all ranges which don't have a decent sum implementation, + // because it is just too slow. + println("Ranges:") + ranges.foreach{range => println(range.sum)} + println("IntRanges:") + println("Disabled #1") + println("Disabled #2") + numericIntRanges.drop(2).foreach{range => println(range.sum)} + println("LongRanges:") + println("Disabled #1") + println("Disabled #2") + numericLongRanges.drop(2).foreach{range => println(range.sum)} + println("BigIntRanges:") + println("Disabled #1") + println("Disabled #2") + numericBigIntRanges.drop(2).foreach{range => println(range.sum)} + } + +} \ No newline at end of file -- cgit v1.2.3 From 66b6ad4cacf69df95499b29f49d03a7e963a9eb9 Mon Sep 17 00:00:00 2001 From: Stefan Zeiger Date: Fri, 2 Dec 2011 15:57:54 +0100 Subject: Make Enumeration.ValueSet Serializable Closes SI-5117. --- src/library/scala/Enumeration.scala | 5 +++-- test/files/run/enums.scala | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'test/files/run') diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index 5889bea0e9..c967a48abc 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -253,10 +253,11 @@ abstract class Enumeration(initial: Int, * @param nnIds The set of ids of values (adjusted so that the lowest value does * not fall below zero), organized as a `BitSet`. */ - class ValueSet private[ValueSet] (val nnIds: immutable.BitSet) + class ValueSet private[ValueSet] (private[this] var nnIds: immutable.BitSet) extends AbstractSet[Value] with immutable.SortedSet[Value] - with SortedSetLike[Value, ValueSet] { + with SortedSetLike[Value, ValueSet] + with Serializable { implicit def ordering: Ordering[Value] = ValueOrdering def rangeImpl(from: Option[Value], until: Option[Value]): ValueSet = diff --git a/test/files/run/enums.scala b/test/files/run/enums.scala index 8c6c88ea07..9cdeed2691 100644 --- a/test/files/run/enums.scala +++ b/test/files/run/enums.scala @@ -94,6 +94,29 @@ object Test5 { } } +object SerializationTest { + object Types extends Enumeration { val X, Y = Value } + class A extends java.io.Serializable { val types = Types.values } + class B extends java.io.Serializable { val types = Set(Types.X, Types.Y) } + + def serialize(obj: AnyRef) = { + val baos = new java.io.ByteArrayOutputStream() + val oos = new java.io.ObjectOutputStream(baos) + oos.writeObject(obj) + oos.close() + val bais = new java.io.ByteArrayInputStream(baos.toByteArray) + val ois = new java.io.ObjectInputStream(bais) + val prime = ois.readObject() + ois.close() + prime + } + + def run { + serialize(new B()) + serialize(new A()) + } +} + //############################################################################ // Test code @@ -125,6 +148,7 @@ object Test { Console.println; Test5.run; Console.println; + SerializationTest.run; } } -- cgit v1.2.3 From 2f02cdaaf3674de490bed30e795f778180329dd9 Mon Sep 17 00:00:00 2001 From: Daniel Capo Sobral Date: Wed, 2 Nov 2011 18:13:24 -0200 Subject: Add filter/withFilter method to Parser Complement map and flatMap when used in for comprehensions. This is required when pattern matching is used on the result of the generators. It is implemented through a new filterWithError method on ParseResult. --- src/library/scala/util/parsing/combinator/Parsers.scala | 14 ++++++++++++++ test/files/run/parserFilter.check | 9 +++++++++ test/files/run/parserFilter.scala | 15 +++++++++++++++ test/files/run/parserForFilter.check | 1 + test/files/run/parserForFilter.scala | 12 ++++++++++++ 5 files changed, 51 insertions(+) create mode 100644 test/files/run/parserFilter.check create mode 100644 test/files/run/parserFilter.scala create mode 100644 test/files/run/parserForFilter.check create mode 100644 test/files/run/parserForFilter.scala (limited to 'test/files/run') diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala index e7ea9f598b..341e654d56 100644 --- a/src/library/scala/util/parsing/combinator/Parsers.scala +++ b/src/library/scala/util/parsing/combinator/Parsers.scala @@ -108,6 +108,8 @@ trait Parsers { def flatMapWithNext[U](f: T => Input => ParseResult[U]): ParseResult[U] + def filterWithError(p: T => Boolean, error: T => String, position: Input): ParseResult[T] + def append[U >: T](a: => ParseResult[U]): ParseResult[U] def isEmpty = !successful @@ -137,6 +139,10 @@ trait Parsers { def flatMapWithNext[U](f: T => Input => ParseResult[U]): ParseResult[U] = f(result)(next) + def filterWithError(p: T => Boolean, error: T => String, position: Input): ParseResult[T] = + if (p(result)) this + else Failure(error(result), position) + def append[U >: T](a: => ParseResult[U]): ParseResult[U] = this def get: T = result @@ -161,6 +167,8 @@ trait Parsers { def flatMapWithNext[U](f: Nothing => Input => ParseResult[U]): ParseResult[U] = this + def filterWithError(p: Nothing => Boolean, error: Nothing => String, position: Input): ParseResult[Nothing] = this + def get: Nothing = sys.error("No result when parsing failed") } /** An extractor so `NoSuccess(msg, next)` can be used in matches. */ @@ -224,6 +232,12 @@ trait Parsers { def map[U](f: T => U): Parser[U] //= flatMap{x => success(f(x))} = Parser{ in => this(in) map(f)} + def filter(p: T => Boolean): Parser[T] + = withFilter(p) + + def withFilter(p: T => Boolean): Parser[T] + = Parser{ in => this(in) filterWithError(p, "Input doesn't match filter: "+_, in)} + // no filter yet, dealing with zero is tricky! @migration(2, 9, "As of 2.9, the call-by-name argument is evaluated at most once per constructed Parser object, instead of on every need that arises during parsing.") diff --git a/test/files/run/parserFilter.check b/test/files/run/parserFilter.check new file mode 100644 index 0000000000..be04454426 --- /dev/null +++ b/test/files/run/parserFilter.check @@ -0,0 +1,9 @@ +[1.3] failure: Input doesn't match filter: false + +if false + ^ +[1.1] failure: Input doesn't match filter: not + +not true +^ +[1.8] parsed: (if~true) diff --git a/test/files/run/parserFilter.scala b/test/files/run/parserFilter.scala new file mode 100644 index 0000000000..d007d441f4 --- /dev/null +++ b/test/files/run/parserFilter.scala @@ -0,0 +1,15 @@ +object Test extends scala.util.parsing.combinator.RegexParsers { + val keywords = Set("if", "false") + def word: Parser[String] = "\\w+".r + + def keyword: Parser[String] = word filter (keywords.contains) + def ident: Parser[String] = word filter(!keywords.contains(_)) + + def test = keyword ~ ident + + def main(args: Array[String]) { + println(parseAll(test, "if false")) + println(parseAll(test, "not true")) + println(parseAll(test, "if true")) + } +} diff --git a/test/files/run/parserForFilter.check b/test/files/run/parserForFilter.check new file mode 100644 index 0000000000..a53c147719 --- /dev/null +++ b/test/files/run/parserForFilter.check @@ -0,0 +1 @@ +[1.13] parsed: (second,first) diff --git a/test/files/run/parserForFilter.scala b/test/files/run/parserForFilter.scala new file mode 100644 index 0000000000..1bc44f8033 --- /dev/null +++ b/test/files/run/parserForFilter.scala @@ -0,0 +1,12 @@ +object Test extends scala.util.parsing.combinator.RegexParsers { + def word: Parser[String] = "\\w+".r + + def twoWords = for { + (a ~ b) <- word ~ word + } yield (b, a) + + def main(args: Array[String]) { + println(parseAll(twoWords, "first second")) + } +} + -- cgit v1.2.3