@@ -4,66 +4,70 @@ import reflect.ClassTag
4
4
/** An immutable array. An `IArray[T]` has the same representation as an `Array[T]`,
5
5
* but it cannot be updated. Unlike regular arrays, immutable arrays are covariant.
6
6
*/
7
- opaque type IArray [+ T ] = Array [_ <: T ]
8
-
9
- /** Defines extension methods for immutable arrays */
10
- implicit object arrayOps {
11
-
12
- /** The selection operation on an immutable array.
13
- *
14
- * @param arr the immutable array
15
- * @param n the index of the element to select
16
- * @return the element of the array at the given index
17
- */
18
- def (arr : IArray [Byte ]) apply (n : Int ): Byte = arr.asInstanceOf [Array [Byte ]].apply(n)
19
- def (arr : IArray [Short ]) apply (n : Int ): Short = arr.asInstanceOf [Array [Short ]].apply(n)
20
- def (arr : IArray [Char ]) apply (n : Int ): Char = arr.asInstanceOf [Array [Char ]].apply(n)
21
- def (arr : IArray [Int ]) apply (n : Int ): Int = arr.asInstanceOf [Array [Int ]].apply(n)
22
- def (arr : IArray [Long ]) apply (n : Int ): Long = arr.asInstanceOf [Array [Long ]].apply(n)
23
- def (arr : IArray [Float ]) apply (n : Int ): Float = arr.asInstanceOf [Array [Float ]].apply(n)
24
- def (arr : IArray [Double ]) apply (n : Int ): Double = arr.asInstanceOf [Array [Double ]].apply(n)
25
- def (arr : IArray [T ]) apply[T <: Object ] (n : Int ): T = arr.asInstanceOf [Array [T ]].apply(n)
26
- def (arr : IArray [T ]) apply[T ] (n : Int ): T = arr.asInstanceOf [Array [T ]].apply(n)
27
-
28
- /** The number of elements in an immutable array
29
- * @param arr the immutable array
30
- */
31
- def (arr : IArray [Byte ]) length : Int = arr.asInstanceOf [Array [Byte ]].length
32
- def (arr : IArray [Short ]) length : Int = arr.asInstanceOf [Array [Short ]].length
33
- def (arr : IArray [Char ]) length : Int = arr.asInstanceOf [Array [Char ]].length
34
- def (arr : IArray [Int ]) length : Int = arr.asInstanceOf [Array [Int ]].length
35
- def (arr : IArray [Long ]) length : Int = arr.asInstanceOf [Array [Long ]].length
36
- def (arr : IArray [Float ]) length : Int = arr.asInstanceOf [Array [Float ]].length
37
- def (arr : IArray [Double ]) length : Int = arr.asInstanceOf [Array [Double ]].length
38
- def (arr : IArray [Object ]) length : Int = arr.asInstanceOf [Array [Object ]].length
39
- def (arr : IArray [T ]) length[T ] : Int = arr.asInstanceOf [Array [T ]].length
7
+ object opaques {
8
+ opaque type IArray [+ T ] = Array [_ <: T ]
9
+
10
+ /** Defines extension methods for immutable arrays */
11
+ given arrayOps {
12
+
13
+ /** The selection operation on an immutable array.
14
+ *
15
+ * @param arr the immutable array
16
+ * @param n the index of the element to select
17
+ * @return the element of the array at the given index
18
+ */
19
+ def (arr : IArray [Byte ]) apply (n : Int ): Byte = arr.asInstanceOf [Array [Byte ]].apply(n)
20
+ def (arr : IArray [Short ]) apply (n : Int ): Short = arr.asInstanceOf [Array [Short ]].apply(n)
21
+ def (arr : IArray [Char ]) apply (n : Int ): Char = arr.asInstanceOf [Array [Char ]].apply(n)
22
+ def (arr : IArray [Int ]) apply (n : Int ): Int = arr.asInstanceOf [Array [Int ]].apply(n)
23
+ def (arr : IArray [Long ]) apply (n : Int ): Long = arr.asInstanceOf [Array [Long ]].apply(n)
24
+ def (arr : IArray [Float ]) apply (n : Int ): Float = arr.asInstanceOf [Array [Float ]].apply(n)
25
+ def (arr : IArray [Double ]) apply (n : Int ): Double = arr.asInstanceOf [Array [Double ]].apply(n)
26
+ def (arr : IArray [T ]) apply[T <: Object ] (n : Int ): T = arr.asInstanceOf [Array [T ]].apply(n)
27
+ def (arr : IArray [T ]) apply[T ] (n : Int ): T = arr.asInstanceOf [Array [T ]].apply(n)
28
+
29
+ /** The number of elements in an immutable array
30
+ * @param arr the immutable array
31
+ */
32
+ def (arr : IArray [Byte ]) length : Int = arr.asInstanceOf [Array [Byte ]].length
33
+ def (arr : IArray [Short ]) length : Int = arr.asInstanceOf [Array [Short ]].length
34
+ def (arr : IArray [Char ]) length : Int = arr.asInstanceOf [Array [Char ]].length
35
+ def (arr : IArray [Int ]) length : Int = arr.asInstanceOf [Array [Int ]].length
36
+ def (arr : IArray [Long ]) length : Int = arr.asInstanceOf [Array [Long ]].length
37
+ def (arr : IArray [Float ]) length : Int = arr.asInstanceOf [Array [Float ]].length
38
+ def (arr : IArray [Double ]) length : Int = arr.asInstanceOf [Array [Double ]].length
39
+ def (arr : IArray [Object ]) length : Int = arr.asInstanceOf [Array [Object ]].length
40
+ def (arr : IArray [T ]) length[T ] : Int = arr.asInstanceOf [Array [T ]].length
41
+ }
40
42
}
43
+ type IArray [+ T ] = opaques.IArray [T ]
41
44
42
45
object IArray {
43
46
44
47
/** An immutable array of length 0.
45
48
*/
46
- def empty [T : ClassTag ]: IArray [T ] = new Array [T ](0 )
49
+ def empty [T : ClassTag ]: IArray [T ] = new Array [T ](0 ). asInstanceOf
47
50
48
51
/** An immutable array with given elements.
49
52
*/
50
- def apply [T : ClassTag ](xs : T * ): IArray [T ] = Array (xs : _* )
51
- def apply (x : Boolean , xs : Boolean * ): IArray [Boolean ] = Array (x, xs : _* )
52
- def apply (x : Byte , xs : Byte * ): IArray [Byte ] = Array (x, xs : _* )
53
- def apply (x : Short , xs : Short * ): IArray [Short ] = Array (x, xs : _* )
54
- def apply (x : Char , xs : Char * ): IArray [Char ] = Array (x, xs : _* )
55
- def apply (x : Int , xs : Int * ): IArray [Int ] = Array (x, xs : _* )
56
- def apply (x : Long , xs : Long * ): IArray [Long ] = Array (x, xs : _* )
57
- def apply (x : Float , xs : Float * ): IArray [Float ] = Array (x, xs : _* )
58
- def apply (x : Double , xs : Double * ): IArray [Double ] = Array (x, xs : _* )
59
- def apply (x : Unit , xs : Unit * ): IArray [Unit ] = Array (x, xs : _* )
53
+ def apply [T : ClassTag ](xs : T * ): IArray [T ] = Array (xs : _* ). asInstanceOf
54
+ def apply (x : Boolean , xs : Boolean * ): IArray [Boolean ] = Array (x, xs : _* ). asInstanceOf
55
+ def apply (x : Byte , xs : Byte * ): IArray [Byte ] = Array (x, xs : _* ). asInstanceOf
56
+ def apply (x : Short , xs : Short * ): IArray [Short ] = Array (x, xs : _* ). asInstanceOf
57
+ def apply (x : Char , xs : Char * ): IArray [Char ] = Array (x, xs : _* ). asInstanceOf
58
+ def apply (x : Int , xs : Int * ): IArray [Int ] = Array (x, xs : _* ). asInstanceOf
59
+ def apply (x : Long , xs : Long * ): IArray [Long ] = Array (x, xs : _* ). asInstanceOf
60
+ def apply (x : Float , xs : Float * ): IArray [Float ] = Array (x, xs : _* ). asInstanceOf
61
+ def apply (x : Double , xs : Double * ): IArray [Double ] = Array (x, xs : _* ). asInstanceOf
62
+ def apply (x : Unit , xs : Unit * ): IArray [Unit ] = Array (x, xs : _* ). asInstanceOf
60
63
61
64
/** Concatenates all arrays into a single immutable array.
62
65
*
63
66
* @param xss the given immutable arrays
64
67
* @return the array created from concatenating `xss`
65
68
*/
66
- def concat [T : ClassTag ](xss : IArray [T ]* ): IArray [T ] = Array .concat[T ](xss.asInstanceOf [Seq [Array [T ]]]: _* )
69
+ def concat [T : ClassTag ](xss : IArray [T ]* ): IArray [T ] =
70
+ Array .concat[T ](xss.asInstanceOf [Seq [Array [T ]]]: _* ).asInstanceOf
67
71
68
72
/** Returns an immutable array that contains the results of some element computation a number
69
73
* of times. Each element is determined by a separate computation.
@@ -72,7 +76,7 @@ object IArray {
72
76
* @param elem the element computation
73
77
*/
74
78
def fill [T : ClassTag ](n : Int )(elem : => T ): IArray [T ] =
75
- Array .fill(n)(elem)
79
+ Array .fill(n)(elem). asInstanceOf
76
80
77
81
/** Returns a two-dimensional immutable array that contains the results of some element computation a number
78
82
* of times. Each element is determined by a separate computation.
@@ -82,7 +86,7 @@ object IArray {
82
86
* @param elem the element computation
83
87
*/
84
88
def fill [T : ClassTag ](n1 : Int , n2 : Int )(elem : => T ): IArray [IArray [T ]] =
85
- Array .fill(n1, n2)(elem)
89
+ Array .fill(n1, n2)(elem). asInstanceOf
86
90
87
91
/** Returns a three-dimensional immutable array that contains the results of some element computation a number
88
92
* of times. Each element is determined by a separate computation.
@@ -93,7 +97,7 @@ object IArray {
93
97
* @param elem the element computation
94
98
*/
95
99
def fill [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int )(elem : => T ): IArray [IArray [IArray [T ]]] =
96
- Array .fill(n1, n2, n3)(elem)
100
+ Array .fill(n1, n2, n3)(elem). asInstanceOf
97
101
98
102
/** Returns a four-dimensional immutable array that contains the results of some element computation a number
99
103
* of times. Each element is determined by a separate computation.
@@ -105,7 +109,7 @@ object IArray {
105
109
* @param elem the element computation
106
110
*/
107
111
def fill [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int )(elem : => T ): IArray [IArray [IArray [IArray [T ]]]] =
108
- Array .fill(n1, n2, n3, n4)(elem)
112
+ Array .fill(n1, n2, n3, n4)(elem). asInstanceOf
109
113
110
114
/** Returns a five-dimensional immutable array that contains the results of some element computation a number
111
115
* of times. Each element is determined by a separate computation.
@@ -118,7 +122,7 @@ object IArray {
118
122
* @param elem the element computation
119
123
*/
120
124
def fill [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int , n5 : Int )(elem : => T ): IArray [IArray [IArray [IArray [IArray [T ]]]]] =
121
- Array .fill(n1, n2, n3, n4, n5)(elem)
125
+ Array .fill(n1, n2, n3, n4, n5)(elem). asInstanceOf
122
126
123
127
/** Returns an immutable array containing values of a given function over a range of integer
124
128
* values starting from 0.
@@ -127,7 +131,7 @@ object IArray {
127
131
* @param f The function computing element values
128
132
*/
129
133
def tabulate [T : ClassTag ](n : Int )(f : Int => T ): IArray [T ] =
130
- Array .tabulate(n)(f)
134
+ Array .tabulate(n)(f). asInstanceOf
131
135
132
136
/** Returns a two-dimensional immutable array containing values of a given function
133
137
* over ranges of integer values starting from `0`.
@@ -137,7 +141,7 @@ object IArray {
137
141
* @param f The function computing element values
138
142
*/
139
143
def tabulate [T : ClassTag ](n1 : Int , n2 : Int )(f : (Int , Int ) => T ): IArray [IArray [T ]] =
140
- Array .tabulate(n1, n2)(f)
144
+ Array .tabulate(n1, n2)(f). asInstanceOf
141
145
142
146
/** Returns a three-dimensional immutable array containing values of a given function
143
147
* over ranges of integer values starting from `0`.
@@ -148,7 +152,7 @@ object IArray {
148
152
* @param f The function computing element values
149
153
*/
150
154
def tabulate [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int )(f : (Int , Int , Int ) => T ): IArray [IArray [IArray [T ]]] =
151
- Array .tabulate(n1, n2, n3)(f)
155
+ Array .tabulate(n1, n2, n3)(f). asInstanceOf
152
156
153
157
/** Returns a four-dimensional immutable array containing values of a given function
154
158
* over ranges of integer values starting from `0`.
@@ -160,7 +164,7 @@ object IArray {
160
164
* @param f The function computing element values
161
165
*/
162
166
def tabulate [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int )(f : (Int , Int , Int , Int ) => T ): IArray [IArray [IArray [IArray [T ]]]] =
163
- Array .tabulate(n1, n2, n3, n4)(f)
167
+ Array .tabulate(n1, n2, n3, n4)(f). asInstanceOf
164
168
165
169
/** Returns a five-dimensional immutable array containing values of a given function
166
170
* over ranges of integer values starting from `0`.
@@ -173,7 +177,7 @@ object IArray {
173
177
* @param f The function computing element values
174
178
*/
175
179
def tabulate [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int , n5 : Int )(f : (Int , Int , Int , Int , Int ) => T ): IArray [IArray [IArray [IArray [IArray [T ]]]]] =
176
- Array .tabulate(n1, n2, n3, n4, n5)(f)
180
+ Array .tabulate(n1, n2, n3, n4, n5)(f). asInstanceOf
177
181
178
182
/** Returns an immutable array containing a sequence of increasing integers in a range.
179
183
*
@@ -182,7 +186,7 @@ object IArray {
182
186
* @return the immutable array with values in range `start, start + 1, ..., end - 1`
183
187
* up to, but excluding, `end`.
184
188
*/
185
- def range (start : Int , end : Int ): IArray [Int ] = Array .range(start, end)
189
+ def range (start : Int , end : Int ): IArray [Int ] = Array .range(start, end). asInstanceOf
186
190
187
191
/** Returns an immutable array containing equally spaced values in some integer interval.
188
192
*
@@ -191,7 +195,7 @@ object IArray {
191
195
* @param step the increment value of the array (may not be zero)
192
196
* @return the immutable array with values in `start, start + step, ...` up to, but excluding `end`
193
197
*/
194
- def range (start : Int , end : Int , step : Int ): IArray [Int ] = Array .range(start, end, step)
198
+ def range (start : Int , end : Int , step : Int ): IArray [Int ] = Array .range(start, end, step). asInstanceOf
195
199
196
200
/** Returns an immutable array containing repeated applications of a function to a start value.
197
201
*
@@ -200,7 +204,7 @@ object IArray {
200
204
* @param f the function that is repeatedly applied
201
205
* @return the immutable array returning `len` values in the sequence `start, f(start), f(f(start)), ...`
202
206
*/
203
- def iterate [T : ClassTag ](start : T , len : Int )(f : T => T ): IArray [T ] = Array .iterate(start, len)(f)
207
+ def iterate [T : ClassTag ](start : T , len : Int )(f : T => T ): IArray [T ] = Array .iterate(start, len)(f). asInstanceOf
204
208
205
209
/** Returns a decomposition of the array into a sequence. This supports
206
210
* a pattern match like `{ case IArray(x,y,z) => println('3 elements')}`.
0 commit comments