Skip to content

Commit

Permalink
Fix: adapt scala2-library-cc to Scala 2 semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
bracevac authored and hamzaremmal committed Feb 12, 2025
1 parent 78c7473 commit 19c42ee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ object TrieMap extends MapFactory[TrieMap] {

// non-final as an extension point for parallel collections
private[collection] class TrieMapIterator[K, V](var level: Int, private var ct: TrieMap[K, V], mustInit: Boolean = true) extends AbstractIterator[(K, V)] {
this:TrieMapIterator[K, V]^ =>
private val stack = new Array[Array[BasicNode]](7)
private val stackpos = new Array[Int](7)
private var depth = -1
Expand Down Expand Up @@ -1161,7 +1162,7 @@ private[collection] class TrieMapIterator[K, V](var level: Int, private var ct:
/** Returns a sequence of iterators over subsets of this iterator.
* It's used to ease the implementation of splitters for a parallel version of the TrieMap.
*/
protected def subdivide(): Seq[Iterator[(K, V)]] = if (subiter ne null) {
protected def subdivide(): Seq[Iterator[(K, V)]^{this}] = if (subiter ne null) {
// the case where an LNode is being iterated
val it = newIterator(level + 1, ct, _mustInit = false)
it.depth = -1
Expand Down
3 changes: 2 additions & 1 deletion scala2-library-cc/src/scala/collection/immutable/Range.scala
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ private class RangeIterator(
lastElement: Int,
initiallyEmpty: Boolean
) extends AbstractIterator[Int] with Serializable {
this: RangeIterator^ =>
private[this] var _hasNext: Boolean = !initiallyEmpty
private[this] var _next: Int = start
override def knownSize: Int = if (_hasNext) (lastElement - _next) / step + 1 else 0
Expand All @@ -656,7 +657,7 @@ private class RangeIterator(
value
}

override def drop(n: Int): Iterator[Int] = {
override def drop(n: Int): Iterator[Int]^{this} = {
if (n > 0) {
val longPos = _next.toLong + step * n
if (step > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import scala.collection.generic.DefaultSerializable
import scala.reflect.ClassTag
import scala.collection.immutable.Nil
import language.experimental.captureChecking
import caps.unsafe.unsafeAssumePure

/** A buffer that stores elements in an unrolled linked list.
*
Expand Down Expand Up @@ -259,13 +260,14 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
/** Unrolled buffer node.
*/
class Unrolled[T: ClassTag] private[collection] (var size: Int, var array: Array[T], var next: Unrolled[T], val buff: UnrolledBuffer[T] = null) {
//this: Unrolled[T]^ =>
private[collection] def this() = this(0, new Array[T](unrolledlength), null, null)
private[collection] def this(b: UnrolledBuffer[T]) = this(0, new Array[T](unrolledlength), null, b)

private def nextlength = if (buff eq null) unrolledlength else buff.calcNextLength(array.length)

// adds and returns itself or the new unrolled if full
@tailrec final def append(elem: T): Unrolled[T] = if (size < array.length) {
@tailrec final def append(elem: T): Unrolled[T]^{this} = if (size < array.length) {
array(size) = elem
size += 1
this
Expand Down Expand Up @@ -307,21 +309,21 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
if (idx < size) array(idx) else next.apply(idx - size)
@tailrec final def update(idx: Int, newelem: T): Unit =
if (idx < size) array(idx) = newelem else next.update(idx - size, newelem)
@tailrec final def locate(idx: Int): Unrolled[T] =
@tailrec final def locate(idx: Int): Unrolled[T]^{this} =
if (idx < size) this else next.locate(idx - size)
def prepend(elem: T) = if (size < array.length) {
def prepend(elem: T): Unrolled[T] = if (size < array.length) {
// shift the elements of the array right
// then insert the element
shiftright()
array(0) = elem
size += 1
this
this.unsafeAssumePure
} else {
// allocate a new node and store element
// then make it point to this
val newhead = new Unrolled[T](buff)
newhead append elem
newhead.next = this
newhead.next = this.unsafeAssumePure
newhead
}
// shifts right assuming enough space
Expand All @@ -340,7 +342,7 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
val r = array(idx)
shiftleft(idx)
size -= 1
if (tryMergeWithNext()) buffer.lastPtr = this
if (tryMergeWithNext()) buffer.lastPtr = this.unsafeAssumePure
r
} else next.remove(idx - size, buffer)

Expand Down Expand Up @@ -397,7 +399,7 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
curr.next = newnextnode

// try to merge the last node of this with the newnextnode and fix tail pointer if needed
if (curr.tryMergeWithNext()) buffer.lastPtr = curr
if (curr.tryMergeWithNext()) buffer.lastPtr = curr.unsafeAssumePure
else if (newnextnode.next eq null) buffer.lastPtr = newnextnode
appended
}
Expand Down

0 comments on commit 19c42ee

Please sign in to comment.