Skip to content

Commit 5301a8a

Browse files
committed
Polish map and set API in dotc.util
- Rename Map to MutableMap, in order to avoid confusion with the default immutable map - Rename Set to MutableSet - Rename `apply` to `lookup` in LinearMap - Drop >: Null lower bound for keys
1 parent 0f35729 commit 5301a8a

File tree

10 files changed

+17
-16
lines changed

10 files changed

+17
-16
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import io.{AbstractFile, PlainFile}
1414
import Phases.unfusedPhases
1515

1616
import scala.io.Codec
17-
import util.{Set => _, _}
17+
import util._
1818
import reporting.Reporter
1919
import rewrites.Rewrites
2020
import java.io.{BufferedWriter, OutputStreamWriter}

compiler/src/dotty/tools/dotc/core/Names.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ object Names {
185185
private var derivedNames: LinearMap[NameInfo, DerivedName] = LinearMap.Empty
186186

187187
private def add(info: NameInfo): TermName = synchronized {
188-
derivedNames(info) match
188+
derivedNames.lookup(info) match
189189
case null =>
190190
val derivedName = new DerivedName(this, info)
191191
derivedNames = derivedNames.updated(info, derivedName)

compiler/src/dotty/tools/dotc/util/GenericHashMap.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ object GenericHashMap:
2222
* However, a table of size up to DenseLimit will be re-sized only
2323
* once the number of elements reaches the table's size.
2424
*/
25-
abstract class GenericHashMap[Key >: Null <: AnyRef, Value >: Null <: AnyRef]
25+
abstract class GenericHashMap[Key <: AnyRef, Value >: Null <: AnyRef]
2626
(protected val initialCapacity: Int = 8,
27-
protected val capacityMultiple: Int = 3) extends Map[Key, Value]:
27+
protected val capacityMultiple: Int = 3) extends MutableMap[Key, Value]:
2828
import GenericHashMap.DenseLimit
2929

3030
protected var used: Int = _

compiler/src/dotty/tools/dotc/util/HashMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotty.tools.dotc.util
33
/** A specialized implementation of GenericHashMap with standard hashCode and equals
44
* as comparison
55
*/
6-
class HashMap[Key >: Null <: AnyRef, Value >: Null <: AnyRef]
6+
class HashMap[Key <: AnyRef, Value >: Null <: AnyRef]
77
(initialCapacity: Int = 8, capacityMultiple: Int = 3)
88
extends GenericHashMap[Key, Value](initialCapacity, capacityMultiple):
99
import GenericHashMap.DenseLimit

compiler/src/dotty/tools/dotc/util/HashSet.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc.util
22

33
/** A hash set that allows some privileged protected access to its internals
44
*/
5-
class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int, loadFactor: Float = 0.25f) extends Set[T] {
5+
class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int = 16, loadFactor: Float = 0.25f) extends MutableSet[T] {
66
private var used: Int = _
77
private var limit: Int = _
88
private var table: Array[AnyRef] = _

compiler/src/dotty/tools/dotc/util/IdentityHashMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotty.tools.dotc.util
33
/** A specialized implementation of GenericHashMap with identity hash and `eq`
44
* as comparison
55
*/
6-
class IdentityHashMap[Key >: Null <: AnyRef, Value >: Null <: AnyRef]
6+
class IdentityHashMap[Key <: AnyRef, Value >: Null <: AnyRef]
77
(initialCapacity: Int = 8, capacityMultiple: Int = 3)
88
extends GenericHashMap[Key, Value](initialCapacity, capacityMultiple):
99
import GenericHashMap.DenseLimit

compiler/src/dotty/tools/dotc/util/LinearMap.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ object LinearMap:
1717

1818
extension [K <: AnyRef, V >: Null <: AnyRef](m: LinearMap[K, V]):
1919

20-
def apply(key: K): V /*| Null*/ = m match
20+
def lookup(key: K): V /*| Null*/ = m match
2121
case m: immutable.Map[K, V] @unchecked =>
2222
if m.contains(key) then m(key) else null
2323
case m: HashMap[K, V] @unchecked =>
24-
m.get(key)
24+
m.lookup(key)
2525

2626
def updated(key: K, value: V): LinearMap[K, V] = m match
2727
case m: immutable.Map[K, V] @unchecked =>
2828
if m.size < 4 then
2929
m.updated(key, value)
3030
else
3131
val m1 = HashMap[K, V]()
32-
m.foreach(m1.put(_, _))
33-
m1.put(key, value)
32+
m.foreach(m1(_) = _)
33+
m1(key) = value
3434
m1
3535
case m: HashMap[K, V] @unchecked =>
36-
m.put(key, value)
36+
m(key) = value
3737
m
3838

3939
def size = m match

compiler/src/dotty/tools/dotc/util/Map.scala renamed to compiler/src/dotty/tools/dotc/util/MutableMap.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc.util
22

33
/** A common class for lightweight mutable maps.
44
*/
5-
abstract class Map[Key >: Null <: AnyRef, Value >: Null <: AnyRef]:
5+
abstract class MutableMap[Key <: AnyRef, Value >: Null <: AnyRef]:
66

77
def lookup(x: Key): Value /* | Null */
88

@@ -17,3 +17,4 @@ abstract class Map[Key >: Null <: AnyRef, Value >: Null <: AnyRef]:
1717
def iterator: Iterator[(Key, Value)]
1818

1919
def get(x: Key): Option[Value] = Option(lookup(x))
20+

compiler/src/dotty/tools/dotc/util/Set.scala renamed to compiler/src/dotty/tools/dotc/util/MutableSet.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package dotty.tools.dotc.util
22

3-
/** A common class for lightweight sets.
3+
/** A common class for lightweight mutable sets.
44
*/
5-
abstract class Set[T >: Null] {
5+
abstract class MutableSet[T >: Null] {
66

77
def findEntry(x: T): T
88

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Comments._, Constants._, Contexts._, Flags._, Names._, NameOps._, Symbols
2323
import classpath.ClassPathEntries
2424
import reporting._
2525
import typer.Typer
26-
import util.{Set => _, _}
26+
import util._
2727
import interactive._, interactive.InteractiveDriver._
2828
import decompiler.IDEDecompilerDriver
2929
import Interactive.Include

0 commit comments

Comments
 (0)