@@ -8,75 +8,55 @@ package ch7
8
8
object Ex6 extends App {
9
9
import scala .concurrent .stm ._
10
10
11
+ /**
12
+ * `Vector` based implementation.
13
+ */
11
14
class TVectorBuffer [T ] extends scala.collection.mutable.Buffer [T ] {
12
- private [this ] val buf : Ref [Vector [T ]] = Ref (Vector .empty[T ]) // should store the immutable object in order to use CAS operations.
13
-
14
- @ annotation.tailrec
15
- final def += (elem : T ): this .type = {
16
- val ts = buf.single()
17
- if (buf.single.compareAndSet(ts, ts :+ elem))
18
- this
19
- else
20
- += (elem)
15
+ private [this ] val buf : Ref [Vector [T ]] = Ref (Vector .empty[T ])
16
+
17
+ def += (elem : T ): this .type = atomic { implicit txn =>
18
+ buf() = buf() :+ elem
19
+ this
21
20
}
22
21
23
- @ annotation.tailrec
24
- final def +=: (elem : T ): this .type = {
25
- val ts = buf.single()
26
- if (buf.single.compareAndSet(ts, elem +: ts))
27
- this
28
- else
29
- +=: (elem)
22
+ def +=: (elem : T ): this .type = atomic { implicit txn =>
23
+ buf() = elem +: buf()
24
+ this
30
25
}
31
26
32
27
def apply (n : Int ): T = buf.single()(n)
33
28
34
- @ annotation.tailrec
35
- final def clear (): Unit = {
36
- val ts = buf.single()
37
- if (! buf.single.compareAndSet(ts, Vector .empty[T ]))
38
- clear()
39
- }
29
+ def clear (): Unit = buf.single.set(Vector .empty[T ])
40
30
41
- @ annotation.tailrec
42
- final def insertAll (n : Int , elems : collection.Traversable [T ]): Unit = {
43
- val ts = buf.single()
31
+ def insertAll (n : Int , elems : collection.Traversable [T ]): Unit = atomic { implicit txn =>
32
+ val ts = buf()
44
33
val len = ts.length
45
34
if (n < 0 || n > len) throw new IndexOutOfBoundsException (n.toString)
46
35
47
36
val left = ts.take(n)
48
37
val right = ts.drop(n)
49
- val nts = left ++ elems ++ right
50
- if (! buf.single.compareAndSet(ts, nts))
51
- insertAll(n, elems)
38
+ buf() = left ++ elems ++ right
52
39
}
53
40
54
41
def length : Int = buf.single().length
55
42
56
- @ annotation.tailrec
57
- final def remove (n : Int ): T = {
58
- val ts = buf.single()
43
+ def remove (n : Int ): T = atomic { implicit txn =>
44
+ val ts = buf()
59
45
val len = ts.length
60
46
if (n < 0 || n >= len) throw new IndexOutOfBoundsException (n.toString)
61
47
62
48
val left = ts.take(n)
63
49
val right = ts.drop(n + 1 )
64
- val nts = left ++ right
65
- if (buf.single.compareAndSet(ts, nts))
66
- ts(n)
67
- else
68
- remove(n)
50
+ buf() = left ++ right
51
+ ts(n)
69
52
}
70
53
71
- @ annotation.tailrec
72
- final def update (n : Int , newelem : T ): Unit = {
73
- val ts = buf.single()
54
+ def update (n : Int , newelem : T ): Unit = atomic { implicit txn =>
55
+ val ts = buf()
74
56
val len = ts.length
75
57
if (n < 0 || n >= len) throw new IndexOutOfBoundsException (n.toString)
76
58
77
- val nts = ts.updated(n, newelem)
78
- if (! buf.single.compareAndSet(ts, nts))
79
- update(n, newelem)
59
+ buf() = ts.updated(n, newelem)
80
60
}
81
61
82
62
def iterator : Iterator [T ] = buf.single().iterator
0 commit comments