This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various Map implementations ported from Scala 2.12
- Loading branch information
Showing
9 changed files
with
2,187 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
collections/src/main/scala/strawman/collection/DefaultMap.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* __ *\ | ||
** ________ ___ / / ___ Scala API ** | ||
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** | ||
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** | ||
** /____/\___/_/ |_/____/_/ | | ** | ||
** |/ ** | ||
\* */ | ||
|
||
package strawman | ||
package collection | ||
|
||
/** A default map which builds a default `immutable.Map` implementation for all | ||
* transformations. | ||
* | ||
* Instances that inherit from `DefaultMap[K, V]` still have to define: | ||
* {{{ | ||
* def get(key: K): Option[V] | ||
* def iterator(): Iterator[(K, V)] | ||
* }}} | ||
* | ||
* It might also be advisable to override `foreach` or `size` if efficient | ||
* implementations can be found. | ||
* | ||
* @since 2.8 | ||
*/ | ||
trait DefaultMap[K, +V] extends Map[K, V] { self => | ||
|
||
// Members declared in IterableOps | ||
def iterableFactory: IterableFactory[Iterable] = Iterable | ||
protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): Map[K,V] = mapFactory.from(coll) | ||
protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), Map[K,V]] = mapFactory.newBuilder() | ||
|
||
// Members declared in MapOps | ||
def mapFactory: MapFactory[Map] = Map | ||
def empty: Map[K,V] = mapFactory.empty | ||
protected[this] def mapFromIterable[K2, V2](it: Iterable[(K2, V2)]): Map[K2,V2] = mapFactory.from(it) | ||
} |
47 changes: 47 additions & 0 deletions
47
collections/src/main/scala/strawman/collection/generic/BitOperations.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* __ *\ | ||
** ________ ___ / / ___ Scala API ** | ||
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** | ||
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** | ||
** /____/\___/_/ |_/____/_/ | | ** | ||
** |/ ** | ||
\* */ | ||
|
||
package strawman.collection | ||
package generic | ||
|
||
import scala.Predef.{String, intWrapper, longWrapper} | ||
|
||
/** Some bit operations. | ||
* | ||
* See http://www.drmaciver.com/2008/08/unsigned-comparison-in-javascala/ for | ||
* an explanation of unsignedCompare. | ||
*/ | ||
private[collection] object BitOperations { | ||
trait Int { | ||
type Int = scala.Int | ||
def zero(i: Int, mask: Int) = (i & mask) == 0 | ||
def mask(i: Int, mask: Int) = i & (complement(mask - 1) ^ mask) | ||
def hasMatch(key: Int, prefix: Int, m: Int) = mask(key, m) == prefix | ||
def unsignedCompare(i: Int, j: Int) = (i < j) ^ (i < 0) ^ (j < 0) | ||
def shorter(m1: Int, m2: Int) = unsignedCompare(m2, m1) | ||
def complement(i: Int) = (-1) ^ i | ||
def bits(num: Int) = 31 to 0 by -1 map (i => (num >>> i & 1) != 0) | ||
def bitString(num: Int, sep: String = "") = bits(num) map (b => if (b) "1" else "0") mkString sep | ||
def highestOneBit(j: Int) = java.lang.Integer.highestOneBit(j) | ||
} | ||
object Int extends Int | ||
|
||
trait Long { | ||
type Long = scala.Long | ||
def zero(i: Long, mask: Long) = (i & mask) == 0L | ||
def mask(i: Long, mask: Long) = i & (complement(mask - 1) ^ mask) | ||
def hasMatch(key: Long, prefix: Long, m: Long) = mask(key, m) == prefix | ||
def unsignedCompare(i: Long, j: Long) = (i < j) ^ (i < 0L) ^ (j < 0L) | ||
def shorter(m1: Long, m2: Long) = unsignedCompare(m2, m1) | ||
def complement(i: Long) = (-1L) ^ i | ||
def bits(num: Long) = 63L to 0L by -1L map (i => (num >>> i & 1L) != 0L) | ||
def bitString(num: Long, sep: String = "") = bits(num) map (b => if (b) "1" else "0") mkString sep | ||
def highestOneBit(j: Long) = java.lang.Long.highestOneBit(j) | ||
} | ||
object Long extends Long | ||
} |
Oops, something went wrong.