8000 Merge pull request #58 from scala-js/main · scala-wasm/scala-wasm@f6b9dde · GitHub
[go: up one dir, main page]

Skip to content

Commit f6b9dde

Browse files
authored
Merge pull request #58 from scala-js/main
[pull] scala-wasm from scala-js:main
2 parents fc9056d + 6b83864 commit f6b9dde

File tree

13 files changed

+271
-308
lines changed

13 files changed

+271
-308
lines changed

javalib/src/main/scala/java/util/ArrayDeque.scala

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ import java.lang.Utils._
1717

1818
import java.util.ScalaOps._
1919

20-
import scala.scalajs.js
21-
2220
class ArrayDeque[E] private (initialCapacity: Int)
2321
extends AbstractCollection[E] with Deque[E] with Cloneable with Serializable {
2422
self =>
2523

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))
2925

3026
private var status = 0
3127
private var startIndex = 0 // inclusive, 0 <= startIndex < inner.length
@@ -56,7 +52,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
5652
startIndex -= 1
5753
if (startIndex < 0)
5854
startIndex = inner.length - 1
59-
inner(startIndex) = e
55+
inner(startIndex) = e.asInstanceOf[AnyRef]
6056
status += 1
6157
empty = false
6258
true
@@ -71,7 +67,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
7167
endIndex += 1
7268
if (endIndex > inner.length)
7369
endIndex = 1
74-
inner(endIndex - 1) = e
70+
inner(endIndex - 1) = e.asInstanceOf[AnyRef]
7571
status += 1
7672
empty = false
7773
true
@@ -95,8 +91,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
9591
def pollFirst(): E = {
9692
if (isEmpty()) null.asInstanceOf[E]
9793
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
10096
startIndex += 1
10197
if (startIndex == endIndex)
10298
empty = true
@@ -111,8 +107,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
111107
if (isEmpty()) {
112108
null.asInstanceOf[E]
113109
} 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
116112
endIndex -= 1
117113
if (startIndex == endIndex)
118114
empty = true
@@ -139,12 +135,12 @@ class ArrayDeque[E] private (initialCapacity: Int)
139135

140136
def peekFirst(): E = {
141137
if (isEmpty()) null.asInstanceOf[E]
142-
else inner(startIndex)
138+
else inner(startIndex).asInstanceOf[E]
143139
}
144140

145141
def peekLast(): E = {
146142
if (isEmpty()) null.asInstanceOf[E]
147-
else inner(endIndex - 1)
143+
else inner(endIndex - 1).asInstanceOf[E]
148144
}
149145

150146
def removeFirstOccurrence(o: Any): Boolean = {
@@ -222,7 +218,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
222218
else if (nextIndex >= inner.length)
223219
nextIndex = 0
224220

225-
inner(lastIndex)
221+
inner(lastIndex).asInstanceOf[E]
226222
}
227223

228224
override def remove(): Unit = {
@@ -278,7 +274,7 @@ class ArrayDeque[E] private (initialCapacity: Int)
278274
nextIndex = inner.length - 1
279275
}
280276

281-
inner(lastIndex)
277+
inner(lastIndex).asInstanceOf[E]
282278
}
283279

284280
override def remove(): Unit = {
@@ -358,20 +354,14 @@ class ArrayDeque[E] private (initialCapacity: Int)
358354
// Nothing to do (constructor ensures capacity is always non-zero).
359355
} else if (startIndex == 0 && endIndex == inner.length) {
360356
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)
365359
} else if (startIndex == endIndex) {
366360
val oldCapacity = inner.length
367-
inner.length *= 2
368361
// 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
375365
endIndex += oldCapacity
376366
}
377367
}
@@ -398,9 +388,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
398388
true
399389
} else if (target < endIndex) {
400390
// 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
404393
status += 1
405394

406395
/* Note that endIndex >= 2:
@@ -429,13 +418,8 @@ class ArrayDeque[E] private (initialCapacity: Int)
429418
* ==> contradiction.
430419
*/
431420

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
439423

440424
status += 1
441425

@@ -451,9 +435,4 @@ class ArrayDeque[E] private (initialCapacity: Int)
451435
false
452436
}
453437
}
454-
455-
private def fillNulls(from: Int, until: Int): Unit = {
456-
for (i <- from until until)
457-
inner(i) = null.asInstanceOf[E]
458-
}
459438
}

0 commit comments

Comments
 (0)
0