Skip to content

Commit

Permalink
Update package names and enhance graph utilities
Browse files Browse the repository at this point in the history
Renamed annotation package from `sschr15.aoc.annotations` to `com.sschr15.aoc.annotations` for better namespace organization. Added new utilities to Graph class for managing incoming and outgoing edges and introduced `indexOfMin` and `indexOfMax` functions for list processing.
  • Loading branch information
sschr15 committed Dec 18, 2024
1 parent 934da0a commit 0d03cf4
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package sschr15.aoc.annotations
package com.sschr15.aoc.annotations

@Retention(AnnotationRetention.SOURCE)
annotation class ExportIr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package sschr15.aoc.annotations
package com.sschr15.aoc.annotations

/**
* Marks a function to be memoized by the compiler plugin.
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class Memoize
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:Suppress("NOTHING_TO_INLINE")

package sschr15.aoc.annotations
package com.sschr15.aoc.annotations

inline fun plus(a: Int, b: Int): Int = Math.addExact(a, b)
inline fun minus(a: Int, b: Int): Int = Math.subtractExact(a, b)
Expand Down
2 changes: 1 addition & 1 deletion compiler-plugin/src/main/kotlin/Memoizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Memoizer(private val context: IrPluginContext) : IrElementTransformerVoid(
private val pair = context.referenceClass(ClassId(FqName("kotlin"), FqName("Pair"), false))!!
private val triple = context.referenceClass(ClassId(FqName("kotlin"), FqName("Triple"), false))!!

private val memoizeAnnotation = FqName("sschr15.aoc.annotations.Memoize")
private val memoizeAnnotation = FqName("com.sschr15.aoc.annotations.Memoize")

private fun IrPluginContext.keyFor(declaration: IrFunction): IrType = when (declaration.valueParameters.size) {
1 -> declaration.valueParameters.single().type
Expand Down
10 changes: 5 additions & 5 deletions compiler-plugin/src/main/kotlin/OverflowUnderflowChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.jetbrains.kotlin.name.Name

@OptIn(UnsafeDuringIrConstructionAPI::class)
class OverflowUnderflowChecker(private val context: IrPluginContext, private val config: CompilerConfiguration) : IrElementTransformerVoid() {
val skipCheckAnnotation = FqName("sschr15.aoc.annotations.SkipOverflowUnderflowCheck")
val skipCheckAnnotation = FqName("com.sschr15.aoc.annotations.SkipOverflowUnderflowCheck")

val singleTypeChecks = setOf(
"plus", "minus", "times",
Expand All @@ -48,7 +48,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val
.irCall(
context.referenceFunctions(
CallableId(
FqName("sschr15.aoc.annotations"),
FqName("com.sschr15.aoc.annotations"),
null,
expression.symbol.owner.name,
)
Expand All @@ -67,7 +67,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val
if (owner.name != absoluteValueName) return null
return context.irBuiltIns.createIrBuilder(expression.symbol, expression.startOffset, expression.endOffset)
.irCall(context.referenceFunctions(CallableId(
FqName("sschr15.aoc.annotations"),
FqName("com.sschr15.aoc.annotations"),
null,
Name.identifier("abs"),
)).single { it.owner.valueParameters.single().type == extension.type })
Expand All @@ -80,7 +80,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val
if (declaration.annotations.any { it.isAnnotationWithEqualFqName(skipCheckAnnotation) })
return declaration // skip checking this and all children

if (declaration.annotations.any { it.isAnnotationWithEqualFqName(FqName("sschr15.aoc.annotations.ExportIr")) }) {
if (declaration.annotations.any { it.isAnnotationWithEqualFqName(FqName("com.sschr15.aoc.annotations.ExportIr")) }) {
config.report(CompilerMessageSeverity.WARNING, declaration.dumpKotlinLike())
config.report(CompilerMessageSeverity.WARNING, declaration.dump())
}
Expand Down Expand Up @@ -119,7 +119,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val

return context.irBuiltIns.createIrBuilder(expression.symbol, expression.startOffset, expression.endOffset)
.irCall(context.referenceFunctions(CallableId(
FqName("sschr15.aoc.annotations"),
FqName("com.sschr15.aoc.annotations"),
null,
expression.symbol.owner.name,
)).single { it.owner.valueParameters.first().type == expression.type }).apply {
Expand Down
35 changes: 19 additions & 16 deletions src/main/kotlin/sschr15/aocsolutions/util/Graphs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import kotlin.collections.ArrayDeque
*/
class Graph<T> {
inner class Node internal constructor(val name: String? = null, val value: T) {
val edges = mutableListOf<Edge>()
private val _edges = mutableListOf<Edge>()
private val _incoming = mutableListOf<Edge>()
private val _outgoing = mutableListOf<Edge>()

val edges: List<Edge> get() = _edges
val incomingEdges: List<Edge> get() = _incoming
val outgoingEdges: List<Edge> get() = _outgoing

/**
* Connects the current node to another node, creating a bidirectional edge between them.
Expand All @@ -26,8 +32,15 @@ class Graph<T> {
* edge list are updated to include this new connection.
*/
fun connectTo(other: Node, weight: Int? = null) = Edge(this, other, weight).also {
edges.add(it)
other.edges.add(it.reversed())
_edges.add(it)
_incoming.add(it)
other._outgoing.add(it)

val rev = it.reversed()
_outgoing.add(rev)
other._edges.add(rev)
other._incoming.add(rev)

this@Graph.edges.add(it)
}

Expand All @@ -39,7 +52,9 @@ class Graph<T> {
* and the parent graph's edge list.
*/
fun oneWayConnectTo(other: Node, weight: Int? = null) = Edge(this, other, weight).also {
edges.add(it)
_edges.add(it)
_outgoing.add(it)
other._incoming.add(it)
this@Graph.edges.add(it)
}

Expand Down Expand Up @@ -79,18 +94,6 @@ class Graph<T> {

override fun toString() = "Graph(nodes=$nodes, edges=$edges)"

fun removeNode(node: Node) {
nodes.remove(node)
edges.removeAll(node.edges.toSet())
node.edges.forEach { it.to.edges.remove(it.reversed()) }
}

fun removeEdge(edge: Edge) {
edges.remove(edge)
edge.from.edges.remove(edge)
edge.to.edges.remove(edge.reversed())
}

/**
* Converts the graph represented by the current instance into a Graphviz DOT format string.
*
Expand Down
134 changes: 134 additions & 0 deletions src/main/kotlin/sschr15/aocsolutions/util/IterableUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,137 @@ inline fun <T> Iterable<T>.noneIndexed(predicate: (Int, T) -> Boolean): Boolean
}
return true
}

inline fun List<Int>.indexOfMin(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var min = Int.MAX_VALUE
var minIndex = -1
for ((i, value) in this.withIndex()) {
if (value < min) {
min = value
minIndex = i
}
}
if (minIndex == -1) return 0 // All values are Int.MAX_VALUE
return minIndex
}

@JvmName("longIndexOfMin")
inline fun List<Long>.indexOfMin(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var min = Long.MAX_VALUE
var minIndex = -1
for ((i, value) in this.withIndex()) {
if (value < min) {
min = value
minIndex = i
}
}
if (minIndex == -1) return 0
return minIndex
}

@JvmName("floatIndexOfMin")
inline fun List<Float>.indexOfMin(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var min = Float.POSITIVE_INFINITY
var minIndex = -1
for ((i, value) in this.withIndex()) {
if (value < min) {
min = value
minIndex = i
}
}
if (minIndex == -1) return 0
return minIndex
}

@JvmName("doubleIndexOfMin")
inline fun List<Double>.indexOfMin(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var min = Double.POSITIVE_INFINITY
var minIndex = -1
for ((i, value) in this.withIndex()) {
if (value < min) {
min = value
minIndex = i
}
}
if (minIndex == -1) return 0
return minIndex
}

inline fun List<Int>.indexOfMax(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var max = Int.MIN_VALUE
var maxIndex = -1
for ((i, value) in this.withIndex()) {
if (value > max) {
max = value
maxIndex = i
}
}
if (maxIndex == -1) return 0
return maxIndex
}

@JvmName("longIndexOfMax")
inline fun List<Long>.indexOfMax(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var max = Long.MIN_VALUE
var maxIndex = -1
for ((i, value) in this.withIndex()) {
if (value > max) {
max = value
maxIndex = i
}
}
if (maxIndex == -1) return 0
return maxIndex
}

@JvmName("floatIndexOfMax")
inline fun List<Float>.indexOfMax(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var max = Float.NEGATIVE_INFINITY
var maxIndex = -1
for ((i, value) in this.withIndex()) {
if (value > max) {
max = value
maxIndex = i
}
}
if (maxIndex == -1) return 0
return maxIndex
}

@JvmName("doubleIndexOfMax")
inline fun List<Double>.indexOfMax(): Int {
if (isEmpty()) return -1
if (size == 1) return 0

var max = Double.NEGATIVE_INFINITY
var maxIndex = -1
for ((i, value) in this.withIndex()) {
if (value > max) {
max = value
maxIndex = i
}
}
if (maxIndex == -1) return 0
return maxIndex
}

0 comments on commit 0d03cf4

Please sign in to comment.