Code cleanup
git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@6651 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
parent
876d5b3e7d
commit
17f7de8c81
|
@ -35,22 +35,30 @@ object Iterator {
|
|||
}
|
||||
|
||||
def fromValues[a](xs: a*) = xs.elements;
|
||||
|
||||
def fromArray[a](xs: Array[a]) = new Iterator[a] {
|
||||
private var i = 0;
|
||||
def hasNext: Boolean = i < xs.length;
|
||||
def next: a =
|
||||
if (i < xs.length) { val x = xs(i) ; i = i + 1 ; x }
|
||||
else Predef.error("next on empty iterator");
|
||||
|
||||
def fromArray[a](xs: Array[a]): BufferedIterator[a] =
|
||||
fromArray(xs, 0, xs.length);
|
||||
|
||||
def fromArray[a](xs: Array[a], start: Int, length: Int): BufferedIterator[a] =
|
||||
new BufferedIterator[a] {
|
||||
private var i = start;
|
||||
val end = if ((start + length) < xs.length) start else xs.length;
|
||||
def hasNext: Boolean = i < end;
|
||||
def next: a = if (hasNext) { val x = xs(i) ; i = i + 1 ; x }
|
||||
else Predef.error("next on empty iterator");
|
||||
def head: a = if (hasNext) xs(i);
|
||||
else Predef.error("head on empty iterator");
|
||||
}
|
||||
|
||||
def fromString(str: String): Iterator[Char] = new Iterator[Char] {
|
||||
private var i = 0;
|
||||
private val len = str.length();
|
||||
def hasNext = i < len;
|
||||
def next = { val c = str charAt i; i = i + 1; c };
|
||||
}
|
||||
|
||||
|
||||
def fromString(str: String): BufferedIterator[Char] =
|
||||
new BufferedIterator[Char] {
|
||||
private var i = 0;
|
||||
private val len = str.length();
|
||||
def hasNext = i < len;
|
||||
def next = { val c = str charAt i; i = i + 1; c };
|
||||
def head = str charAt i;
|
||||
}
|
||||
|
||||
def fromCaseClass(n:CaseClass): Iterator[Any] = new Iterator[Any] {
|
||||
private var c:Int = 0;
|
||||
private val cmax = n.caseArity;
|
||||
|
@ -67,7 +75,7 @@ object Iterator {
|
|||
* @param end the end value of the iterator
|
||||
* @return the iterator with values in range [lo;end).
|
||||
*/
|
||||
def range(lo: Int, end: Int): Iterator[Int] =
|
||||
def range(lo: Int, end: Int): BufferedIterator[Int] =
|
||||
range(lo, end, 1);
|
||||
|
||||
/** Create an iterator with elements
|
||||
|
@ -80,9 +88,9 @@ object Iterator {
|
|||
* @param step the increment value of the iterator (must be positive or negative)
|
||||
* @return the iterator with values in range [lo;end).
|
||||
*/
|
||||
def range(lo: Int, end: Int, step: Int): Iterator[Int] = {
|
||||
def range(lo: Int, end: Int, step: Int): BufferedIterator[Int] = {
|
||||
assert(step != 0);
|
||||
new Iterator[Int] {
|
||||
new BufferedIterator[Int] {
|
||||
private var i = lo;
|
||||
def hasNext: Boolean = if (step > 0) i < end else i > end;
|
||||
def next: Int =
|
||||
|
@ -102,14 +110,15 @@ object Iterator {
|
|||
* @param step the increment function of the iterator
|
||||
* @return the iterator with values in range [lo;end).
|
||||
*/
|
||||
def range(lo: Int, end: Int, step: Int => Int): Iterator[Int] = new Iterator[Int] {
|
||||
private var i = lo;
|
||||
def hasNext: Boolean = i < end;
|
||||
def next: Int =
|
||||
if (i < end) { val j = i; i = step(i); j } else Predef.error("next on empty iterator");
|
||||
def head: Int =
|
||||
if (i < end) i else Predef.error("head on empty iterator");
|
||||
}
|
||||
def range(lo: Int, end: Int, step: Int => Int): BufferedIterator[Int] =
|
||||
new BufferedIterator[Int] {
|
||||
private var i = lo;
|
||||
def hasNext: Boolean = i < end;
|
||||
def next: Int =
|
||||
if (i < end) { val j = i; i = step(i); j } else Predef.error("next on empty iterator");
|
||||
def head: Int =
|
||||
if (i < end) i else Predef.error("head on empty iterator");
|
||||
}
|
||||
|
||||
/** Create an iterator with elements
|
||||
* <code>e<sub>n+1</sub> = e<sub>n</sub> + 1</code>
|
||||
|
@ -118,7 +127,7 @@ object Iterator {
|
|||
* @param lo the start value of the iterator
|
||||
* @return the iterator starting at value <code>lo</code>.
|
||||
*/
|
||||
def from(lo: Int): Iterator[Int] =
|
||||
def from(lo: Int): BufferedIterator[Int] =
|
||||
from(lo, 1);
|
||||
|
||||
/** Create an iterator with elements
|
||||
|
@ -129,11 +138,13 @@ object Iterator {
|
|||
* @param step the increment value of the iterator
|
||||
* @return the iterator starting at value <code>lo</code>.
|
||||
*/
|
||||
def from(lo: Int, step: Int) = new Iterator[Int] {
|
||||
private var i = lo;
|
||||
def hasNext: Boolean = true;
|
||||
def next: Int = { val j = i; i = i + step; j }
|
||||
}
|
||||
def from(lo: Int, step: Int): BufferedIterator[Int] =
|
||||
new BufferedIterator[Int] {
|
||||
private var i = lo;
|
||||
def hasNext: Boolean = true;
|
||||
def next: Int = { val j = i; i = i + step; j }
|
||||
def head: Int = i;
|
||||
}
|
||||
|
||||
/** Create an iterator with elements
|
||||
* <code>e<sub>n+1</sub> = step(e<sub>n</sub>)</code>
|
||||
|
@ -143,11 +154,13 @@ object Iterator {
|
|||
* @param step the increment function of the iterator
|
||||
* @return the iterator starting at value <code>lo</code>.
|
||||
*/
|
||||
def from(lo: Int, step: Int => Int) = new Iterator[Int] {
|
||||
private var i = lo;
|
||||
def hasNext: Boolean = true;
|
||||
def next: Int = { val j = i; i = step(i); j }
|
||||
}
|
||||
def from(lo: Int, step: Int => Int): BufferedIterator[Int] =
|
||||
new BufferedIterator[Int] {
|
||||
private var i = lo;
|
||||
def hasNext: Boolean = true;
|
||||
def next: Int = { val j = i; i = step(i); j }
|
||||
def head: Int = i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -232,7 +245,7 @@ mixin class Iterator[+A] {
|
|||
* @param p the redicate used to filter the iterator.
|
||||
* @return the elements of this iterator satisfying <code>p</code>.
|
||||
*/
|
||||
def filter(p: A => Boolean): Iterator[A] = new BufferedIterator[A] {
|
||||
def filter(p: A => Boolean): BufferedIterator[A] = new BufferedIterator[A] {
|
||||
private val source = Iterator.this.buffered;
|
||||
private def skip: Unit =
|
||||
while (source.hasNext && !p(source.head)) { source.next; () }
|
||||
|
|
|
@ -28,8 +28,7 @@ object List {
|
|||
* @param xs the elements to put in the list
|
||||
* @return the list containing elements xs.
|
||||
*/
|
||||
def apply[A](xs: A*): List[A] =
|
||||
fromArray(xs.asInstanceOf[Array[A]]);
|
||||
def apply[A](xs: A*): List[A] = xs.toList;
|
||||
|
||||
/** Create a sorted list of all integers in a range.
|
||||
*
|
||||
|
@ -153,12 +152,8 @@ object List {
|
|||
* @return a list that contains the elements returned by successive
|
||||
* calls to <code>it.next</code>
|
||||
*/
|
||||
def fromIterator[a](it: Iterator[a]): List[a] = {
|
||||
val b = new ListBuffer[a]
|
||||
while (it.hasNext) b += it.next
|
||||
b.toList
|
||||
}
|
||||
|
||||
def fromIterator[a](it: Iterator[a]): List[a] = it.toList;
|
||||
|
||||
/** Converts an array into a list.
|
||||
*
|
||||
* @param arr the array to convert
|
||||
|
@ -166,7 +161,7 @@ object List {
|
|||
* in the same order
|
||||
*/
|
||||
def fromArray[a](arr: Array[a]): List[a] = fromArray(arr, 0, arr.length);
|
||||
|
||||
|
||||
/** Converts a range of an array into a list.
|
||||
*
|
||||
* @param arr the array to convert
|
||||
|
@ -209,16 +204,9 @@ object List {
|
|||
* @param str the string to convert.
|
||||
* @return the string as a list of characters.
|
||||
*/
|
||||
def fromString(str: String): List[Char] = {
|
||||
var res: List[Char] = Nil;
|
||||
var i = str.length();
|
||||
while (i > 0) {
|
||||
i = i - 1;
|
||||
res = str.charAt(i) :: res;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
def fromString(str: String): List[Char] =
|
||||
Iterator.fromString(str).toList;
|
||||
|
||||
/** Returns the given list of characters as a string.
|
||||
*
|
||||
* @param xs the list to convert.
|
||||
|
|
|
@ -24,21 +24,22 @@ object Seq {
|
|||
def apply(i:Int) = x; // caller's responsibility to check isDefinedAt
|
||||
}
|
||||
|
||||
def view[A <% Ordered[A]](xs: Seq[A]): Ordered[Seq[A]] = new Ordered[Seq[A]] with Proxy {
|
||||
def self: Any = xs;
|
||||
def compareTo[B >: Seq[A] <% Ordered[B]](that: B): Int = that match {
|
||||
case ys: Seq[A] =>
|
||||
var res = 0;
|
||||
val xsit = xs.elements;
|
||||
val ysit = ys.elements;
|
||||
while (xsit.hasNext && ysit.hasNext && (res == 0)) {
|
||||
res = xsit.next compareTo ysit.next;
|
||||
}
|
||||
if (res != 0) res else if (xsit.hasNext) 1 else -1
|
||||
case _ =>
|
||||
-(that compareTo xs)
|
||||
implicit def view[A <% Ordered[A]](xs: Seq[A]): Ordered[Seq[A]] =
|
||||
new Ordered[Seq[A]] with Proxy {
|
||||
def self: Any = xs;
|
||||
def compareTo[B >: Seq[A] <% Ordered[B]](that: B): Int = that match {
|
||||
case ys: Seq[A] =>
|
||||
var res = 0;
|
||||
val xsit = xs.elements;
|
||||
val ysit = ys.elements;
|
||||
while (xsit.hasNext && ysit.hasNext && (res == 0)) {
|
||||
res = xsit.next compareTo ysit.next;
|
||||
}
|
||||
if (res != 0) res else if (xsit.hasNext) 1 else -1
|
||||
case _ =>
|
||||
-(that compareTo xs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -156,15 +157,8 @@ mixin class Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A]
|
|||
* @param start starting index.
|
||||
* @return the given array <code>xs</code> filled with this list.
|
||||
*/
|
||||
def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] = {
|
||||
val it = elements;
|
||||
var i = start;
|
||||
while (it.hasNext) {
|
||||
xs(i) = it.next;
|
||||
i = i + 1;
|
||||
}
|
||||
xs
|
||||
}
|
||||
def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] =
|
||||
elements.copyToArray(xs, start);
|
||||
|
||||
/** Transform this sequence into a list of all elements.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue