StdLib generators: provide extension points for family properties specific to documentation and code.

This commit is contained in:
Ilya Gorbunov
2016-02-13 00:03:47 +03:00
parent 9781578af2
commit 8761819117
3 changed files with 70 additions and 64 deletions
@@ -446,18 +446,16 @@ fun aggregates(): List<GenericFunction> {
}
returns("T")
body { f ->
with (DocExtensions) {
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
var accumulator = this[0]
for (index in 1..lastIndex) {
accumulator = operation(index, accumulator, this[index])
}
return accumulator
"""
var accumulator = this[0]
for (index in 1..lastIndex) {
accumulator = operation(index, accumulator, this[index])
}
return accumulator
"""
}
}
@@ -488,18 +486,16 @@ fun aggregates(): List<GenericFunction> {
"""
}
body(ArraysOfObjects) { f ->
with (DocExtensions) {
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
var accumulator: S = this[0]
for (index in 1..lastIndex) {
accumulator = operation(index, accumulator, this[index])
}
return accumulator
"""
var accumulator: S = this[0]
for (index in 1..lastIndex) {
accumulator = operation(index, accumulator, this[index])
}
return accumulator
"""
}
}
@@ -515,20 +511,18 @@ fun aggregates(): List<GenericFunction> {
}
returns("T")
body { f ->
with (DocExtensions) {
"""
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
"""
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(index, get(index), accumulator)
--index
}
return accumulator
"""
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(index, get(index), accumulator)
--index
}
return accumulator
"""
}
}
@@ -546,20 +540,18 @@ fun aggregates(): List<GenericFunction> {
typeParam("T: S")
returns("S")
body { f ->
with (DocExtensions) {
"""
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
"""
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
var accumulator: S = get(index--)
while (index >= 0) {
accumulator = operation(index, get(index), accumulator)
--index
}
return accumulator
"""
var accumulator: S = get(index--)
while (index >= 0) {
accumulator = operation(index, get(index), accumulator)
--index
}
return accumulator
"""
}
}
@@ -570,18 +562,16 @@ fun aggregates(): List<GenericFunction> {
doc { f -> "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}." }
returns("T")
body { f ->
with (DocExtensions) {
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
var accumulator = this[0]
for (index in 1..lastIndex) {
accumulator = operation(accumulator, this[index])
}
return accumulator
"""
var accumulator = this[0]
for (index in 1..lastIndex) {
accumulator = operation(accumulator, this[index])
}
return accumulator
"""
}
}
@@ -606,18 +596,16 @@ fun aggregates(): List<GenericFunction> {
"""
}
body(ArraysOfObjects) { f ->
with (DocExtensions) {
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
"""
if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
var accumulator: S = this[0]
for (index in 1..lastIndex) {
accumulator = operation(accumulator, this[index])
}
return accumulator
"""
var accumulator: S = this[0]
for (index in 1..lastIndex) {
accumulator = operation(accumulator, this[index])
}
return accumulator
"""
}
}
@@ -25,6 +25,11 @@ enum class Family {
val isPrimitiveSpecialization: Boolean by lazy { this in primitiveSpecializations }
class DocExtension(val family: Family)
class CodeExtension(val family: Family)
val doc = DocExtension(this)
val code = CodeExtension(this)
companion object {
val primitiveSpecializations = setOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives)
val defaultFamilies = setOf(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
@@ -2,6 +2,19 @@ package templates
import templates.Family.*
val Family.DocExtension.collection: String
get() = with(DocExtensions) { family.collection }
val Family.DocExtension.element: String
get() = with(DocExtensions) { family.element }
val Family.CodeExtension.size: String
get() = when (family) {
Iterables, Collections, Lists, Sets, Maps, InvariantArraysOfObjects, ArraysOfObjects, ArraysOfPrimitives -> "size"
CharSequences, Strings -> "length"
else -> error("size property isn't supported for $family")
}
object DocExtensions {
val Family.element: String