@@ -18,14 +18,13 @@ import java.lang.Utils._
18
18
import java .util .ScalaOps ._
19
19
20
20
import scala .scalajs .js
21
+ import scala .scalajs .LinkingInfo
21
22
22
23
class ArrayDeque [E ] private (initialCapacity : Int )
23
24
extends AbstractCollection [E ] with Deque [E ] with Cloneable with Serializable {
24
25
self =>
25
26
26
- private val inner : js.Array [E ] = new js.Array [E ](Math .max(initialCapacity, 16 ))
27
-
28
- fillNulls(0 , inner.length)
27
+ private var inner : Array [AnyRef ] = new Array [AnyRef ](Math .max(initialCapacity,
10000
16 ))
29
28
30
29
private var status = 0
31
30
private var startIndex = 0 // inclusive, 0 <= startIndex < inner.length
@@ -56,7 +55,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
56
55
startIndex -= 1
57
56
if (startIndex < 0 )
58
57
startIndex = inner.length - 1
59
- inner(startIndex) = e
58
+ inner(startIndex) = e. asInstanceOf [ AnyRef ]
60
59
status += 1
61
60
empty = false
62
61
true
@@ -71,7 +70,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
71
70
endIndex += 1
72
71
if (endIndex > inner.length)
73
72
endIndex = 1
74
- inner(endIndex - 1 ) = e
73
+ inner(endIndex - 1 ) = e. asInstanceOf [ AnyRef ]
75
74
status += 1
76
75
empty = false
77
76
true
@@ -95,8 +94,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
95
94
def pollFirst (): E = {
96
95
if (isEmpty()) null .asInstanceOf [E ]
97
96
else {
98
- val res = inner(startIndex)
99
- inner(startIndex) = null . asInstanceOf [ E ] // free reference for GC
97
+ val res = inner(startIndex). asInstanceOf [ E ]
98
+ inner(startIndex) = null // free reference for GC
100
99
startIndex += 1
101
100
if (startIndex == endIndex)
102
101
empty = true
@@ -111,8 +110,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
111
110
if (isEmpty()) {
112
111
null .asInstanceOf [E ]
113
112
} else {
114
- val res = inner(endIndex - 1 )
115
- inner(endIndex - 1 ) = null . asInstanceOf [ E ] // free reference for GC
113
+ val res = inner(endIndex - 1 ). asInstanceOf [ E ]
114
+ inner(endIndex - 1 ) = null // free reference for GC
116
115
endIndex -= 1
117
116
if (startIndex == endIndex)
118
117
empty = true
@@ -139,12 +138,12 @@ class ArrayDeque[E] private (initialCapacity: Int)
139
138
140
139
def peekFirst (): E = {
141
140
if (isEmpty()) null .asInstanceOf [E ]
142
- else inner(startIndex)
141
+ else inner(startIndex). asInstanceOf [ E ]
143
142
}
144
143
145
144
def peekLast (): E = {
146
145
if (isEmpty()) null .asInstanceOf [E ]
147
- else inner(endIndex - 1 )
146
+ else inner(endIndex - 1 ). asInstanceOf [ E ]
148
147
}
149
148
150
149
def removeFirstOccurrence (o : Any ): Boolean = {
@@ -222,7 +221,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
222
221
else if (nextIndex >= inner.length)
223
222
nextIndex = 0
224
223
225
- inner(lastIndex)
224
+ inner(lastIndex). asInstanceOf [ E ]
226
225
}
227
226
228
227
override def remove (): Unit = {
@@ -278,7 +277,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
278
277
nextIndex = inner.length - 1
279
278
}
280
279
281
- inner(lastIndex)
280
+ inner(lastIndex). asInstanceOf [ E ]
282
281
}
283
282
284
283
override def remove (): Unit = {
@@ -327,7 +326,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
327
326
do {
328
327
if (i >= capacity)
329
328
i = 0
330
- if (Objects .equals(inner(i), o))
329
+ if (Objects .equals(inner(i). asInstanceOf [ E ] , o))
331
330
return i
332
331
i += 1 // let i overrun so we catch endIndex == capacity
333
332
} while (i != endIndex)
@@ -346,7 +345,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
346
345
i -= 1
347
346
if (i < 0 )
348
347
i = inner.length - 1
349
- if (Objects .equals(inner(i), o))
348
+ if (Objects .equals(inner(i). asInstanceOf [ E ] , o))
350
349
return i
351
350
} while (i != startIndex)
352
351
- 1
@@ -358,20 +357,12 @@ class ArrayDeque[E] private (initialCapacity: Int)
358
357
// Nothing to do (constructor ensures capacity is always non-zero).
359
358
} else if (startIndex == 0 && endIndex == inner.length) {
360
359
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)
360
+ inner = Arrays .copyOf(inner, oldCapacity * 2 )
365
361
} else if (startIndex == endIndex) {
366
362
val oldCapacity = inner.length
367
- inner.length *= 2
368
- // 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)
363
+ val newArr = new Array [AnyRef ](oldCapacity * 2 )
364
+ System .arraycopy(inner, 0 , newArr, oldCapacity, endIndex)
365
+ inner= newArr
375
366
endIndex += oldCapacity
376
367
}
377
368
}
@@ -397,10 +388,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
397
388
pollLast()
398
389
true
399
390
} else if (target < endIndex) {
400
- // 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)
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