-
Notifications
You must be signed in to change notification settings - Fork 62
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
Add persistent Collection builder functions #166
Open
Goooler
wants to merge
1
commit into
Kotlin:master
Choose a base branch
from
Goooler:add-build-extensions
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.
Open
Changes from all commits
Commits
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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -236,6 +236,8 @@ final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutab | |
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin.sequences/Sequence<kotlin/Pair<#A, #B>>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.sequences.Sequence<kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin/Array<out kotlin/Pair<#A, #B>>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.Array<out|kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin/Pair<#A, #B>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.Pair<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> kotlinx.collections.immutable/buildPersistentHashMap(kotlin/Function1<kotlinx.collections.immutable/PersistentMap.Builder<#A, #B>, kotlin/Unit>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/buildPersistentHashMap|buildPersistentHashMap(kotlin.Function1<kotlinx.collections.immutable.PersistentMap.Builder<0:0,0:1>,kotlin.Unit>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> kotlinx.collections.immutable/buildPersistentMap(kotlin/Function1<kotlinx.collections.immutable/PersistentMap.Builder<#A, #B>, kotlin/Unit>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/buildPersistentMap|buildPersistentMap(kotlin.Function1<kotlinx.collections.immutable.PersistentMap.Builder<0:0,0:1>,kotlin.Unit>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentCollection<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentCollection<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentCollection<#A>).kotlinx.collections.immutable/plus(#A): kotlinx.collections.immutable/PersistentCollection<#A> // kotlinx.collections.immutable/plus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentList<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentList<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0] | ||
|
@@ -244,3 +246,6 @@ final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentList | |
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentSet<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentSet<#A>).kotlinx.collections.immutable/mutate(kotlin/Function1<kotlin.collections/MutableSet<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/mutate|[email protected]<0:0>(kotlin.Function1<kotlin.collections.MutableSet<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentSet<#A>).kotlinx.collections.immutable/plus(#A): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/plus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> kotlinx.collections.immutable/buildPersistentHashSet(kotlin/Function1<kotlinx.collections.immutable/PersistentSet.Builder<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/buildPersistentHashSet|buildPersistentHashSet(kotlin.Function1<kotlinx.collections.immutable.PersistentSet.Builder<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> kotlinx.collections.immutable/buildPersistentList(kotlin/Function1<kotlinx.collections.immutable/PersistentList.Builder<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentList<#A> // kotlinx.collections.immutable/buildPersistentList|buildPersistentList(kotlin.Function1<kotlinx.collections.immutable.PersistentList.Builder<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: kotlin/Any?> kotlinx.collections.immutable/buildPersistentSet(kotlin/Function1<kotlinx.collections.immutable/PersistentSet.Builder<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/buildPersistentSet|buildPersistentSet(kotlin.Function1<kotlinx.collections.immutable.PersistentSet.Builder<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0] |
This file contains 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 |
---|---|---|
|
@@ -16,6 +16,10 @@ import kotlinx.collections.immutable.implementations.persistentOrderedMap.Persis | |
import kotlinx.collections.immutable.implementations.persistentOrderedMap.PersistentOrderedMapBuilder | ||
import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSet | ||
import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSetBuilder | ||
import kotlin.contracts.ExperimentalContracts | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
import kotlin.experimental.ExperimentalTypeInference | ||
|
||
//@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") | ||
//inline fun <T> @kotlin.internal.Exact ImmutableCollection<T>.mutate(mutator: (MutableCollection<T>) -> Unit): ImmutableCollection<T> = builder().apply(mutator).build() | ||
|
@@ -766,3 +770,76 @@ public fun <K, V> Map<K, V>.toPersistentHashMap(): PersistentMap<K, V> | |
= this as? PersistentHashMap | ||
?: (this as? PersistentHashMapBuilder<K, V>)?.build() | ||
?: PersistentHashMap.emptyOf<K, V>().putAll(this) | ||
|
||
/** | ||
* Builds a new [PersistentList] by populating a [PersistentList.Builder] using the given [builderAction] | ||
* and returning an immutable list with the same elements. | ||
* | ||
* The list passed as a receiver to the [builderAction] is valid only inside that function. | ||
* Using it outside the function produces an unspecified behavior. | ||
*/ | ||
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) | ||
public inline fun <T> buildPersistentList(@BuilderInference builderAction: PersistentList.Builder<T>.() -> Unit): PersistentList<T> { | ||
contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } | ||
return persistentListOf<T>().builder().apply(builderAction).build() | ||
} | ||
Comment on lines
+781
to
+785
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/** | ||
* Builds a new [PersistentSet] by populating a [PersistentSet.Builder] using the given [builderAction] | ||
* and returning an immutable set with the same elements. | ||
* | ||
* The set passed as a receiver to the [builderAction] is valid only inside that function. | ||
* Using it outside the function produces an unspecified behavior. | ||
* | ||
* Elements of the set are iterated in the order they were added by the [builderAction]. | ||
*/ | ||
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) | ||
public inline fun <T> buildPersistentSet(@BuilderInference builderAction: PersistentSet.Builder<T>.() -> Unit): PersistentSet<T> { | ||
contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } | ||
return persistentSetOf<T>().builder().apply(builderAction).build() | ||
} | ||
|
||
/** | ||
* Builds a new [PersistentSet] by populating a [PersistentSet.Builder] using the given [builderAction] | ||
* and returning an immutable set with the same elements. | ||
* | ||
* The set passed as a receiver to the [builderAction] is valid only inside that function. | ||
* Using it outside the function produces an unspecified behavior. | ||
* | ||
* Order of the elements in the returned set is unspecified. | ||
*/ | ||
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) | ||
public inline fun <T> buildPersistentHashSet(@BuilderInference builderAction: PersistentSet.Builder<T>.() -> Unit): PersistentSet<T> { | ||
contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } | ||
return persistentHashSetOf<T>().builder().apply(builderAction).build() | ||
} | ||
|
||
/** | ||
* Builds a new [PersistentMap] by populating a [PersistentMap.Builder] using the given [builderAction] | ||
* and returning an immutable map with the same key-value pairs. | ||
* | ||
* The map passed as a receiver to the [builderAction] is valid only inside that function. | ||
* Using it outside the function produces an unspecified behavior. | ||
* | ||
* Entries of the map are iterated in the order they were added by the [builderAction]. | ||
*/ | ||
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) | ||
public inline fun <K, V> buildPersistentMap(@BuilderInference builderAction: PersistentMap.Builder<K, V>.() -> Unit): PersistentMap<K, V> { | ||
contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } | ||
return persistentMapOf<K, V>().builder().apply(builderAction).build() | ||
} | ||
|
||
/** | ||
* Builds a new [PersistentMap] by populating a [PersistentMap.Builder] using the given [builderAction] | ||
* and returning an immutable map with the same key-value pairs. | ||
* | ||
* The map passed as a receiver to the [builderAction] is valid only inside that function. | ||
* Using it outside the function produces an unspecified behavior. | ||
* | ||
* Order of the entries in the returned map is unspecified. | ||
*/ | ||
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) | ||
public inline fun <K, V> buildPersistentHashMap(@BuilderInference builderAction: PersistentMap.Builder<K, V>.() -> Unit): PersistentMap<K, V> { | ||
contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } | ||
return persistentHashMapOf<K, V>().builder().apply(builderAction).build() | ||
} |
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Builders in kotlinx.collections.immutable are capable of constructing a new persistent collection multiple times. However, I'm struggling to identify a compelling use case for retaining a reference to the builder and utilizing it outside the builderAction. If we constraint the builder to be valid only within the function, could using a
MutableList
as the receiver be a more performance-efficient choice?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @ilya-g, WDYT?