Skip to content

Commit

Permalink
Explicit visibility everywhere
Browse files Browse the repository at this point in the history
Developing in API-mode (to build better libraries) requires explicitly stating public/protected/internal/private for everything and explicit return types for everything.
  • Loading branch information
Mikael Vejdemo-Johansson committed Feb 21, 2024
1 parent 80dbcde commit 1db5e7e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/org/appliedtopology/tda4j/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package org.appliedtopology.tda4j

import kotlin.jvm.JvmName

object Api {
fun <VertexT : Comparable<VertexT>, CoefficientT, R> tdaWith(
public object Api {
public fun <VertexT : Comparable<VertexT>, CoefficientT, R> tdaWith(
fieldContext: FieldContext<CoefficientT>,
chainContext: ChainContext<VertexT, CoefficientT>,
block: ChainContext<VertexT, CoefficientT>.() -> R,
Expand Down
51 changes: 28 additions & 23 deletions src/commonMain/kotlin/org/appliedtopology/tda4j/Chain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public open class Chain<VertexT, CoefficientT> protected constructor(
public val simplexComparator: Comparator<AbstractSimplex<VertexT>>,
internal val chainMap: MutableMap<AbstractSimplex<VertexT>, CoefficientT>,
) {
fun <V> mapToChain(transform: (CoefficientT) -> V): Chain<VertexT, V> =
public fun <V> mapToChain(transform: (CoefficientT) -> V): Chain<VertexT, V> =
Chain(
this.vertexComparator,
this.simplexComparator,
Expand All @@ -20,33 +20,33 @@ public open class Chain<VertexT, CoefficientT> protected constructor(
chainMap.keys.sortedWith(simplexComparator).map { simplex ->
"${chainMap[simplex]}*$simplex"
}.joinToString(" + ")
if (retval != "") {
return (retval)
return if (retval != "") {
(retval)
} else {
return ("0")
("0")
}
}

operator fun set(
public operator fun set(
simplex: AbstractSimplex<VertexT>,
value: CoefficientT,
) {
chainMap[simplex] = value
}

operator fun get(simplex: AbstractSimplex<VertexT>) = chainMap[simplex]
public fun getOrNull(simplex: AbstractSimplex<VertexT>): CoefficientT? = chainMap[simplex]

companion object {
public companion object {
public operator fun <VertexT, CoefficientT> invoke(
vertexComparator: Comparator<VertexT>,
simplexComparator: Comparator<AbstractSimplex<VertexT>>,
chainMap: MutableMap<AbstractSimplex<VertexT>, CoefficientT>,
) = Chain(vertexComparator, simplexComparator, chainMap)
): Chain<VertexT, CoefficientT> = Chain(vertexComparator, simplexComparator, chainMap)

public operator fun <VertexT : Comparable<VertexT>, CoefficientT> invoke(
simplexComparator: Comparator<AbstractSimplex<VertexT>>,
chainMap: MutableMap<AbstractSimplex<VertexT>, CoefficientT>,
) = Chain(naturalOrder(), simplexComparator, chainMap)
): Chain<VertexT, CoefficientT> = Chain(naturalOrder(), simplexComparator, chainMap)

public operator fun <VertexT : Comparable<VertexT>, CoefficientT> invoke(
chainMap: MutableMap<AbstractSimplex<VertexT>, CoefficientT>,
Expand All @@ -61,11 +61,11 @@ public open class Chain<VertexT, CoefficientT> protected constructor(

public class ChainContext<VertexT, CoefficientT> protected constructor(
vertexComparator: Comparator<VertexT>,
val coefficientContext: FieldContext<CoefficientT>,
public val coefficientContext: FieldContext<CoefficientT>,
) :
Group<Chain<VertexT, CoefficientT>>,
SimplexContext<VertexT>(vertexComparator) {
fun Chain<VertexT, CoefficientT>.zipToChain(
public fun Chain<VertexT, CoefficientT>.zipToChain(
other: Chain<VertexT, CoefficientT>,
operator: (CoefficientT, CoefficientT) -> CoefficientT,
): Chain<VertexT, CoefficientT> {
Expand Down Expand Up @@ -96,57 +96,62 @@ public class ChainContext<VertexT, CoefficientT> protected constructor(
right: Chain<VertexT, CoefficientT>,
): Chain<VertexT, CoefficientT> = left.zipToChain(right, coefficientContext::add)

operator fun CoefficientT.times(other: Chain<VertexT, CoefficientT>): Chain<VertexT, CoefficientT> =
public operator fun CoefficientT.times(other: Chain<VertexT, CoefficientT>): Chain<VertexT, CoefficientT> =
other.mapToChain {
with(coefficientContext) {
this@times * it
}
}

operator fun CoefficientT.times(other: AbstractSimplex<VertexT>): Chain<VertexT, CoefficientT> = this@times * Chain(other)
public operator fun CoefficientT.times(other: AbstractSimplex<VertexT>): Chain<VertexT, CoefficientT> = this@times * Chain(other)

fun AbstractSimplex<VertexT>.plus(other: Chain<VertexT, CoefficientT>): Chain<VertexT, CoefficientT> =
public fun AbstractSimplex<VertexT>.plus(other: Chain<VertexT, CoefficientT>): Chain<VertexT, CoefficientT> =
other +
Chain(
other.vertexComparator, other.simplexComparator,
mutableMapOf(this@plus to coefficientContext.one),
)

val AbstractSimplex<VertexT>.boundary: Chain<VertexT, CoefficientT>
public val AbstractSimplex<VertexT>.boundary: Chain<VertexT, CoefficientT>
get() = this@boundary.boundary(coefficientContext)

operator fun Chain.Companion.invoke(simplex: AbstractSimplex<VertexT>): Chain<VertexT, CoefficientT> =
public operator fun Chain.Companion.invoke(simplex: AbstractSimplex<VertexT>): Chain<VertexT, CoefficientT> =
Chain(vertexComparator, SimplexComparator(vertexComparator), mutableMapOf(simplex to coefficientContext.one))

operator fun Chain.Companion.invoke(vararg mappings: Pair<AbstractSimplex<VertexT>, CoefficientT>): Chain<VertexT, CoefficientT> =
public operator fun Chain.Companion.invoke(
vararg mappings: Pair<AbstractSimplex<VertexT>, CoefficientT>,
): Chain<VertexT, CoefficientT> =
Chain(
vertexComparator,
SimplexComparator(vertexComparator),
mutableMapOf(*mappings.toList().toTypedArray()),
)

val Chain<VertexT, CoefficientT>.boundary
public operator fun Chain<VertexT, CoefficientT>.get(simplex: AbstractSimplex<VertexT>): CoefficientT =
this@get.getOrNull(simplex) ?: coefficientContext.zero

public val Chain<VertexT, CoefficientT>.boundary: Chain<VertexT, CoefficientT>
get() =
this.chainMap.map { (simplex, coeff) ->
coeff * simplex.boundary
}.fold(zero, ::add)

val emptyChain: Chain<VertexT, CoefficientT> =
public val emptyChain: Chain<VertexT, CoefficientT> =
Chain(
vertexComparator,
SimplexComparator(vertexComparator),
mutableMapOf(),
)

// TODO Implement these parts of the Chain interface!
fun Chain<VertexT, CoefficientT>.isZero(): Boolean = TODO()
public fun Chain<VertexT, CoefficientT>.isZero(): Boolean = TODO()

val Chain<VertexT, CoefficientT>.leadingSimplex: AbstractSimplex<VertexT>
public val Chain<VertexT, CoefficientT>.leadingSimplex: AbstractSimplex<VertexT>
get() = TODO()
val Chain<VertexT, CoefficientT>.leadingCoefficient: CoefficientT
public val Chain<VertexT, CoefficientT>.leadingCoefficient: CoefficientT
get() = TODO()

companion object {
public companion object {
public operator fun <VertexT, CoefficientT> invoke(
vertexComparator: Comparator<VertexT>,
coefficientContext: FieldContext<CoefficientT>,
Expand Down
2 changes: 0 additions & 2 deletions src/commonMain/kotlin/org/appliedtopology/tda4j/Field.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public object DoubleContext : FieldContext<Double> {

override fun number(value: Number): Double = value.toDouble()

override fun Double.unaryMinus(): Double = -this

override fun add(
left: Double,
right: Double,
Expand Down
26 changes: 13 additions & 13 deletions src/commonMain/kotlin/org/appliedtopology/tda4j/FiniteField.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package org.appliedtopology.tda4j

public open class Fp(val x: Int) {
public open class Fp(public val x: Int) {
public override fun toString(): String = "fp($x)"
}

interface FpFactory {
fun fp(x: Number): Fp
public interface FpFactory {
public fun fp(x: Number): Fp
}

public open class FiniteFieldContext(val p: Int) : FieldContext<Fp>, FpFactory {
public open class FiniteFieldContext(public val p: Int) : FieldContext<Fp>, FpFactory {
// Make finite field elements
public override fun fp(x: Number): Fp = Fp(x.toInt() % p)
final override fun fp(x: Number): Fp = Fp(x.toInt() % p)

override fun number(value: Number): Fp = Fp(value.toInt() % p)

override val zero = number(0)
override val one = number(1)
override val zero: Fp = fp(0)
override val one: Fp = fp(1)

// Finite field arithmetic
override fun Fp.unaryMinus(): Fp {
Expand All @@ -32,7 +32,7 @@ public open class FiniteFieldContext(val p: Int) : FieldContext<Fp>, FpFactory {
right: Fp,
): Fp = Fp((left.x.toLong() * right.x.toLong() % p.toLong()).toInt())

fun inverse(a: Int): Int {
protected fun inverse(a: Int): Int {
var u = a % p
var v = p
var x1 = 1
Expand All @@ -55,7 +55,7 @@ public open class FiniteFieldContext(val p: Int) : FieldContext<Fp>, FpFactory {
// Using a Map here takes up unnecessarily large amounts of space and runtime complexity
// for what should be a simple Array lookup.
// Performance note: This runs every time the context is instantiated. Reuse context when possible.
val inverseTable: IntArray = IntArray(p) { if (it == 0) 0 else inverse(it) }
protected val inverseTable: IntArray = IntArray(p) { if (it == 0) 0 else inverse(it) }

override fun divide(
left: Fp,
Expand All @@ -68,18 +68,18 @@ public open class FiniteFieldContext(val p: Int) : FieldContext<Fp>, FpFactory {
}

// Normalize, convert, print
fun norm(a: Fp): Int =
public fun norm(a: Fp): Int =
if (a.x % p < 0) {
(a.x % p) + p
} else {
a.x % p
}

fun Fp.normal(): Fp = Fp(norm(this))
public fun Fp.normal(): Fp = Fp(norm(this))

fun canonical(a: Fp): Int = norm(a) - (p - 1) / 2
public fun canonical(a: Fp): Int = norm(a) - (p - 1) / 2

fun Fp.toInt(): Int = canonical(this@toInt)
public fun Fp.toInt(): Int = canonical(this@toInt)

public override infix fun Fp.eq(other: Any?): Boolean =
if (other !is Fp) {
Expand Down
66 changes: 33 additions & 33 deletions src/commonMain/kotlin/org/appliedtopology/tda4j/Sets.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.appliedtopology.tda4j

open class ArrayMutableSortedSet<T>
protected constructor(capacity: Int = 8, val comparator: Comparator<T>) : MutableSet<T> {
public open class ArrayMutableSortedSet<T>
protected constructor(capacity: Int = 8, protected val comparator: Comparator<T>) : MutableSet<T> {
@Suppress("ktlint:standard:property-naming")
internal val _set: ArrayList<T> = ArrayList(capacity)

fun index(element: T): Int = _set.binarySearch(element, comparator)
public fun index(element: T): Int = _set.binarySearch(element, comparator)

override fun add(element: T): Boolean {
val index = index(element)
if (index >= 0) {
return false
return if (index >= 0) {
false
} else {
_set.add((-index) - 1, element)
return true
true
}
}

Expand All @@ -24,7 +24,7 @@ open class ArrayMutableSortedSet<T>
override val size: Int
get() = _set.size

override fun clear() = _set.clear()
override fun clear(): Unit = _set.clear()

override fun isEmpty(): Boolean = _set.isEmpty()

Expand Down Expand Up @@ -52,11 +52,11 @@ open class ArrayMutableSortedSet<T>

override fun remove(element: T): Boolean {
val index = index(element)
if (index < 0) {
return false
return if (index < 0) {
false
} else {
_set.removeAt(index)
return true
true
}
}

Expand All @@ -71,12 +71,12 @@ open class ArrayMutableSortedSet<T>
}
}

typealias MutableSortedSet<V> = ArrayMutableSortedSet<V>
public typealias MutableSortedSet<V> = ArrayMutableSortedSet<V>

open class ArrayMutableSortedMap<K, V> protected constructor(
public open class ArrayMutableSortedMap<K, V> protected constructor(
capacity: Int = 8,
val comparator: Comparator<K>,
val defaultValue: V? = null,
protected val comparator: Comparator<K>,
public val defaultValue: V? = null,
) : MutableMap<K, V> {
internal val _keys: ArrayMutableSortedSet<K> = ArrayMutableSortedSet(capacity, comparator)
internal val _values: ArrayList<V> = ArrayList(capacity)
Expand Down Expand Up @@ -119,41 +119,41 @@ open class ArrayMutableSortedMap<K, V> protected constructor(

override fun isEmpty(): Boolean = _keys.isEmpty()

fun ordinalKey(index: Int): K? = _keys._set.getOrNull(index)
public fun ordinalKey(index: Int): K? = _keys._set.getOrNull(index)

fun ordinalValue(index: Int): V? = _values.getOrElse(index) { i -> defaultValue }
public fun ordinalValue(index: Int): V? = _values.getOrElse(index) { i -> defaultValue }

fun ordinalItem(index: Int): MutableMap.MutableEntry<K, V>? {
public fun ordinalItem(index: Int): MutableMap.MutableEntry<K, V>? {
val ok = ordinalKey(index)
val ov = ordinalValue(index)
if (ok == null || ov == null) {
return null
return if (ok == null || ov == null) {
null
} else {
return PairEntry(ordinalKey(index)!!, ordinalValue(index)!!)
PairEntry(ordinalKey(index)!!, ordinalValue(index)!!)
}
}

val headKey: K?
public val headKey: K?
get() = ordinalKey(0)
val headValue: V?
public val headValue: V?
get() = ordinalValue(0)
val headItem: MutableMap.MutableEntry<K, V>?
public val headItem: MutableMap.MutableEntry<K, V>?
get() = ordinalItem(0)

fun replaceAllValues(transform: (V) -> V) {
public fun replaceAllValues(transform: (V) -> V) {
for (idx in _values.indices)
_values[idx] = transform(_values[idx])
}

override fun remove(key: K): V? {
val idx = _keys.index(key)
if (idx >= 0) {
return if (idx >= 0) {
val retval = _values[idx]
_keys._set.removeAt(idx)
_values.removeAt(idx)
return retval
retval
} else {
return null
null
}
}

Expand All @@ -166,29 +166,29 @@ open class ArrayMutableSortedMap<K, V> protected constructor(
value: V,
): V? {
val idx = _keys.index(key)
if (idx >= 0) {
return if (idx >= 0) {
val retval = _values[idx]
_values[idx] = value
return retval
retval
} else {
_keys.add(key)
_values.add((-idx) - 1, value)
return null
null
}
}

override fun toString(): String {
return "ArrayMutableSortedMap(${entries.joinToString()})"
}

companion object {
operator fun <K, V> invoke(
public companion object {
public operator fun <K, V> invoke(
capacity: Int = 8,
comparator: Comparator<K>,
defaultValue: V? = null,
): ArrayMutableSortedMap<K, V> = ArrayMutableSortedMap(capacity, comparator, defaultValue)

operator fun <K : Comparable<K>, V> invoke(
public operator fun <K : Comparable<K>, V> invoke(
capacity: Int = 8,
defaultValue: V? = null,
): ArrayMutableSortedMap<K, V> = ArrayMutableSortedMap(capacity, naturalOrder(), defaultValue)
Expand Down
Loading

0 comments on commit 1db5e7e

Please sign in to comment.