@@ -18,6 +18,7 @@ import scala.collection.generic.DefaultSerializable
18
18
import scala .reflect .ClassTag
19
19
import scala .collection .immutable .Nil
20
20
import language .experimental .captureChecking
21
+ import caps .unsafe .unsafeAssumePure
21
22
22
23
/** A buffer that stores elements in an unrolled linked list.
23
24
*
@@ -259,13 +260,14 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
259
260
/** Unrolled buffer node.
260
261
*/
261
262
class Unrolled [T : ClassTag ] private [collection] (var size : Int , var array : Array [T ], var next : Unrolled [T ], val buff : UnrolledBuffer [T ] = null ) {
263
+ // this: Unrolled[T]^ =>
262
264
private [collection] def this () = this (0 , new Array [T ](unrolledlength), null , null )
263
265
private [collection] def this (b : UnrolledBuffer [T ]) = this (0 , new Array [T ](unrolledlength), null , b)
264
266
265
267
private def nextlength = if (buff eq null ) unrolledlength else buff.calcNextLength(array.length)
266
268
267
269
// adds and returns itself or the new unrolled if full
268
- @ tailrec final def append (elem : T ): Unrolled [T ] = if (size < array.length) {
270
+ @ tailrec final def append (elem : T ): Unrolled [T ]^ { this } = if (size < array.length) {
269
271
array(size) = elem
270
272
size += 1
271
273
this
@@ -307,21 +309,21 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
307
309
if (idx < size) array(idx) else next.apply(idx - size)
308
310
@ tailrec final def update (idx : Int , newelem : T ): Unit =
309
311
if (idx < size) array(idx) = newelem else next.update(idx - size, newelem)
310
- @ tailrec final def locate (idx : Int ): Unrolled [T ] =
312
+ @ tailrec final def locate (idx : Int ): Unrolled [T ]^ { this } =
311
313
if (idx < size) this else next.locate(idx - size)
312
- def prepend (elem : T ) = if (size < array.length) {
314
+ def prepend (elem : T ): Unrolled [ T ] = if (size < array.length) {
313
315
// shift the elements of the array right
314
316
// then insert the element
315
317
shiftright()
316
318
array(0 ) = elem
317
319
size += 1
318
- this
320
+ this .unsafeAssumePure
319
321
} else {
320
322
// allocate a new node and store element
321
323
// then make it point to this
322
324
val newhead = new Unrolled [T ](buff)
323
325
newhead append elem
324
- newhead.next = this
326
+ newhead.next = this .unsafeAssumePure
325
327
newhead
326
328
}
327
329
// shifts right assuming enough space
@@ -340,7 +342,7 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
340
342
val r = array(idx)
341
343
shiftleft(idx)
342
344
size -= 1
343
- if (tryMergeWithNext()) buffer.lastPtr = this
345
+ if (tryMergeWithNext()) buffer.lastPtr = this .unsafeAssumePure
344
346
r
345
347
} else next.remove(idx - size, buffer)
346
348
@@ -397,7 +399,7 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
397
399
curr.next = newnextnode
398
400
399
401
// try to merge the last node of this with the newnextnode and fix tail pointer if needed
400
- if (curr.tryMergeWithNext()) buffer.lastPtr = curr
402
+ if (curr.tryMergeWithNext()) buffer.lastPtr = curr.unsafeAssumePure
401
403
else if (newnextnode.next eq null ) buffer.lastPtr = newnextnode
402
404
appended
403
405
}
0 commit comments