From efefa64ac5d1609f0a8ee89b233817843299323b Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 8 Nov 2017 08:05:54 +0300 Subject: [PATCH] Rewrite snapshot and set operations in new DSL --- .../src/generators/GenerateStandardLib.kt | 4 +- .../kotlin-stdlib-gen/src/templates/Sets.kt | 91 +++++---- .../src/templates/Snapshots.kt | 190 +++++++++++------- 3 files changed, 173 insertions(+), 112 deletions(-) diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index a9c15d68c64..821c64d78c9 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -16,9 +16,9 @@ fun main(args: Array) { Filtering, Ordering, // ArrayOps, -// Snapshots, + Snapshots, // Mapping, -// SetOps, + SetOps, Aggregates, Guards, Generators, diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt index 690ed798368..41fc8360972 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt @@ -1,14 +1,30 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package templates import templates.Family.* import templates.SequenceClass.* -fun sets(): List { - val templates = arrayListOf() +object SetOps : TemplateGroupBase() { - templates add f("toMutableSet()") { - exclude(Strings) - doc { f -> + val f_toMutableSet = fn("toMutableSet()") { + includeDefault() + } builder { + doc { """ Returns a mutable set containing all distinct ${f.element.pluralize()} from the given ${f.collection}. @@ -41,9 +57,10 @@ fun sets(): List { } } - templates add f("distinct()") { - exclude(Strings) - doc { f -> + val f_distinct = fn("distinct()") { + includeDefault() + } builder { + doc { """ Returns a ${f.mapResult} containing only distinct ${f.element.pluralize()} from the given ${f.collection}. @@ -53,14 +70,17 @@ fun sets(): List { returns("List") body { "return this.toMutableSet().toList()" } - sequenceClassification(intermediate, stateful) - returns(Sequences) { "Sequence" } - body(Sequences) { "return this.distinctBy { it }" } + specialFor(Sequences) { + sequenceClassification(intermediate, stateful) + returns("Sequence") + body { "return this.distinctBy { it }" } + } } - templates add f("distinctBy(selector: (T) -> K)") { - exclude(Strings) - doc { f -> + val f_distinctBy = fn("distinctBy(selector: (T) -> K)") { + includeDefault() + } builder { + doc { """ Returns a ${f.mapResult} containing only ${f.element.pluralize()} from the given ${f.collection} having distinct keys returned by the given [selector] function. @@ -69,7 +89,7 @@ fun sets(): List { """ } - inline(true) + inline() typeParam("K") returns("List") body { @@ -85,21 +105,19 @@ fun sets(): List { """ } - inline(false, Sequences) - returns(Sequences) { "Sequence" } - sequenceClassification(intermediate, stateful) - body(Sequences) { - """ - return DistinctSequence(this, selector) - """ + specialFor(Sequences) { + inline(Inline.No) + returns("Sequence") + sequenceClassification(intermediate, stateful) + body { """return DistinctSequence(this, selector)""" } } - } - templates add f("union(other: Iterable)") { + val f_union = fn("union(other: Iterable)") { + include(Family.defaultFamilies - Sequences) + } builder { infix(true) - exclude(Strings, Sequences) - doc { f -> + doc { """ Returns a set containing all distinct elements from both collections. @@ -118,10 +136,11 @@ fun sets(): List { } } - templates add f("intersect(other: Iterable)") { - infix(true) - exclude(Strings, Sequences) - doc { f -> + val f_intersect = fn("intersect(other: Iterable)") { + include(Family.defaultFamilies - Sequences) + } builder { + infix() + doc { """ Returns a set containing all elements that are contained by both this set and the specified collection. @@ -138,10 +157,11 @@ fun sets(): List { } } - templates add f("subtract(other: Iterable)") { - infix(true) - exclude(Strings, Sequences) - doc { f -> + val f_subtract = fn("subtract(other: Iterable)") { + include(Family.defaultFamilies - Sequences) + } builder { + infix() + doc { """ Returns a set containing all elements that are contained by this ${f.collection} and not contained by the specified collection. @@ -158,5 +178,4 @@ fun sets(): List { } } - return templates -} \ No newline at end of file +} diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 85e8a93307d..d0be59d8caa 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -1,13 +1,36 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package templates import templates.Family.* -fun snapshots(): List { - val templates = arrayListOf() +object Snapshots : TemplateGroupBase() { - templates add f("toCollection(destination: C)") { + init { + defaultBuilder { + sequenceClassification(SequenceClass.terminal) + } + } + + val f_toCollection = fn("toCollection(destination: C)") { + includeDefault() include(CharSequences) - doc { f -> "Appends all ${f.element.pluralize()} to the given [destination] collection." } + } builder { + doc { "Appends all ${f.element.pluralize()} to the given [destination] collection." } returns("C") typeParam("C : MutableCollection") body { @@ -20,8 +43,11 @@ fun snapshots(): List { } } - templates add f("toSet()") { - doc { f -> + val f_toSet = fn("toSet()") { + includeDefault() + include(CharSequences) + } builder { + doc { """ Returns a [Set] of all ${f.element.pluralize()}. @@ -44,7 +70,7 @@ fun snapshots(): List { } body(Sequences) { "return toCollection(LinkedHashSet()).optimizeReadOnlySet()" } - body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { f -> + body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { val size = if (f == CharSequences) "length" else "size" """ return when ($size) { @@ -56,8 +82,11 @@ fun snapshots(): List { } } - templates add f("toHashSet()") { - doc { f -> "Returns a [HashSet] of all ${f.element.pluralize()}." } + val f_toHashSet = fn("toHashSet()") { + includeDefault() + include(CharSequences) + } builder { + doc { "Returns a [HashSet] of all ${f.element.pluralize()}." } returns("HashSet") body { "return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12))))" } body(Sequences) { "return toCollection(HashSet())" } @@ -65,19 +94,24 @@ fun snapshots(): List { body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet(mapCapacity(size)))" } } - templates add f("toSortedSet()") { + val f_toSortedSet = fn("toSortedSet()") { + includeDefault() include(CharSequences) - jvmOnly(true) + platforms(Platform.JVM) + } builder { + jvmOnly = true typeParam("T: Comparable") - doc { f -> "Returns a [SortedSet] of all ${f.element.pluralize()}." } + doc { "Returns a [SortedSet] of all ${f.element.pluralize()}." } returns("SortedSet") body { "return toCollection(TreeSet())" } } - templates add f("toSortedSet(comparator: Comparator)") { - only(Iterables, ArraysOfObjects, Sequences) - jvmOnly(true) - doc { f -> + val f_toSortedSet_comparator = fn("toSortedSet(comparator: Comparator)") { + include(Iterables, ArraysOfObjects, Sequences) + platforms(Platform.JVM) + } builder { + jvmOnly = true + doc { """ Returns a [SortedSet] of all ${f.element.pluralize()}. @@ -88,8 +122,11 @@ fun snapshots(): List { body { "return toCollection(TreeSet(comparator))" } } - templates add f("toMutableList()") { - doc { f -> "Returns a [MutableList] filled with all ${f.element.pluralize()} of this ${f.collection}." } + val f_toMutableList = fn("toMutableList()") { + includeDefault() + include(Collections, CharSequences) + } builder { + doc { "Returns a [MutableList] filled with all ${f.element.pluralize()} of this ${f.collection}." } returns("MutableList") body { "return toCollection(ArrayList())" } body(Iterables) { @@ -111,33 +148,11 @@ fun snapshots(): List { } } - templates add f("toList()") { - only(Maps) - doc { "Returns a [List] containing all key-value pairs." } - returns("List>") - body { - """ - if (size == 0) - return emptyList() - val iterator = entries.iterator() - if (!iterator.hasNext()) - return emptyList() - val first = iterator.next() - if (!iterator.hasNext()) - return listOf(first.toPair()) - val result = ArrayList>(size) - result.add(first.toPair()) - do { - result.add(iterator.next().toPair()) - } while (iterator.hasNext()) - return result - """ - } - } - - templates add f("toList()") { - include(CharSequences) - doc { f -> "Returns a [List] containing all ${f.element.pluralize()}." } + val f_toList = fn("toList()") { + includeDefault() + include(Maps, CharSequences) + } builder { + doc { "Returns a [List] containing all ${f.element.pluralize()}." } returns("List") body { "return this.toMutableList().optimizeReadOnlyList()" } body(Iterables) { @@ -152,7 +167,7 @@ fun snapshots(): List { return this.toMutableList().optimizeReadOnlyList() """ } - body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { f -> + body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { """ return when (${ if (f == CharSequences) "length" else "size" }) { 0 -> emptyList() @@ -161,15 +176,39 @@ fun snapshots(): List { } """ } + specialFor(Maps) { + doc { "Returns a [List] containing all key-value pairs." } + returns("List>") + body { + """ + if (size == 0) + return emptyList() + val iterator = entries.iterator() + if (!iterator.hasNext()) + return emptyList() + val first = iterator.next() + if (!iterator.hasNext()) + return listOf(first.toPair()) + val result = ArrayList>(size) + result.add(first.toPair()) + do { + result.add(iterator.next().toPair()) + } while (iterator.hasNext()) + return result + """ + } + } } - templates add f("associate(transform: (T) -> Pair)") { - inline(true) + val f_associate = fn("associate(transform: (T) -> Pair)") { + includeDefault() include(CharSequences) + } builder { + inline() typeParam("K") typeParam("V") returns("Map") - doc { f -> + doc { """ Returns a [Map] containing key-value pairs provided by [transform] function applied to ${f.element.pluralize()} of the given ${f.collection}. @@ -204,14 +243,16 @@ fun snapshots(): List { } } - templates add f("associateTo(destination: M, transform: (T) -> Pair)") { - inline(true) + val f_associateTo = fn("associateTo(destination: M, transform: (T) -> Pair)") { + includeDefault() include(CharSequences) + } builder { + inline() typeParam("K") typeParam("V") typeParam("M : MutableMap") returns("M") - doc { f -> + doc { """ Populates and returns the [destination] mutable map with key-value pairs provided by [transform] function applied to each ${f.element} of the given ${f.collection}. @@ -229,11 +270,13 @@ fun snapshots(): List { } } - templates add f("associateBy(keySelector: (T) -> K)") { - inline(true) + val f_associateBy_key = fn("associateBy(keySelector: (T) -> K)") { + includeDefault() include(CharSequences) + } builder { + inline() typeParam("K") - doc { f -> + doc { """ Returns a [Map] containing the ${f.element.pluralize()} from the given ${f.collection} indexed by the key returned from [keySelector] function applied to each ${f.element}. @@ -245,11 +288,8 @@ fun snapshots(): List { } returns("Map") - /** - * Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection - * constructor. - */ - + // Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection + // constructor. body { """ val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) @@ -275,13 +315,15 @@ fun snapshots(): List { } } - templates add f("associateByTo(destination: M, keySelector: (T) -> K)") { - inline(true) + val f_associateByTo_key = fn("associateByTo(destination: M, keySelector: (T) -> K)") { + includeDefault() include(CharSequences) + } builder { + inline() typeParam("K") typeParam("M : MutableMap") returns("M") - doc { f -> + doc { """ Populates and returns the [destination] mutable map with key-value pairs, where key is provided by the [keySelector] function applied to each ${f.element} of the given ${f.collection} @@ -300,12 +342,14 @@ fun snapshots(): List { } } - templates add f("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { - inline(true) + val f_associateBy_key_value = fn("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { + includeDefault() include(CharSequences) + } builder { + inline() typeParam("K") typeParam("V") - doc { f -> + doc { """ Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to ${f.element.pluralize()} of the given ${f.collection}. @@ -346,15 +390,17 @@ fun snapshots(): List { } } - templates add f("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") { - inline(true) + val f_associateByTo_key_value = fn("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") { + includeDefault() include(CharSequences) + } builder { + inline() typeParam("K") typeParam("V") typeParam("M : MutableMap") returns("M") - doc { f -> + doc { """ Populates and returns the [destination] mutable map with key-value pairs, where key is provided by the [keySelector] function and @@ -372,8 +418,4 @@ fun snapshots(): List { """ } } - - templates.forEach { it.sequenceClassification(SequenceClass.terminal) } - - return templates -} \ No newline at end of file +}