Skip to content

Commit 32f664b

Browse files
committed
Add LazyZipOps and some other files to stdlib
1 parent 608e855 commit 32f664b

File tree

5 files changed

+855
-1
lines changed

5 files changed

+855
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.collection
14+
import language.experimental.captureChecking
15+
16+
/** Buffered iterators are iterators which provide a method `head`
17+
* that inspects the next element without discarding it.
18+
*/
19+
trait BufferedIterator[+A] extends Iterator[A] {
20+
21+
/** Returns next element of iterator without advancing beyond it.
22+
*/
23+
def head: A
24+
25+
/** Returns an option of the next element of an iterator without advancing beyond it.
26+
* @return the next element of this iterator if it has a next element
27+
* `None` if it does not
28+
*/
29+
def headOption : Option[A] = if (hasNext) Some(head) else None
30+
31+
override def buffered: this.type = this
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala
14+
package collection
15+
import language.experimental.captureChecking
16+
17+
18+
protected[collection] object Hashing {
19+
20+
def elemHashCode(key: Any): Int = key.##
21+
22+
def improve(hcode: Int): Int = {
23+
var h: Int = hcode + ~(hcode << 9)
24+
h = h ^ (h >>> 14)
25+
h = h + (h << 4)
26+
h ^ (h >>> 10)
27+
}
28+
29+
def computeHash(key: Any): Int =
30+
improve(elemHashCode(key))
31+
32+
/**
33+
* Utility method to keep a subset of all bits in a given bitmap
34+
*
35+
* Example
36+
* bitmap (binary): 00000001000000010000000100000001
37+
* keep (binary): 1010
38+
* result (binary): 00000001000000000000000100000000
39+
*
40+
* @param bitmap the bitmap
41+
* @param keep a bitmask containing which bits to keep
42+
* @return the original bitmap with all bits where keep is not 1 set to 0
43+
*/
44+
def keepBits(bitmap: Int, keep: Int): Int = {
45+
var result = 0
46+
var current = bitmap
47+
var kept = keep
48+
while (kept != 0) {
49+
// lowest remaining bit in current
50+
val lsb = current ^ (current & (current - 1))
51+
if ((kept & 1) != 0) {
52+
// mark bit in result bitmap
53+
result |= lsb
54+
}
55+
// clear lowest remaining one bit in abm
56+
current &= ~lsb
57+
// look at the next kept bit
58+
kept >>>= 1
59+
}
60+
result
61+
}
62+
63+
}

tests/pos-special/stdlib/collection/Iterable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ trait Iterable[+A] extends IterableOnce[A]
9696
* @return a decorator `LazyZip2` that allows strict operations to be performed on the lazily evaluated pairs
9797
* or chained calls to `lazyZip`. Implicit conversion to `Iterable[(A, B)]` is also supported.
9898
*/
99-
def lazyZip[B](that: Iterable[B]): LazyZip2[A, B, this.type] = new LazyZip2(this, this, that)
99+
def lazyZip[B](that: Iterable[B]^): LazyZip2[A, B, this.type]^{this, that} = new LazyZip2(this, this, that)
100100
}
101101

102102
/** Base trait for Iterable operations

0 commit comments

Comments
 (0)