@@ -64,12 +64,19 @@ object ArraySeq {
64
64
private val EmptyArraySeq = new ofRef[AnyRef ](new Array [AnyRef ](0 ))
65
65
def empty [T <: AnyRef ]: ArraySeq [T ] = EmptyArraySeq .asInstanceOf [ArraySeq [T ]]
66
66
67
- // If make is called explicitly we use whatever we're given, even if it's
68
- // empty. This may be unnecessary (if ArraySeq is to honor the collections
69
- // contract all empty ones must be equal, so discriminating based on the reference
70
- // equality of an empty array should not come up) but we may as well be
71
- // conservative since wrapRefArray contributes most of the unnecessary allocations.
72
- def unsafeWrapArray [T ](x : AnyRef ): ArraySeq [T ] = (x match {
67
+ /**
68
+ * Wrap an existing `Array` into an `ArraySeq` of the proper primitive specialization type
69
+ * without copying.
70
+ *
71
+ * Note that an array containing boxed primitives can be wrapped in an `ArraySeq` without
72
+ * copying. For example, `val a: Array[Any] = Array(1)` is an array of `Object` at runtime,
73
+ * containing `Integer`s. An `ArraySeq[Int]` can be obtained with a cast:
74
+ * `ArraySeq.unsafeWrapArray(a).asInstanceOf[ArraySeq[Int]]`. The values are still
75
+ * boxed, the resulting instance is an [[ArraySeq.ofRef ]]. Writing
76
+ * `ArraySeq.unsafeWrapArray(a.asInstanceOf[Array[Int]])` does not work, it throws a
77
+ * `ClassCastException` at runtime.
78
+ */
79
+ def unsafeWrapArray [T ](x : Array [T ]): ArraySeq [T ] = (x.asInstanceOf [Array [_]] match {
73
80
case null => null
74
81
case x : Array [AnyRef ] => new ofRef[AnyRef ](x)
75
82
case x : Array [Int ] => new ofInt(x)
@@ -91,6 +98,7 @@ object ArraySeq {
91
98
ArrayBuilder .make[T ]()(m) mapResult ArraySeq .unsafeWrapArray[T ]
92
99
}
93
100
101
+ @ SerialVersionUID (3L )
94
102
final class ofRef [T <: AnyRef ](val unsafeArray : Array [T ]) extends ArraySeq [T ] with Serializable {
95
103
lazy val elemTag = ClassTag [T ](unsafeArray.getClass.getComponentType)
96
104
def length : Int = unsafeArray.length
@@ -103,6 +111,7 @@ object ArraySeq {
103
111
}
104
112
}
105
113
114
+ @ SerialVersionUID (3L )
106
115
final class ofByte (val unsafeArray : Array [Byte ]) extends ArraySeq [Byte ] with Serializable {
107
116
def elemTag = ClassTag .Byte
108
117
def length : Int = unsafeArray.length
@@ -115,6 +124,7 @@ object ArraySeq {
115
124
}
116
125
}
117
126
127
+ @ SerialVersionUID (3L )
118
128
final class ofShort (val unsafeArray : Array [Short ]) extends ArraySeq [Short ] with Serializable {
119
129
def elemTag = ClassTag .Short
120
130
def length : Int = unsafeArray.length
@@ -127,6 +137,7 @@ object ArraySeq {
127
137
}
128
138
}
129
139
140
+ @ SerialVersionUID (3L )
130
141
final class ofChar (val unsafeArray : Array [Char ]) extends ArraySeq [Char ] with Serializable {
131
142
def elemTag = ClassTag .Char
132
143
def length : Int = unsafeArray.length
@@ -139,6 +150,7 @@ object ArraySeq {
139
150
}
140
151
}
141
152
153
+ @ SerialVersionUID (3L )
142
154
final class ofInt (val unsafeArray : Array [Int ]) extends ArraySeq [Int ] with Serializable {
143
155
def elemTag = ClassTag .Int
144
156
def length : Int = unsafeArray.length
@@ -151,6 +163,7 @@ object ArraySeq {
151
163
}
152
164
}
153
165
166
+ @ SerialVersionUID (3L )
154
167
final class ofLong (val unsafeArray : Array [Long ]) extends ArraySeq [Long ] with Serializable {
155
168
def elemTag = ClassTag .Long
156
169
def length : Int = unsafeArray.length
@@ -163,6 +176,7 @@ object ArraySeq {
163
176
}
164
177
}
165
178
179
+ @ SerialVersionUID (3L )
166
180
final class ofFloat (val unsafeArray : Array [Float ]) extends ArraySeq [Float ] with Serializable {
167
181
def elemTag = ClassTag .Float
168
182
def length : Int = unsafeArray.length
@@ -175,6 +189,7 @@ object ArraySeq {
175
189
}
176
190
}
177
191
192
+ @ SerialVersionUID (3L )
178
193
final class ofDouble (val unsafeArray : Array [Double ]) extends ArraySeq [Double ] with Serializable {
179
194
def elemTag = ClassTag .Double
180
195
def length : Int = unsafeArray.length
@@ -187,6 +202,7 @@ object ArraySeq {
187
202
}
188
203
}
189
204
205
+ @ SerialVersionUID (3L )
190
206
final class ofBoolean (val unsafeArray : Array [Boolean ]) extends ArraySeq [Boolean ] with Serializable {
191
207
def elemTag = ClassTag .Boolean
192
208
def length : Int = unsafeArray.length
@@ -199,6 +215,7 @@ object ArraySeq {
199
215
}
200
216
}
201
217
218
+ @ SerialVersionUID (3L )
202
219
final class ofUnit (val unsafeArray : Array [Unit ]) extends ArraySeq [Unit ] with Serializable {
203
220
def elemTag = ClassTag .Unit
204
221
def length : Int = unsafeArray.length
0 commit comments