提交 e09c0ae2 编写于 作者: I Ilya Gorbunov

Rename toMap { selector } to toMapBy.

#KT-6657
上级 fd7b6704
......@@ -5514,236 +5514,281 @@ public fun ShortArray.toList(): List<Short> {
return this.toArrayList()
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <T, K> Array<out T>.toMap(selector: (T) -> K): Map<K, T> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> BooleanArray.toMap(selector: (Boolean) -> K): Map<K, Boolean> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> ByteArray.toMap(selector: (Byte) -> K): Map<K, Byte> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> CharArray.toMap(selector: (Char) -> K): Map<K, Char> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> DoubleArray.toMap(selector: (Double) -> K): Map<K, Double> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> FloatArray.toMap(selector: (Float) -> K): Map<K, Float> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> IntArray.toMap(selector: (Int) -> K): Map<K, Int> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> LongArray.toMap(selector: (Long) -> K): Map<K, Long> {
return toMapBy(selector)
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> ShortArray.toMap(selector: (Short) -> K): Map<K, Short> {
return toMapBy(selector)
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <T, K> Array<out T>.toMap(selector: (T) -> K): Map<K, T> {
public inline fun <T, K, V> Array<out T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> BooleanArray.toMap(selector: (Boolean) -> K): Map<K, Boolean> {
public inline fun <K, V> BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Boolean>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> ByteArray.toMap(selector: (Byte) -> K): Map<K, Byte> {
public inline fun <K, V> ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Byte>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> CharArray.toMap(selector: (Char) -> K): Map<K, Char> {
public inline fun <K, V> CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> DoubleArray.toMap(selector: (Double) -> K): Map<K, Double> {
public inline fun <K, V> DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Double>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> FloatArray.toMap(selector: (Float) -> K): Map<K, Float> {
public inline fun <K, V> FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Float>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> IntArray.toMap(selector: (Int) -> K): Map<K, Int> {
public inline fun <K, V> IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Int>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> LongArray.toMap(selector: (Long) -> K): Map<K, Long> {
public inline fun <K, V> LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Long>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> ShortArray.toMap(selector: (Short) -> K): Map<K, Short> {
public inline fun <K, V> ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Short>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <T, K, V> Array<out T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
public inline fun <T, K> Array<out T>.toMapBy(selector: (T) -> K): Map<K, T> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map<K, V> {
public inline fun <K> BooleanArray.toMapBy(selector: (Boolean) -> K): Map<K, Boolean> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Boolean>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map<K, V> {
public inline fun <K> ByteArray.toMapBy(selector: (Byte) -> K): Map<K, Byte> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Byte>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
public inline fun <K> CharArray.toMapBy(selector: (Char) -> K): Map<K, Char> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map<K, V> {
public inline fun <K> DoubleArray.toMapBy(selector: (Double) -> K): Map<K, Double> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Double>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map<K, V> {
public inline fun <K> FloatArray.toMapBy(selector: (Float) -> K): Map<K, Float> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Float>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map<K, V> {
public inline fun <K> IntArray.toMapBy(selector: (Int) -> K): Map<K, Int> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Int>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map<K, V> {
public inline fun <K> LongArray.toMapBy(selector: (Long) -> K): Map<K, Long> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Long>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map<K, V> {
public inline fun <K> ShortArray.toMapBy(selector: (Short) -> K): Map<K, Short> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Short>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
......
......@@ -894,28 +894,33 @@ public fun <T> Iterable<T>.toList(): List<T> {
return this.toArrayList()
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <T, K> Iterable<T>.toMap(selector: (T) -> K): Map<K, T> {
return toMapBy(selector)
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <T, K> Iterable<T>.toMap(selector: (T) -> K): Map<K, T> {
public inline fun <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
public inline fun <T, K> Iterable<T>.toMapBy(selector: (T) -> K): Map<K, T> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
......
......@@ -441,26 +441,31 @@ public fun <T> Sequence<T>.toList(): List<T> {
return this.toArrayList()
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <T, K> Sequence<T>.toMap(selector: (T) -> K): Map<K, T> {
return toMapBy(selector)
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <T, K> Sequence<T>.toMap(selector: (T) -> K): Map<K, T> {
val result = LinkedHashMap<K, T>()
public inline fun <T, K, V> Sequence<T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val result = LinkedHashMap<K, V>()
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <T, K, V> Sequence<T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val result = LinkedHashMap<K, V>()
public inline fun <T, K> Sequence<T>.toMapBy(selector: (T) -> K): Map<K, T> {
val result = LinkedHashMap<K, T>()
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
......
......@@ -386,28 +386,33 @@ public fun String.toList(): List<Char> {
return this.toArrayList()
}
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
return toMapBy(selector)
}
/**
* Returns Map containing the values from the given collection indexed by [selector].
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
public inline fun <K> String.toMapBy(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(selector(element), element)
}
return result
}
......
......@@ -160,7 +160,7 @@ class MapTest {
}
@test fun createWithSelector() {
val map = listOf("a", "bb", "ccc").toMap { it.length() }
val map = listOf("a", "bb", "ccc").toMapBy { it.length() }
assertEquals(3, map.size())
assertEquals("a", map.get(1))
assertEquals("bb", map.get(2))
......@@ -168,7 +168,7 @@ class MapTest {
}
@test fun createWithSelectorAndOverwrite() {
val map = listOf("aa", "bb", "ccc").toMap { it.length() }
val map = listOf("aa", "bb", "ccc").toMapBy { it.length() }
assertEquals(2, map.size())
assertEquals("bb", map.get(2))
assertEquals("ccc", map.get(3))
......
......@@ -94,6 +94,16 @@ fun snapshots(): List<GenericFunction> {
}
templates add f("toMap(selector: (T) -> K)") {
inline(true)
include(Strings)
typeParam("K")
returns("Map<K, T>")
deprecate("Use toMapBy instead.")
deprecateReplacement("toMapBy(selector)")
body("return ${deprecateReplacement.default}")
}
templates add f("toMapBy(selector: (T) -> K)") {
inline(true)
typeParam("K")
doc {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册