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

Commit

Permalink
Various Map implementations ported from Scala 2.12
Browse files Browse the repository at this point in the history
  • Loading branch information
szeiger committed Jan 8, 2018
1 parent 43bd6ec commit 2e14651
Show file tree
Hide file tree
Showing 8 changed files with 2,137 additions and 0 deletions.
37 changes: 37 additions & 0 deletions collections/src/main/scala/strawman/collection/DefaultMap.scala
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: IterableFactoryLike[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)
}
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
}
Loading

0 comments on commit 2e14651

Please sign in to comment.