8
8
9
9
package scala .scalajs .js
10
10
11
+ import scala .language .implicitConversions
12
+
11
13
import scala .collection .mutable
12
14
import mutable .Builder
13
15
14
- import scala .collection .generic .CanBuildFrom
16
+ import scala .collection .generic .{ CanBuildFrom , GenericCompanion , SeqFactory }
15
17
16
18
/** Equivalent of scm.WrappedArray for js.Array */
17
- class WrappedArray [A ](val array : Array [A ])
19
+ @ inline
20
+ final class WrappedArray [A ](val array : Array [A ])
18
21
extends mutable.AbstractSeq [A ]
22
+ with scala.collection.generic.GenericTraversableTemplate [A , WrappedArray ]
19
23
with mutable.IndexedSeq [A ]
24
+ with mutable.Buffer [A ]
25
+ with mutable.BufferLike [A , WrappedArray [A ]]
20
26
with mutable.ArrayLike [A , WrappedArray [A ]]
21
27
with Builder [A , WrappedArray [A ]] {
22
28
23
29
/** Creates a new empty [[WrappedArray ]]. */
24
30
def this () = this (Array ())
25
31
26
- // IndexedSeq interface
32
+ override def companion : GenericCompanion [ WrappedArray ] = WrappedArray
27
33
28
- def update (index : Int , elem : A ): Unit = array(index) = elem
29
- def apply (index : Int ): A = array(index)
30
- def length : Int = array.length
34
+ // IndexedSeq interface
31
35
32
- override protected [this ] def newBuilder : Builder [A , WrappedArray [A ]] =
33
- new WrappedArray [A ]
36
+ @ inline def update (index : Int , elem : A ): Unit = array(index) = elem
37
+ @ inline def apply (index : Int ): A = array(index)
38
+ @ inline def length : Int = array.length
34
39
35
40
// Builder interface
36
41
@@ -44,19 +49,45 @@ class WrappedArray[A](val array: Array[A])
44
49
45
50
@ inline override def result (): WrappedArray [A ] = this
46
51
47
- }
52
+ // Rest of BufferLike interface
48
53
49
- object WrappedArray {
54
+ @ inline override def +=: (elem : A ): this .type = {
55
+ array.unshift(elem)
56
+ this
57
+ }
50
58
51
- def empty [A ]: WrappedArray [A ] = new WrappedArray [A ]
59
+ @ inline override def ++=: (xs : TraversableOnce [A ]): this .type = {
60
+ array.unshift(xs.toSeq: _* )
61
+ this
62
+ }
52
63
53
- implicit def canBuildFrom [A ]: CanBuildFrom [WrappedArray [_], A , WrappedArray [A ]] = {
54
- new CanBuildFrom [WrappedArray [_], A , WrappedArray [A ]] {
55
- def apply (from : WrappedArray [_]): Builder [A , WrappedArray [A ]] =
56
- new WrappedArray [A ]
57
- def apply : Builder [A , WrappedArray [A ]] =
58
- new WrappedArray [A ]
59
- }
64
+ @ inline override def insertAll (n : Int ,
65
+ elems : scala.collection.Traversable [A ]): Unit = {
66
+ array.splice(n, 0 , elems.toSeq: _* )
60
67
}
61
68
69
+ @ inline override def remove (n : Int ): A =
70
+ array.splice(n, 1 )(0 )
71
+
72
+ @ inline override def remove (n : Int , count : Int ): Unit =
73
+ array.splice(n, count)
74
+
75
+ @ inline override def stringPrefix : String = " WrappedArray"
76
+
77
+ }
78
+
79
+ /** $factoryInfo
80
+ * @define coll wrapped array
81
+ * @define Coll `WrappedArray`
82
+ */
83
+ object WrappedArray extends SeqFactory [WrappedArray ] {
84
+ /** $genericCanBuildFromInfo */
85
+ implicit def canBuildFrom [A ]: CanBuildFrom [Coll , A , WrappedArray [A ]] =
86
+ ReusableCBF .asInstanceOf [GenericCanBuildFrom [A ]]
87
+
88
+ def newBuilder [A ]: Builder [A , WrappedArray [A ]] = new WrappedArray [A ]
89
+
90
+ implicit def toJSArray [A ](wrappedArray : WrappedArray [A ]): Array [A ] =
91
+ wrappedArray.array
92
+
62
93
}
0 commit comments