diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt index 2a4777839fd..d3675e141a7 100644 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ b/libraries/stdlib/src/generated/_Snapshots.kt @@ -14,88 +14,77 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col * Returns an ArrayList of all elements */ public fun Array.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun BooleanArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun ByteArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun CharArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun DoubleArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun FloatArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun IntArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun LongArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() } /** * Returns an ArrayList of all elements */ public fun ShortArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.asList().toArrayList() +} + +/** + * Returns an ArrayList of all elements + */ +public fun Collection.toArrayList(): ArrayList { + return ArrayList(this) } /** * Returns an ArrayList of all elements */ public fun Iterable.toArrayList(): ArrayList { - return toCollection(ArrayList(collectionSizeOrDefault(10))) + return toCollection(ArrayList()) } /** @@ -257,70 +246,70 @@ public fun > String.toCollection(collection: C): * Returns a HashSet of all elements */ public fun Array.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun BooleanArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun ByteArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun CharArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun DoubleArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun FloatArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun IntArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun LongArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun ShortArray.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(size()))) } /** * Returns a HashSet of all elements */ public fun Iterable.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12)))) } /** @@ -343,7 +332,7 @@ public fun Stream.toHashSet(): HashSet { * Returns a HashSet of all elements */ public fun String.toHashSet(): HashSet { - return toCollection(HashSet()) + return toCollection(HashSet(mapCapacity(length()))) } /** @@ -453,95 +442,77 @@ public fun Map.toList(): List> { * Returns a List containing all elements */ public fun Array.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun BooleanArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun ByteArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun CharArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun DoubleArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun FloatArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun IntArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun LongArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun ShortArray.toList(): List { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list + return this.toArrayList() } /** * Returns a List containing all elements */ public fun Iterable.toList(): List { - return toCollection(ArrayList(collectionSizeOrDefault(10))) + return this.toArrayList() } /** * Returns a List containing all elements */ public fun Sequence.toList(): List { - return toCollection(ArrayList()) + return this.toArrayList() } @@ -550,21 +521,22 @@ deprecated("Migrate to using Sequence and respective functions") * Returns a List containing all elements */ public fun Stream.toList(): List { - return toCollection(ArrayList()) + return this.toArrayList() } /** * Returns a List containing all elements */ public fun String.toList(): List { - return toCollection(ArrayList(length())) + return this.toArrayList() } /** * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun Array.toMap(selector: (T) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -575,7 +547,8 @@ public inline fun Array.toMap(selector: (T) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun BooleanArray.toMap(selector: (Boolean) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -586,7 +559,8 @@ public inline fun BooleanArray.toMap(selector: (Boolean) -> K): Map ByteArray.toMap(selector: (Byte) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -597,7 +571,8 @@ public inline fun ByteArray.toMap(selector: (Byte) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun CharArray.toMap(selector: (Char) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -608,7 +583,8 @@ public inline fun CharArray.toMap(selector: (Char) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun DoubleArray.toMap(selector: (Double) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -619,7 +595,8 @@ public inline fun DoubleArray.toMap(selector: (Double) -> K): Map * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun FloatArray.toMap(selector: (Float) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -630,7 +607,8 @@ public inline fun FloatArray.toMap(selector: (Float) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun IntArray.toMap(selector: (Int) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -641,7 +619,8 @@ public inline fun IntArray.toMap(selector: (Int) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun LongArray.toMap(selector: (Long) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -652,7 +631,8 @@ public inline fun LongArray.toMap(selector: (Long) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun ShortArray.toMap(selector: (Short) -> K): Map { - val result = LinkedHashMap() + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -663,7 +643,8 @@ public inline fun ShortArray.toMap(selector: (Short) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun Iterable.toMap(selector: (T) -> K): Map { - val result = LinkedHashMap() + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } @@ -698,81 +679,238 @@ public inline fun Stream.toMap(selector: (T) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun String.toMap(selector: (Char) -> K): Map { - val result = LinkedHashMap() + val capacity = (length()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { result.put(selector(element), element) } return result } +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun Array.toMap(selector: (T) -> K, transform: (T) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun Iterable.toMap(selector: (T) -> K, transform: (T) -> V): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun Sequence.toMap(selector: (T) -> K, transform: (T) -> V): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + + +deprecated("Migrate to using Sequence and respective functions") +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun Stream.toMap(selector: (T) -> K, transform: (T) -> V): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + */ +public inline fun String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { + val capacity = (length()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + /** * Returns a Set of all elements */ public fun Array.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun BooleanArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun ByteArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun CharArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun DoubleArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun FloatArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun IntArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun LongArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun ShortArray.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(size()))) } /** * Returns a Set of all elements */ public fun Iterable.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(collectionSizeOrDefault(12)))) } /** @@ -795,7 +933,7 @@ public fun Stream.toSet(): Set { * Returns a Set of all elements */ public fun String.toSet(): Set { - return toCollection(LinkedHashSet()) + return toCollection(LinkedHashSet(mapCapacity(length()))) } /** diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index c27daaa8665..4993a3bcdcf 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -46,7 +46,7 @@ public fun listOf(vararg values: T): List = if (values.size() == 0) emptyL public fun listOf(): List = emptyList() /** Returns a new read-only ordered set with the given elements. */ -public fun setOf(vararg values: T): Set = if (values.size() == 0) emptySet() else values.toCollection(LinkedHashSet()) +public fun setOf(vararg values: T): Set = if (values.size() == 0) emptySet() else values.toCollection(LinkedHashSet(mapCapacityForValues(values))) /** Returns an empty read-only set. */ public fun setOf(): Set = emptySet() @@ -58,10 +58,10 @@ public fun linkedListOf(vararg values: T): LinkedList = values.toCollectio public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size())) /** Returns a new [HashSet] with the given elements. */ -public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(values.size())) +public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(mapCapacityForValues(values))) /** Returns a new [LinkedHashSet] with the given elements. */ -public fun linkedSetOf(vararg values: T): LinkedHashSet = values.toCollection(LinkedHashSet(values.size())) +public fun linkedSetOf(vararg values: T): LinkedHashSet = values.toCollection(LinkedHashSet(mapCapacityForValues(values))) /** * Returns an [IntRange] of the valid indices for this collection. diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index d2148da4f5d..7c7fc425821 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -38,7 +38,7 @@ public fun mapOf(): Map = emptyMap() * @sample test.collections.MapTest.createUsingPairs */ public fun hashMapOf(vararg values: Pair): HashMap { - val answer = HashMap(values.size()) + val answer = HashMap(mapCapacityForValues(values)) answer.putAll(*values) return answer } @@ -51,11 +51,33 @@ public fun hashMapOf(vararg values: Pair): HashMap { * @sample test.collections.MapTest.createLinkedMap */ public fun linkedMapOf(vararg values: Pair): LinkedHashMap { - val answer = LinkedHashMap(values.size()) + val answer = LinkedHashMap(mapCapacityForValues(values)) answer.putAll(*values) return answer } +/** + * Calculate the initial capacity of a map, based on Guava's com.google.common.collect.Maps approach. This is equivalent + * to the Collection constructor for HashSet, (c.size()/.75f) + 1, but provides further optimisations for very small or + * very large sizes, allows support non-collection classes, and provides consistency for all map based class construction. + */ + +private val MAX_POWER_OF_TWO: Int = 1 shl (Integer.SIZE - 2) + +private fun mapCapacity(expectedSize: Int): Int { + if (expectedSize < 3) { + return expectedSize + 1 + } + if (expectedSize < MAX_POWER_OF_TWO) { + return expectedSize + expectedSize / 3 + } + return Integer.MAX_VALUE // any large value +} + +private fun mapCapacityForValues(values: Array): Int { + return mapCapacity(values.size()) +} + /** * Returns the [Map] if its not null, or the empty [Map] otherwise. */ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 20e19348ed6..d409c266944 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -1,6 +1,7 @@ package templates import templates.Family.* +import java.util.ArrayList fun snapshots(): List { val templates = arrayListOf() @@ -22,13 +23,19 @@ fun snapshots(): List { templates add f("toSet()") { doc { "Returns a Set of all elements" } returns("Set") - body { "return toCollection(LinkedHashSet())" } + body { "return toCollection(LinkedHashSet(mapCapacity(collectionSizeOrDefault(12))))" } + body(Sequences) { "return toCollection(LinkedHashSet())" } + body(Strings) { "return toCollection(LinkedHashSet(mapCapacity(length())))" } + body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(LinkedHashSet(mapCapacity(size())))" } } templates add f("toHashSet()") { doc { "Returns a HashSet of all elements" } returns("HashSet") - body { "return toCollection(HashSet())" } + body { "return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12))))" } + body(Sequences) { "return toCollection(HashSet())" } + body(Strings) { "return toCollection(HashSet(mapCapacity(length())))" } + body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet(mapCapacity(size())))" } } templates add f("toSortedSet()") { @@ -40,16 +47,10 @@ fun snapshots(): List { templates add f("toArrayList()") { doc { "Returns an ArrayList of all elements" } returns("ArrayList") - body { "return toCollection(ArrayList(collectionSizeOrDefault(10)))" } - body(Sequences) { "return toCollection(ArrayList())" } + body { "return toCollection(ArrayList())" } + body(Collections) { "return ArrayList(this)" } body(Strings) { "return toCollection(ArrayList(length()))" } - body(ArraysOfObjects, ArraysOfPrimitives) { - """ - val list = ArrayList(size()) - for (item in this) list.add(item) - return list - """ - } + body(ArraysOfObjects, ArraysOfPrimitives) { "return this.asList().toArrayList()" } } templates add f("toList()") { @@ -69,16 +70,7 @@ fun snapshots(): List { templates add f("toList()") { doc { "Returns a List containing all elements" } returns("List") - body { "return toCollection(ArrayList(collectionSizeOrDefault(10)))" } - body(Sequences) { "return toCollection(ArrayList())" } - body(Strings) { "return toCollection(ArrayList(length()))" } - body(ArraysOfObjects, ArraysOfPrimitives) { - """ - val list = ArrayList(size()) - for (item in this) list.add(item) - return list - """ - } + body { "return this.toArrayList()" } } templates add f("toLinkedList()") { @@ -96,7 +88,23 @@ fun snapshots(): List { """ } returns("Map") + + /** + * Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection + * constructor. + */ + body { + """ + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result + """ + } + body(Sequences) { """ val result = LinkedHashMap() for (element in this) { @@ -105,6 +113,83 @@ fun snapshots(): List { return result """ } + body(Strings) { + """ + val capacity = (length()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result + """ + } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result + """ + } + } + + templates add f("toMap(selector: (T) -> K, transform: (T) -> V)") { + inline(true) + typeParam("K") + typeParam("V") + doc { + """ + Returns Map containing all the values provided by *transform* and indexed by *selector* from the given collection + """ + } + returns("Map") + + /** + * Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection + * constructor. + */ + + body { + """ + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result + """ + } + body(Sequences) { + """ + val result = LinkedHashMap() + for (element in this) { + result.put(selector(element), transform(element)) + } + return result + """ + } + body(Strings) { + """ + val capacity = (length()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result + """ + } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result + """ + } } return templates