Rename toMap { selector } to toMapBy.

#KT-6657
This commit is contained in:
Ilya Gorbunov
2015-10-08 00:39:47 +03:00
parent fd7b670414
commit e09c0ae2ff
6 changed files with 191 additions and 121 deletions
+135 -90
View File
@@ -5514,121 +5514,49 @@ public fun ShortArray.toList(): List<Short> {
return this.toArrayList()
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <T, K> Array<out T>.toMap(selector: (T) -> K): Map<K, T> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> BooleanArray.toMap(selector: (Boolean) -> K): Map<K, Boolean> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Boolean>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> ByteArray.toMap(selector: (Byte) -> K): Map<K, Byte> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Byte>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> CharArray.toMap(selector: (Char) -> K): Map<K, Char> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> DoubleArray.toMap(selector: (Double) -> K): Map<K, Double> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Double>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> FloatArray.toMap(selector: (Float) -> K): Map<K, Float> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Float>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> IntArray.toMap(selector: (Int) -> K): Map<K, Int> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Int>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> LongArray.toMap(selector: (Long) -> K): Map<K, Long> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Long>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> ShortArray.toMap(selector: (Short) -> K): Map<K, Short> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Short>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
@@ -5748,6 +5676,123 @@ public inline fun <K, V> ShortArray.toMap(selector: (Short) -> K, transform: (Sh
return result
}
/**
* 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> Array<out T>.toMapBy(selector: (T) -> K): Map<K, T> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> BooleanArray.toMapBy(selector: (Boolean) -> K): Map<K, Boolean> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Boolean>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> ByteArray.toMapBy(selector: (Byte) -> K): Map<K, Byte> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Byte>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> CharArray.toMapBy(selector: (Char) -> K): Map<K, Char> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> DoubleArray.toMapBy(selector: (Double) -> K): Map<K, Double> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Double>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> FloatArray.toMapBy(selector: (Float) -> K): Map<K, Float> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Float>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> IntArray.toMapBy(selector: (Int) -> K): Map<K, Int> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Int>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> LongArray.toMapBy(selector: (Long) -> K): Map<K, Long> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Long>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* 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> ShortArray.toMapBy(selector: (Short) -> K): Map<K, Short> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Short>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* Returns a [Set] of all elements.
*/
+15 -10
View File
@@ -894,17 +894,9 @@ public fun <T> Iterable<T>.toList(): List<T> {
return this.toArrayList()
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <T, K> Iterable<T>.toMap(selector: (T) -> K): Map<K, T> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
@@ -920,6 +912,19 @@ public inline fun <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T)
return result
}
/**
* 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> Iterable<T>.toMapBy(selector: (T) -> K): Map<K, T> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* Returns a [Set] of all elements.
*/
+14 -9
View File
@@ -441,16 +441,9 @@ public fun <T> Sequence<T>.toList(): List<T> {
return this.toArrayList()
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <T, K> Sequence<T>.toMap(selector: (T) -> K): Map<K, T> {
val result = LinkedHashMap<K, T>()
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
@@ -465,6 +458,18 @@ public inline fun <T, K, V> Sequence<T>.toMap(selector: (T) -> K, transform: (T)
return result
}
/**
* 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> Sequence<T>.toMapBy(selector: (T) -> K): Map<K, T> {
val result = LinkedHashMap<K, T>()
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* Returns a [Set] of all elements.
*/
+15 -10
View File
@@ -386,17 +386,9 @@ public fun String.toList(): List<Char> {
return this.toArrayList()
}
/**
* 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.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
return toMapBy(selector)
}
/**
@@ -412,6 +404,19 @@ public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -
return result
}
/**
* 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> String.toMapBy(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
}
return result
}
/**
* Returns a [Set] of all elements.
*/