diff options
-rw-r--r-- | src/dotnet-library/scala/runtime/RichChar.scala | 15 | ||||
-rw-r--r-- | src/library/scala/Iterator.scala | 9 | ||||
-rw-r--r-- | src/library/scala/RandomAccessSeq.scala | 7 | ||||
-rw-r--r-- | src/library/scala/Range.scala | 45 | ||||
-rw-r--r-- | src/library/scala/Seq.scala | 4 | ||||
-rw-r--r-- | src/library/scala/collection/jcl/CollectionWrapper.scala | 1 | ||||
-rw-r--r-- | src/library/scala/collection/jcl/IterableWrapper.scala | 2 | ||||
-rw-r--r-- | src/library/scala/collection/jcl/MapWrapper.scala | 2 | ||||
-rw-r--r-- | src/library/scala/runtime/RichInt.scala | 30 |
9 files changed, 62 insertions, 53 deletions
diff --git a/src/dotnet-library/scala/runtime/RichChar.scala b/src/dotnet-library/scala/runtime/RichChar.scala index c9cde3f11a..e35663a6dd 100644 --- a/src/dotnet-library/scala/runtime/RichChar.scala +++ b/src/dotnet-library/scala/runtime/RichChar.scala @@ -48,23 +48,20 @@ final class RichChar(x: Char) extends Proxy with Ordered[Char] { def asDigit: Int = System.Char.GetNumericValue(x).toInt - private class SequentialCharIterator(limit: Char) extends BufferedIterator[Char] { + /** Create an Iterator[Char] over the characters from 'x' to 'y' - 1 + */ + def until(limit: Char): Iterator[Char] = new Iterator[Char] { private var ch = x def hasNext: Boolean = ch < limit - def next(): Char = + def next: Char = if (hasNext) { val j = ch; ch = (ch + 1).toChar; j } else throw new NoSuchElementException("next on empty iterator") - def head: Char = - if (hasNext) ch - else throw new NoSuchElementException("head on empty iterator") } - /** Create an Iterator[Char] over the characters from 'x' to 'y' - 1 - */ - def until(y: Char): Iterator[Char] = new SequentialCharIterator(y) + //def until(y: Char): Iterator[Char] = to(y) /** Create an Iterator[Char] over the characters from 'x' to 'y' */ - def to(y: Char): Iterator[Char] = new SequentialCharIterator((y + 1).toChar) + def to(y: Char): Iterator[Char] = until((y + 1).toChar) } diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index 0e6c9bb4ac..5486b93518 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -104,15 +104,16 @@ object Iterator { def fromCaseClass(n: Product) = fromProduct(n) /** Create an iterator with elements - * <code>e<sub>n+1</sub> = e<sub>n</sub> + 1</code> + * <code>e<sub>n+1</sub> = e<sub>n</sub> +/- 1</code> * where <code>e<sub>0</sub> = start</code> * and <code>e<sub>i</sub> < end</code>. * + * @deprecated use <code>Int.until</code> instead. * @param start the start value of the iterator * @param end the end value of the iterator * @return the iterator with values in range <code>[start;end)</code>. */ - def range(start: Int, end: Int): Range = + @deprecated def range(start: Int, end: Int): Range = if (start < end) range(start, end, 1) else range(start, end, -1) @@ -121,13 +122,13 @@ object Iterator { * where <code>e<sub>0</sub> = start</code> * and <code>e<sub>i</sub> < end</code>. * + * @deprecated use <code>Int.until</code> instead. * @param start the start value of the iterator * @param end the end value of the iterator * @param step the increment value of the iterator (must be positive or negative) * @return the iterator with values in range <code>[start;end)</code>. */ - def range(start: Int, end: Int, step: Int): Range = - new Range(start, end, step) + @deprecated def range(start: Int, end: Int, step: Int): Range = start.until(end, step) /** Create an iterator with elements * <code>e<sub>n+1</sub> = step(e<sub>n</sub>)</code> diff --git a/src/library/scala/RandomAccessSeq.scala b/src/library/scala/RandomAccessSeq.scala index 45c7230178..90f9b57a59 100644 --- a/src/library/scala/RandomAccessSeq.scala +++ b/src/library/scala/RandomAccessSeq.scala @@ -10,6 +10,7 @@ object RandomAccessSeq { new Projection[A] { def length = Projection.this.length - n def apply(idx : Int) = Projection.this.apply(idx + n) + override def stringPrefix = Projection.this.stringPrefix + "D" + n } } /** non-strict */ @@ -18,6 +19,7 @@ object RandomAccessSeq { else new Projection[A] { def length = n def apply(idx : Int) = Projection.this.apply(idx) + override def stringPrefix = Projection.this.stringPrefix + "T" + n } } /** non-strict */ @@ -26,6 +28,8 @@ object RandomAccessSeq { override def reverse : Projection[A] = new Projection[A] { def length = Projection.this.length def apply(idx : Int) = Projection.this.apply(length - idx - 1) + override def stringPrefix = Projection.this.stringPrefix + "R" + override def reverse : Projection[A] = Projection.this } } } @@ -38,6 +42,7 @@ trait RandomAccessSeq[+A] extends Seq[A] { def length = RandomAccessSeq.this.length def apply(idx : Int) = RandomAccessSeq.this.apply(idx) override def elements = RandomAccessSeq.this.elements + override def stringPrefix = RandomAccessSeq.this.stringPrefix + "P" } override def elements : Iterator[A] = new Iterator[A] { var idx = 0 @@ -56,4 +61,4 @@ trait RandomAccessSeq[+A] extends Seq[A] { if (idx < RandomAccessSeq.this.length) RandomAccessSeq.this(idx) else that(idx - RandomAccessSeq.this.length) } -}
\ No newline at end of file +} diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala index b894d6ae5f..6c85231915 100644 --- a/src/library/scala/Range.scala +++ b/src/library/scala/Range.scala @@ -26,39 +26,30 @@ import Predef._ * @author Stephane Micheloud * @version 1.0, 01/05/2007 */ -class Range(val start: Int, val end: Int, val step: Int) extends BufferedIterator[Int] { +class Range(val start: Int, val end: Int, val step: Int) extends RandomAccessSeq.Projection[Int] { assert(step != 0) assert(if (step > 0) end >= start else end <= start) - private var i = start - override def hasNext: Boolean = if (step > 0) i < end else i > end - - def next: Int = - if (hasNext) { val j = i; i += step; j } - else throw new NoSuchElementException("next on empty iterator") + override def length = { + val base = if (start < end) end - start + else start - end + assert(base >= 0) + val step = if (this.step < 0) -this.step else this.step + assert(step >= 0) + base / step + (if (base % step != 0) 1 else 0) + } - def peekList(sz : Int) = new RandomAccessSeq[Int] { - def length = Math.min(sz, length0(i)); - def apply(idx : Int) = { - if (idx >= length) throw new IndexOutOfBoundsException - i + (idx * step) - } + override def apply(idx : Int) = { + if (idx < 0 || idx >= length) throw new Predef.IndexOutOfBoundsException + start + (step * idx) } - protected override def defaultPeek : Int = throw new NoSuchElementException + override protected def stringPrefix = "Range" - private def length0(i : Int) = { - if (step > 0) length1(i, end, step) - else length1(end, i, -step) + def contains(x : Int): Boolean = { + x >= start && x < end && (((x - start) % step) == 0) } - private def length1(start : Int, end : Int, step : Int) = { - assert(start <= end && step > 0) - val n = (end - start) / step - val m = (end - start) % step - n + (if (m == 0) 0 else 1) + override def contains(elem: Any): Boolean = elem match { + case elem : Int => contains(elem) + case _ => false } - def length: Int = length0(start) - - def contains(x: Int): Boolean = - Iterator.range(0, length) exists (i => x == start + i * step) - } diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index 6ab277c88c..c44c8fa552 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -66,8 +66,10 @@ object Seq { def length = Projection.this.length def elements = Projection.this.elements.map(f) def apply(idx : Int) = f(Projection.this.apply(idx)) + override def stringPrefix = Projection.this.stringPrefix + "M" } override def flatMap[B](f: A => Iterable[B]): Projection[B] = new Projection[B] { + override def stringPrefix = Projection.this.stringPrefix + "G" def elements = Projection.this.elements.flatMap(a => f(a).elements) def length = { var sz = 0 @@ -293,9 +295,11 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] { def elements = Seq.this.elements def length = Seq.this.length def apply(idx : Int) = (Seq.this.apply(idx)) + override def stringPrefix = Seq.this.stringPrefix + "P" } class Filter(p : A => Boolean) extends Seq.Projection[A] { + override def stringPrefix = Seq.this.stringPrefix + "F" override def elements = Seq.this.elements.filter(p) override def apply(idx : Int) : A = { var jdx = 0 diff --git a/src/library/scala/collection/jcl/CollectionWrapper.scala b/src/library/scala/collection/jcl/CollectionWrapper.scala index 0116a4e790..690f7e3d94 100644 --- a/src/library/scala/collection/jcl/CollectionWrapper.scala +++ b/src/library/scala/collection/jcl/CollectionWrapper.scala @@ -21,6 +21,7 @@ trait CollectionWrapper[A] extends Collection[A] with IterableWrapper[A] { def underlying : java.util.Collection; override def has(a : A) = underlying.contains(a); override def elements : MutableIterator[A] = super.elements; + override def size = underlying.size; override def hasAll(that : Iterable[A]) = that match { case that : CollectionWrapper[_] => underlying.containsAll(that.underlying); diff --git a/src/library/scala/collection/jcl/IterableWrapper.scala b/src/library/scala/collection/jcl/IterableWrapper.scala index 8b488499ab..c89d7bf748 100644 --- a/src/library/scala/collection/jcl/IterableWrapper.scala +++ b/src/library/scala/collection/jcl/IterableWrapper.scala @@ -25,7 +25,7 @@ trait IterableWrapper[A] extends MutableIterable[A] { case that : IterableWrapper[_] => underlying.retainAll(that.underlying); case _ => super.retainAll(that); } - //override def size = underlying.size; + override def size = underlying.size; override def isEmpty = underlying.isEmpty; override def clear = underlying.clear; override def elements : MutableIterator[A] = new IteratorWrapper(underlying.iterator); diff --git a/src/library/scala/collection/jcl/MapWrapper.scala b/src/library/scala/collection/jcl/MapWrapper.scala index ef45501b4d..0af497d382 100644 --- a/src/library/scala/collection/jcl/MapWrapper.scala +++ b/src/library/scala/collection/jcl/MapWrapper.scala @@ -54,7 +54,7 @@ trait MapWrapper[K,E] extends jcl.Map[K,E] { val underlying = MapWrapper.this.underlying.keySet; } class ValueSet extends IterableWrapper[E] with MutableIterable.Projection[E] { - def size = MapWrapper.this.size; + override def size = MapWrapper.this.size; val underlying = MapWrapper.this.underlying.values; override def has(e : E) = MapWrapper.this.underlying.containsValue(e); } diff --git a/src/library/scala/runtime/RichInt.scala b/src/library/scala/runtime/RichInt.scala index d78b7cc4e2..a3e39a4346 100644 --- a/src/library/scala/runtime/RichInt.scala +++ b/src/library/scala/runtime/RichInt.scala @@ -10,22 +10,32 @@ package scala.runtime -final class RichInt(x: Int) extends Proxy with Ordered[Int] { +final class RichInt(start: Int) extends Proxy with Ordered[Int] { // Proxy - def self: Any = x + def self: Any = start // Ordered[Int] - def compare (y: Int): Int = if (x < y) -1 else if (x > y) 1 else 0 + def compare (that: Int): Int = if (start < that) -1 else if (start > that) 1 else 0 + + /** Creates an iteration of integers from <code>this</code> until <code>end</code>, + incrementing or decrementing by 1 as appropriate. */ + def until(end: Int): Range = until(end, +1) + def until(end : Int, step : Int) : Range = { + if (start <= end && step > 0) new Range(start, end, step) + else if (start >= end && step < 0) new Range(start, end, step) + else if (start >= end && step > 0) new Range(start, end, -step) + else throw new Predef.IllegalArgumentException("" + start + " until " + end + " with step " + step) + } - def until(y: Int): Iterator[Int] = { - if (y > x) Iterator.range(x, y, +1) - else Iterator.range(x, y, -1) + def to(end: Int): Range = { + if (start == end) until(start) + else if (start < end) until(end + 1) + else until(end - 1) } - def to(y: Int): Iterator[Int] = until(y + 1) - def min(y: Int): Int = if (x < y) x else y - def max(y: Int): Int = if (x > y) x else y - def abs: Int = if (x < 0) -x else x + def min(that: Int): Int = if (start < that) start else that + def max(that: Int): Int = if (start > that) start else that + def abs: Int = if (start < 0) -start else start } |