-
Notifications
You must be signed in to change notification settings - Fork 6k
[KT-78533] (JVM) ArrayDeque.removeIf implementation #5492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MukjepScarlet
wants to merge
3
commits into
JetBrains:master
Choose a base branch
from
Konditorei-La-Soleil:laSoleil-ArrayDeque-JVM
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
libraries/stdlib/common-non-jvm/src/kotlin/collections/ArrayDeque.commonNonJvm.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package kotlin.collections | ||
|
||
/** | ||
* Resizable-array implementation of the deque data structure. | ||
* | ||
* The name deque is short for "double ended queue" and is usually pronounced "deck". | ||
* | ||
* The collection provide methods for convenient access to the both ends. | ||
* It also implements [MutableList] interface and supports efficient get/set operations by index. | ||
*/ | ||
@SinceKotlin("1.4") | ||
public actual class ArrayDeque<E> private actual constructor( | ||
internal actual var elementData: Array<Any?>, | ||
) : AbstractMutableList<E>() { | ||
internal actual var head: Int = 0 | ||
|
||
actual override var size: Int = 0 | ||
internal set | ||
|
||
/** | ||
* Constructs an empty deque with specified [initialCapacity], or throws [IllegalArgumentException] if [initialCapacity] is negative. | ||
*/ | ||
public actual constructor(initialCapacity: Int) : this( | ||
when { | ||
initialCapacity == 0 -> EMPTY_ARRAY_OF_ANY | ||
initialCapacity > 0 -> arrayOfNulls(initialCapacity) | ||
else -> throw IllegalArgumentException("Illegal Capacity: $initialCapacity") | ||
} | ||
) | ||
|
||
/** | ||
* Constructs an empty deque. | ||
*/ | ||
public actual constructor() : this(EMPTY_ARRAY_OF_ANY) | ||
|
||
/** | ||
* Constructs a deque that contains the same elements as the specified [elements] collection in the same order. | ||
*/ | ||
public actual constructor(elements: Collection<E>) : this(if (elements.isEmpty()) EMPTY_ARRAY_OF_ANY else elements.toTypedArray()) { | ||
size = elementData.size | ||
} | ||
|
||
override fun isEmpty(): Boolean = commonIsEmpty() | ||
|
||
/** | ||
* Returns the first element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
public actual fun first(): E = commonFirst() | ||
|
||
/** | ||
* Returns the first element, or `null` if this deque is empty. | ||
*/ | ||
public actual fun firstOrNull(): E? = commonFirstOrNull() | ||
|
||
/** | ||
* Returns the last element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
public actual fun last(): E = commonLast() | ||
|
||
/** | ||
* Returns the last element, or `null` if this deque is empty. | ||
*/ | ||
public actual fun lastOrNull(): E? = commonLastOrNull() | ||
|
||
/** | ||
* Prepends the specified [element] to this deque. | ||
*/ | ||
public actual fun addFirst(element: E): Unit = commonAddFirst(element) | ||
|
||
/** | ||
* Appends the specified [element] to this deque. | ||
*/ | ||
public actual fun addLast(element: E): Unit = commonAddLast(element) | ||
|
||
/** | ||
* Removes the first element from this deque and returns that removed element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeFirst(): E = commonRemoveFirst() | ||
|
||
/** | ||
* Removes the first element from this deque and returns that removed element, or returns `null` if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeFirstOrNull(): E? = commonRemoveFirstOrNull() | ||
|
||
/** | ||
* Removes the last element from this deque and returns that removed element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeLast(): E = commonRemoveLast() | ||
|
||
/** | ||
* Removes the last element from this deque and returns that removed element, or returns `null` if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeLastOrNull(): E? = commonRemoveLastOrNull() | ||
|
||
// MutableList, MutableCollection | ||
@IgnorableReturnValue | ||
public actual override fun add(element: E): Boolean = commonAdd(element) | ||
|
||
public actual override fun add(index: Int, element: E): Unit = commonAdd(index, element) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun addAll(elements: Collection<E>): Boolean = commonAddAll(elements) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun addAll(index: Int, elements: Collection<E>): Boolean = commonAddAll(index, elements) | ||
|
||
public actual override fun get(index: Int): E = commonGet(index) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun set(index: Int, element: E): E = commonSet(index, element) | ||
|
||
public actual override fun contains(element: E): Boolean = commonContains(element) | ||
|
||
public actual override fun indexOf(element: E): Int = commonIndexOf(element) | ||
|
||
public actual override fun lastIndexOf(element: E): Int = commonLastIndexOf(element) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun remove(element: E): Boolean = commonRemove(element) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun removeAt(index: Int): E = commonRemoveAt(index) | ||
|
||
@IgnorableReturnValue | ||
public override fun removeAll(elements: Collection<E>): Boolean = commonRemoveAll(elements) | ||
|
||
@IgnorableReturnValue | ||
public override fun retainAll(elements: Collection<E>): Boolean = commonRetainAll(elements) | ||
|
||
public actual override fun clear(): Unit = commonClear() | ||
|
||
@Suppress("NOTHING_TO_OVERRIDE", "NO_EXPLICIT_VISIBILITY_IN_API_MODE") // different visibility inherited from the base class | ||
override fun <T> toArray(array: Array<T>): Array<T> = commonToArray(array) | ||
|
||
@Suppress("NOTHING_TO_OVERRIDE", "NO_EXPLICIT_VISIBILITY_IN_API_MODE") // different visibility inherited from the base class | ||
override fun toArray(): Array<Any?> = commonToArray() | ||
|
||
override fun removeRange(fromIndex: Int, toIndex: Int): Unit = commonRemoveRange(fromIndex, toIndex) | ||
|
||
internal actual fun registerModification() { | ||
++modCount | ||
} | ||
|
||
internal actual companion object | ||
} |
164 changes: 164 additions & 0 deletions
164
libraries/stdlib/jvm/src/kotlin/collections/ArrayDeque.jvm.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package kotlin.collections | ||
|
||
import kotlin.IllegalArgumentException | ||
|
||
/** | ||
* Resizable-array implementation of the deque data structure. | ||
* | ||
* The name deque is short for "double ended queue" and is usually pronounced "deck". | ||
* | ||
* The collection provide methods for convenient access to the both ends. | ||
* It also implements [MutableList] interface and supports efficient get/set operations by index. | ||
*/ | ||
@SinceKotlin("1.4") | ||
// removeRange, modCount: Kotlin `protected` visibility is different from Java | ||
@Suppress("NO_ACTUAL_CLASS_MEMBER_FOR_EXPECTED_CLASS") | ||
public actual class ArrayDeque<E> private actual constructor( | ||
internal actual var elementData: Array<Any?>, | ||
) : AbstractMutableList<E>() { | ||
internal actual var head: Int = 0 | ||
|
||
actual override var size: Int = 0 | ||
internal set | ||
|
||
/** | ||
* Constructs an empty deque with specified [initialCapacity], or throws [IllegalArgumentException] if [initialCapacity] is negative. | ||
*/ | ||
public actual constructor(initialCapacity: Int) : this( | ||
when { | ||
initialCapacity == 0 -> EMPTY_ARRAY_OF_ANY | ||
initialCapacity > 0 -> arrayOfNulls(initialCapacity) | ||
else -> throw IllegalArgumentException("Illegal Capacity: $initialCapacity") | ||
} | ||
) | ||
|
||
/** | ||
* Constructs an empty deque. | ||
*/ | ||
public actual constructor() : this(EMPTY_ARRAY_OF_ANY) | ||
|
||
/** | ||
* Constructs a deque that contains the same elements as the specified [elements] collection in the same order. | ||
*/ | ||
public actual constructor(elements: Collection<E>) : this( | ||
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") | ||
if (elements.isEmpty()) EMPTY_ARRAY_OF_ANY else (elements as java.util.Collection<*>).toArray() | ||
) { | ||
size = elementData.size | ||
} | ||
|
||
override fun isEmpty(): Boolean = commonIsEmpty() | ||
|
||
/** | ||
* Returns the first element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
public actual fun first(): E = commonFirst() | ||
|
||
/** | ||
* Returns the first element, or `null` if this deque is empty. | ||
*/ | ||
public actual fun firstOrNull(): E? = commonFirstOrNull() | ||
|
||
/** | ||
* Returns the last element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
public actual fun last(): E = commonLast() | ||
|
||
/** | ||
* Returns the last element, or `null` if this deque is empty. | ||
*/ | ||
public actual fun lastOrNull(): E? = commonLastOrNull() | ||
|
||
/** | ||
* Prepends the specified [element] to this deque. | ||
*/ | ||
public actual fun addFirst(element: E): Unit = commonAddFirst(element) | ||
|
||
/** | ||
* Appends the specified [element] to this deque. | ||
*/ | ||
public actual fun addLast(element: E): Unit = commonAddLast(element) | ||
|
||
/** | ||
* Removes the first element from this deque and returns that removed element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeFirst(): E = commonRemoveFirst() | ||
|
||
/** | ||
* Removes the first element from this deque and returns that removed element, or returns `null` if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeFirstOrNull(): E? = commonRemoveFirstOrNull() | ||
|
||
/** | ||
* Removes the last element from this deque and returns that removed element, or throws [NoSuchElementException] if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeLast(): E = commonRemoveLast() | ||
|
||
/** | ||
* Removes the last element from this deque and returns that removed element, or returns `null` if this deque is empty. | ||
*/ | ||
@IgnorableReturnValue | ||
public actual fun removeLastOrNull(): E? = commonRemoveLastOrNull() | ||
|
||
// MutableList, MutableCollection | ||
@IgnorableReturnValue | ||
public actual override fun add(element: E): Boolean = commonAdd(element) | ||
|
||
public actual override fun add(index: Int, element: E): Unit = commonAdd(index, element) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun addAll(elements: Collection<E>): Boolean = commonAddAll(elements) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun addAll(index: Int, elements: Collection<E>): Boolean = commonAddAll(index, elements) | ||
|
||
public actual override fun get(index: Int): E = commonGet(index) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun set(index: Int, element: E): E = commonSet(index, element) | ||
|
||
public actual override fun contains(element: E): Boolean = commonContains(element) | ||
|
||
public actual override fun indexOf(element: E): Int = commonIndexOf(element) | ||
|
||
public actual override fun lastIndexOf(element: E): Int = commonLastIndexOf(element) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun remove(element: E): Boolean = commonRemove(element) | ||
|
||
@IgnorableReturnValue | ||
public actual override fun removeAt(index: Int): E = commonRemoveAt(index) | ||
|
||
@IgnorableReturnValue | ||
public override fun removeAll(elements: Collection<E>): Boolean = commonRemoveAll(elements) | ||
|
||
@IgnorableReturnValue | ||
public override fun retainAll(elements: Collection<E>): Boolean = commonRetainAll(elements) | ||
|
||
public actual override fun clear(): Unit = commonClear() | ||
|
||
@Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE") // different visibility inherited from the base class | ||
override fun <T> toArray(array: Array<T>): Array<T> = commonToArray(array) | ||
|
||
@Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE") // different visibility inherited from the base class | ||
override fun toArray(): Array<Any?> = commonToArray() | ||
|
||
override fun removeRange(fromIndex: Int, toIndex: Int): Unit = commonRemoveRange(fromIndex, toIndex) | ||
|
||
// JVM-only | ||
public override fun removeIf(filter: java.util.function.Predicate<in E>): Boolean = filterInPlace { !filter.test(it) } | ||
|
||
internal actual fun registerModification() { | ||
++modCount | ||
} | ||
|
||
internal actual companion object | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.