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
|
@ -36,19 +36,27 @@ object Iterator {
|
||||||
|
|
||||||
def fromValues[a](xs: a*) = xs.elements;
|
def fromValues[a](xs: a*) = xs.elements;
|
||||||
|
|
||||||
def fromArray[a](xs: Array[a]) = new Iterator[a] {
|
def fromArray[a](xs: Array[a]): BufferedIterator[a] =
|
||||||
private var i = 0;
|
fromArray(xs, 0, xs.length);
|
||||||
def hasNext: Boolean = i < xs.length;
|
|
||||||
def next: a =
|
def fromArray[a](xs: Array[a], start: Int, length: Int): BufferedIterator[a] =
|
||||||
if (i < xs.length) { val x = xs(i) ; i = i + 1 ; x }
|
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");
|
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] {
|
def fromString(str: String): BufferedIterator[Char] =
|
||||||
|
new BufferedIterator[Char] {
|
||||||
private var i = 0;
|
private var i = 0;
|
||||||
private val len = str.length();
|
private val len = str.length();
|
||||||
def hasNext = i < len;
|
def hasNext = i < len;
|
||||||
def next = { val c = str charAt i; i = i + 1; c };
|
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] {
|
def fromCaseClass(n:CaseClass): Iterator[Any] = new Iterator[Any] {
|
||||||
|
@ -67,7 +75,7 @@ object Iterator {
|
||||||
* @param end the end value of the iterator
|
* @param end the end value of the iterator
|
||||||
* @return the iterator with values in range [lo;end).
|
* @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);
|
range(lo, end, 1);
|
||||||
|
|
||||||
/** Create an iterator with elements
|
/** Create an iterator with elements
|
||||||
|
@ -80,9 +88,9 @@ object Iterator {
|
||||||
* @param step the increment value of the iterator (must be positive or negative)
|
* @param step the increment value of the iterator (must be positive or negative)
|
||||||
* @return the iterator with values in range [lo;end).
|
* @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);
|
assert(step != 0);
|
||||||
new Iterator[Int] {
|
new BufferedIterator[Int] {
|
||||||
private var i = lo;
|
private var i = lo;
|
||||||
def hasNext: Boolean = if (step > 0) i < end else i > end;
|
def hasNext: Boolean = if (step > 0) i < end else i > end;
|
||||||
def next: Int =
|
def next: Int =
|
||||||
|
@ -102,7 +110,8 @@ object Iterator {
|
||||||
* @param step the increment function of the iterator
|
* @param step the increment function of the iterator
|
||||||
* @return the iterator with values in range [lo;end).
|
* @return the iterator with values in range [lo;end).
|
||||||
*/
|
*/
|
||||||
def range(lo: Int, end: Int, step: Int => Int): Iterator[Int] = new Iterator[Int] {
|
def range(lo: Int, end: Int, step: Int => Int): BufferedIterator[Int] =
|
||||||
|
new BufferedIterator[Int] {
|
||||||
private var i = lo;
|
private var i = lo;
|
||||||
def hasNext: Boolean = i < end;
|
def hasNext: Boolean = i < end;
|
||||||
def next: Int =
|
def next: Int =
|
||||||
|
@ -118,7 +127,7 @@ object Iterator {
|
||||||
* @param lo the start value of the iterator
|
* @param lo the start value of the iterator
|
||||||
* @return the iterator starting at value <code>lo</code>.
|
* @return the iterator starting at value <code>lo</code>.
|
||||||
*/
|
*/
|
||||||
def from(lo: Int): Iterator[Int] =
|
def from(lo: Int): BufferedIterator[Int] =
|
||||||
from(lo, 1);
|
from(lo, 1);
|
||||||
|
|
||||||
/** Create an iterator with elements
|
/** Create an iterator with elements
|
||||||
|
@ -129,10 +138,12 @@ object Iterator {
|
||||||
* @param step the increment value of the iterator
|
* @param step the increment value of the iterator
|
||||||
* @return the iterator starting at value <code>lo</code>.
|
* @return the iterator starting at value <code>lo</code>.
|
||||||
*/
|
*/
|
||||||
def from(lo: Int, step: Int) = new Iterator[Int] {
|
def from(lo: Int, step: Int): BufferedIterator[Int] =
|
||||||
|
new BufferedIterator[Int] {
|
||||||
private var i = lo;
|
private var i = lo;
|
||||||
def hasNext: Boolean = true;
|
def hasNext: Boolean = true;
|
||||||
def next: Int = { val j = i; i = i + step; j }
|
def next: Int = { val j = i; i = i + step; j }
|
||||||
|
def head: Int = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create an iterator with elements
|
/** Create an iterator with elements
|
||||||
|
@ -143,10 +154,12 @@ object Iterator {
|
||||||
* @param step the increment function of the iterator
|
* @param step the increment function of the iterator
|
||||||
* @return the iterator starting at value <code>lo</code>.
|
* @return the iterator starting at value <code>lo</code>.
|
||||||
*/
|
*/
|
||||||
def from(lo: Int, step: Int => Int) = new Iterator[Int] {
|
def from(lo: Int, step: Int => Int): BufferedIterator[Int] =
|
||||||
|
new BufferedIterator[Int] {
|
||||||
private var i = lo;
|
private var i = lo;
|
||||||
def hasNext: Boolean = true;
|
def hasNext: Boolean = true;
|
||||||
def next: Int = { val j = i; i = step(i); j }
|
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.
|
* @param p the redicate used to filter the iterator.
|
||||||
* @return the elements of this iterator satisfying <code>p</code>.
|
* @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 val source = Iterator.this.buffered;
|
||||||
private def skip: Unit =
|
private def skip: Unit =
|
||||||
while (source.hasNext && !p(source.head)) { source.next; () }
|
while (source.hasNext && !p(source.head)) { source.next; () }
|
||||||
|
|
|
@ -28,8 +28,7 @@ object List {
|
||||||
* @param xs the elements to put in the list
|
* @param xs the elements to put in the list
|
||||||
* @return the list containing elements xs.
|
* @return the list containing elements xs.
|
||||||
*/
|
*/
|
||||||
def apply[A](xs: A*): List[A] =
|
def apply[A](xs: A*): List[A] = xs.toList;
|
||||||
fromArray(xs.asInstanceOf[Array[A]]);
|
|
||||||
|
|
||||||
/** Create a sorted list of all integers in a range.
|
/** Create a sorted list of all integers in a range.
|
||||||
*
|
*
|
||||||
|
@ -153,11 +152,7 @@ object List {
|
||||||
* @return a list that contains the elements returned by successive
|
* @return a list that contains the elements returned by successive
|
||||||
* calls to <code>it.next</code>
|
* calls to <code>it.next</code>
|
||||||
*/
|
*/
|
||||||
def fromIterator[a](it: Iterator[a]): List[a] = {
|
def fromIterator[a](it: Iterator[a]): List[a] = it.toList;
|
||||||
val b = new ListBuffer[a]
|
|
||||||
while (it.hasNext) b += it.next
|
|
||||||
b.toList
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Converts an array into a list.
|
/** Converts an array into a list.
|
||||||
*
|
*
|
||||||
|
@ -209,15 +204,8 @@ object List {
|
||||||
* @param str the string to convert.
|
* @param str the string to convert.
|
||||||
* @return the string as a list of characters.
|
* @return the string as a list of characters.
|
||||||
*/
|
*/
|
||||||
def fromString(str: String): List[Char] = {
|
def fromString(str: String): List[Char] =
|
||||||
var res: List[Char] = Nil;
|
Iterator.fromString(str).toList;
|
||||||
var i = str.length();
|
|
||||||
while (i > 0) {
|
|
||||||
i = i - 1;
|
|
||||||
res = str.charAt(i) :: res;
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the given list of characters as a string.
|
/** Returns the given list of characters as a string.
|
||||||
*
|
*
|
||||||
|
|
|
@ -24,7 +24,8 @@ object Seq {
|
||||||
def apply(i:Int) = x; // caller's responsibility to check isDefinedAt
|
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 {
|
implicit def view[A <% Ordered[A]](xs: Seq[A]): Ordered[Seq[A]] =
|
||||||
|
new Ordered[Seq[A]] with Proxy {
|
||||||
def self: Any = xs;
|
def self: Any = xs;
|
||||||
def compareTo[B >: Seq[A] <% Ordered[B]](that: B): Int = that match {
|
def compareTo[B >: Seq[A] <% Ordered[B]](that: B): Int = that match {
|
||||||
case ys: Seq[A] =>
|
case ys: Seq[A] =>
|
||||||
|
@ -156,15 +157,8 @@ mixin class Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A]
|
||||||
* @param start starting index.
|
* @param start starting index.
|
||||||
* @return the given array <code>xs</code> filled with this list.
|
* @return the given array <code>xs</code> filled with this list.
|
||||||
*/
|
*/
|
||||||
def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] = {
|
def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] =
|
||||||
val it = elements;
|
elements.copyToArray(xs, start);
|
||||||
var i = start;
|
|
||||||
while (it.hasNext) {
|
|
||||||
xs(i) = it.next;
|
|
||||||
i = i + 1;
|
|
||||||
}
|
|
||||||
xs
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Transform this sequence into a list of all elements.
|
/** Transform this sequence into a list of all elements.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue