Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
3640ff4
Don't recheck inherited trait parameters during capture checking
odersky Oct 30, 2023
b65fd3a
Add SeqView to stdlib
odersky Oct 30, 2023
0ada1d4
Add IndexedSeqView to lib
odersky Oct 30, 2023
dbd0c0e
Add CheckedIndexedView to stdlib
odersky Oct 30, 2023
8a60bc2
Add ArrayBuffer (unchecked) to stdlib
odersky Oct 30, 2023
61bf139
Rename annotation source file
odersky Oct 30, 2023
6b91167
Also count @Sealed annotated abstract types as sealed
odersky Oct 30, 2023
d3876ad
Add ArrayBuffer and GrowableBuilder to stdlib
odersky Oct 30, 2023
75c2058
Coarse restriction to disallow local roots in external types
odersky Oct 30, 2023
bb0b774
Require array element types to be sealed
odersky Oct 31, 2023
6b5e494
Add ArrayBuilder.scala in unchanged form to stdlib
odersky Oct 31, 2023
609549c
Make ArrayBuilder capture checked
odersky Oct 31, 2023
d76f092
Add ArrayDeque.scala in unchanged for to stdlib
odersky Oct 31, 2023
e33fc7d
Make ArrayDeque capture checked
odersky Oct 31, 2023
37b41b6
Add Stepper.scala and ArraySeq.scala in unchanged form to stdlib
odersky Oct 31, 2023
ec70a23
Make Stepper capture checked
odersky Oct 31, 2023
24b8a4c
Don't generate capture set variables for self types of pure classes
odersky Oct 31, 2023
6ba065e
Make ArraySeq capture checked
odersky Oct 31, 2023
bb65046
Add mutable/IndexedSeq.scala to stdlib
odersky Oct 31, 2023
c811e35
Add Queue.scala to stdlib
odersky Oct 31, 2023
f0a1241
Add PriorityQueue.scala to stdlib
odersky Oct 31, 2023
4ac2139
Make PriorityQueue capture checked
odersky Oct 31, 2023
4795ef8
Add Stack.scala and ReusableBuilder.scala to stdlib
odersky Oct 31, 2023
0bf1903
Add Set.scala to stdlib
odersky Oct 31, 2023
35e7166
Make collection/Set capture checked
odersky Oct 31, 2023
14b5093
Add mutable/immutable Set to stdlib
odersky Oct 31, 2023
1f414a3
Make mutable/immutable Set capture checked
odersky Oct 31, 2023
f92c881
Add BitSet classes to stdlib (capture checked)
odersky Oct 31, 2023
6a831aa
Add SortedSet classes to stdlib
odersky Oct 31, 2023
3b6b684
Add Factory.scala to stdlib
odersky Oct 31, 2023
5852a76
Make Factory capture checked
odersky Oct 31, 2023
c9cb044
Avoid reporting post check messages several times
odersky Oct 31, 2023
0a350ea
Add SortedSet and BitSet to stdlib (capture checked)
odersky Oct 31, 2023
06744c2
Add BuildFrom.scala to stdlib
odersky Oct 31, 2023
51c9385
Make BuildFrom capture checked
odersky Oct 31, 2023
238317c
Add WithFilter.scala to stdlib
odersky Nov 1, 2023
1b757b9
Make WithFilter capture checked
odersky Nov 1, 2023
4956e6d
Add ArrayOps to stdlib
odersky Nov 1, 2023
7273c4d
Don't flag wildcard array arguments for not being sealed
odersky Nov 1, 2023
b67422e
Fix isPureClass test
odersky Nov 1, 2023
c64c057
Make sealed an annotation
odersky Nov 1, 2023
2083af3
Make ArrayOps capture checked
odersky Nov 1, 2023
9909d95
Add LazyZipOps and some other files to stdlib
odersky Nov 1, 2023
caeac20
Add mutable/TreeSet.scala to stdlib
odersky Nov 1, 2023
1f4f875
Add mutable and concurrent Map to stdlib
odersky Nov 1, 2023
3dc0eae
Add HashMaps to stdlib
odersky Nov 1, 2023
20f3a97
Add LongMap + friends to stdlib
odersky Nov 1, 2023
ae605d6
Add WeakHashMap and MultiMap to stdlib
odersky Nov 1, 2023
b0d3bbc
Add UnrolledBuffer to stdlib
odersky Nov 1, 2023
09e0868
Add mutable TreeMap and RedBlackTree to stdlib
odersky Nov 1, 2023
0888c40
Add mutable SortedMap and SeqMap to stdlib
odersky Nov 1, 2023
a76129f
Add AnyRefMap to stdlib
odersky Nov 1, 2023
73f9474
Add CollisionProofHashMap to stdlib
odersky Nov 1, 2023
ffbaf1f
Avoid infinite recursions when checking F-bounded types
odersky Nov 1, 2023
7304ba6
Add HashTable.scala to stdlib
odersky Nov 1, 2023
f3ed83b
Add HashSet to stdlib
odersky Nov 1, 2023
7d2f133
Add OpenHashMap to stdlib
odersky Nov 1, 2023
9021cfd
Add MapView to stdlib
odersky Nov 1, 2023
cb66516
Add StrictOptimizedMapOps to stdlib
odersky Nov 1, 2023
be20a7c
Add collection/SortedMap to stdlib
odersky Nov 1, 2023
766b020
Add more Ops classes to stdlib
odersky Nov 1, 2023
36975c1
Add remaining collection classes to stdlib
odersky Nov 1, 2023
b899f8d
Add immutable ArraySeq to stdlib
odersky Nov 2, 2023
1f3fe9e
Add immutable HashSet and HashMap to stdlib
odersky Nov 2, 2023
528c249
Add immutable IntMap and LongMap to stdlib
odersky Nov 2, 2023
02c6281
Add immutable ListSet and ListMap to stdlib
odersky Nov 2, 2023
2cb34e5
Add immutable Map to stdlib
odersky Nov 2, 2023
a1fc706
Add Range classes to stdlib
odersky Nov 2, 2023
5817d40
Add immutable Queue to stdlib
odersky Nov 2, 2023
c16afe5
Add immutable RedBlackTree to stdlib
odersky Nov 2, 2023
d526e10
Add immutable SeqMap to stdlib
odersky Nov 2, 2023
26dde72
Add immutable SortedMap to stdlib
odersky Nov 2, 2023
a08460d
Add immutable StrictOptimizedSeqOps to stdlib
odersky Nov 2, 2023
c66c833
Add Vector and VectorMap to stdlib
odersky Nov 2, 2023
eea6bca
Add immutable TreeSet and TreeMap to stdlib
odersky Nov 2, 2023
4625afc
Add immutable TreeSeqMap to stdlib
odersky Nov 2, 2023
9ba2c39
Add unchecked LazyList to stdlib
odersky Nov 2, 2023
bf58e2b
Survive "cannot establish a reference" errors in TreeTypeMap
odersky Nov 2, 2023
6ae16a4
Add checked LazyList to stdlib
odersky Nov 2, 2023
0acfb8c
Rename LazyList to LazyListIterable
odersky Nov 2, 2023
fd88dc1
Allow deep subtypes when compiling stdlib test
odersky Nov 3, 2023
9ae3ee7
Add generic/IsSeq to stdlib
odersky Nov 3, 2023
f8f2050
Make SubstRecThis typemap idempotent
odersky Nov 3, 2023
b19f981
Capturecheck all files in generic
odersky Nov 3, 2023
868d65b
Drop an unsafeAssumePure in Iterator
odersky Nov 3, 2023
f4066c0
Better error message for capture errors involving self types
odersky Nov 3, 2023
e22744d
Add sealed refs test and fix check files of other tests
odersky Nov 4, 2023
cfec1d0
Simplify CapturedVars phase
odersky Nov 4, 2023
5e49b12
Refine isParametric tests
odersky Nov 4, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add OpenHashMap to stdlib
  • Loading branch information
odersky committed Nov 4, 2023
commit 7d2f133e56af2d59df684ea467df53b7ca6ed8f5
307 changes: 307 additions & 0 deletions tests/pos-special/stdlib/collection/mutable/OpenHashMap.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.collection
package mutable

import java.lang.Integer.numberOfLeadingZeros
import java.util.ConcurrentModificationException
import scala.collection.generic.DefaultSerializable
import language.experimental.captureChecking

/**
* @define Coll `OpenHashMap`
* @define coll open hash map
*/
@deprecated("Use HashMap or one of the specialized versions (LongMap, AnyRefMap) instead of OpenHashMap", "2.13.0")
@SerialVersionUID(3L)
object OpenHashMap extends MapFactory[OpenHashMap] {

def empty[sealed K, sealed V] = new OpenHashMap[K, V]
def from[sealed K, sealed V](it: IterableOnce[(K, V)]^): OpenHashMap[K,V] = empty ++= it

def newBuilder[sealed K, sealed V]: Builder[(K, V), OpenHashMap[K,V]] =
new GrowableBuilder[(K, V), OpenHashMap[K, V]](empty)

/** A hash table entry.
*
* The entry is occupied if and only if its `value` is a `Some`;
* deleted if and only if its `value` is `None`.
* If its `key` is not the default value of type `Key`, the entry is occupied.
* If the entry is occupied, `hash` contains the hash value of `key`.
*/
final private class OpenEntry[sealed Key, sealed Value](var key: Key,
var hash: Int,
var value: Option[Value])

private[mutable] def nextPositivePowerOfTwo(target: Int): Int = 1 << -numberOfLeadingZeros(target - 1)
}

