@@ -17,15 +17,11 @@ import java.lang.Utils._
17
17
18
18
import java .util .ScalaOps ._
19
19
20
- import scala .scalajs .js
21
-
22
20
class ArrayDeque [E ] private (initialCapacity : Int )
23
21
extends AbstractCollection [E ] with Deque [E ] with Cloneable with Serializable {
24
22
self =>
25
23
26
- private val inner : js.Array [E ] = new js.Array [E ](Math .max(initialCapacity, 16 ))
27
-
28
- fillNulls(0 , inner.length)
24
+ private var inner : Array [AnyRef ] = new Array [AnyRef ](Math .max(initialCapacity, 16 ))
29
25
30
26
private var status = 0
31
27
private var startIndex = 0 // inclusive, 0 <= startIndex < inner.length
@@ -56,7 +52,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
56
52
startIndex -= 1
57
53
if (startIndex < 0 )
58
54
startIndex = inner.length - 1
59
- inner(startIndex) = e
55
+ inner(startIndex) = e. asInstanceOf [ AnyRef ]
60
56
status += 1
61
57
empty = false
62
58
true
@@ -71,7 +67,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
71
67
endIndex += 1
72
68
if (endIndex > inner.length)
73
69
endIndex = 1
74
- inner(endIndex - 1 ) = e
70
+ inner(endIndex - 1 ) = e. asInstanceOf [ AnyRef ]
75
71
status += 1
76
72
empty = false
77
73
true
@@ -95,8 +91,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
95
91
def pollFirst (): E = {
96
92
if (isEmpty()) null .asInstanceOf [E ]
97
93
else {
98
- val res = inner(startIndex)
99
- inner(startIndex) = null . asInstanceOf [ E ] // free reference for GC
94
+ val res = inner(startIndex). asInstanceOf [ E ]
95
+ inner(startIndex) = null // free reference for GC
100
96
startIndex += 1
101
97
if (startIndex == endIndex)
102
98
empty = true
@@ -111,8 +107,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
111
107
if (isEmpty()) {
112
108
null .asInstanceOf [E ]
113
109
} else {
114
- val res = inner(endIndex - 1 )
115
- inner(endIndex - 1 ) = null . asInstanceOf [ E ] // free reference for GC
110
+ val res = inner(endIndex - 1 ). asInstanceOf [ E ]
111
+ inner(endIndex - 1 ) = null // free reference for GC
116
112
endIndex -= 1
117
113
if (startIndex == endIndex)
118
114
empty = true
@@ -139,12 +135,12 @@ class ArrayDeque[E] private (initialCapacity: Int)
139
135
140
136
def peekFirst (): E = {
141
137
if (isEmpty()) null .asInstanceOf [E ]
142
- else inner(startIndex)
138
+ else inner(startIndex). asInstanceOf [ E ]
143
139
}
144
140
145
141
def peekLast (): E = {
146
142
if (isEmpty()) null .asInstanceOf [E ]
147
- else inner(endIndex - 1 )
143
+ else inner(endIndex - 1 ). asInstanceOf [ E ]
148
144
}
149
145
150
146
def removeFirstOccurrence (o : Any ): Boolean = {
@@ -222,7 +218,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
222
218
else if (nextIndex >= inner.length)
223
219
nextIndex = 0
224
220
225
- inner(lastIndex)
221
+ inner(lastIndex). asInstanceOf [ E ]
226
222
}
227
223
228
224
override def remove (): Unit = {
@@ -278,7 +274,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
278
274
nextIndex = inner.length - 1
279
275
}
280
276
281
- inner(lastIndex)
277
+ inner(lastIndex). asInstanceOf [ E ]
282
278
}
283
279
284
280
override def remove (): Unit = {
@@ -358,20 +354,14 @@ class ArrayDeque[E] private (initialCapacity: Int)
358
354
// Nothing to do (constructor ensures capacity is always non-zero).
359
355
} else if (startIndex == 0 && endIndex == inner.length) {
360
356
val oldCapacity = inner.length
361
- inner.length *= 2
362
- // no copying required: We just keep adding to the end.
363
- // However, ensure array is dense.
364
- fillNulls(oldCapacity, inner.length)
357
+ // No moving required within the array; we grow only at the end.
358
+ inner = Arrays .copyOf(inner, oldCapacity * 2 )
365
359
} else if (startIndex == endIndex) {
366
360
val oldCapacity = inner.length
367
- inner.length *= 2
368
361
// move beginning of array to end
369
- for (i <- 0 until endIndex) {
370
- inner(i + oldCapacity) = inner(i)
371
- inner(i) = null .asInstanceOf [E ] // free old reference for GC
372
- }
373
- // ensure rest of array is dense
374
- fillNulls(endIndex + oldCapacity, inner.length)
362
+ val newArr = new Array [AnyRef ](oldCapacity * 2 )
363
+ System .arraycopy(inner, 0 , newArr, oldCapacity, endIndex)
364
+ inner = newArr
375
365
endIndex += oldCapacity
376
366
}
377
367
}
@@ -398,9 +388,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
398
388
true
399
389
} else if (target < endIndex) {
400
390
// Shift elements from endIndex towards target
401
- for (i <- target until endIndex - 1 )
402
- inner(i) = inner(i + 1 )
403
- inner(endIndex - 1 ) = null .asInstanceOf [E ] // free reference for GC
391
+ System .arraycopy(inner, target + 1 , inner, target, endIndex - (target + 1 ))
392
+ inner(endIndex - 1 ) = null // free reference for GC
404
393
status += 1
405
394
406
395
/* Note that endIndex >= 2:
@@ -429,13 +418,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
429
418
* ==> contradiction.
430
419
*/
431
420
432
- // for (i <- target until startIndex by -1)
433
- var i = target
434
- while (i != startIndex) {
435
- inner(i) = inner(i - 1 )
436
- i -= 1
437
- }
438
- inner(startIndex) = null .asInstanceOf [E ] // free reference for GC
421
+ System .arraycopy(inner, startIndex, inner, startIndex + 1 , target - startIndex)
422
+ inner(startIndex) = null // free reference for GC
439
423
440
424
status += 1
441
425
@@ -451,9 +435,4 @@ class ArrayDeque[E] private (initialCapacity: Int)
451
435
false
452
436
}
453
437
}
454
-
455
- private def fillNulls (from : Int , until : Int ): Unit = {
456
- for (i <- from until until)
457
- inner(i) = null .asInstanceOf [E ]
458
- }
459
438
}
0 commit comments