Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit c2d6fd1

Browse files
committed
Fix semantic issue according to Stefan's review comments and add test cases again
1 parent 29e0506 commit c2d6fd1

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

collections/src/main/scala/strawman/collection/immutable/ImmutableArray.scala

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
187187
override def slice(from: Int, until: Int): ImmutableArray[T] = {
188188
val lo = scala.math.max(from, 0)
189189
val hi = scala.math.min(until, length)
190-
new ofRef(Arrays.copyOfRange[T](unsafeArray, lo, hi))
190+
if (lo >= hi)
191+
emptyImpl
192+
else
193+
new ofRef(Arrays.copyOfRange[T](unsafeArray, lo, hi))
191194
}
192195
}
193196

@@ -203,7 +206,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
203206
override def slice(from: Int, until: Int): ImmutableArray[Byte] = {
204207
val lo = scala.math.max(from, 0)
205208
val hi = scala.math.min(until, length)
206-
new ofByte(Arrays.copyOfRange(unsafeArray, lo, hi))
209+
if (lo >= hi)
210+
emptyImpl
211+
else
212+
new ofByte(Arrays.copyOfRange(unsafeArray, lo, hi))
207213
}
208214
}
209215

@@ -219,7 +225,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
219225
override def slice(from: Int, until: Int): ImmutableArray[Short] = {
220226
val lo = scala.math.max(from, 0)
221227
val hi = scala.math.min(until, length)
222-
new ofShort(Arrays.copyOfRange(unsafeArray, lo, hi))
228+
if (lo > hi)
229+
emptyImpl
230+
else
231+
new ofShort(Arrays.copyOfRange(unsafeArray, lo, hi))
223232
}
224233
}
225234

@@ -235,7 +244,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
235244
override def slice(from: Int, until: Int): ImmutableArray[Char] = {
236245
val lo = scala.math.max(from, 0)
237246
val hi = scala.math.min(until, length)
238-
new ofChar(Arrays.copyOfRange(unsafeArray, lo, hi))
247+
if (lo >= hi)
248+
emptyImpl
249+
else
250+
new ofChar(Arrays.copyOfRange(unsafeArray, lo, hi))
239251
}
240252
}
241253

@@ -251,7 +263,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
251263
override def slice(from: Int, until: Int): ImmutableArray[Int] = {
252264
val lo = scala.math.max(from, 0)
253265
val hi = scala.math.min(until, length)
254-
new ofInt(Arrays.copyOfRange(unsafeArray, lo, hi))
266+
if (lo >= hi)
267+
emptyImpl
268+
else
269+
new ofInt(Arrays.copyOfRange(unsafeArray, lo, hi))
255270
}
256271
}
257272

@@ -267,7 +282,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
267282
override def slice(from: Int, until: Int): ImmutableArray[Long] = {
268283
val lo = scala.math.max(from, 0)
269284
val hi = scala.math.min(until, length)
270-
new ofLong(Arrays.copyOfRange(unsafeArray, lo, hi))
285+
if (lo >= hi)
286+
emptyImpl
287+
else
288+
new ofLong(Arrays.copyOfRange(unsafeArray, lo, hi))
271289
}
272290
}
273291

@@ -283,7 +301,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
283301
override def slice(from: Int, until: Int): ImmutableArray[Float] = {
284302
val lo = scala.math.max(from, 0)
285303
val hi = scala.math.min(until, length)
286-
new ofFloat(Arrays.copyOfRange(unsafeArray, lo, hi))
304+
if (lo >= hi)
305+
emptyImpl
306+
else
307+
new ofFloat(Arrays.copyOfRange(unsafeArray, lo, hi))
287308
}
288309
}
289310

@@ -299,7 +320,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
299320
override def slice(from: Int, until: Int): ImmutableArray[Double] = {
300321
val lo = scala.math.max(from, 0)
301322
val hi = scala.math.min(until, length)
302-
new ofDouble(Arrays.copyOfRange(unsafeArray, lo, hi))
323+
if (lo >= hi)
324+
emptyImpl
325+
else
326+
new ofDouble(Arrays.copyOfRange(unsafeArray, lo, hi))
303327
}
304328
}
305329

@@ -315,7 +339,10 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
315339
override def slice(from: Int, until: Int): ImmutableArray[Boolean] = {
316340
val lo = scala.math.max(from, 0)
317341
val hi = scala.math.min(until, length)
318-
new ofBoolean(Arrays.copyOfRange(unsafeArray, lo, hi))
342+
if (lo >= hi)
343+
emptyImpl
344+
else
345+
new ofBoolean(Arrays.copyOfRange(unsafeArray, lo, hi))
319346
}
320347
}
321348

@@ -334,9 +361,13 @@ object ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray] {
334361
// cant use util.Arrays.copyOfRange[Unit](repr, from, until) - Unit is special and doesnt compile
335362
val lo = scala.math.max(from, 0)
336363
val hi = scala.math.min(until, length)
337-
val slicedLenght = hi - lo
338-
val res = new Array[Unit](slicedLenght)
339-
new ofUnit(res)
364+
val slicedLength = hi - lo
365+
if (slicedLength <= 0)
366+
emptyImpl
367+
else {
368+
val res = new Array[Unit](slicedLength)
369+
new ofUnit(res)
370+
}
340371
}
341372
}
342373
}

test/junit/src/test/scala/strawman/collection/immutable/ImmutableArrayTest.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ class ImmutableArrayTest {
5656
Assert.assertEquals(array, array.slice(-1, 5))
5757
Assert.assertEquals(expectedSliceResult1, array.slice(0, 2))
5858
Assert.assertEquals(expectedSliceResult2, array.slice(1, 3))
59+
Assert.assertEquals(ImmutableArray.empty[T], array.slice(1, 1))
60+
Assert.assertEquals(ImmutableArray.empty[T], array.slice(2, 1))
5961
}
6062
}

0 commit comments

Comments
 (0)