/** A mutable hash map based on an open addressing method. The precise scheme is
* undefined, but it should make a reasonable effort to ensure that an insert
* with consecutive hash codes is not unnecessarily penalised. In particular,
* mappings of consecutive integer keys should work without significant
* performance loss.
*
* @tparam Key type of the keys in this map.
* @tparam Value type of the values in this map.
* @param initialSize the initial size of the internal hash table.
*
* @define Coll `OpenHashMap`
* @define coll open hash map
* @define mayNotTerminateInf
* @define willNotTerminateInf
*/
@deprecated("Use HashMap or one of the specialized versions (LongMap, AnyRefMap) instead of OpenHashMap", "2.13.0")
class OpenHashMap[sealed Key, sealed Value](initialSize : Int)
extends AbstractMap[Key, Value]
with MapOps[Key, Value, OpenHashMap, OpenHashMap[Key, Value]]
with StrictOptimizedIterableOps[(Key, Value), Iterable, OpenHashMap[Key, Value]]
with MapFactoryDefaults[Key, Value, OpenHashMap, Iterable]
with DefaultSerializable {

import OpenHashMap.OpenEntry
private type Entry = OpenEntry[Key, Value]

/** A default constructor creates a hashmap with initial size `8`.
*/
def this() = this(8)

override def mapFactory: MapFactory[OpenHashMap] = OpenHashMap

private[this] val actualInitialSize = OpenHashMap.nextPositivePowerOfTwo(initialSize)

private[this] var mask = actualInitialSize - 1

/** The hash table.
*
* The table's entries are initialized to `null`, indication of an empty slot.
* A slot is either deleted or occupied if and only if the entry is non-`null`.
*/
private[this] var table = new Array[Entry](actualInitialSize)

private[this] var _size = 0
private[this] var deleted = 0

// Used for tracking inserts so that iterators can determine if concurrent modification has occurred.
private[this] var modCount = 0

override def size = _size
override def knownSize: Int = size
private[this] def size_=(s : Int): Unit = _size = s
override def isEmpty: Boolean = _size == 0
/** Returns a mangled hash code of the provided key. */
protected def hashOf(key: Key) = {
var h = key.##
h ^= ((h >>> 20) ^ (h >>> 12))
h ^ (h >>> 7) ^ (h >>> 4)
}

/** Increase the size of the table.
* Copy only the occupied slots, effectively eliminating the deleted slots.
*/
private[this] def growTable() = {
val oldSize = mask + 1
val newSize = 4 * oldSize
val oldTable = table
table = new Array[Entry](newSize)
mask = newSize - 1
oldTable.foreach( entry =>
if (entry != null && entry.value != None)
table(findIndex(entry.key, entry.hash)) = entry )
deleted = 0
}

/** Return the index of the first slot in the hash table (in probe order)
* that is, in order of preference, either occupied by the given key, deleted, or empty.
*
* @param hash hash value for `key`
*/
private[this] def findIndex(key: Key, hash: Int): Int = {
var index = hash & mask
var j = 0

// Index of the first slot containing a deleted entry, or -1 if none found yet
var firstDeletedIndex = -1

var entry = table(index)
while (entry != null) {
if (entry.hash == hash && entry.key == key && entry.value != None)
return index

if (firstDeletedIndex == -1 && entry.value == None)
firstDeletedIndex = index

j += 1
index = (index + j) & mask
entry = table(index)
}

if (firstDeletedIndex == -1) index else firstDeletedIndex
}

// TODO refactor `put` to extract `findOrAddEntry` and implement this in terms of that to avoid Some boxing.
override def update(key: Key, value: Value): Unit = put(key, value)

@deprecatedOverriding("addOne should not be overridden in order to maintain consistency with put.", "2.11.0")
def addOne (kv: (Key, Value)): this.type = { put(kv._1, kv._2); this }

@deprecatedOverriding("subtractOne should not be overridden in order to maintain consistency with remove.", "2.11.0")
def subtractOne (key: Key): this.type = { remove(key); this }

override def put(key: Key, value: Value): Option[Value] =
put(key, hashOf(key), value)

private def put(key: Key, hash: Int, value: Value): Option[Value] = {
if (2 * (size + deleted) > mask) growTable()
val index = findIndex(key, hash)
val entry = table(index)
if (entry == null) {
table(index) = new OpenEntry(key, hash, Some(value))
modCount += 1
size += 1
None
} else {
val res = entry.value
if (entry.value == None) {
entry.key = key
entry.hash = hash
size += 1
deleted -= 1
modCount += 1
}
entry.value = Some(value)
res
}
}

/** Delete the hash table slot contained in the given entry. */
@`inline`
private[this] def deleteSlot(entry: Entry) = {
entry.key = null.asInstanceOf[Key]
entry.hash = 0
entry.value = None

size -= 1
deleted += 1
}

override def remove(key : Key): Option[Value] = {
val entry = table(findIndex(key, hashOf(key)))
if (entry != null && entry.value != None) {
val res = entry.value
deleteSlot(entry)
res
} else None
}

def get(key : Key) : Option[Value] = {
val hash = hashOf(key)
var index = hash & mask
var entry = table(index)
var j = 0
while(entry != null){
if (entry.hash == hash &&
entry.key == key){
return entry.value
}

j += 1
index = (index + j) & mask
entry = table(index)
}
None
}

/** An iterator over the elements of this map. Use of this iterator follows
* the same contract for concurrent modification as the foreach method.
*
* @return the iterator
*/
def iterator: Iterator[(Key, Value)] = new OpenHashMapIterator[(Key, Value)] {
override protected def nextResult(node: Entry): (Key, Value) = (node.key, node.value.get)
}

override def keysIterator: Iterator[Key] = new OpenHashMapIterator[Key] {
override protected def nextResult(node: Entry): Key = node.key
}
override def valuesIterator: Iterator[Value] = new OpenHashMapIterator[Value] {
override protected def nextResult(node: Entry): Value = node.value.get
}

private abstract class OpenHashMapIterator[A] extends AbstractIterator[A] {
private[this] var index = 0
private[this] val initialModCount = modCount

private[this] def advance(): Unit = {
if (initialModCount != modCount) throw new ConcurrentModificationException
while((index <= mask) && (table(index) == null || table(index).value == None)) index+=1
}

def hasNext = {advance(); index <= mask }

def next() = {
advance()
val result = table(index)
index += 1
nextResult(result)
}
protected def nextResult(node: Entry): A
}

override def clone() = {
val it = new OpenHashMap[Key, Value]
foreachUndeletedEntry(entry => it.put(entry.key, entry.hash, entry.value.get))
it
}

/** Loop over the key, value mappings of this map.
*
* The behaviour of modifying the map during an iteration is as follows:
* - Deleting a mapping is always permitted.
* - Changing the value of mapping which is already present is permitted.
* - Anything else is not permitted. It will usually, but not always, throw an exception.
*
* @tparam U The return type of the specified function `f`, return result of which is ignored.
* @param f The function to apply to each key, value mapping.
*/
override def foreach[U](f : ((Key, Value)) => U): Unit = {
val startModCount = modCount
foreachUndeletedEntry(entry => {
if (modCount != startModCount) throw new ConcurrentModificationException
f((entry.key, entry.value.get))}
)
}
override def foreachEntry[U](f : (Key, Value) => U): Unit = {
val startModCount = modCount
foreachUndeletedEntry(entry => {
if (modCount != startModCount) throw new ConcurrentModificationException
f(entry.key, entry.value.get)}
)
}

private[this] def foreachUndeletedEntry(f : Entry => Unit): Unit = {
table.foreach(entry => if (entry != null && entry.value != None) f(entry))
}

override def mapValuesInPlace(f : (Key, Value) => Value): this.type = {
foreachUndeletedEntry(entry => entry.value = Some(f(entry.key, entry.value.get)))
this
}

override def filterInPlace(f : (Key, Value) => Boolean): this.type = {
foreachUndeletedEntry(entry => if (!f(entry.key, entry.value.get)) deleteSlot(entry))
this
}

override protected[this] def stringPrefix = "OpenHashMap"
}