Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit 2e14651

Browse files
committed
Various Map implementations ported from Scala 2.12
1 parent 43bd6ec commit 2e14651

File tree

8 files changed

+2137
-0
lines changed

8 files changed

+2137
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
9+
package strawman
10+
package collection
11+
12+
/** A default map which builds a default `immutable.Map` implementation for all
13+
* transformations.
14+
*
15+
* Instances that inherit from `DefaultMap[K, V]` still have to define:
16+
* {{{
17+
* def get(key: K): Option[V]
18+
* def iterator(): Iterator[(K, V)]
19+
* }}}
20+
*
21+
* It might also be advisable to override `foreach` or `size` if efficient
22+
* implementations can be found.
23+
*
24+
* @since 2.8
25+
*/
26+
trait DefaultMap[K, +V] extends Map[K, V] { self =>
27+
28+
// Members declared in IterableOps
29+
def iterableFactory: IterableFactoryLike[Iterable] = Iterable
30+
protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): Map[K,V] = mapFactory.from(coll)
31+
protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), Map[K,V]] = mapFactory.newBuilder()
32+
33+
// Members declared in MapOps
34+
def mapFactory: MapFactory[Map] = Map
35+
def empty: Map[K,V] = mapFactory.empty
36+
protected[this] def mapFromIterable[K2, V2](it: Iterable[(K2, V2)]): Map[K2,V2] = mapFactory.from(it)
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
9+
package strawman.collection
10+
package generic
11+
12+
import scala.Predef.{String, intWrapper, longWrapper}
13+
14+
/** Some bit operations.
15+
*
16+
* See http://www.drmaciver.com/2008/08/unsigned-comparison-in-javascala/ for
17+
* an explanation of unsignedCompare.
18+
*/
19+
private[collection] object BitOperations {
20+
trait Int {
21+
type Int = scala.Int
22+
def zero(i: Int, mask: Int) = (i & mask) == 0
23+
def mask(i: Int, mask: Int) = i & (complement(mask - 1) ^ mask)
24+
def hasMatch(key: Int, prefix: Int, m: Int) = mask(key, m) == prefix
25+
def unsignedCompare(i: Int, j: Int) = (i < j) ^ (i < 0) ^ (j < 0)
26+
def shorter(m1: Int, m2: Int) = unsignedCompare(m2, m1)
27+
def complement(i: Int) = (-1) ^ i
28+
def bits(num: Int) = 31 to 0 by -1 map (i => (num >>> i & 1) != 0)
29+
def bitString(num: Int, sep: String = "") = bits(num) map (b => if (b) "1" else "0") mkString sep
30+
def highestOneBit(j: Int) = java.lang.Integer.highestOneBit(j)
31+
}
32+
object Int extends Int
33+
34+
trait Long {
35+
type Long = scala.Long
36+
def zero(i: Long, mask: Long) = (i & mask) == 0L
37+
def mask(i: Long, mask: Long) = i & (complement(mask - 1) ^ mask)
38+
def hasMatch(key: Long, prefix: Long, m: Long) = mask(key, m) == prefix
39+
def unsignedCompare(i: Long, j: Long) = (i < j) ^ (i < 0L) ^ (j < 0L)
40+
def shorter(m1: Long, m2: Long) = unsignedCompare(m2, m1)
41+
def complement(i: Long) = (-1L) ^ i
42+
def bits(num: Long) = 63L to 0L by -1L map (i => (num >>> i & 1L) != 0L)
43+
def bitString(num: Long, sep: String = "") = bits(num) map (b => if (b) "1" else "0") mkString sep
44+
def highestOneBit(j: Long) = java.lang.Long.highestOneBit(j)
45+
}
46+
object Long extends Long
47+
}

0 commit comments

Comments
 (0)